Skip to content
This repository was archived by the owner on Mar 16, 2021. It is now read-only.

Commit 566f2a3

Browse files
billimekmxschmitt
authored andcommitted
support for custom github endpoints (#115)
* support for custom github endpoints * implementing requested changes * refactor 'GitHubEndpointURL' to 'EndpointURL'
1 parent c885c70 commit 566f2a3

File tree

4 files changed

+14
-3
lines changed

4 files changed

+14
-3
lines changed

config/example.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Google: # only relevant when using the oauth authbackend
1313
GitHub: # only relevant when using the oauth authbackend
1414
ClientID: replace me
1515
ClientSecret: replace me
16+
EndpointURL: # (OPTIONAL) URL for custom endpoint (currently only for github); e.g. 'https://github.mydomain.com'
1617
Microsoft: # only relevant when using the oauth authbackend
1718
ClientID: replace me
1819
ClientSecret: 'replace me'

internal/handlers/auth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func (h *Handler) initOAuth() {
2525
}
2626
github := util.GetConfig().GitHub
2727
if github.Enabled() {
28-
auth.WithAdapterWrapper(auth.NewGithubAdapter(github.ClientID, github.ClientSecret), h.engine.Group("/api/v1/auth/github"))
28+
auth.WithAdapterWrapper(auth.NewGithubAdapter(github.ClientID, github.ClientSecret, github.EndpointURL), h.engine.Group("/api/v1/auth/github"))
2929
h.providers = append(h.providers, "github")
3030
}
3131
microsoft := util.GetConfig().Microsoft

internal/handlers/auth/github.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ type githubAdapter struct {
1818
}
1919

2020
// NewGithubAdapter creates an oAuth adapter out of the credentials and the baseURL
21-
func NewGithubAdapter(clientID, clientSecret string) Adapter {
21+
func NewGithubAdapter(clientID, clientSecret, endpointURL string) Adapter {
22+
if endpointURL != "" {
23+
github.Endpoint.AuthURL = endpointURL + "/login/oauth/authorize"
24+
github.Endpoint.TokenURL = endpointURL + "/login/oauth/access_token"
25+
}
2226
return &githubAdapter{&oauth2.Config{
2327
ClientID: clientID,
2428
ClientSecret: clientSecret,
@@ -40,7 +44,12 @@ func (a *githubAdapter) GetUserData(state, code string) (*user, error) {
4044
if err != nil {
4145
return nil, errors.Wrap(err, "could not exchange code")
4246
}
43-
oAuthUserInfoReq, err := a.config.Client(context.Background(), oAuthToken).Get("https://api.github.com/user")
47+
48+
gitHubUserURL := "https://api.github.com/user"
49+
if util.GetConfig().GitHub.EndpointURL != "" {
50+
gitHubUserURL = util.GetConfig().GitHub.EndpointURL + "/api/v3/user"
51+
}
52+
oAuthUserInfoReq, err := a.config.Client(context.Background(), oAuthToken).Get(gitHubUserURL)
4453
if err != nil {
4554
return nil, errors.Wrap(err, "could not get user data")
4655
}

internal/util/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ type redisConf struct {
4343
type oAuthConf struct {
4444
ClientID string `yaml:"ClientID" env:"CLIENT_ID"`
4545
ClientSecret string `yaml:"ClientSecret" env:"CLIENT_SECRET"`
46+
EndpointURL string `yaml:"EndPointURL" env:"ENDPOINT_URL"` // optional for only GitHub
4647
}
4748

4849
type proxyAuthConf struct {

0 commit comments

Comments
 (0)