|
| 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 | +} |
0 commit comments