ExchangeToken returns the exchanged OAuth2 token using the given redirect URL and authorization code.
(ctx context.Context, redirectURL, code string)
| 64 | // ExchangeToken returns the exchanged OAuth2 token using the given redirect |
| 65 | // URL and authorization code. |
| 66 | func (p *IdentityProvider) ExchangeToken(ctx context.Context, redirectURL, code string) (*oauth2.Token, error) { |
| 67 | oauth2Config := oauth2.Config{ |
| 68 | ClientID: p.config.ClientId, |
| 69 | ClientSecret: p.config.ClientSecret, |
| 70 | RedirectURL: redirectURL, |
| 71 | |
| 72 | // Discovery returns the OAuth2 endpoints. |
| 73 | Endpoint: p.provider.Endpoint(), |
| 74 | Scopes: p.config.Scopes, |
| 75 | } |
| 76 | |
| 77 | authStyle := oauth2.AuthStyleInParams |
| 78 | if p.config.AuthStyle == storepb.OAuth2AuthStyle_IN_HEADER { |
| 79 | authStyle = oauth2.AuthStyleInHeader |
| 80 | } |
| 81 | oauth2Config.Endpoint.AuthStyle = authStyle |
| 82 | |
| 83 | ctx = context.WithValue(ctx, oauth2.HTTPClient, p.client) |
| 84 | token, err := oauth2Config.Exchange(ctx, code) |
| 85 | if err != nil { |
| 86 | return nil, errors.Wrap(err, "exchange token") |
| 87 | } |
| 88 | return token, nil |
| 89 | } |
| 90 | |
| 91 | // UserInfo returns the parsed user information using the given OAuth2 token. |
| 92 | // The nonce is used for request validation, which should be the same value as |
no outgoing calls