NewGithubProvider creates a Github account provider.
(ext conf.OAuthProviderConfiguration, scopes string)
| 37 | |
| 38 | // NewGithubProvider creates a Github account provider. |
| 39 | func NewGithubProvider(ext conf.OAuthProviderConfiguration, scopes string) (OAuthProvider, error) { |
| 40 | if err := ext.ValidateOAuth(); err != nil { |
| 41 | return nil, err |
| 42 | } |
| 43 | |
| 44 | authHost := chooseHost(ext.URL, defaultGitHubAuthBase) |
| 45 | apiHost := chooseHost(ext.URL, defaultGitHubAPIBase) |
| 46 | if !strings.HasSuffix(apiHost, defaultGitHubAPIBase) { |
| 47 | apiHost += "/api/v3" |
| 48 | } |
| 49 | |
| 50 | oauthScopes := []string{ |
| 51 | "user:email", |
| 52 | } |
| 53 | |
| 54 | if scopes != "" { |
| 55 | oauthScopes = append(oauthScopes, strings.Split(scopes, ",")...) |
| 56 | } |
| 57 | |
| 58 | return &githubProvider{ |
| 59 | Config: &oauth2.Config{ |
| 60 | ClientID: ext.ClientID[0], |
| 61 | ClientSecret: ext.Secret, |
| 62 | Endpoint: oauth2.Endpoint{ |
| 63 | AuthURL: authHost + "/login/oauth/authorize", |
| 64 | TokenURL: authHost + "/login/oauth/access_token", |
| 65 | }, |
| 66 | RedirectURL: ext.RedirectURI, |
| 67 | Scopes: oauthScopes, |
| 68 | }, |
| 69 | APIHost: apiHost, |
| 70 | }, nil |
| 71 | } |
| 72 | |
| 73 | func (g githubProvider) GetOAuthToken(ctx context.Context, code string, opts ...oauth2.AuthCodeOption) (*oauth2.Token, error) { |
| 74 | return g.Exchange(ctx, code, opts...) |
no test coverage detected