goweb-oidc/option.go

77 lines
1.4 KiB
Go
Raw Normal View History

2020-05-20 10:43:12 +02:00
package oidc
import (
"context"
2023-11-02 18:21:54 +01:00
"github.com/coreos/go-oidc/v3/oidc"
2020-05-20 10:43:12 +02:00
)
type OptionFunc func(*Option)
type Option struct {
Provider *oidc.Provider
ClientID string
ClientSecret string
RedirectURL string
Scopes []string
AcrValues string
SkipIssuerCheck bool
2020-05-20 10:43:12 +02:00
}
2020-05-20 13:06:04 +02:00
func WithRedirectURL(url string) OptionFunc {
return func(opt *Option) {
opt.RedirectURL = url
}
}
2020-05-20 10:43:12 +02:00
func WithCredentials(clientID, clientSecret string) OptionFunc {
return func(opt *Option) {
opt.ClientID = clientID
opt.ClientSecret = clientSecret
}
}
func WithScopes(scopes ...string) OptionFunc {
return func(opt *Option) {
opt.Scopes = scopes
}
}
2023-11-02 18:21:54 +01:00
func WithAcrValues(acrValues string) OptionFunc {
return func(opt *Option) {
opt.AcrValues = acrValues
}
}
func WithSkipIssuerCheck(skip bool) OptionFunc {
return func(opt *Option) {
opt.SkipIssuerCheck = skip
}
}
2023-11-02 18:21:54 +01:00
func NewProvider(ctx context.Context, issuer string, skipIssuerVerification bool) (*oidc.Provider, error) {
if skipIssuerVerification {
ctx = oidc.InsecureIssuerURLContext(ctx, issuer)
}
2020-05-20 10:43:12 +02:00
return oidc.NewProvider(ctx, issuer)
}
func WithProvider(provider *oidc.Provider) OptionFunc {
return func(opt *Option) {
opt.Provider = provider
}
}
func fromDefault(funcs ...OptionFunc) *Option {
opt := &Option{
Scopes: []string{oidc.ScopeOpenID},
}
for _, f := range funcs {
f(opt)
}
return opt
}