| 1 |
package storage |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"time" |
| 6 |
|
| 7 |
jose "github.com/go-jose/go-jose/v4" |
| 8 |
|
| 9 |
"github.com/zitadel/oidc/v3/pkg/oidc" |
| 10 |
"github.com/zitadel/oidc/v3/pkg/op" |
| 11 |
) |
| 12 |
|
| 13 |
type multiStorage struct { |
| 14 |
issuers map[string]*Storage |
| 15 |
} |
| 16 |
|
| 17 |
// NewMultiStorage implements the op.Storage interface by wrapping multiple storage structs |
| 18 |
// and selecting them by the calling issuer |
| 19 |
func NewMultiStorage(issuers []string) *multiStorage { |
| 20 |
s := make(map[string]*Storage) |
| 21 |
for _, issuer := range issuers { |
| 22 |
s[issuer] = NewStorage(NewUserStore(issuer)) |
| 23 |
} |
| 24 |
return &multiStorage{issuers: s} |
| 25 |
} |
| 26 |
|
| 27 |
// CheckUsernamePassword implements the `authenticate` interface of the login |
| 28 |
func (s *multiStorage) CheckUsernamePassword(ctx context.Context, username, password, id string) error { |
| 29 |
storage, err := s.storageFromContext(ctx) |
| 30 |
if err != nil { |
| 31 |
return err |
| 32 |
} |
| 33 |
return storage.CheckUsernamePassword(username, password, id) |
| 34 |
} |
| 35 |
|
| 36 |
// CreateAuthRequest implements the op.Storage interface |
| 37 |
// it will be called after parsing and validation of the authentication request |
| 38 |
func (s *multiStorage) CreateAuthRequest(ctx context.Context, authReq *oidc.AuthRequest, userID string) (op.AuthRequest, error) { |
| 39 |
storage, err := s.storageFromContext(ctx) |
| 40 |
if err != nil { |
| 41 |
return nil, err |
| 42 |
} |
| 43 |
return storage.CreateAuthRequest(ctx, authReq, userID) |
| 44 |
} |
| 45 |
|
| 46 |
// AuthRequestByID implements the op.Storage interface |
| 47 |
// it will be called after the Login UI redirects back to the OIDC endpoint |
| 48 |
func (s *multiStorage) AuthRequestByID(ctx context.Context, id string) (op.AuthRequest, error) { |
| 49 |
storage, err := s.storageFromContext(ctx) |
| 50 |
if err != nil { |
| 51 |
return nil, err |
| 52 |
} |
| 53 |
return storage.AuthRequestByID(ctx, id) |
| 54 |
} |
| 55 |
|
| 56 |
// AuthRequestByCode implements the op.Storage interface |
| 57 |
// it will be called after parsing and validation of the token request (in an authorization code flow) |
| 58 |
func (s *multiStorage) AuthRequestByCode(ctx context.Context, code string) (op.AuthRequest, error) { |
| 59 |
storage, err := s.storageFromContext(ctx) |
| 60 |
if err != nil { |
| 61 |
return nil, err |
| 62 |
} |
| 63 |
return storage.AuthRequestByCode(ctx, code) |
| 64 |
} |
| 65 |
|
| 66 |
// SaveAuthCode implements the op.Storage interface |
| 67 |
// it will be called after the authentication has been successful and before redirecting the user agent to the redirect_uri |
| 68 |
// (in an authorization code flow) |
| 69 |
func (s *multiStorage) SaveAuthCode(ctx context.Context, id string, code string) error { |
| 70 |
storage, err := s.storageFromContext(ctx) |
| 71 |
if err != nil { |
| 72 |
return err |
| 73 |
} |
| 74 |
return storage.SaveAuthCode(ctx, id, code) |
| 75 |
} |
| 76 |
|
| 77 |
// DeleteAuthRequest implements the op.Storage interface |
| 78 |
// it will be called after creating the token response (id and access tokens) for a valid |
| 79 |
// - authentication request (in an implicit flow) |
| 80 |
// - token request (in an authorization code flow) |
| 81 |
func (s *multiStorage) DeleteAuthRequest(ctx context.Context, id string) error { |
| 82 |
storage, err := s.storageFromContext(ctx) |
| 83 |
if err != nil { |
| 84 |
return err |
| 85 |
} |
| 86 |
return storage.DeleteAuthRequest(ctx, id) |
| 87 |
} |
| 88 |
|
| 89 |
// CreateAccessToken implements the op.Storage interface |
| 90 |
// it will be called for all requests able to return an access token (Authorization Code Flow, Implicit Flow, JWT Profile, ...) |
| 91 |
func (s *multiStorage) CreateAccessToken(ctx context.Context, request op.TokenRequest) (string, time.Time, error) { |
| 92 |
storage, err := s.storageFromContext(ctx) |
| 93 |
if err != nil { |
| 94 |
return "", time.Time{}, err |
| 95 |
} |
| 96 |
return storage.CreateAccessToken(ctx, request) |
| 97 |
} |
| 98 |
|
| 99 |
// CreateAccessAndRefreshTokens implements the op.Storage interface |
| 100 |
// it will be called for all requests able to return an access and refresh token (Authorization Code Flow, Refresh Token Request) |
| 101 |
func (s *multiStorage) CreateAccessAndRefreshTokens(ctx context.Context, request op.TokenRequest, currentRefreshToken string) (accessTokenID string, newRefreshToken string, expiration time.Time, err error) { |
| 102 |
storage, err := s.storageFromContext(ctx) |
| 103 |
if err != nil { |
| 104 |
return "", "", time.Time{}, err |
| 105 |
} |
| 106 |
return storage.CreateAccessAndRefreshTokens(ctx, request, currentRefreshToken) |
| 107 |
} |
| 108 |
|
| 109 |
// TokenRequestByRefreshToken implements the op.Storage interface |
| 110 |
// it will be called after parsing and validation of the refresh token request |
| 111 |
func (s *multiStorage) TokenRequestByRefreshToken(ctx context.Context, refreshToken string) (op.RefreshTokenRequest, error) { |
| 112 |
storage, err := s.storageFromContext(ctx) |
| 113 |
if err != nil { |
| 114 |
return nil, err |
| 115 |
} |
| 116 |
return storage.TokenRequestByRefreshToken(ctx, refreshToken) |
| 117 |
} |
| 118 |
|
| 119 |
// TerminateSession implements the op.Storage interface |
| 120 |
// it will be called after the user signed out, therefore the access and refresh token of the user of this client must be removed |
| 121 |
func (s *multiStorage) TerminateSession(ctx context.Context, userID string, clientID string) error { |
| 122 |
storage, err := s.storageFromContext(ctx) |
| 123 |
if err != nil { |
| 124 |
return err |
| 125 |
} |
| 126 |
return storage.TerminateSession(ctx, userID, clientID) |
| 127 |
} |
| 128 |
|
| 129 |
// GetRefreshTokenInfo looks up a refresh token and returns the token id and user id. |
| 130 |
// If given something that is not a refresh token, it must return error. |
| 131 |
func (s *multiStorage) GetRefreshTokenInfo(ctx context.Context, clientID string, token string) (userID string, tokenID string, err error) { |
| 132 |
storage, err := s.storageFromContext(ctx) |
| 133 |
if err != nil { |
| 134 |
return "", "", err |
| 135 |
} |
| 136 |
return storage.GetRefreshTokenInfo(ctx, clientID, token) |
| 137 |
} |
| 138 |
|
| 139 |
// RevokeToken implements the op.Storage interface |
| 140 |
// it will be called after parsing and validation of the token revocation request |
| 141 |
func (s *multiStorage) RevokeToken(ctx context.Context, token string, userID string, clientID string) *oidc.Error { |
| 142 |
storage, err := s.storageFromContext(ctx) |
| 143 |
if err != nil { |
| 144 |
return err |
| 145 |
} |
| 146 |
return storage.RevokeToken(ctx, token, userID, clientID) |
| 147 |
} |
| 148 |
|
| 149 |
// SigningKey implements the op.Storage interface |
| 150 |
// it will be called when creating the OpenID Provider |
| 151 |
func (s *multiStorage) SigningKey(ctx context.Context) (op.SigningKey, error) { |
| 152 |
storage, err := s.storageFromContext(ctx) |
| 153 |
if err != nil { |
| 154 |
return nil, err |
| 155 |
} |
| 156 |
return storage.SigningKey(ctx) |
| 157 |
} |
| 158 |
|
| 159 |
// SignatureAlgorithms implements the op.Storage interface |
| 160 |
// it will be called to get the sign |
| 161 |
func (s *multiStorage) SignatureAlgorithms(ctx context.Context) ([]jose.SignatureAlgorithm, error) { |
| 162 |
storage, err := s.storageFromContext(ctx) |
| 163 |
if err != nil { |
| 164 |
return nil, err |
| 165 |
} |
| 166 |
return storage.SignatureAlgorithms(ctx) |
| 167 |
} |
| 168 |
|
| 169 |
// KeySet implements the op.Storage interface |
| 170 |
// it will be called to get the current (public) keys, among others for the keys_endpoint or for validating access_tokens on the userinfo_endpoint, ... |
| 171 |
func (s *multiStorage) KeySet(ctx context.Context) ([]op.Key, error) { |
| 172 |
storage, err := s.storageFromContext(ctx) |
| 173 |
if err != nil { |
| 174 |
return nil, err |
| 175 |
} |
| 176 |
return storage.KeySet(ctx) |
| 177 |
} |
| 178 |
|
| 179 |
// GetClientByClientID implements the op.Storage interface |
| 180 |
// it will be called whenever information (type, redirect_uris, ...) about the client behind the client_id is needed |
| 181 |
func (s *multiStorage) GetClientByClientID(ctx context.Context, clientID string) (op.Client, error) { |
| 182 |
storage, err := s.storageFromContext(ctx) |
| 183 |
if err != nil { |
| 184 |
return nil, err |
| 185 |
} |
| 186 |
return storage.GetClientByClientID(ctx, clientID) |
| 187 |
} |
| 188 |
|
| 189 |
// AuthorizeClientIDSecret implements the op.Storage interface |
| 190 |
// it will be called for validating the client_id, client_secret on token or introspection requests |
| 191 |
func (s *multiStorage) AuthorizeClientIDSecret(ctx context.Context, clientID, clientSecret string) error { |
| 192 |
storage, err := s.storageFromContext(ctx) |
| 193 |
if err != nil { |
| 194 |
return err |
| 195 |
} |
| 196 |
return storage.AuthorizeClientIDSecret(ctx, clientID, clientSecret) |
| 197 |
} |
| 198 |
|
| 199 |
// SetUserinfoFromScopes implements the op.Storage interface. |
| 200 |
// Provide an empty implementation and use SetUserinfoFromRequest instead. |
| 201 |
func (s *multiStorage) SetUserinfoFromScopes(ctx context.Context, userinfo *oidc.UserInfo, userID, clientID string, scopes []string) error { |
| 202 |
storage, err := s.storageFromContext(ctx) |
| 203 |
if err != nil { |
| 204 |
return err |
| 205 |
} |
| 206 |
return storage.SetUserinfoFromScopes(ctx, userinfo, userID, clientID, scopes) |
| 207 |
} |
| 208 |
|
| 209 |
// SetUserinfoFromRequests implements the op.CanSetUserinfoFromRequest interface. In the |
| 210 |
// next major release, it will be required for op.Storage. |
| 211 |
// It will be called for the creation of an id_token, so we'll just pass it to the private function without any further check |
| 212 |
func (s *multiStorage) SetUserinfoFromRequest(ctx context.Context, userinfo *oidc.UserInfo, token op.IDTokenRequest, scopes []string) error { |
| 213 |
storage, err := s.storageFromContext(ctx) |
| 214 |
if err != nil { |
| 215 |
return err |
| 216 |
} |
| 217 |
return storage.SetUserinfoFromRequest(ctx, userinfo, token, scopes) |
| 218 |
} |
| 219 |
|
| 220 |
// SetUserinfoFromToken implements the op.Storage interface |
| 221 |
// it will be called for the userinfo endpoint, so we read the token and pass the information from that to the private function |
| 222 |
func (s *multiStorage) SetUserinfoFromToken(ctx context.Context, userinfo *oidc.UserInfo, tokenID, subject, origin string) error { |
| 223 |
storage, err := s.storageFromContext(ctx) |
| 224 |
if err != nil { |
| 225 |
return err |
| 226 |
} |
| 227 |
return storage.SetUserinfoFromToken(ctx, userinfo, tokenID, subject, origin) |
| 228 |
} |
| 229 |
|
| 230 |
// SetIntrospectionFromToken implements the op.Storage interface |
| 231 |
// it will be called for the introspection endpoint, so we read the token and pass the information from that to the private function |
| 232 |
func (s *multiStorage) SetIntrospectionFromToken(ctx context.Context, introspection *oidc.IntrospectionResponse, tokenID, subject, clientID string) error { |
| 233 |
storage, err := s.storageFromContext(ctx) |
| 234 |
if err != nil { |
| 235 |
return err |
| 236 |
} |
| 237 |
return storage.SetIntrospectionFromToken(ctx, introspection, tokenID, subject, clientID) |
| 238 |
} |
| 239 |
|
| 240 |
// GetPrivateClaimsFromScopes implements the op.Storage interface |
| 241 |
// it will be called for the creation of a JWT access token to assert claims for custom scopes |
| 242 |
func (s *multiStorage) GetPrivateClaimsFromScopes(ctx context.Context, userID, clientID string, scopes []string) (claims map[string]any, err error) { |
| 243 |
storage, err := s.storageFromContext(ctx) |
| 244 |
if err != nil { |
| 245 |
return nil, err |
| 246 |
} |
| 247 |
return storage.GetPrivateClaimsFromScopes(ctx, userID, clientID, scopes) |
| 248 |
} |
| 249 |
|
| 250 |
// GetKeyByIDAndClientID implements the op.Storage interface |
| 251 |
// it will be called to validate the signatures of a JWT (JWT Profile Grant and Authentication) |
| 252 |
func (s *multiStorage) GetKeyByIDAndClientID(ctx context.Context, keyID, clientID string) (*jose.JSONWebKey, error) { |
| 253 |
storage, err := s.storageFromContext(ctx) |
| 254 |
if err != nil { |
| 255 |
return nil, err |
| 256 |
} |
| 257 |
return storage.GetKeyByIDAndClientID(ctx, keyID, clientID) |
| 258 |
} |
| 259 |
|
| 260 |
// ValidateJWTProfileScopes implements the op.Storage interface |
| 261 |
// it will be called to validate the scopes of a JWT Profile Authorization Grant request |
| 262 |
func (s *multiStorage) ValidateJWTProfileScopes(ctx context.Context, userID string, scopes []string) ([]string, error) { |
| 263 |
storage, err := s.storageFromContext(ctx) |
| 264 |
if err != nil { |
| 265 |
return nil, err |
| 266 |
} |
| 267 |
return storage.ValidateJWTProfileScopes(ctx, userID, scopes) |
| 268 |
} |
| 269 |
|
| 270 |
// Health implements the op.Storage interface |
| 271 |
func (s *multiStorage) Health(ctx context.Context) error { |
| 272 |
return nil |
| 273 |
} |
| 274 |
|
| 275 |
func (s *multiStorage) storageFromContext(ctx context.Context) (*Storage, *oidc.Error) { |
| 276 |
storage, ok := s.issuers[op.IssuerFromContext(ctx)] |
| 277 |
if !ok { |
| 278 |
return nil, oidc.ErrInvalidRequest().WithDescription("invalid issuer") |
| 279 |
} |
| 280 |
return storage, nil |
| 281 |
} |