Skip to content

Commit 3fd69fa

Browse files
authored
fix: support custom OAuth2 URLs for GitHub Enterprise and self-hosted providers (#3513)
- Use custom authURL/tokenURL from config instead of hardcoded github.com endpoints - Properly configure GitHub Enterprise API base URL from auth endpoints Fixes OAuth2 authentication with GitHub Enterprise Server and other self-hosted OAuth2 providers. Signed-off-by: Mathias Bogaert <mathias.bogaert@gmail.com>
1 parent fdba14b commit 3fd69fa

4 files changed

Lines changed: 56 additions & 2 deletions

File tree

examples/README.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ In the case of running zot with openid enabled behind a proxy/load balancer http
346346
"http": {
347347
"address": "0.0.0.0",
348348
"port": "8080",
349-
"externalUrl: "https://zot.example.com",
349+
"externalUrl": "https://zot.example.com",
350350
"auth": {
351351
"openid": {
352352
"providers": {
@@ -362,6 +362,40 @@ In the case of running zot with openid enabled behind a proxy/load balancer http
362362
```
363363
This config value will be used by oauth2/openid clients to redirect back to zot.
364364

365+
### OpenID/OAuth2 Social Login with Custom URLs (Self-Hosted Providers)
366+
367+
#### Use Cases
368+
- GitHub Enterprise Server (on-premises GitHub)
369+
- GitLab Self-Managed instances
370+
- Custom corporate OAuth2/OIDC providers
371+
372+
When integrating zot with self-hosted OAuth2 providers like GitHub Enterprise Server, GitLab Self-Managed,
373+
or custom OIDC implementations, you must specify custom authentication and token endpoints since
374+
the default public endpoints won't work.
375+
376+
```
377+
"http": {
378+
"address": "0.0.0.0",
379+
"port": "8080",
380+
"externalUrl": "https://zot.example.com",
381+
"auth": {
382+
"openid": {
383+
"providers": {
384+
"github": {
385+
"clientid": <client_id>,
386+
"clientsecret": <client_secret>,
387+
"authurl": "https://github.qkg1.toppany.com/login/oauth/authorize", // Custom GHE authorization endpoint
388+
"tokenurl": "https://github.qkg1.toppany.com/login/oauth/access_token", // Custom GHE token endpoint
389+
"scopes": ["read:org", "user", "repo"]
390+
}
391+
}
392+
}
393+
}
394+
}
395+
```
396+
397+
Without `authurl`/`tokenurl`, zot assumes public GitHub.com endpoints.
398+
365399
### Session based login
366400

367401
Whenever a user logs in zot using any of the auth options available(basic auth/openid) zot will set a 'session' cookie on its response.

pkg/api/authn.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,12 +615,24 @@ func NewRelyingPartyGithub(config *config.Config, provider string, hashKey, encr
615615
_, clientID, clientSecret, redirectURI, scopes,
616616
options := getRelyingPartyArgs(config, provider, hashKey, encryptKey, log)
617617

618+
var endpoint oauth2.Endpoint
619+
620+
// Use custom endpoints if provided, otherwise fallback to GitHub's endpoints
621+
if provider := config.HTTP.Auth.OpenID.Providers[provider]; provider.AuthURL != "" && provider.TokenURL != "" {
622+
endpoint = oauth2.Endpoint{
623+
AuthURL: provider.AuthURL,
624+
TokenURL: provider.TokenURL,
625+
}
626+
} else {
627+
endpoint = githubOAuth.Endpoint
628+
}
629+
618630
rpConfig := &oauth2.Config{
619631
ClientID: clientID,
620632
ClientSecret: clientSecret,
621633
RedirectURL: redirectURI,
622634
Scopes: scopes,
623-
Endpoint: githubOAuth.Endpoint,
635+
Endpoint: endpoint,
624636
}
625637

626638
relyingParty, err := rp.NewRelyingPartyOAuth(rpConfig, options...)

pkg/api/config/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ type OpenIDProviderConfig struct {
167167
ClientSecret string
168168
KeyPath string
169169
Issuer string
170+
AuthURL string
171+
TokenURL string
170172
Scopes []string
171173
}
172174

@@ -606,6 +608,8 @@ func (c *Config) Sanitize() *Config {
606608
ClientSecret: "******",
607609
KeyPath: config.KeyPath,
608610
Issuer: config.Issuer,
611+
AuthURL: config.AuthURL,
612+
TokenURL: config.TokenURL,
609613
Scopes: config.Scopes,
610614
}
611615
}

pkg/api/config/config_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ func TestConfig(t *testing.T) {
119119
Name: "GitHub",
120120
ClientID: "github-client-id",
121121
ClientSecret: "github-client-secret",
122+
AuthURL: "github-auth-url",
123+
TokenURL: "github-token-url",
122124
Scopes: []string{"user:email"},
123125
},
124126
},
@@ -143,6 +145,8 @@ func TestConfig(t *testing.T) {
143145
// Verify original config is not modified
144146
So(conf.HTTP.Auth.OpenID.Providers["google"].ClientSecret, ShouldEqual, "google-client-secret")
145147
So(conf.HTTP.Auth.OpenID.Providers["github"].ClientSecret, ShouldEqual, "github-client-secret")
148+
So(conf.HTTP.Auth.OpenID.Providers["github"].AuthURL, ShouldEqual, "github-auth-url")
149+
So(conf.HTTP.Auth.OpenID.Providers["github"].TokenURL, ShouldEqual, "github-token-url")
146150
})
147151

148152
Convey("Test Sanitize() with Event sink credentials", func() {

0 commit comments

Comments
 (0)