| 1 |
package op |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"net/http" |
| 6 |
|
| 7 |
jose "github.com/go-jose/go-jose/v4" |
| 8 |
|
| 9 |
httphelper "github.com/zitadel/oidc/v3/pkg/http" |
| 10 |
"github.com/zitadel/oidc/v3/pkg/oidc" |
| 11 |
) |
| 12 |
|
| 13 |
type DiscoverStorage interface { |
| 14 |
SignatureAlgorithms(context.Context) ([]jose.SignatureAlgorithm, error) |
| 15 |
} |
| 16 |
|
| 17 |
var DefaultSupportedScopes = []string{ |
| 18 |
oidc.ScopeOpenID, |
| 19 |
oidc.ScopeProfile, |
| 20 |
oidc.ScopeEmail, |
| 21 |
oidc.ScopePhone, |
| 22 |
oidc.ScopeAddress, |
| 23 |
oidc.ScopeOfflineAccess, |
| 24 |
} |
| 25 |
|
| 26 |
func discoveryHandler(c Configuration, s DiscoverStorage) func(http.ResponseWriter, *http.Request) { |
| 27 |
return func(w http.ResponseWriter, r *http.Request) { |
| 28 |
Discover(w, CreateDiscoveryConfig(r.Context(), c, s)) |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
func Discover(w http.ResponseWriter, config *oidc.DiscoveryConfiguration) { |
| 33 |
httphelper.MarshalJSON(w, config) |
| 34 |
} |
| 35 |
|
| 36 |
func CreateDiscoveryConfig(ctx context.Context, config Configuration, storage DiscoverStorage) *oidc.DiscoveryConfiguration { |
| 37 |
issuer := IssuerFromContext(ctx) |
| 38 |
return &oidc.DiscoveryConfiguration{ |
| 39 |
Issuer: issuer, |
| 40 |
AuthorizationEndpoint: config.AuthorizationEndpoint().Absolute(issuer), |
| 41 |
TokenEndpoint: config.TokenEndpoint().Absolute(issuer), |
| 42 |
IntrospectionEndpoint: config.IntrospectionEndpoint().Absolute(issuer), |
| 43 |
UserinfoEndpoint: config.UserinfoEndpoint().Absolute(issuer), |
| 44 |
RevocationEndpoint: config.RevocationEndpoint().Absolute(issuer), |
| 45 |
EndSessionEndpoint: config.EndSessionEndpoint().Absolute(issuer), |
| 46 |
JwksURI: config.KeysEndpoint().Absolute(issuer), |
| 47 |
DeviceAuthorizationEndpoint: config.DeviceAuthorizationEndpoint().Absolute(issuer), |
| 48 |
CheckSessionIframe: config.CheckSessionIframe().Absolute(issuer), |
| 49 |
ScopesSupported: Scopes(config), |
| 50 |
ResponseTypesSupported: ResponseTypes(config), |
| 51 |
GrantTypesSupported: GrantTypes(config), |
| 52 |
SubjectTypesSupported: SubjectTypes(config), |
| 53 |
IDTokenSigningAlgValuesSupported: SigAlgorithms(ctx, storage), |
| 54 |
RequestObjectSigningAlgValuesSupported: RequestObjectSigAlgorithms(config), |
| 55 |
TokenEndpointAuthMethodsSupported: AuthMethodsTokenEndpoint(config), |
| 56 |
TokenEndpointAuthSigningAlgValuesSupported: TokenSigAlgorithms(config), |
| 57 |
IntrospectionEndpointAuthSigningAlgValuesSupported: IntrospectionSigAlgorithms(config), |
| 58 |
IntrospectionEndpointAuthMethodsSupported: AuthMethodsIntrospectionEndpoint(config), |
| 59 |
RevocationEndpointAuthSigningAlgValuesSupported: RevocationSigAlgorithms(config), |
| 60 |
RevocationEndpointAuthMethodsSupported: AuthMethodsRevocationEndpoint(config), |
| 61 |
ClaimsSupported: SupportedClaims(config), |
| 62 |
CodeChallengeMethodsSupported: CodeChallengeMethods(config), |
| 63 |
UILocalesSupported: config.SupportedUILocales(), |
| 64 |
RequestParameterSupported: config.RequestObjectSupported(), |
| 65 |
BackChannelLogoutSupported: config.BackChannelLogoutSupported(), |
| 66 |
BackChannelLogoutSessionSupported: config.BackChannelLogoutSessionSupported(), |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
func createDiscoveryConfigV2(ctx context.Context, config Configuration, storage DiscoverStorage, endpoints *Endpoints) *oidc.DiscoveryConfiguration { |
| 71 |
issuer := IssuerFromContext(ctx) |
| 72 |
return &oidc.DiscoveryConfiguration{ |
| 73 |
Issuer: issuer, |
| 74 |
AuthorizationEndpoint: endpoints.Authorization.Absolute(issuer), |
| 75 |
TokenEndpoint: endpoints.Token.Absolute(issuer), |
| 76 |
IntrospectionEndpoint: endpoints.Introspection.Absolute(issuer), |
| 77 |
UserinfoEndpoint: endpoints.Userinfo.Absolute(issuer), |
| 78 |
RevocationEndpoint: endpoints.Revocation.Absolute(issuer), |
| 79 |
EndSessionEndpoint: endpoints.EndSession.Absolute(issuer), |
| 80 |
JwksURI: endpoints.JwksURI.Absolute(issuer), |
| 81 |
DeviceAuthorizationEndpoint: endpoints.DeviceAuthorization.Absolute(issuer), |
| 82 |
ScopesSupported: Scopes(config), |
| 83 |
ResponseTypesSupported: ResponseTypes(config), |
| 84 |
GrantTypesSupported: GrantTypes(config), |
| 85 |
SubjectTypesSupported: SubjectTypes(config), |
| 86 |
IDTokenSigningAlgValuesSupported: SigAlgorithms(ctx, storage), |
| 87 |
RequestObjectSigningAlgValuesSupported: RequestObjectSigAlgorithms(config), |
| 88 |
TokenEndpointAuthMethodsSupported: AuthMethodsTokenEndpoint(config), |
| 89 |
TokenEndpointAuthSigningAlgValuesSupported: TokenSigAlgorithms(config), |
| 90 |
IntrospectionEndpointAuthSigningAlgValuesSupported: IntrospectionSigAlgorithms(config), |
| 91 |
IntrospectionEndpointAuthMethodsSupported: AuthMethodsIntrospectionEndpoint(config), |
| 92 |
RevocationEndpointAuthSigningAlgValuesSupported: RevocationSigAlgorithms(config), |
| 93 |
RevocationEndpointAuthMethodsSupported: AuthMethodsRevocationEndpoint(config), |
| 94 |
ClaimsSupported: SupportedClaims(config), |
| 95 |
CodeChallengeMethodsSupported: CodeChallengeMethods(config), |
| 96 |
UILocalesSupported: config.SupportedUILocales(), |
| 97 |
RequestParameterSupported: config.RequestObjectSupported(), |
| 98 |
BackChannelLogoutSupported: config.BackChannelLogoutSupported(), |
| 99 |
BackChannelLogoutSessionSupported: config.BackChannelLogoutSessionSupported(), |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
func Scopes(c Configuration) []string { |
| 104 |
provider, ok := c.(*Provider) |
| 105 |
if ok && provider.config.SupportedScopes != nil { |
| 106 |
return provider.config.SupportedScopes |
| 107 |
} |
| 108 |
return DefaultSupportedScopes |
| 109 |
} |
| 110 |
|
| 111 |
func ResponseTypes(c Configuration) []string { |
| 112 |
return []string{ |
| 113 |
string(oidc.ResponseTypeCode), |
| 114 |
string(oidc.ResponseTypeIDTokenOnly), |
| 115 |
string(oidc.ResponseTypeIDToken), |
| 116 |
} // TODO: ok for now, check later if dynamic needed |
| 117 |
} |
| 118 |
|
| 119 |
func GrantTypes(c Configuration) []oidc.GrantType { |
| 120 |
grantTypes := []oidc.GrantType{ |
| 121 |
oidc.GrantTypeCode, |
| 122 |
oidc.GrantTypeImplicit, |
| 123 |
} |
| 124 |
if c.GrantTypeRefreshTokenSupported() { |
| 125 |
grantTypes = append(grantTypes, oidc.GrantTypeRefreshToken) |
| 126 |
} |
| 127 |
if c.GrantTypeClientCredentialsSupported() { |
| 128 |
grantTypes = append(grantTypes, oidc.GrantTypeClientCredentials) |
| 129 |
} |
| 130 |
if c.GrantTypeTokenExchangeSupported() { |
| 131 |
grantTypes = append(grantTypes, oidc.GrantTypeTokenExchange) |
| 132 |
} |
| 133 |
if c.GrantTypeJWTAuthorizationSupported() { |
| 134 |
grantTypes = append(grantTypes, oidc.GrantTypeBearer) |
| 135 |
} |
| 136 |
if c.GrantTypeDeviceCodeSupported() { |
| 137 |
grantTypes = append(grantTypes, oidc.GrantTypeDeviceCode) |
| 138 |
} |
| 139 |
return grantTypes |
| 140 |
} |
| 141 |
|
| 142 |
func SubjectTypes(c Configuration) []string { |
| 143 |
return []string{"public"} // TODO: config |
| 144 |
} |
| 145 |
|
| 146 |
func SigAlgorithms(ctx context.Context, storage DiscoverStorage) []string { |
| 147 |
ctx, span := Tracer.Start(ctx, "SigAlgorithms") |
| 148 |
defer span.End() |
| 149 |
|
| 150 |
algorithms, err := storage.SignatureAlgorithms(ctx) |
| 151 |
if err != nil { |
| 152 |
return nil |
| 153 |
} |
| 154 |
algs := make([]string, len(algorithms)) |
| 155 |
for i, algorithm := range algorithms { |
| 156 |
algs[i] = string(algorithm) |
| 157 |
} |
| 158 |
return algs |
| 159 |
} |
| 160 |
|
| 161 |
func RequestObjectSigAlgorithms(c Configuration) []string { |
| 162 |
if !c.RequestObjectSupported() { |
| 163 |
return nil |
| 164 |
} |
| 165 |
return c.RequestObjectSigningAlgorithmsSupported() |
| 166 |
} |
| 167 |
|
| 168 |
func AuthMethodsTokenEndpoint(c Configuration) []oidc.AuthMethod { |
| 169 |
authMethods := []oidc.AuthMethod{ |
| 170 |
oidc.AuthMethodNone, |
| 171 |
oidc.AuthMethodBasic, |
| 172 |
} |
| 173 |
if c.AuthMethodPostSupported() { |
| 174 |
authMethods = append(authMethods, oidc.AuthMethodPost) |
| 175 |
} |
| 176 |
if c.AuthMethodPrivateKeyJWTSupported() { |
| 177 |
authMethods = append(authMethods, oidc.AuthMethodPrivateKeyJWT) |
| 178 |
} |
| 179 |
return authMethods |
| 180 |
} |
| 181 |
|
| 182 |
func TokenSigAlgorithms(c Configuration) []string { |
| 183 |
if !c.AuthMethodPrivateKeyJWTSupported() { |
| 184 |
return nil |
| 185 |
} |
| 186 |
return c.TokenEndpointSigningAlgorithmsSupported() |
| 187 |
} |
| 188 |
|
| 189 |
func IntrospectionSigAlgorithms(c Configuration) []string { |
| 190 |
if !c.IntrospectionAuthMethodPrivateKeyJWTSupported() { |
| 191 |
return nil |
| 192 |
} |
| 193 |
return c.IntrospectionEndpointSigningAlgorithmsSupported() |
| 194 |
} |
| 195 |
|
| 196 |
func AuthMethodsIntrospectionEndpoint(c Configuration) []oidc.AuthMethod { |
| 197 |
authMethods := []oidc.AuthMethod{ |
| 198 |
oidc.AuthMethodBasic, |
| 199 |
} |
| 200 |
if c.AuthMethodPrivateKeyJWTSupported() { |
| 201 |
authMethods = append(authMethods, oidc.AuthMethodPrivateKeyJWT) |
| 202 |
} |
| 203 |
return authMethods |
| 204 |
} |
| 205 |
|
| 206 |
func RevocationSigAlgorithms(c Configuration) []string { |
| 207 |
if !c.RevocationAuthMethodPrivateKeyJWTSupported() { |
| 208 |
return nil |
| 209 |
} |
| 210 |
return c.RevocationEndpointSigningAlgorithmsSupported() |
| 211 |
} |
| 212 |
|
| 213 |
func AuthMethodsRevocationEndpoint(c Configuration) []oidc.AuthMethod { |
| 214 |
authMethods := []oidc.AuthMethod{ |
| 215 |
oidc.AuthMethodNone, |
| 216 |
oidc.AuthMethodBasic, |
| 217 |
} |
| 218 |
if c.AuthMethodPostSupported() { |
| 219 |
authMethods = append(authMethods, oidc.AuthMethodPost) |
| 220 |
} |
| 221 |
if c.AuthMethodPrivateKeyJWTSupported() { |
| 222 |
authMethods = append(authMethods, oidc.AuthMethodPrivateKeyJWT) |
| 223 |
} |
| 224 |
return authMethods |
| 225 |
} |
| 226 |
|
| 227 |
func SupportedClaims(c Configuration) []string { |
| 228 |
provider, ok := c.(*Provider) |
| 229 |
if ok && provider.config.SupportedClaims != nil { |
| 230 |
return provider.config.SupportedClaims |
| 231 |
} |
| 232 |
|
| 233 |
return DefaultSupportedClaims |
| 234 |
} |
| 235 |
|
| 236 |
func CodeChallengeMethods(c Configuration) []oidc.CodeChallengeMethod { |
| 237 |
codeMethods := make([]oidc.CodeChallengeMethod, 0, 1) |
| 238 |
if c.CodeMethodS256Supported() { |
| 239 |
codeMethods = append(codeMethods, oidc.CodeChallengeMethodS256) |
| 240 |
} |
| 241 |
return codeMethods |
| 242 |
} |