Skip to content

Commit f3ec507

Browse files
Merge pull request #27 from Laceyoo/main
fix:Authorization Code模式下REQUEST INFO错误显示为response的body
2 parents 8c21ad8 + 8afb2d4 commit f3ec507

File tree

7 files changed

+934
-4
lines changed

7 files changed

+934
-4
lines changed

controller/oauth2_authorization_code.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package controller
33
import (
44
"fmt"
55
"net/http"
6+
"net/url"
67
"strconv"
78

89
"github.com/ECNU/Open-OAuth2Playground/g"
@@ -20,7 +21,7 @@ type ExchangeTokenByCodeRequest struct {
2021

2122
type RefreshTokenRequest struct {
2223
ClientID string `json:"client_id"`
23-
ClientSecret string `json:"client_secret"`
24+
ClientSecret string `json:"client_secret,omitempty"` // client_secret is optional for refresh_token
2425
RefreshToken string `json:"refresh_token"`
2526
}
2627

@@ -58,7 +59,11 @@ func refreshToken(c *gin.Context) {
5859
method := "POST"
5960
apiAddr := g.Config().Endpoints.Token
6061
grant_type := "refresh_token"
61-
body := fmt.Sprintf("grant_type=%s&client_id=%s&client_secret=%s&refresh_token=%s", grant_type, request.ClientID, request.ClientSecret, request.RefreshToken)
62+
body := fmt.Sprintf("grant_type=%s&client_id=%s&refresh_token=%s", grant_type, request.ClientID, request.RefreshToken)
63+
64+
if request.ClientSecret != "" {
65+
body += fmt.Sprintf("&client_secret=%s", url.QueryEscape(request.ClientSecret))
66+
}
6267

6368
header := make(map[string]string)
6469
header["Content-Type"] = "application/x-www-form-urlencoded"

controller/oauth2_pkce.go

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package controller
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"strconv"
7+
8+
"github.com/ECNU/Open-OAuth2Playground/g"
9+
"github.com/ECNU/Open-OAuth2Playground/models"
10+
"github.com/gin-gonic/gin"
11+
)
12+
13+
type ReqPkceData struct {
14+
Code string `json:"code"`
15+
ClientID string `json:"client_id"`
16+
CodeVerifier string `json:"code_verifier"`
17+
Scope string `json:"scope"`
18+
RedirectURI string `json:"redirect_uri"`
19+
}
20+
21+
func pkce(c *gin.Context) {
22+
request := ReqPkceData{}
23+
if err := c.Bind(&request); err != nil {
24+
c.JSON(http.StatusOK, handleError(err.Error()))
25+
return
26+
}
27+
28+
method := "POST"
29+
apiAddr := g.Config().Endpoints.Token
30+
grant_type := "authorization_code"
31+
body := fmt.Sprintf("code=%s&redirect_uri=%s&client_id=%s&scope=%s&grant_type=%s&code_verifier=%s",
32+
request.Code, request.RedirectURI, request.ClientID, request.Scope, grant_type, request.CodeVerifier)
33+
34+
header := make(map[string]string)
35+
header["Content-Type"] = "application/x-www-form-urlencoded"
36+
header["Content-Length"] = strconv.Itoa(len(body))
37+
38+
res, err := models.HandleRequest(method, apiAddr, g.UserAgent, body, g.Config().Timeout, header)
39+
if err != nil {
40+
c.JSON(http.StatusOK, handleError(err.Error()))
41+
return
42+
}
43+
c.JSON(http.StatusOK, handleSuccess(res))
44+
}

controller/route.go

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ func Routes(r *gin.Engine) {
1919
playground := r.Group(g.Config().Http.RouteBase + "v1")
2020
playground.Use(IPLimitCheck)
2121
playground.Use(NoCache())
22+
playground.POST("/oauth2/pkce", pkce)
2223
playground.POST("/oauth2/device_flow", deviceFlow)
2324
playground.POST("/oauth2/client_credentials", clientCredentials)
2425
playground.POST("/oauth2/password", passwordMode)

front-standalone/src/api/playground.ts

+7
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,10 @@ export const fetchACTokenByPassword = (data) => {
4646
export const fetchACTokenByDevice = (data) => {
4747
return http.post<Result>("/oauth2/device_flow", data);
4848
};
49+
50+
/** PKCE */
51+
/** Step 2 */
52+
/** Get access_token with PKCE */
53+
export const fetchACTokenByPkce = (data) => {
54+
return http.post<Result>("/oauth2/pkce", data);
55+
};

front-standalone/src/views/playground/components/Authorization.vue

+1-2
Original file line numberDiff line numberDiff line change
@@ -538,8 +538,7 @@ const handleDrag = (floatButton, container) => {
538538
<div class="http-content" style="text-align: start; padding: 0em; position: relative; overflow: auto; max-height: 350px; width: 100%">
539539
<el-scrollbar class="http-render">
540540
<highlightjs autodetect :code="requestInfo.code"/>
541-
<highlightjs v-if="isJsonResponse(responseInfo.header)" autodetect :code="formatJson(responseInfo.body)"/>
542-
<highlightjs v-else autodetect :code="responseInfo.body"></highlightjs>
541+
<highlightjs :class="{ 'bodyWrap': isWrapRes }" autodetect :code="requestInfo.body"/>
543542
</el-scrollbar>
544543
<el-checkbox v-model="isWrapRes" label="Wrap Lines"
545544
size="large"/>

0 commit comments

Comments
 (0)