Skip to content

Commit

Permalink
feat(oidc): customizable base redirect uri
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr committed Mar 7, 2022
1 parent d1c5085 commit fa1f234
Show file tree
Hide file tree
Showing 13 changed files with 43 additions and 11 deletions.
5 changes: 5 additions & 0 deletions driver/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ const (
ViperKeyPasswordIdentifierSimilarityCheckEnabled = "selfservice.methods.password.config.identifier_similarity_check_enabled"
ViperKeyIgnoreNetworkErrors = "selfservice.methods.password.config.ignore_network_errors"
ViperKeyTOTPIssuer = "selfservice.methods.totp.config.issuer"
ViperKeyOIDCBaseRedirectURL = "selfservice.methods.oidc.config.base_redirect_uri"
ViperKeyWebAuthnRPDisplayName = "selfservice.methods.webauthn.config.rp.display_name"
ViperKeyWebAuthnRPID = "selfservice.methods.webauthn.config.rp.id"
ViperKeyWebAuthnRPOrigin = "selfservice.methods.webauthn.config.rp.origin"
Expand Down Expand Up @@ -506,6 +507,10 @@ func (p *Config) TOTPIssuer() string {
return p.Source().StringF(ViperKeyTOTPIssuer, p.SelfPublicURL().Hostname())
}

func (p *Config) OIDCRedirectURIBase() *url.URL {
return p.Source().URIF(ViperKeyOIDCBaseRedirectURL, p.SelfPublicURL())
}

func (p *Config) IdentityTraitsSchemas() (Schemas, error) {
var ss Schemas
out, err := p.p.Marshal(kjson.Parser())
Expand Down
9 changes: 9 additions & 0 deletions embedx/config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1382,6 +1382,15 @@
"type": "object",
"additionalProperties": false,
"properties": {
"base_redirect_uri": {
"type": "string",
"title": "Base URL for OAuth2 Redirect URIs",
"description": "Can be used to modify the base URL for OAuth2 Redirect URLs. If unset, the Public Base URL will be used.",
"format": "uri",
"examples": [
"https://auth.myexample.org/"
]
},
"providers": {
"title": "OpenID Connect and OAuth2 Providers",
"description": "A list and configuration of OAuth2 and OpenID Connect providers Ory Kratos should integrate with.",
Expand Down
2 changes: 1 addition & 1 deletion selfservice/strategy/oidc/provider_auth0.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (g *ProviderAuth0) oauth2(ctx context.Context) (*oauth2.Config, error) {
TokenURL: tokenUrl.String(),
},
Scopes: g.config.Scope,
RedirectURL: g.config.Redir(g.reg.Config(ctx).SelfPublicURL()),
RedirectURL: g.config.Redir(g.reg.Config(ctx).OIDCRedirectURIBase()),
}

return c, nil
Expand Down
2 changes: 1 addition & 1 deletion selfservice/strategy/oidc/provider_discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (d *ProviderDiscord) oauth2(ctx context.Context) *oauth2.Config {
AuthURL: discordgo.EndpointOauth2 + "authorize",
TokenURL: discordgo.EndpointOauth2 + "token",
},
RedirectURL: d.config.Redir(d.reg.Config(ctx).SelfPublicURL()),
RedirectURL: d.config.Redir(d.reg.Config(ctx).OIDCRedirectURIBase()),
Scopes: d.config.Scope,
}
}
Expand Down
2 changes: 1 addition & 1 deletion selfservice/strategy/oidc/provider_generic_oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (g *ProviderGenericOIDC) oauth2ConfigFromEndpoint(ctx context.Context, endp
ClientSecret: g.config.ClientSecret,
Endpoint: endpoint,
Scopes: scope,
RedirectURL: g.config.Redir(g.reg.Config(ctx).SelfPublicURL()),
RedirectURL: g.config.Redir(g.reg.Config(ctx).OIDCRedirectURIBase()),
}
}

