2020-06-05 05:50:04 +00:00
|
|
|
package oidc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
2021-09-10 07:49:49 +00:00
|
|
|
"strings"
|
2020-06-05 05:50:04 +00:00
|
|
|
"time"
|
|
|
|
|
2024-06-17 09:50:12 +00:00
|
|
|
"github.com/zitadel/logging"
|
2023-10-17 15:19:51 +00:00
|
|
|
"github.com/zitadel/oidc/v3/pkg/oidc"
|
|
|
|
"github.com/zitadel/oidc/v3/pkg/op"
|
2020-06-05 05:50:04 +00:00
|
|
|
"golang.org/x/text/language"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
|
|
|
http_utils "github.com/zitadel/zitadel/internal/api/http"
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/user/model"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2020-06-05 05:50:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type AuthRequest struct {
|
2021-02-08 10:30:30 +00:00
|
|
|
*domain.AuthRequest
|
2020-06-05 05:50:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AuthRequest) GetID() string {
|
|
|
|
return a.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AuthRequest) GetACR() string {
|
|
|
|
// return a.
|
|
|
|
return "" //PLANNED: impl
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AuthRequest) GetAMR() []string {
|
2023-07-10 13:27:00 +00:00
|
|
|
list := make([]string, 0)
|
2020-06-05 05:50:04 +00:00
|
|
|
if a.PasswordVerified {
|
2023-07-12 12:24:01 +00:00
|
|
|
list = append(list, Password, PWD)
|
2020-06-05 05:50:04 +00:00
|
|
|
}
|
2020-12-02 16:00:04 +00:00
|
|
|
if len(a.MFAsVerified) > 0 {
|
2023-07-12 12:24:01 +00:00
|
|
|
list = append(list, MFA)
|
2020-12-02 16:00:04 +00:00
|
|
|
for _, mfa := range a.MFAsVerified {
|
|
|
|
if amrMFA := AMRFromMFAType(mfa); amrMFA != "" {
|
2023-07-10 13:27:00 +00:00
|
|
|
list = append(list, amrMFA)
|
2020-06-05 05:50:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-07-10 13:27:00 +00:00
|
|
|
return list
|
2020-06-05 05:50:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AuthRequest) GetAudience() []string {
|
|
|
|
return a.Audience
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AuthRequest) GetAuthTime() time.Time {
|
|
|
|
return a.AuthTime
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AuthRequest) GetClientID() string {
|
|
|
|
return a.ApplicationID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AuthRequest) GetCodeChallenge() *oidc.CodeChallenge {
|
|
|
|
return CodeChallengeToOIDC(a.oidc().CodeChallenge)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AuthRequest) GetNonce() string {
|
|
|
|
return a.oidc().Nonce
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AuthRequest) GetRedirectURI() string {
|
|
|
|
return a.CallbackURI
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AuthRequest) GetResponseType() oidc.ResponseType {
|
|
|
|
return ResponseTypeToOIDC(a.oidc().ResponseType)
|
|
|
|
}
|
|
|
|
|
2021-11-03 07:35:24 +00:00
|
|
|
func (a *AuthRequest) GetResponseMode() oidc.ResponseMode {
|
2024-06-17 09:50:12 +00:00
|
|
|
return ResponseModeToOIDC(a.oidc().ResponseMode)
|
2021-11-03 07:35:24 +00:00
|
|
|
}
|
|
|
|
|
2020-06-05 05:50:04 +00:00
|
|
|
func (a *AuthRequest) GetScopes() []string {
|
|
|
|
return a.oidc().Scopes
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AuthRequest) GetState() string {
|
|
|
|
return a.TransferState
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AuthRequest) GetSubject() string {
|
|
|
|
return a.UserID
|
|
|
|
}
|
|
|
|
|
2021-02-08 10:30:30 +00:00
|
|
|
func (a *AuthRequest) oidc() *domain.AuthRequestOIDC {
|
|
|
|
return a.Request.(*domain.AuthRequestOIDC)
|
2020-06-05 05:50:04 +00:00
|
|
|
}
|
|
|
|
|
2024-05-23 05:35:10 +00:00
|
|
|
func AuthRequestFromBusiness(authReq *domain.AuthRequest) (_ *AuthRequest, err error) {
|
2021-02-08 10:30:30 +00:00
|
|
|
if _, ok := authReq.Request.(*domain.AuthRequestOIDC); !ok {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "OIDC-Haz7A", "auth request is not of type oidc")
|
2020-06-05 05:50:04 +00:00
|
|
|
}
|
|
|
|
return &AuthRequest{authReq}, nil
|
|
|
|
}
|
|
|
|
|
2024-03-21 17:42:44 +00:00
|
|
|
func CreateAuthRequestToBusiness(ctx context.Context, authReq *oidc.AuthRequest, userAgentID, userID string, audience []string) *domain.AuthRequest {
|
2021-02-08 10:30:30 +00:00
|
|
|
return &domain.AuthRequest{
|
2021-09-10 07:49:49 +00:00
|
|
|
CreationDate: time.Now(),
|
|
|
|
AgentID: userAgentID,
|
|
|
|
BrowserInfo: ParseBrowserInfoFromContext(ctx),
|
|
|
|
ApplicationID: authReq.ClientID,
|
|
|
|
CallbackURI: authReq.RedirectURI,
|
|
|
|
TransferState: authReq.State,
|
|
|
|
Prompt: PromptToBusiness(authReq.Prompt),
|
|
|
|
PossibleLOAs: ACRValuesToBusiness(authReq.ACRValues),
|
|
|
|
UiLocales: UILocalesToBusiness(authReq.UILocales),
|
|
|
|
LoginHint: authReq.LoginHint,
|
|
|
|
SelectedIDPConfigID: GetSelectedIDPIDFromScopes(authReq.Scopes),
|
|
|
|
MaxAuthAge: MaxAgeToBusiness(authReq.MaxAge),
|
|
|
|
UserID: userID,
|
2022-03-29 09:53:19 +00:00
|
|
|
InstanceID: authz.GetInstance(ctx).InstanceID(),
|
2024-03-21 17:42:44 +00:00
|
|
|
Audience: audience,
|
2021-02-08 10:30:30 +00:00
|
|
|
Request: &domain.AuthRequestOIDC{
|
2020-06-05 05:50:04 +00:00
|
|
|
Scopes: authReq.Scopes,
|
|
|
|
ResponseType: ResponseTypeToBusiness(authReq.ResponseType),
|
2024-06-17 09:50:12 +00:00
|
|
|
ResponseMode: ResponseModeToBusiness(authReq.ResponseMode),
|
2020-06-05 05:50:04 +00:00
|
|
|
Nonce: authReq.Nonce,
|
|
|
|
CodeChallenge: CodeChallengeToBusiness(authReq.CodeChallenge, authReq.CodeChallengeMethod),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-08 10:30:30 +00:00
|
|
|
func ParseBrowserInfoFromContext(ctx context.Context) *domain.BrowserInfo {
|
2020-06-05 05:50:04 +00:00
|
|
|
userAgent, acceptLang := HttpHeadersFromContext(ctx)
|
|
|
|
ip := IpFromContext(ctx)
|
2021-02-08 10:30:30 +00:00
|
|
|
return &domain.BrowserInfo{RemoteIP: ip, UserAgent: userAgent, AcceptLanguage: acceptLang}
|
2020-06-05 05:50:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func HttpHeadersFromContext(ctx context.Context) (userAgent, acceptLang string) {
|
|
|
|
ctxHeaders, ok := http_utils.HeadersFromCtx(ctx)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2020-07-08 11:56:37 +00:00
|
|
|
if agents, ok := ctxHeaders[http_utils.UserAgentHeader]; ok {
|
2020-06-05 05:50:04 +00:00
|
|
|
userAgent = agents[0]
|
|
|
|
}
|
2020-07-08 11:56:37 +00:00
|
|
|
if langs, ok := ctxHeaders[http_utils.AcceptLanguage]; ok {
|
2020-06-05 05:50:04 +00:00
|
|
|
acceptLang = langs[0]
|
|
|
|
}
|
|
|
|
return userAgent, acceptLang
|
|
|
|
}
|
|
|
|
|
|
|
|
func IpFromContext(ctx context.Context) net.IP {
|
|
|
|
ipString := http_utils.RemoteIPFromCtx(ctx)
|
|
|
|
if ipString == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return net.ParseIP(ipString)
|
|
|
|
}
|
|
|
|
|
2021-06-16 08:02:15 +00:00
|
|
|
func PromptToBusiness(oidcPrompt []string) []domain.Prompt {
|
2024-12-04 20:56:36 +00:00
|
|
|
prompts := make([]domain.Prompt, 0, len(oidcPrompt))
|
2021-06-16 08:02:15 +00:00
|
|
|
for _, oidcPrompt := range oidcPrompt {
|
|
|
|
switch oidcPrompt {
|
|
|
|
case oidc.PromptNone:
|
|
|
|
prompts = append(prompts, domain.PromptNone)
|
|
|
|
case oidc.PromptLogin:
|
|
|
|
prompts = append(prompts, domain.PromptLogin)
|
|
|
|
case oidc.PromptConsent:
|
|
|
|
prompts = append(prompts, domain.PromptConsent)
|
|
|
|
case oidc.PromptSelectAccount:
|
|
|
|
prompts = append(prompts, domain.PromptSelectAccount)
|
|
|
|
case "create": //this prompt is not final yet, so not implemented in oidc lib
|
|
|
|
prompts = append(prompts, domain.PromptCreate)
|
|
|
|
}
|
2020-06-05 05:50:04 +00:00
|
|
|
}
|
2021-06-16 08:02:15 +00:00
|
|
|
return prompts
|
2020-06-05 05:50:04 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 10:30:30 +00:00
|
|
|
func ACRValuesToBusiness(values []string) []domain.LevelOfAssurance {
|
2020-06-05 05:50:04 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func UILocalesToBusiness(tags []language.Tag) []string {
|
|
|
|
if tags == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
locales := make([]string, len(tags))
|
|
|
|
for i, tag := range tags {
|
|
|
|
locales[i] = tag.String()
|
|
|
|
}
|
|
|
|
return locales
|
|
|
|
}
|
|
|
|
|
2021-09-10 07:49:49 +00:00
|
|
|
func GetSelectedIDPIDFromScopes(scopes oidc.SpaceDelimitedArray) string {
|
|
|
|
for _, scope := range scopes {
|
2022-03-31 09:36:26 +00:00
|
|
|
if strings.HasPrefix(scope, domain.SelectIDPScope) {
|
|
|
|
return strings.TrimPrefix(scope, domain.SelectIDPScope)
|
2021-09-10 07:49:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2021-06-16 08:02:15 +00:00
|
|
|
func MaxAgeToBusiness(maxAge *uint) *time.Duration {
|
|
|
|
if maxAge == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
dur := time.Duration(*maxAge) * time.Second
|
|
|
|
return &dur
|
|
|
|
}
|
|
|
|
|
2021-02-08 10:30:30 +00:00
|
|
|
func ResponseTypeToBusiness(responseType oidc.ResponseType) domain.OIDCResponseType {
|
2020-06-05 05:50:04 +00:00
|
|
|
switch responseType {
|
|
|
|
case oidc.ResponseTypeCode:
|
2021-02-08 10:30:30 +00:00
|
|
|
return domain.OIDCResponseTypeCode
|
2020-06-05 05:50:04 +00:00
|
|
|
case oidc.ResponseTypeIDTokenOnly:
|
2021-02-08 10:30:30 +00:00
|
|
|
return domain.OIDCResponseTypeIDToken
|
2020-07-09 13:52:20 +00:00
|
|
|
case oidc.ResponseTypeIDToken:
|
2021-02-08 10:30:30 +00:00
|
|
|
return domain.OIDCResponseTypeIDTokenToken
|
2020-06-05 05:50:04 +00:00
|
|
|
default:
|
2021-02-08 10:30:30 +00:00
|
|
|
return domain.OIDCResponseTypeCode
|
2020-06-05 05:50:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-08 10:30:30 +00:00
|
|
|
func ResponseTypeToOIDC(responseType domain.OIDCResponseType) oidc.ResponseType {
|
2020-06-05 05:50:04 +00:00
|
|
|
switch responseType {
|
2021-02-08 10:30:30 +00:00
|
|
|
case domain.OIDCResponseTypeCode:
|
2020-06-05 05:50:04 +00:00
|
|
|
return oidc.ResponseTypeCode
|
2021-02-08 10:30:30 +00:00
|
|
|
case domain.OIDCResponseTypeIDTokenToken:
|
2020-06-05 05:50:04 +00:00
|
|
|
return oidc.ResponseTypeIDToken
|
2021-02-08 10:30:30 +00:00
|
|
|
case domain.OIDCResponseTypeIDToken:
|
2020-06-05 05:50:04 +00:00
|
|
|
return oidc.ResponseTypeIDTokenOnly
|
|
|
|
default:
|
|
|
|
return oidc.ResponseTypeCode
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-17 09:50:12 +00:00
|
|
|
// ResponseModeToBusiness returns the OIDCResponseMode enum value from the domain package.
|
|
|
|
// An empty or invalid value defaults to unspecified.
|
|
|
|
func ResponseModeToBusiness(responseMode oidc.ResponseMode) domain.OIDCResponseMode {
|
|
|
|
if responseMode == "" {
|
|
|
|
return domain.OIDCResponseModeUnspecified
|
|
|
|
}
|
|
|
|
out, err := domain.OIDCResponseModeString(string(responseMode))
|
|
|
|
logging.OnError(err).Debugln("invalid oidc response_mode, using default")
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResponseModeToOIDC return the oidc string representation of the enum value from the domain package.
|
|
|
|
// When responseMode is `0 - unspecified`, an empty string is returned.
|
|
|
|
// This allows the oidc package to pick the appropriate response mode based on the response type.
|
|
|
|
func ResponseModeToOIDC(responseMode domain.OIDCResponseMode) oidc.ResponseMode {
|
|
|
|
if responseMode == domain.OIDCResponseModeUnspecified || !responseMode.IsAOIDCResponseMode() {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return oidc.ResponseMode(responseMode.String())
|
|
|
|
}
|
|
|
|
|
2021-02-08 10:30:30 +00:00
|
|
|
func CodeChallengeToBusiness(challenge string, method oidc.CodeChallengeMethod) *domain.OIDCCodeChallenge {
|
2020-06-05 05:50:04 +00:00
|
|
|
if challenge == "" {
|
|
|
|
return nil
|
|
|
|
}
|
2021-02-08 10:30:30 +00:00
|
|
|
challengeMethod := domain.CodeChallengeMethodPlain
|
2020-06-05 05:50:04 +00:00
|
|
|
if method == oidc.CodeChallengeMethodS256 {
|
2021-02-08 10:30:30 +00:00
|
|
|
challengeMethod = domain.CodeChallengeMethodS256
|
2020-06-05 05:50:04 +00:00
|
|
|
}
|
2021-02-08 10:30:30 +00:00
|
|
|
return &domain.OIDCCodeChallenge{
|
2020-06-05 05:50:04 +00:00
|
|
|
Challenge: challenge,
|
|
|
|
Method: challengeMethod,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-08 10:30:30 +00:00
|
|
|
func CodeChallengeToOIDC(challenge *domain.OIDCCodeChallenge) *oidc.CodeChallenge {
|
2020-06-05 05:50:04 +00:00
|
|
|
if challenge == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
challengeMethod := oidc.CodeChallengeMethodPlain
|
2021-02-08 10:30:30 +00:00
|
|
|
if challenge.Method == domain.CodeChallengeMethodS256 {
|
2020-06-05 05:50:04 +00:00
|
|
|
challengeMethod = oidc.CodeChallengeMethodS256
|
|
|
|
}
|
|
|
|
return &oidc.CodeChallenge{
|
|
|
|
Challenge: challenge.Challenge,
|
|
|
|
Method: challengeMethod,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-08 10:30:30 +00:00
|
|
|
func AMRFromMFAType(mfaType domain.MFAType) string {
|
2020-06-05 05:50:04 +00:00
|
|
|
switch mfaType {
|
2023-08-15 12:47:05 +00:00
|
|
|
case domain.MFATypeTOTP,
|
|
|
|
domain.MFATypeOTPSMS,
|
|
|
|
domain.MFATypeOTPEmail:
|
2023-07-12 12:24:01 +00:00
|
|
|
return OTP
|
2021-02-08 10:30:30 +00:00
|
|
|
case domain.MFATypeU2F,
|
|
|
|
domain.MFATypeU2FUserVerification:
|
2023-07-12 12:24:01 +00:00
|
|
|
return UserPresence
|
2020-06-05 05:50:04 +00:00
|
|
|
default:
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
2021-05-20 11:33:35 +00:00
|
|
|
|
|
|
|
func RefreshTokenRequestFromBusiness(tokenView *model.RefreshTokenView) op.RefreshTokenRequest {
|
|
|
|
return &RefreshTokenRequest{tokenView}
|
|
|
|
}
|
|
|
|
|
|
|
|
type RefreshTokenRequest struct {
|
|
|
|
*model.RefreshTokenView
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RefreshTokenRequest) GetAMR() []string {
|
|
|
|
return r.AuthMethodsReferences
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RefreshTokenRequest) GetAudience() []string {
|
|
|
|
return r.Audience
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RefreshTokenRequest) GetAuthTime() time.Time {
|
|
|
|
return r.AuthTime
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RefreshTokenRequest) GetClientID() string {
|
|
|
|
return r.ClientID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RefreshTokenRequest) GetScopes() []string {
|
|
|
|
return r.Scopes
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RefreshTokenRequest) GetSubject() string {
|
|
|
|
return r.UserID
|
|
|
|
}
|
|
|
|
|
2021-06-16 08:02:15 +00:00
|
|
|
func (r *RefreshTokenRequest) SetCurrentScopes(scopes []string) {
|
2021-05-20 11:33:35 +00:00
|
|
|
r.Scopes = scopes
|
|
|
|
}
|
2024-03-20 10:18:46 +00:00
|
|
|
|
|
|
|
func (r *RefreshTokenRequest) GetActor() *oidc.ActorClaims {
|
|
|
|
return actorDomainToClaims(r.Actor)
|
|
|
|
}
|