| 1 |
package storage |
| 2 |
|
| 3 |
import ( |
| 4 |
"log/slog" |
| 5 |
"time" |
| 6 |
|
| 7 |
"golang.org/x/text/language" |
| 8 |
|
| 9 |
"github.com/zitadel/oidc/v3/pkg/oidc" |
| 10 |
"github.com/zitadel/oidc/v3/pkg/op" |
| 11 |
) |
| 12 |
|
| 13 |
const ( |
| 14 |
// CustomScope is an example for how to use custom scopes in this library |
| 15 |
//(in this scenario, when requested, it will return a custom claim) |
| 16 |
CustomScope = "custom_scope" |
| 17 |
|
| 18 |
// CustomClaim is an example for how to return custom claims with this library |
| 19 |
CustomClaim = "custom_claim" |
| 20 |
|
| 21 |
// CustomScopeImpersonatePrefix is an example scope prefix for passing user id to impersonate using token exchange |
| 22 |
CustomScopeImpersonatePrefix = "custom_scope:impersonate:" |
| 23 |
) |
| 24 |
|
| 25 |
type AuthRequest struct { |
| 26 |
ID string |
| 27 |
CreationDate time.Time |
| 28 |
ApplicationID string |
| 29 |
CallbackURI string |
| 30 |
TransferState string |
| 31 |
Prompt []string |
| 32 |
UiLocales []language.Tag |
| 33 |
LoginHint string |
| 34 |
MaxAuthAge *time.Duration |
| 35 |
UserID string |
| 36 |
Scopes []string |
| 37 |
ResponseType oidc.ResponseType |
| 38 |
ResponseMode oidc.ResponseMode |
| 39 |
Nonce string |
| 40 |
CodeChallenge *OIDCCodeChallenge |
| 41 |
|
| 42 |
done bool |
| 43 |
authTime time.Time |
| 44 |
} |
| 45 |
|
| 46 |
// LogValue allows you to define which fields will be logged. |
| 47 |
// Implements the [slog.LogValuer] |
| 48 |
func (a *AuthRequest) LogValue() slog.Value { |
| 49 |
return slog.GroupValue( |
| 50 |
slog.String("id", a.ID), |
| 51 |
slog.Time("creation_date", a.CreationDate), |
| 52 |
slog.Any("scopes", a.Scopes), |
| 53 |
slog.String("response_type", string(a.ResponseType)), |
| 54 |
slog.String("app_id", a.ApplicationID), |
| 55 |
slog.String("callback_uri", a.CallbackURI), |
| 56 |
) |
| 57 |
} |
| 58 |
|
| 59 |
func (a *AuthRequest) GetID() string { |
| 60 |
return a.ID |
| 61 |
} |
| 62 |
|
| 63 |
func (a *AuthRequest) GetACR() string { |
| 64 |
return "" // we won't handle acr in this example |
| 65 |
} |
| 66 |
|
| 67 |
func (a *AuthRequest) GetAMR() []string { |
| 68 |
// this example only uses password for authentication |
| 69 |
if a.done { |
| 70 |
return []string{"pwd"} |
| 71 |
} |
| 72 |
return nil |
| 73 |
} |
| 74 |
|
| 75 |
func (a *AuthRequest) GetAudience() []string { |
| 76 |
return []string{a.ApplicationID} // this example will always just use the client_id as audience |
| 77 |
} |
| 78 |
|
| 79 |
func (a *AuthRequest) GetAuthTime() time.Time { |
| 80 |
return a.authTime |
| 81 |
} |
| 82 |
|
| 83 |
func (a *AuthRequest) GetClientID() string { |
| 84 |
return a.ApplicationID |
| 85 |
} |
| 86 |
|
| 87 |
func (a *AuthRequest) GetCodeChallenge() *oidc.CodeChallenge { |
| 88 |
return CodeChallengeToOIDC(a.CodeChallenge) |
| 89 |
} |
| 90 |
|
| 91 |
func (a *AuthRequest) GetNonce() string { |
| 92 |
return a.Nonce |
| 93 |
} |
| 94 |
|
| 95 |
func (a *AuthRequest) GetRedirectURI() string { |
| 96 |
return a.CallbackURI |
| 97 |
} |
| 98 |
|
| 99 |
func (a *AuthRequest) GetResponseType() oidc.ResponseType { |
| 100 |
return a.ResponseType |
| 101 |
} |
| 102 |
|
| 103 |
func (a *AuthRequest) GetResponseMode() oidc.ResponseMode { |
| 104 |
return a.ResponseMode |
| 105 |
} |
| 106 |
|
| 107 |
func (a *AuthRequest) GetScopes() []string { |
| 108 |
return a.Scopes |
| 109 |
} |
| 110 |
|
| 111 |
func (a *AuthRequest) GetState() string { |
| 112 |
return a.TransferState |
| 113 |
} |
| 114 |
|
| 115 |
func (a *AuthRequest) GetSubject() string { |
| 116 |
return a.UserID |
| 117 |
} |
| 118 |
|
| 119 |
func (a *AuthRequest) Done() bool { |
| 120 |
return a.done |
| 121 |
} |
| 122 |
|
| 123 |
func PromptToInternal(oidcPrompt oidc.SpaceDelimitedArray) []string { |
| 124 |
prompts := make([]string, 0, len(oidcPrompt)) |
| 125 |
for _, oidcPrompt := range oidcPrompt { |
| 126 |
switch oidcPrompt { |
| 127 |
case oidc.PromptNone, |
| 128 |
oidc.PromptLogin, |
| 129 |
oidc.PromptConsent, |
| 130 |
oidc.PromptSelectAccount: |
| 131 |
prompts = append(prompts, oidcPrompt) |
| 132 |
} |
| 133 |
} |
| 134 |
return prompts |
| 135 |
} |
| 136 |
|
| 137 |
func MaxAgeToInternal(maxAge *uint) *time.Duration { |
| 138 |
if maxAge == nil { |
| 139 |
return nil |
| 140 |
} |
| 141 |
dur := time.Duration(*maxAge) * time.Second |
| 142 |
return &dur |
| 143 |
} |
| 144 |
|
| 145 |
func authRequestToInternal(authReq *oidc.AuthRequest, userID string) *AuthRequest { |
| 146 |
var codeChallenge *OIDCCodeChallenge |
| 147 |
if authReq.CodeChallenge != "" { |
| 148 |
codeChallenge = &OIDCCodeChallenge{ |
| 149 |
Challenge: authReq.CodeChallenge, |
| 150 |
Method: string(authReq.CodeChallengeMethod), |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
return &AuthRequest{ |
| 155 |
CreationDate: time.Now(), |
| 156 |
ApplicationID: authReq.ClientID, |
| 157 |
CallbackURI: authReq.RedirectURI, |
| 158 |
TransferState: authReq.State, |
| 159 |
Prompt: PromptToInternal(authReq.Prompt), |
| 160 |
UiLocales: authReq.UILocales, |
| 161 |
LoginHint: authReq.LoginHint, |
| 162 |
MaxAuthAge: MaxAgeToInternal(authReq.MaxAge), |
| 163 |
UserID: userID, |
| 164 |
Scopes: authReq.Scopes, |
| 165 |
ResponseType: authReq.ResponseType, |
| 166 |
ResponseMode: authReq.ResponseMode, |
| 167 |
Nonce: authReq.Nonce, |
| 168 |
CodeChallenge: codeChallenge, |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
type AuthRequestWithSessionState struct { |
| 173 |
*AuthRequest |
| 174 |
SessionState string |
| 175 |
} |
| 176 |
|
| 177 |
func (a *AuthRequestWithSessionState) GetSessionState() string { |
| 178 |
return a.SessionState |
| 179 |
} |
| 180 |
|
| 181 |
type OIDCCodeChallenge struct { |
| 182 |
Challenge string |
| 183 |
Method string |
| 184 |
} |
| 185 |
|
| 186 |
func CodeChallengeToOIDC(challenge *OIDCCodeChallenge) *oidc.CodeChallenge { |
| 187 |
if challenge == nil { |
| 188 |
return nil |
| 189 |
} |
| 190 |
challengeMethod := oidc.CodeChallengeMethodPlain |
| 191 |
if challenge.Method == "S256" { |
| 192 |
challengeMethod = oidc.CodeChallengeMethodS256 |
| 193 |
} |
| 194 |
return &oidc.CodeChallenge{ |
| 195 |
Challenge: challenge.Challenge, |
| 196 |
Method: challengeMethod, |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
// RefreshTokenRequestFromBusiness will simply wrap the storage RefreshToken to implement the op.RefreshTokenRequest interface |
| 201 |
func RefreshTokenRequestFromBusiness(token *RefreshToken) op.RefreshTokenRequest { |
| 202 |
return &RefreshTokenRequest{token} |
| 203 |
} |
| 204 |
|
| 205 |
type RefreshTokenRequest struct { |
| 206 |
*RefreshToken |
| 207 |
} |
| 208 |
|
| 209 |
func (r *RefreshTokenRequest) GetAMR() []string { |
| 210 |
return r.AMR |
| 211 |
} |
| 212 |
|
| 213 |
func (r *RefreshTokenRequest) GetAudience() []string { |
| 214 |
return r.Audience |
| 215 |
} |
| 216 |
|
| 217 |
func (r *RefreshTokenRequest) GetAuthTime() time.Time { |
| 218 |
return r.AuthTime |
| 219 |
} |
| 220 |
|
| 221 |
func (r *RefreshTokenRequest) GetClientID() string { |
| 222 |
return r.ApplicationID |
| 223 |
} |
| 224 |
|
| 225 |
func (r *RefreshTokenRequest) GetScopes() []string { |
| 226 |
return r.Scopes |
| 227 |
} |
| 228 |
|
| 229 |
func (r *RefreshTokenRequest) GetSubject() string { |
| 230 |
return r.UserID |
| 231 |
} |
| 232 |
|
| 233 |
func (r *RefreshTokenRequest) SetCurrentScopes(scopes []string) { |
| 234 |
r.Scopes = scopes |
| 235 |
} |