Expand Down
18 changes: 18 additions & 0 deletions selfservice/strategy/oidc/provider_generic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,24 @@ func makeAuthCodeURL(t *testing.T, r *login.Flow, reg *driver.RegistryDefault) s
func TestProviderGenericOIDC_AddAuthCodeURLOptions(t *testing.T) {
conf, reg := internal.NewFastRegistryWithMocks(t)
conf.MustSet(config.ViperKeyPublicBaseURL, "https://ory.sh")
t.Run("case=redirectURI is public base url", func(t *testing.T) {
r := &login.Flow{ID: x.NewUUID(), Refresh: true}
actual, err := url.ParseRequestURI(makeAuthCodeURL(t, r, reg))
require.NoError(t, err)
assert.Contains(t, actual.Query().Get("redirect_uri"), "https://ory.sh")
})

t.Run("case=redirectURI is public base url", func(t *testing.T) {
conf.MustSet(config.ViperKeyOIDCBaseRedirectURL, "https://example.org")
t.Cleanup(func() {
conf.MustSet(config.ViperKeyOIDCBaseRedirectURL, nil)
})
r := &login.Flow{ID: x.NewUUID(), Refresh: true}
actual, err := url.ParseRequestURI(makeAuthCodeURL(t, r, reg))
require.NoError(t, err)
assert.Contains(t, actual.Query().Get("redirect_uri"), "https://example.org")
})

t.Run("case=expect prompt to be login with forced flag", func(t *testing.T) {
r := &login.Flow{
ID: x.NewUUID(),
Expand Down
2 changes: 1 addition & 1 deletion selfservice/strategy/oidc/provider_github.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (g *ProviderGitHub) oauth2(ctx context.Context) *oauth2.Config {
ClientSecret: g.config.ClientSecret,
Endpoint: github.Endpoint,
Scopes: g.config.Scope,
RedirectURL: g.config.Redir(g.reg.Config(ctx).SelfPublicURL()),
RedirectURL: g.config.Redir(g.reg.Config(ctx).OIDCRedirectURIBase()),
}
}

Expand Down
2 changes: 1 addition & 1 deletion selfservice/strategy/oidc/provider_github_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (g *ProviderGitHubApp) oauth2(ctx context.Context) *oauth2.Config {
ClientSecret: g.config.ClientSecret,
Endpoint: github.Endpoint,
Scopes: g.config.Scope,
RedirectURL: g.config.Redir(g.reg.Config(ctx).SelfPublicURL()),
RedirectURL: g.config.Redir(g.reg.Config(ctx).OIDCRedirectURIBase()),
}
}

Expand Down
2 changes: 1 addition & 1 deletion selfservice/strategy/oidc/provider_gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (g *ProviderGitLab) oauth2(ctx context.Context) (*oauth2.Config, error) {
TokenURL: tokenUrl.String(),
},
Scopes: g.config.Scope,
RedirectURL: g.config.Redir(g.reg.Config(ctx).SelfPublicURL()),
RedirectURL: g.config.Redir(g.reg.Config(ctx).OIDCRedirectURIBase()),
}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion selfservice/strategy/oidc/provider_slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (d *ProviderSlack) oauth2(ctx context.Context) *oauth2.Config {
AuthURL: "https://slack.com/oauth/authorize",
TokenURL: slack.APIURL + "oauth.access",
},
RedirectURL: d.config.Redir(d.reg.Config(ctx).SelfPublicURL()),
RedirectURL: d.config.Redir(d.reg.Config(ctx).OIDCRedirectURIBase()),
Scopes: d.config.Scope,
}
}
Expand Down
4 changes: 2 additions & 2 deletions selfservice/strategy/oidc/provider_spotify.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (g *ProviderSpotify) oauth2(ctx context.Context) *oauth2.Config {
ClientSecret: g.config.ClientSecret,
Endpoint: spotify.Endpoint,
Scopes: g.config.Scope,
RedirectURL: g.config.Redir(g.reg.Config(ctx).SelfPublicURL()),
RedirectURL: g.config.Redir(g.reg.Config(ctx).OIDCRedirectURIBase()),
}
}

Expand All @@ -64,7 +64,7 @@ func (g *ProviderSpotify) Claims(ctx context.Context, exchange *oauth2.Token) (*
}

auth := spotifyauth.New(
spotifyauth.WithRedirectURL(g.config.Redir(g.reg.Config(ctx).SelfPublicURL())),
spotifyauth.WithRedirectURL(g.config.Redir(g.reg.Config(ctx).OIDCRedirectURIBase())),
spotifyauth.WithScopes(spotifyauth.ScopeUserReadPrivate))

client := spotifyapi.New(auth.Client(ctx, exchange))
Expand Down
2 changes: 1 addition & 1 deletion selfservice/strategy/oidc/provider_vk.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (g *ProviderVK) oauth2(ctx context.Context) *oauth2.Config {
TokenURL: "https://oauth.vk.com/access_token",
},
Scopes: g.config.Scope,
RedirectURL: g.config.Redir(g.reg.Config(ctx).SelfPublicURL()),
RedirectURL: g.config.Redir(g.reg.Config(ctx).OIDCRedirectURIBase()),
}
}

Expand Down
2 changes: 1 addition & 1 deletion selfservice/strategy/oidc/provider_yandex.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (g *ProviderYandex) oauth2(ctx context.Context) *oauth2.Config {
TokenURL: "https://oauth.yandex.com/token",
},
Scopes: g.config.Scope,
RedirectURL: g.config.Redir(g.reg.Config(ctx).SelfPublicURL()),
RedirectURL: g.config.Redir(g.reg.Config(ctx).OIDCRedirectURIBase()),
}
}

Expand Down

0 comments on commit fa1f234

Please sign in to comment.