MCPcopy Index your code
hub / github.com/writefreely/writefreely / exchangeOauthCode

Method exchangeOauthCode

oauth_gitea.go:61–93  ·  view source on GitHub ↗
(ctx context.Context, code string)

Source from the content-addressed store, hash-verified

59}
60
61func (c giteaOauthClient) exchangeOauthCode(ctx context.Context, code string) (*TokenResponse, error) {
62 form := url.Values{}
63 form.Add("grant_type", "authorization_code")
64 form.Add("redirect_uri", c.CallbackLocation)
65 form.Add("scope", c.Scope)
66 form.Add("code", code)
67 req, err := http.NewRequest("POST", c.ExchangeLocation, strings.NewReader(form.Encode()))
68 if err != nil {
69 return nil, err
70 }
71 req.WithContext(ctx)
72 req.Header.Set("User-Agent", ServerUserAgent(""))
73 req.Header.Set("Accept", "application/json")
74 req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
75 req.SetBasicAuth(c.ClientID, c.ClientSecret)
76
77 resp, err := c.HttpClient.Do(req)
78 if err != nil {
79 return nil, err
80 }
81 if resp.StatusCode != http.StatusOK {
82 return nil, errors.New("unable to exchange code for access token")
83 }
84
85 var tokenResponse TokenResponse
86 if err := limitedJsonUnmarshal(resp.Body, tokenRequestMaxLen, &tokenResponse); err != nil {
87 return nil, err
88 }
89 if tokenResponse.Error != "" {
90 return nil, errors.New(tokenResponse.Error)
91 }
92 return &tokenResponse, nil
93}
94
95func (c giteaOauthClient) inspectOauthAccessToken(ctx context.Context, accessToken string) (*InspectResponse, error) {
96 req, err := http.NewRequest("GET", c.InspectLocation, nil)

Callers

nothing calls this directly

Calls 3

ServerUserAgentFunction · 0.85
limitedJsonUnmarshalFunction · 0.85
DoMethod · 0.65

Tested by

no test coverage detected