2020-06-05 05:50:04 +00:00
|
|
|
package oidc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/caos/logging"
|
|
|
|
"github.com/caos/oidc/pkg/op"
|
|
|
|
|
|
|
|
http_utils "github.com/caos/zitadel/internal/api/http"
|
2020-06-17 06:06:40 +00:00
|
|
|
"github.com/caos/zitadel/internal/api/http/middleware"
|
2020-06-05 05:50:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/auth/repository"
|
|
|
|
"github.com/caos/zitadel/internal/config/types"
|
|
|
|
"github.com/caos/zitadel/internal/id"
|
|
|
|
)
|
|
|
|
|
|
|
|
type OPHandlerConfig struct {
|
|
|
|
OPConfig *op.Config
|
|
|
|
StorageConfig StorageConfig
|
2020-08-31 06:49:35 +00:00
|
|
|
UserAgentCookieConfig *middleware.UserAgentCookieConfig
|
2020-06-17 06:06:40 +00:00
|
|
|
Cache *middleware.CacheConfig
|
2020-06-05 05:50:04 +00:00
|
|
|
Endpoints *EndpointConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
type StorageConfig struct {
|
|
|
|
DefaultLoginURL string
|
|
|
|
SigningKeyAlgorithm string
|
|
|
|
DefaultAccessTokenLifetime types.Duration
|
|
|
|
DefaultIdTokenLifetime types.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
type EndpointConfig struct {
|
|
|
|
Auth *Endpoint
|
|
|
|
Token *Endpoint
|
|
|
|
Userinfo *Endpoint
|
|
|
|
EndSession *Endpoint
|
|
|
|
Keys *Endpoint
|
|
|
|
}
|
|
|
|
|
|
|
|
type Endpoint struct {
|
|
|
|
Path string
|
|
|
|
URL string
|
|
|
|
}
|
|
|
|
|
|
|
|
type OPStorage struct {
|
|
|
|
repo repository.Repository
|
|
|
|
defaultLoginURL string
|
|
|
|
defaultAccessTokenLifetime time.Duration
|
|
|
|
defaultIdTokenLifetime time.Duration
|
|
|
|
signingKeyAlgorithm string
|
|
|
|
}
|
|
|
|
|
2020-08-31 06:49:35 +00:00
|
|
|
func NewProvider(ctx context.Context, config OPHandlerConfig, repo repository.Repository, localDevMode bool) op.OpenIDProvider {
|
|
|
|
cookieHandler, err := middleware.NewUserAgentHandler(config.UserAgentCookieConfig, id.SonyFlakeGenerator, localDevMode)
|
2020-06-05 05:50:04 +00:00
|
|
|
logging.Log("OIDC-sd4fd").OnError(err).Panic("cannot user agent handler")
|
2020-08-27 15:18:23 +00:00
|
|
|
config.OPConfig.CodeMethodS256 = true
|
2020-09-17 06:49:33 +00:00
|
|
|
provider, err := op.NewOpenIDProvider(
|
2020-06-05 05:50:04 +00:00
|
|
|
ctx,
|
|
|
|
config.OPConfig,
|
|
|
|
newStorage(config.StorageConfig, repo),
|
2020-08-31 06:49:35 +00:00
|
|
|
op.WithHttpInterceptors(
|
2020-10-21 08:18:34 +00:00
|
|
|
middleware.TraceHandler(),
|
2020-08-31 06:49:35 +00:00
|
|
|
middleware.NoCacheInterceptor,
|
|
|
|
cookieHandler,
|
|
|
|
http_utils.CopyHeadersToContext,
|
2020-06-05 05:50:04 +00:00
|
|
|
),
|
|
|
|
op.WithCustomAuthEndpoint(op.NewEndpointWithURL(config.Endpoints.Auth.Path, config.Endpoints.Auth.URL)),
|
|
|
|
op.WithCustomTokenEndpoint(op.NewEndpointWithURL(config.Endpoints.Token.Path, config.Endpoints.Token.URL)),
|
|
|
|
op.WithCustomUserinfoEndpoint(op.NewEndpointWithURL(config.Endpoints.Userinfo.Path, config.Endpoints.Userinfo.URL)),
|
|
|
|
op.WithCustomEndSessionEndpoint(op.NewEndpointWithURL(config.Endpoints.EndSession.Path, config.Endpoints.EndSession.URL)),
|
|
|
|
op.WithCustomKeysEndpoint(op.NewEndpointWithURL(config.Endpoints.Keys.Path, config.Endpoints.Keys.URL)),
|
|
|
|
op.WithRetry(3, time.Duration(30*time.Second)),
|
|
|
|
)
|
|
|
|
logging.Log("OIDC-asf13").OnError(err).Panic("cannot create provider")
|
|
|
|
return provider
|
|
|
|
}
|
|
|
|
|
|
|
|
func newStorage(config StorageConfig, repo repository.Repository) *OPStorage {
|
|
|
|
return &OPStorage{
|
|
|
|
repo: repo,
|
|
|
|
defaultLoginURL: config.DefaultLoginURL,
|
|
|
|
signingKeyAlgorithm: config.SigningKeyAlgorithm,
|
|
|
|
defaultAccessTokenLifetime: config.DefaultAccessTokenLifetime.Duration,
|
|
|
|
defaultIdTokenLifetime: config.DefaultIdTokenLifetime.Duration,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *OPStorage) Health(ctx context.Context) error {
|
|
|
|
return o.repo.Health(ctx)
|
|
|
|
}
|