| 1 |
package op |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"fmt" |
| 6 |
"log/slog" |
| 7 |
"net/http" |
| 8 |
"time" |
| 9 |
|
| 10 |
"github.com/go-chi/chi/v5" |
| 11 |
"github.com/go-jose/go-jose/v4" |
| 12 |
"github.com/rs/cors" |
| 13 |
"github.com/zitadel/oidc/v3/internal/otel" |
| 14 |
"github.com/zitadel/schema" |
| 15 |
"golang.org/x/text/language" |
| 16 |
|
| 17 |
httphelper "github.com/zitadel/oidc/v3/pkg/http" |
| 18 |
"github.com/zitadel/oidc/v3/pkg/oidc" |
| 19 |
) |
| 20 |
|
| 21 |
const ( |
| 22 |
healthEndpoint = "/healthz" |
| 23 |
readinessEndpoint = "/ready" |
| 24 |
authCallbackPathSuffix = "/callback" |
| 25 |
defaultAuthorizationEndpoint = "authorize" |
| 26 |
defaultTokenEndpoint = "oauth/token" |
| 27 |
defaultIntrospectEndpoint = "oauth/introspect" |
| 28 |
defaultUserinfoEndpoint = "userinfo" |
| 29 |
defaultRevocationEndpoint = "revoke" |
| 30 |
defaultEndSessionEndpoint = "end_session" |
| 31 |
defaultKeysEndpoint = "keys" |
| 32 |
defaultDeviceAuthzEndpoint = "/device_authorization" |
| 33 |
) |
| 34 |
|
| 35 |
var ( |
| 36 |
DefaultEndpoints = &Endpoints{ |
| 37 |
Authorization: NewEndpoint(defaultAuthorizationEndpoint), |
| 38 |
Token: NewEndpoint(defaultTokenEndpoint), |
| 39 |
Introspection: NewEndpoint(defaultIntrospectEndpoint), |
| 40 |
Userinfo: NewEndpoint(defaultUserinfoEndpoint), |
| 41 |
Revocation: NewEndpoint(defaultRevocationEndpoint), |
| 42 |
EndSession: NewEndpoint(defaultEndSessionEndpoint), |
| 43 |
JwksURI: NewEndpoint(defaultKeysEndpoint), |
| 44 |
DeviceAuthorization: NewEndpoint(defaultDeviceAuthzEndpoint), |
| 45 |
} |
| 46 |
|
| 47 |
DefaultSupportedClaims = []string{ |
| 48 |
"sub", |
| 49 |
"aud", |
| 50 |
"exp", |
| 51 |
"iat", |
| 52 |
"iss", |
| 53 |
"auth_time", |
| 54 |
"nonce", |
| 55 |
"acr", |
| 56 |
"amr", |
| 57 |
"c_hash", |
| 58 |
"at_hash", |
| 59 |
"act", |
| 60 |
"scopes", |
| 61 |
"client_id", |
| 62 |
"azp", |
| 63 |
"preferred_username", |
| 64 |
"name", |
| 65 |
"family_name", |
| 66 |
"given_name", |
| 67 |
"locale", |
| 68 |
"email", |
| 69 |
"email_verified", |
| 70 |
"phone_number", |
| 71 |
"phone_number_verified", |
| 72 |
} |
| 73 |
|
| 74 |
defaultCORSOptions = cors.Options{ |
| 75 |
AllowCredentials: true, |
| 76 |
AllowedHeaders: []string{ |
| 77 |
"Origin", |
| 78 |
"Accept", |
| 79 |
"Accept-Language", |
| 80 |
"Authorization", |
| 81 |
"Content-Type", |
| 82 |
"X-Requested-With", |
| 83 |
}, |
| 84 |
AllowedMethods: []string{ |
| 85 |
http.MethodGet, |
| 86 |
http.MethodHead, |
| 87 |
http.MethodPost, |
| 88 |
}, |
| 89 |
ExposedHeaders: []string{ |
| 90 |
"Location", |
| 91 |
"Content-Length", |
| 92 |
}, |
| 93 |
AllowOriginFunc: func(_ string) bool { |
| 94 |
return true |
| 95 |
}, |
| 96 |
} |
| 97 |
) |
| 98 |
|
| 99 |
var Tracer = otel.Tracer("github.com/zitadel/oidc/pkg/op") |
| 100 |
|
| 101 |
type OpenIDProvider interface { |
| 102 |
http.Handler |
| 103 |
Configuration |
| 104 |
Storage() Storage |
| 105 |
Decoder() httphelper.Decoder |
| 106 |
Encoder() httphelper.Encoder |
| 107 |
IDTokenHintVerifier(context.Context) *IDTokenHintVerifier |
| 108 |
AccessTokenVerifier(context.Context) *AccessTokenVerifier |
| 109 |
Crypto() Crypto |
| 110 |
DefaultLogoutRedirectURI() string |
| 111 |
Probes() []ProbesFn |
| 112 |
// Deprecated: configure logging with slog.SetDefault. |
| 113 |
Logger() *slog.Logger |
| 114 |
|
| 115 |
// Deprecated: Provider now implements http.Handler directly. |
| 116 |
HttpHandler() http.Handler |
| 117 |
} |
| 118 |
|
| 119 |
type HttpInterceptor func(http.Handler) http.Handler |
| 120 |
|
| 121 |
type corsOptioner interface { |
| 122 |
CORSOptions() *cors.Options |
| 123 |
} |
| 124 |
|
| 125 |
func CreateRouter(o OpenIDProvider, interceptors ...HttpInterceptor) chi.Router { |
| 126 |
router := chi.NewRouter() |
| 127 |
if co, ok := o.(corsOptioner); ok { |
| 128 |
if opts := co.CORSOptions(); opts != nil { |
| 129 |
router.Use(cors.New(*opts).Handler) |
| 130 |
} |
| 131 |
} else { |
| 132 |
router.Use(cors.New(defaultCORSOptions).Handler) |
| 133 |
} |
| 134 |
router.Use(intercept(o.IssuerFromRequest, interceptors...)) |
| 135 |
router.HandleFunc(healthEndpoint, healthHandler) |
| 136 |
router.HandleFunc(readinessEndpoint, readyHandler(o.Probes())) |
| 137 |
router.HandleFunc(oidc.DiscoveryEndpoint, discoveryHandler(o, o.Storage())) |
| 138 |
router.HandleFunc(o.AuthorizationEndpoint().Relative(), authorizeHandler(o)) |
| 139 |
router.HandleFunc(authCallbackPath(o), AuthorizeCallbackHandler(o)) |
| 140 |
router.HandleFunc(o.TokenEndpoint().Relative(), tokenHandler(o)) |
| 141 |
router.HandleFunc(o.IntrospectionEndpoint().Relative(), introspectionHandler(o)) |
| 142 |
router.HandleFunc(o.UserinfoEndpoint().Relative(), userinfoHandler(o)) |
| 143 |
router.HandleFunc(o.RevocationEndpoint().Relative(), revocationHandler(o)) |
| 144 |
router.HandleFunc(o.EndSessionEndpoint().Relative(), endSessionHandler(o)) |
| 145 |
router.HandleFunc(o.KeysEndpoint().Relative(), keysHandler(o.Storage())) |
| 146 |
router.HandleFunc(o.DeviceAuthorizationEndpoint().Relative(), DeviceAuthorizationHandler(o)) |
| 147 |
return router |
| 148 |
} |
| 149 |
|
| 150 |
// AuthCallbackURL builds the url for the redirect (with the requestID) after a successful login |
| 151 |
func AuthCallbackURL(o OpenIDProvider) func(context.Context, string) string { |
| 152 |
return func(ctx context.Context, requestID string) string { |
| 153 |
return o.AuthorizationEndpoint().Absolute(IssuerFromContext(ctx)) + authCallbackPathSuffix + "?id=" + requestID |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
func authCallbackPath(o OpenIDProvider) string { |
| 158 |
return o.AuthorizationEndpoint().Relative() + authCallbackPathSuffix |
| 159 |
} |
| 160 |
|
| 161 |
type Config struct { |
| 162 |
CryptoKey [32]byte // for encrypting access token via NewAESCrypto; will be overwritten by WithCrypto |
| 163 |
CryptoKeyId string |
| 164 |
DefaultLogoutRedirectURI string |
| 165 |
CodeMethodS256 bool |
| 166 |
AuthMethodPost bool |
| 167 |
AuthMethodPrivateKeyJWT bool |
| 168 |
GrantTypeRefreshToken bool |
| 169 |
RequestObjectSupported bool |
| 170 |
SupportedUILocales []language.Tag |
| 171 |
SupportedClaims []string |
| 172 |
SupportedScopes []string |
| 173 |
DeviceAuthorization DeviceAuthorizationConfig |
| 174 |
BackChannelLogoutSupported bool |
| 175 |
BackChannelLogoutSessionSupported bool |
| 176 |
} |
| 177 |
|
| 178 |
// Endpoints defines endpoint routes. |
| 179 |
type Endpoints struct { |
| 180 |
Authorization *Endpoint |
| 181 |
Token *Endpoint |
| 182 |
Introspection *Endpoint |
| 183 |
Userinfo *Endpoint |
| 184 |
Revocation *Endpoint |
| 185 |
EndSession *Endpoint |
| 186 |
CheckSessionIframe *Endpoint |
| 187 |
JwksURI *Endpoint |
| 188 |
DeviceAuthorization *Endpoint |
| 189 |
} |
| 190 |
|
| 191 |
// NewOpenIDProvider creates a provider. The provider provides (with HttpHandler()) |
| 192 |
// a http.Router that handles a suite of endpoints (some paths can be overridden): |
| 193 |
// |
| 194 |
// /healthz |
| 195 |
// /ready |
| 196 |
// /.well-known/openid-configuration |
| 197 |
// /oauth/token |
| 198 |
// /oauth/introspect |
| 199 |
// /callback |
| 200 |
// /authorize |
| 201 |
// /userinfo |
| 202 |
// /revoke |
| 203 |
// /end_session |
| 204 |
// /keys |
| 205 |
// /device_authorization |
| 206 |
// |
| 207 |
// This does not include login. Login is handled with a redirect that includes the |
| 208 |
// request ID. The redirect for logins is specified per-client by Client.LoginURL(). |
| 209 |
// Successful logins should mark the request as authorized and redirect back to |
| 210 |
// op.AuthCallbackURL(provider) which is probably /callback. On the redirect back |
| 211 |
// to the AuthCallbackURL, the request id should be passed as the "id" parameter. |
| 212 |
// |
| 213 |
// Deprecated: use [NewProvider] with an issuer function direct. |
| 214 |
func NewOpenIDProvider(issuer string, config *Config, storage Storage, opOpts ...Option) (*Provider, error) { |
| 215 |
return NewProvider(config, storage, StaticIssuer(issuer), opOpts...) |
| 216 |
} |
| 217 |
|
| 218 |
// NewForwardedOpenIDProvider tries to establish the issuer from the request Host. |
| 219 |
// |
| 220 |
// Deprecated: use [NewProvider] with an issuer function direct. |
| 221 |
func NewDynamicOpenIDProvider(path string, config *Config, storage Storage, opOpts ...Option) (*Provider, error) { |
| 222 |
return NewProvider(config, storage, IssuerFromHost(path), opOpts...) |
| 223 |
} |
| 224 |
|
| 225 |
// NewForwardedOpenIDProvider tries to establish the Issuer from a Forwarded request header, if it is set. |
| 226 |
// See [IssuerFromForwardedOrHost] for details. |
| 227 |
// |
| 228 |
// Deprecated: use [NewProvider] with an issuer function direct. |
| 229 |
func NewForwardedOpenIDProvider(path string, config *Config, storage Storage, opOpts ...Option) (*Provider, error) { |
| 230 |
return NewProvider(config, storage, IssuerFromForwardedOrHost(path), opOpts...) |
| 231 |
} |
| 232 |
|
| 233 |
// NewProvider creates a provider with a router on it's embedded http.Handler. |
| 234 |
// Issuer is a function that must return the issuer on every request. |
| 235 |
// Typically [StaticIssuer], [IssuerFromHost] or [IssuerFromForwardedOrHost] can be used. |
| 236 |
// |
| 237 |
// The router handles a suite of endpoints (some paths can be overridden): |
| 238 |
// |
| 239 |
// /healthz |
| 240 |
// /ready |
| 241 |
// /.well-known/openid-configuration |
| 242 |
// /oauth/token |
| 243 |
// /oauth/introspect |
| 244 |
// /callback |
| 245 |
// /authorize |
| 246 |
// /userinfo |
| 247 |
// /revoke |
| 248 |
// /end_session |
| 249 |
// /keys |
| 250 |
// /device_authorization |
| 251 |
// |
| 252 |
// This does not include login. Login is handled with a redirect that includes the |
| 253 |
// request ID. The redirect for logins is specified per-client by Client.LoginURL(). |
| 254 |
// Successful logins should mark the request as authorized and redirect back to |
| 255 |
// op.AuthCallbackURL(provider) which is probably /callback. On the redirect back |
| 256 |
// to the AuthCallbackURL, the request id should be passed as the "id" parameter. |
| 257 |
func NewProvider( |
| 258 |
config *Config, |
| 259 |
storage Storage, |
| 260 |
issuer func(insecure bool) (IssuerFromRequest, error), |
| 261 |
opOpts ...Option, |
| 262 |
) (_ *Provider, err error) { |
| 263 |
keySet := &OpenIDKeySet{storage} |
| 264 |
easgcmCrypto := NewAES256GCMCrypto(config.CryptoKey, config.CryptoKeyId) |
| 265 |
crypto := NewCompositeCrypto( |
| 266 |
easgcmCrypto, |
| 267 |
[]Decrypter{ |
| 268 |
easgcmCrypto, |
| 269 |
NewAESCrypto(config.CryptoKey), |
| 270 |
}, |
| 271 |
) |
| 272 |
o := &Provider{ |
| 273 |
config: config, |
| 274 |
storage: storage, |
| 275 |
accessTokenKeySet: keySet, |
| 276 |
idTokenHinKeySet: keySet, |
| 277 |
crypto: crypto, |
| 278 |
endpoints: DefaultEndpoints, |
| 279 |
timer: make(<-chan time.Time), |
| 280 |
corsOpts: &defaultCORSOptions, |
| 281 |
} |
| 282 |
|
| 283 |
for _, optFunc := range opOpts { |
| 284 |
if err := optFunc(o); err != nil { |
| 285 |
return nil, err |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
o.issuer, err = issuer(o.insecure) |
| 290 |
if err != nil { |
| 291 |
return nil, err |
| 292 |
} |
| 293 |
o.Handler = CreateRouter(o, o.interceptors...) |
| 294 |
o.decoder = schema.NewDecoder() |
| 295 |
o.decoder.IgnoreUnknownKeys(true) |
| 296 |
o.encoder = oidc.NewEncoder() |
| 297 |
return o, nil |
| 298 |
} |
| 299 |
|
| 300 |
type Provider struct { |
| 301 |
http.Handler |
| 302 |
config *Config |
| 303 |
issuer IssuerFromRequest |
| 304 |
insecure bool |
| 305 |
endpoints *Endpoints |
| 306 |
storage Storage |
| 307 |
accessTokenKeySet oidc.KeySet |
| 308 |
idTokenHinKeySet oidc.KeySet |
| 309 |
crypto Crypto |
| 310 |
decoder *schema.Decoder |
| 311 |
encoder *schema.Encoder |
| 312 |
interceptors []HttpInterceptor |
| 313 |
timer <-chan time.Time |
| 314 |
accessTokenVerifierOpts []AccessTokenVerifierOpt |
| 315 |
idTokenHintVerifierOpts []IDTokenHintVerifierOpt |
| 316 |
corsOpts *cors.Options |
| 317 |
} |
| 318 |
|
| 319 |
func (o *Provider) IssuerFromRequest(r *http.Request) string { |
| 320 |
return o.issuer(r) |
| 321 |
} |
| 322 |
|
| 323 |
func (o *Provider) Insecure() bool { |
| 324 |
return o.insecure |
| 325 |
} |
| 326 |
|
| 327 |
func (o *Provider) AuthorizationEndpoint() *Endpoint { |
| 328 |
return o.endpoints.Authorization |
| 329 |
} |
| 330 |
|
| 331 |
func (o *Provider) TokenEndpoint() *Endpoint { |
| 332 |
return o.endpoints.Token |
| 333 |
} |
| 334 |
|
| 335 |
func (o *Provider) IntrospectionEndpoint() *Endpoint { |
| 336 |
return o.endpoints.Introspection |
| 337 |
} |
| 338 |
|
| 339 |
func (o *Provider) UserinfoEndpoint() *Endpoint { |
| 340 |
return o.endpoints.Userinfo |
| 341 |
} |
| 342 |
|
| 343 |
func (o *Provider) RevocationEndpoint() *Endpoint { |
| 344 |
return o.endpoints.Revocation |
| 345 |
} |
| 346 |
|
| 347 |
func (o *Provider) EndSessionEndpoint() *Endpoint { |
| 348 |
return o.endpoints.EndSession |
| 349 |
} |
| 350 |
|
| 351 |
func (o *Provider) DeviceAuthorizationEndpoint() *Endpoint { |
| 352 |
return o.endpoints.DeviceAuthorization |
| 353 |
} |
| 354 |
|
| 355 |
func (o *Provider) CheckSessionIframe() *Endpoint { |
| 356 |
return o.endpoints.CheckSessionIframe |
| 357 |
} |
| 358 |
|
| 359 |
func (o *Provider) KeysEndpoint() *Endpoint { |
| 360 |
return o.endpoints.JwksURI |
| 361 |
} |
| 362 |
|
| 363 |
func (o *Provider) AuthMethodPostSupported() bool { |
| 364 |
return o.config.AuthMethodPost |
| 365 |
} |
| 366 |
|
| 367 |
func (o *Provider) CodeMethodS256Supported() bool { |
| 368 |
return o.config.CodeMethodS256 |
| 369 |
} |
| 370 |
|
| 371 |
func (o *Provider) AuthMethodPrivateKeyJWTSupported() bool { |
| 372 |
return o.config.AuthMethodPrivateKeyJWT |
| 373 |
} |
| 374 |
|
| 375 |
func (o *Provider) TokenEndpointSigningAlgorithmsSupported() []string { |
| 376 |
return []string{"RS256"} |
| 377 |
} |
| 378 |
|
| 379 |
func (o *Provider) GrantTypeRefreshTokenSupported() bool { |
| 380 |
return o.config.GrantTypeRefreshToken |
| 381 |
} |
| 382 |
|
| 383 |
func (o *Provider) GrantTypeTokenExchangeSupported() bool { |
| 384 |
_, ok := o.storage.(TokenExchangeStorage) |
| 385 |
return ok |
| 386 |
} |
| 387 |
|
| 388 |
func (o *Provider) GrantTypeJWTAuthorizationSupported() bool { |
| 389 |
return true |
| 390 |
} |
| 391 |
|
| 392 |
func (o *Provider) GrantTypeDeviceCodeSupported() bool { |
| 393 |
_, ok := o.storage.(DeviceAuthorizationStorage) |
| 394 |
return ok |
| 395 |
} |
| 396 |
|
| 397 |
func (o *Provider) IntrospectionAuthMethodPrivateKeyJWTSupported() bool { |
| 398 |
return true |
| 399 |
} |
| 400 |
|
| 401 |
func (o *Provider) IntrospectionEndpointSigningAlgorithmsSupported() []string { |
| 402 |
return []string{"RS256"} |
| 403 |
} |
| 404 |
|
| 405 |
func (o *Provider) GrantTypeClientCredentialsSupported() bool { |
| 406 |
_, ok := o.storage.(ClientCredentialsStorage) |
| 407 |
return ok |
| 408 |
} |
| 409 |
|
| 410 |
func (o *Provider) RevocationAuthMethodPrivateKeyJWTSupported() bool { |
| 411 |
return true |
| 412 |
} |
| 413 |
|
| 414 |
func (o *Provider) RevocationEndpointSigningAlgorithmsSupported() []string { |
| 415 |
return []string{"RS256"} |
| 416 |
} |
| 417 |
|
| 418 |
func (o *Provider) RequestObjectSupported() bool { |
| 419 |
return o.config.RequestObjectSupported |
| 420 |
} |
| 421 |
|
| 422 |
func (o *Provider) RequestObjectSigningAlgorithmsSupported() []string { |
| 423 |
return []string{"RS256"} |
| 424 |
} |
| 425 |
|
| 426 |
func (o *Provider) SupportedUILocales() []language.Tag { |
| 427 |
return o.config.SupportedUILocales |
| 428 |
} |
| 429 |
|
| 430 |
func (o *Provider) DeviceAuthorization() DeviceAuthorizationConfig { |
| 431 |
return o.config.DeviceAuthorization |
| 432 |
} |
| 433 |
|
| 434 |
func (o *Provider) BackChannelLogoutSupported() bool { |
| 435 |
return o.config.BackChannelLogoutSupported |
| 436 |
} |
| 437 |
|
| 438 |
func (o *Provider) BackChannelLogoutSessionSupported() bool { |
| 439 |
return o.config.BackChannelLogoutSessionSupported |
| 440 |
} |
| 441 |
|
| 442 |
func (o *Provider) Storage() Storage { |
| 443 |
return o.storage |
| 444 |
} |
| 445 |
|
| 446 |
func (o *Provider) Decoder() httphelper.Decoder { |
| 447 |
return o.decoder |
| 448 |
} |
| 449 |
|
| 450 |
func (o *Provider) Encoder() httphelper.Encoder { |
| 451 |
return o.encoder |
| 452 |
} |
| 453 |
|
| 454 |
func (o *Provider) IDTokenHintVerifier(ctx context.Context) *IDTokenHintVerifier { |
| 455 |
return NewIDTokenHintVerifier(IssuerFromContext(ctx), o.idTokenHinKeySet, o.idTokenHintVerifierOpts...) |
| 456 |
} |
| 457 |
|
| 458 |
func (o *Provider) JWTProfileVerifier(ctx context.Context) *JWTProfileVerifier { |
| 459 |
return NewJWTProfileVerifier(o.Storage(), IssuerFromContext(ctx), 1*time.Hour, time.Second) |
| 460 |
} |
| 461 |
|
| 462 |
func (o *Provider) AccessTokenVerifier(ctx context.Context) *AccessTokenVerifier { |
| 463 |
return NewAccessTokenVerifier(IssuerFromContext(ctx), o.accessTokenKeySet, o.accessTokenVerifierOpts...) |
| 464 |
} |
| 465 |
|
| 466 |
func (o *Provider) Crypto() Crypto { |
| 467 |
return o.crypto |
| 468 |
} |
| 469 |
|
| 470 |
func (o *Provider) DefaultLogoutRedirectURI() string { |
| 471 |
return o.config.DefaultLogoutRedirectURI |
| 472 |
} |
| 473 |
|
| 474 |
func (o *Provider) Probes() []ProbesFn { |
| 475 |
return []ProbesFn{ |
| 476 |
ReadyStorage(o.Storage()), |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
func (o *Provider) CORSOptions() *cors.Options { |
| 481 |
return o.corsOpts |
| 482 |
} |
| 483 |
|
| 484 |
// Logger returns the global default logger. |
| 485 |
// Deprecated: configure logging with [slog.SetDefault]. |
| 486 |
func (o *Provider) Logger() *slog.Logger { |
| 487 |
return slog.Default() |
| 488 |
} |
| 489 |
|
| 490 |
// Deprecated: Provider now implements http.Handler directly. |
| 491 |
func (o *Provider) HttpHandler() http.Handler { |
| 492 |
return o |
| 493 |
} |
| 494 |
|
| 495 |
type OpenIDKeySet struct { |
| 496 |
Storage |
| 497 |
} |
| 498 |
|
| 499 |
// VerifySignature implements the oidc.KeySet interface |
| 500 |
// providing an implementation for the keys stored in the OP Storage interface |
| 501 |
func (o *OpenIDKeySet) VerifySignature(ctx context.Context, jws *jose.JSONWebSignature) ([]byte, error) { |
| 502 |
keySet, err := o.Storage.KeySet(ctx) |
| 503 |
if err != nil { |
| 504 |
return nil, fmt.Errorf("error fetching keys: %w", err) |
| 505 |
} |
| 506 |
keyID, alg := oidc.GetKeyIDAndAlg(jws) |
| 507 |
key, err := oidc.FindMatchingKey(keyID, oidc.KeyUseSignature, alg, jsonWebKeySet(keySet).Keys...) |
| 508 |
if err != nil { |
| 509 |
return nil, fmt.Errorf("invalid signature: %w", err) |
| 510 |
} |
| 511 |
return jws.Verify(&key) |
| 512 |
} |
| 513 |
|
| 514 |
type Option func(o *Provider) error |
| 515 |
|
| 516 |
// WithAllowInsecure allows the use of http (instead of https) for issuers |
| 517 |
// this is not recommended for production use and violates the OIDC specification |
| 518 |
func WithAllowInsecure() Option { |
| 519 |
return func(o *Provider) error { |
| 520 |
o.insecure = true |
| 521 |
return nil |
| 522 |
} |
| 523 |
} |
| 524 |
|
| 525 |
func WithCustomAuthEndpoint(endpoint *Endpoint) Option { |
| 526 |
return func(o *Provider) error { |
| 527 |
if err := endpoint.Validate(); err != nil { |
| 528 |
return err |
| 529 |
} |
| 530 |
o.endpoints.Authorization = endpoint |
| 531 |
return nil |
| 532 |
} |
| 533 |
} |
| 534 |
|
| 535 |
func WithCustomTokenEndpoint(endpoint *Endpoint) Option { |
| 536 |
return func(o *Provider) error { |
| 537 |
if err := endpoint.Validate(); err != nil { |
| 538 |
return err |
| 539 |
} |
| 540 |
o.endpoints.Token = endpoint |
| 541 |
return nil |
| 542 |
} |
| 543 |
} |
| 544 |
|
| 545 |
func WithCustomIntrospectionEndpoint(endpoint *Endpoint) Option { |
| 546 |
return func(o *Provider) error { |
| 547 |
if err := endpoint.Validate(); err != nil { |
| 548 |
return err |
| 549 |
} |
| 550 |
o.endpoints.Introspection = endpoint |
| 551 |
return nil |
| 552 |
} |
| 553 |
} |
| 554 |
|
| 555 |
func WithCustomUserinfoEndpoint(endpoint *Endpoint) Option { |
| 556 |
return func(o *Provider) error { |
| 557 |
if err := endpoint.Validate(); err != nil { |
| 558 |
return err |
| 559 |
} |
| 560 |
o.endpoints.Userinfo = endpoint |
| 561 |
return nil |
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
func WithCustomRevocationEndpoint(endpoint *Endpoint) Option { |
| 566 |
return func(o *Provider) error { |
| 567 |
if err := endpoint.Validate(); err != nil { |
| 568 |
return err |
| 569 |
} |
| 570 |
o.endpoints.Revocation = endpoint |
| 571 |
return nil |
| 572 |
} |
| 573 |
} |
| 574 |
|
| 575 |
func WithCustomEndSessionEndpoint(endpoint *Endpoint) Option { |
| 576 |
return func(o *Provider) error { |
| 577 |
if err := endpoint.Validate(); err != nil { |
| 578 |
return err |
| 579 |
} |
| 580 |
o.endpoints.EndSession = endpoint |
| 581 |
return nil |
| 582 |
} |
| 583 |
} |
| 584 |
|
| 585 |
func WithCustomKeysEndpoint(endpoint *Endpoint) Option { |
| 586 |
return func(o *Provider) error { |
| 587 |
if err := endpoint.Validate(); err != nil { |
| 588 |
return err |
| 589 |
} |
| 590 |
o.endpoints.JwksURI = endpoint |
| 591 |
return nil |
| 592 |
} |
| 593 |
} |
| 594 |
|
| 595 |
func WithCustomDeviceAuthorizationEndpoint(endpoint *Endpoint) Option { |
| 596 |
return func(o *Provider) error { |
| 597 |
if err := endpoint.Validate(); err != nil { |
| 598 |
return err |
| 599 |
} |
| 600 |
o.endpoints.DeviceAuthorization = endpoint |
| 601 |
return nil |
| 602 |
} |
| 603 |
} |
| 604 |
|
| 605 |
// WithCustomEndpoints sets multiple endpoints at once. |
| 606 |
// None of the endpoints may be nil, or an error will |
| 607 |
// be returned when the Option used by the Provider. |
| 608 |
func WithCustomEndpoints(auth, token, userInfo, revocation, endSession, keys *Endpoint) Option { |
| 609 |
return func(o *Provider) error { |
| 610 |
for _, e := range []*Endpoint{auth, token, userInfo, revocation, endSession, keys} { |
| 611 |
if err := e.Validate(); err != nil { |
| 612 |
return err |
| 613 |
} |
| 614 |
} |
| 615 |
o.endpoints.Authorization = auth |
| 616 |
o.endpoints.Token = token |
| 617 |
o.endpoints.Userinfo = userInfo |
| 618 |
o.endpoints.Revocation = revocation |
| 619 |
o.endpoints.EndSession = endSession |
| 620 |
o.endpoints.JwksURI = keys |
| 621 |
return nil |
| 622 |
} |
| 623 |
} |
| 624 |
|
| 625 |
func WithHttpInterceptors(interceptors ...HttpInterceptor) Option { |
| 626 |
return func(o *Provider) error { |
| 627 |
o.interceptors = append(o.interceptors, interceptors...) |
| 628 |
return nil |
| 629 |
} |
| 630 |
} |
| 631 |
|
| 632 |
// WithAccessTokenKeySet allows passing a KeySet with public keys for Access Token verification. |
| 633 |
// The default KeySet uses the [Storage] interface |
| 634 |
func WithAccessTokenKeySet(keySet oidc.KeySet) Option { |
| 635 |
return func(o *Provider) error { |
| 636 |
o.accessTokenKeySet = keySet |
| 637 |
return nil |
| 638 |
} |
| 639 |
} |
| 640 |
|
| 641 |
func WithAccessTokenVerifierOpts(opts ...AccessTokenVerifierOpt) Option { |
| 642 |
return func(o *Provider) error { |
| 643 |
o.accessTokenVerifierOpts = opts |
| 644 |
return nil |
| 645 |
} |
| 646 |
} |
| 647 |
|
| 648 |
// WithIDTokenHintKeySet allows passing a KeySet with public keys for ID Token Hint verification. |
| 649 |
// The default KeySet uses the [Storage] interface. |
| 650 |
func WithIDTokenHintKeySet(keySet oidc.KeySet) Option { |
| 651 |
return func(o *Provider) error { |
| 652 |
o.idTokenHinKeySet = keySet |
| 653 |
return nil |
| 654 |
} |
| 655 |
} |
| 656 |
|
| 657 |
func WithIDTokenHintVerifierOpts(opts ...IDTokenHintVerifierOpt) Option { |
| 658 |
return func(o *Provider) error { |
| 659 |
o.idTokenHintVerifierOpts = opts |
| 660 |
return nil |
| 661 |
} |
| 662 |
} |
| 663 |
|
| 664 |
func WithCORSOptions(opts *cors.Options) Option { |
| 665 |
return func(o *Provider) error { |
| 666 |
o.corsOpts = opts |
| 667 |
return nil |
| 668 |
} |
| 669 |
} |
| 670 |
|
| 671 |
// WithLogger is retained for source compatibility. |
| 672 |
// Deprecated: use [slog.SetDefault]. This option has no effect. |
| 673 |
func WithLogger(*slog.Logger) Option { |
| 674 |
return func(*Provider) error { |
| 675 |
return nil |
| 676 |
} |
| 677 |
} |
| 678 |
|
| 679 |
// WithCrypto allows the user to pass their own Crypto implementation. |
| 680 |
// |
| 681 |
// If provided, this will overwrite Config.CryptoKey. |
| 682 |
func WithCrypto(crypto Crypto) Option { |
| 683 |
return func(o *Provider) error { |
| 684 |
o.crypto = crypto |
| 685 |
return nil |
| 686 |
} |
| 687 |
} |
| 688 |
|
| 689 |
func intercept(i IssuerFromRequest, interceptors ...HttpInterceptor) func(handler http.Handler) http.Handler { |
| 690 |
issuerInterceptor := NewIssuerInterceptor(i) |
| 691 |
return func(handler http.Handler) http.Handler { |
| 692 |
for i := len(interceptors) - 1; i >= 0; i-- { |
| 693 |
handler = interceptors[i](handler) |
| 694 |
} |
| 695 |
return issuerInterceptor.Handler(handler) |
| 696 |
} |
| 697 |
} |