2023-11-21 12:11:38 +00:00
package oidc
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
2024-05-31 13:06:59 +00:00
"maps"
2024-04-09 13:15:35 +00:00
"net/http"
2023-11-21 12:11:38 +00:00
"slices"
"strings"
2024-05-31 10:10:18 +00:00
"sync"
2023-11-21 12:11:38 +00:00
"github.com/dop251/goja"
"github.com/zitadel/logging"
"github.com/zitadel/oidc/v3/pkg/oidc"
2024-04-09 13:15:35 +00:00
"github.com/zitadel/oidc/v3/pkg/op"
2023-11-21 12:11:38 +00:00
"github.com/zitadel/zitadel/internal/actions"
"github.com/zitadel/zitadel/internal/actions/object"
2024-04-09 13:15:35 +00:00
"github.com/zitadel/zitadel/internal/api/authz"
2023-11-21 12:11:38 +00:00
"github.com/zitadel/zitadel/internal/domain"
"github.com/zitadel/zitadel/internal/query"
2024-04-09 13:15:35 +00:00
"github.com/zitadel/zitadel/internal/telemetry/tracing"
2024-05-16 05:07:56 +00:00
"github.com/zitadel/zitadel/internal/zerrors"
2023-11-21 12:11:38 +00:00
)
2024-04-09 13:15:35 +00:00
func ( s * Server ) UserInfo ( ctx context . Context , r * op . Request [ oidc . UserInfoRequest ] ) ( _ * op . Response , err error ) {
ctx , span := tracing . NewSpan ( ctx )
defer func ( ) {
err = oidcError ( err )
span . EndWithError ( err )
} ( )
features := authz . GetFeatures ( ctx )
if features . LegacyIntrospection {
return s . LegacyServer . UserInfo ( ctx , r )
}
if features . TriggerIntrospectionProjections {
query . TriggerOIDCUserInfoProjections ( ctx )
}
token , err := s . verifyAccessToken ( ctx , r . Data . AccessToken )
if err != nil {
2024-08-20 06:45:24 +00:00
return nil , op . NewStatusError ( oidc . ErrAccessDenied ( ) . WithDescription ( "access token invalid" ) . WithParent ( err ) . WithReturnParentToClient ( authz . GetFeatures ( ctx ) . DebugOIDCParentError ) , http . StatusUnauthorized )
2024-04-09 13:15:35 +00:00
}
var (
projectID string
assertion bool
)
if token . clientID != "" {
projectID , assertion , err = s . query . GetOIDCUserinfoClientByID ( ctx , token . clientID )
2024-05-16 05:07:56 +00:00
// token.clientID might contain a username (e.g. client credentials) -> ignore the not found
if err != nil && ! zerrors . IsNotFound ( err ) {
2024-04-09 13:15:35 +00:00
return nil , err
}
}
2024-05-31 10:10:18 +00:00
userInfo , err := s . userInfo (
token . userID ,
token . scope ,
projectID ,
assertion ,
true ,
false ,
) ( ctx , true , domain . TriggerTypePreUserinfoCreation )
2024-04-09 13:15:35 +00:00
if err != nil {
2024-09-17 13:21:49 +00:00
if ! zerrors . IsNotFound ( err ) {
return nil , err
}
return nil , op . NewStatusError ( oidc . ErrAccessDenied ( ) . WithDescription ( "no active user" ) . WithParent ( err ) . WithReturnParentToClient ( authz . GetFeatures ( ctx ) . DebugOIDCParentError ) , http . StatusUnauthorized )
2024-04-09 13:15:35 +00:00
}
return op . NewResponse ( userInfo ) , nil
}
fix(oidc): roles in userinfo for client credentials token (#7763)
* fix(oidc): roles in userinfo for client credentials token
When tokens were obtained using the client credentials grant,
with audience and role scopes, userinfo would not return the role claims. This had multiple causes:
1. There is no auth request flow, so for legacy userinfo project data was never attached to the token
2. For optimized userinfo, there is no client ID that maps to an application. The client ID for client credentials is the machine user's name. There we can't obtain a project ID. When the project ID remained empty, we always ignored the roleAudience.
This PR fixes situation 2, by always taking the roleAudience into account, even when the projectID is empty. The code responsible for the bug is also refactored to be more readable and understandable, including additional godoc.
The fix only applies to the optimized userinfo code introduced in #7706 and released in v2.50 (currently in RC). Therefore it can't be back-ported to earlier versions.
Fixes #6662
* chore(deps): update all go deps (#7764)
This change updates all go modules, including oidc, a major version of go-jose and the go 1.22 release.
* Revert "chore(deps): update all go deps" (#7772)
Revert "chore(deps): update all go deps (#7764)"
This reverts commit 6893e7d060a953d595a18ff8daa979834c4324d5.
---------
Co-authored-by: Livio Spring <livio.a@gmail.com>
2024-04-16 13:02:38 +00:00
// userInfo gets the user's data based on the scope.
// The returned UserInfo contains standard and reserved claims, documented
// here: https://zitadel.com/docs/apis/openidoauth/claims.
//
2024-05-31 10:10:18 +00:00
// User information is only retrieved once from the database.
// However, each time, role claims are asserted and also action flows will trigger.
//
fix(oidc): roles in userinfo for client credentials token (#7763)
* fix(oidc): roles in userinfo for client credentials token
When tokens were obtained using the client credentials grant,
with audience and role scopes, userinfo would not return the role claims. This had multiple causes:
1. There is no auth request flow, so for legacy userinfo project data was never attached to the token
2. For optimized userinfo, there is no client ID that maps to an application. The client ID for client credentials is the machine user's name. There we can't obtain a project ID. When the project ID remained empty, we always ignored the roleAudience.
This PR fixes situation 2, by always taking the roleAudience into account, even when the projectID is empty. The code responsible for the bug is also refactored to be more readable and understandable, including additional godoc.
The fix only applies to the optimized userinfo code introduced in #7706 and released in v2.50 (currently in RC). Therefore it can't be back-ported to earlier versions.
Fixes #6662
* chore(deps): update all go deps (#7764)
This change updates all go modules, including oidc, a major version of go-jose and the go 1.22 release.
* Revert "chore(deps): update all go deps" (#7772)
Revert "chore(deps): update all go deps (#7764)"
This reverts commit 6893e7d060a953d595a18ff8daa979834c4324d5.
---------
Co-authored-by: Livio Spring <livio.a@gmail.com>
2024-04-16 13:02:38 +00:00
// projectID is an optional parameter which defines the default audience when there are any (or all) role claims requested.
// projectRoleAssertion sets the default of returning all project roles, only if no specific roles were requested in the scope.
2024-05-31 10:10:18 +00:00
// roleAssertion decides whether the roles will be returned (in the token or response)
// userInfoAssertion decides whether the user information (profile data like name, email, ...) are returned
fix(oidc): roles in userinfo for client credentials token (#7763)
* fix(oidc): roles in userinfo for client credentials token
When tokens were obtained using the client credentials grant,
with audience and role scopes, userinfo would not return the role claims. This had multiple causes:
1. There is no auth request flow, so for legacy userinfo project data was never attached to the token
2. For optimized userinfo, there is no client ID that maps to an application. The client ID for client credentials is the machine user's name. There we can't obtain a project ID. When the project ID remained empty, we always ignored the roleAudience.
This PR fixes situation 2, by always taking the roleAudience into account, even when the projectID is empty. The code responsible for the bug is also refactored to be more readable and understandable, including additional godoc.
The fix only applies to the optimized userinfo code introduced in #7706 and released in v2.50 (currently in RC). Therefore it can't be back-ported to earlier versions.
Fixes #6662
* chore(deps): update all go deps (#7764)
This change updates all go modules, including oidc, a major version of go-jose and the go 1.22 release.
* Revert "chore(deps): update all go deps" (#7772)
Revert "chore(deps): update all go deps (#7764)"
This reverts commit 6893e7d060a953d595a18ff8daa979834c4324d5.
---------
Co-authored-by: Livio Spring <livio.a@gmail.com>
2024-04-16 13:02:38 +00:00
//
// currentProjectOnly can be set to use the current project ID only and ignore the audience from the scope.
// It should be set in cases where the client doesn't need to know roles outside its own project,
// for example an introspection client.
2024-05-31 10:10:18 +00:00
func ( s * Server ) userInfo (
userID string ,
scope [ ] string ,
projectID string ,
projectRoleAssertion , userInfoAssertion , currentProjectOnly bool ,
) func ( ctx context . Context , roleAssertion bool , triggerType domain . TriggerType ) ( _ * oidc . UserInfo , err error ) {
var (
once sync . Once
2024-05-31 13:06:59 +00:00
rawUserInfo * oidc . UserInfo
2024-05-31 10:10:18 +00:00
qu * query . OIDCUserInfo
roleAudience , requestedRoles [ ] string
)
return func ( ctx context . Context , roleAssertion bool , triggerType domain . TriggerType ) ( _ * oidc . UserInfo , err error ) {
once . Do ( func ( ) {
ctx , span := tracing . NewSpan ( ctx )
defer func ( ) { span . EndWithError ( err ) } ( )
2024-04-10 15:05:13 +00:00
2024-05-31 10:10:18 +00:00
roleAudience , requestedRoles = prepareRoles ( ctx , scope , projectID , projectRoleAssertion , currentProjectOnly )
2024-06-14 08:00:43 +00:00
roleOrgIDs := domain . RoleOrgIDsFromScope ( scope )
qu , err = s . query . GetOIDCUserInfo ( ctx , userID , roleAudience , roleOrgIDs ... )
2024-05-31 10:10:18 +00:00
if err != nil {
return
}
2024-05-31 13:06:59 +00:00
rawUserInfo = userInfoToOIDC ( qu , userInfoAssertion , scope , s . assetAPIPrefix ( ctx ) )
2024-05-31 10:10:18 +00:00
} )
2024-07-04 14:11:06 +00:00
if err != nil {
return nil , err
}
2024-05-31 13:06:59 +00:00
// copy the userinfo to make sure the assert roles and actions use their own copy (e.g. map)
userInfo := & oidc . UserInfo {
Subject : rawUserInfo . Subject ,
UserInfoProfile : rawUserInfo . UserInfoProfile ,
UserInfoEmail : rawUserInfo . UserInfoEmail ,
UserInfoPhone : rawUserInfo . UserInfoPhone ,
Address : rawUserInfo . Address ,
Claims : maps . Clone ( rawUserInfo . Claims ) ,
}
assertRoles ( projectID , qu , roleAudience , requestedRoles , roleAssertion , userInfo )
return userInfo , s . userinfoFlows ( ctx , qu , userInfo , triggerType )
2023-11-21 12:11:38 +00:00
}
}
fix(oidc): roles in userinfo for client credentials token (#7763)
* fix(oidc): roles in userinfo for client credentials token
When tokens were obtained using the client credentials grant,
with audience and role scopes, userinfo would not return the role claims. This had multiple causes:
1. There is no auth request flow, so for legacy userinfo project data was never attached to the token
2. For optimized userinfo, there is no client ID that maps to an application. The client ID for client credentials is the machine user's name. There we can't obtain a project ID. When the project ID remained empty, we always ignored the roleAudience.
This PR fixes situation 2, by always taking the roleAudience into account, even when the projectID is empty. The code responsible for the bug is also refactored to be more readable and understandable, including additional godoc.
The fix only applies to the optimized userinfo code introduced in #7706 and released in v2.50 (currently in RC). Therefore it can't be back-ported to earlier versions.
Fixes #6662
* chore(deps): update all go deps (#7764)
This change updates all go modules, including oidc, a major version of go-jose and the go 1.22 release.
* Revert "chore(deps): update all go deps" (#7772)
Revert "chore(deps): update all go deps (#7764)"
This reverts commit 6893e7d060a953d595a18ff8daa979834c4324d5.
---------
Co-authored-by: Livio Spring <livio.a@gmail.com>
2024-04-16 13:02:38 +00:00
// prepareRoles scans the requested scopes and builds the requested roles
// and the audience for which roles need to be asserted.
2023-11-21 12:11:38 +00:00
//
2024-04-09 13:15:35 +00:00
// Scopes with [ScopeProjectRolePrefix] are added to requestedRoles.
fix(oidc): roles in userinfo for client credentials token (#7763)
* fix(oidc): roles in userinfo for client credentials token
When tokens were obtained using the client credentials grant,
with audience and role scopes, userinfo would not return the role claims. This had multiple causes:
1. There is no auth request flow, so for legacy userinfo project data was never attached to the token
2. For optimized userinfo, there is no client ID that maps to an application. The client ID for client credentials is the machine user's name. There we can't obtain a project ID. When the project ID remained empty, we always ignored the roleAudience.
This PR fixes situation 2, by always taking the roleAudience into account, even when the projectID is empty. The code responsible for the bug is also refactored to be more readable and understandable, including additional godoc.
The fix only applies to the optimized userinfo code introduced in #7706 and released in v2.50 (currently in RC). Therefore it can't be back-ported to earlier versions.
Fixes #6662
* chore(deps): update all go deps (#7764)
This change updates all go modules, including oidc, a major version of go-jose and the go 1.22 release.
* Revert "chore(deps): update all go deps" (#7772)
Revert "chore(deps): update all go deps (#7764)"
This reverts commit 6893e7d060a953d595a18ff8daa979834c4324d5.
---------
Co-authored-by: Livio Spring <livio.a@gmail.com>
2024-04-16 13:02:38 +00:00
// When [ScopeProjectsRoles] is present project IDs with the [domain.ProjectIDScope]
// prefix are added to the returned audience.
2023-11-21 12:11:38 +00:00
//
fix(oidc): roles in userinfo for client credentials token (#7763)
* fix(oidc): roles in userinfo for client credentials token
When tokens were obtained using the client credentials grant,
with audience and role scopes, userinfo would not return the role claims. This had multiple causes:
1. There is no auth request flow, so for legacy userinfo project data was never attached to the token
2. For optimized userinfo, there is no client ID that maps to an application. The client ID for client credentials is the machine user's name. There we can't obtain a project ID. When the project ID remained empty, we always ignored the roleAudience.
This PR fixes situation 2, by always taking the roleAudience into account, even when the projectID is empty. The code responsible for the bug is also refactored to be more readable and understandable, including additional godoc.
The fix only applies to the optimized userinfo code introduced in #7706 and released in v2.50 (currently in RC). Therefore it can't be back-ported to earlier versions.
Fixes #6662
* chore(deps): update all go deps (#7764)
This change updates all go modules, including oidc, a major version of go-jose and the go 1.22 release.
* Revert "chore(deps): update all go deps" (#7772)
Revert "chore(deps): update all go deps (#7764)"
This reverts commit 6893e7d060a953d595a18ff8daa979834c4324d5.
---------
Co-authored-by: Livio Spring <livio.a@gmail.com>
2024-04-16 13:02:38 +00:00
// If projectRoleAssertion is true and there were no specific roles requested,
// the current projectID will always be parts of the returned audience.
func prepareRoles ( ctx context . Context , scope [ ] string , projectID string , projectRoleAssertion , currentProjectOnly bool ) ( roleAudience , requestedRoles [ ] string ) {
2023-11-21 12:11:38 +00:00
for _ , s := range scope {
if role , ok := strings . CutPrefix ( s , ScopeProjectRolePrefix ) ; ok {
requestedRoles = append ( requestedRoles , role )
}
}
fix(oidc): roles in userinfo for client credentials token (#7763)
* fix(oidc): roles in userinfo for client credentials token
When tokens were obtained using the client credentials grant,
with audience and role scopes, userinfo would not return the role claims. This had multiple causes:
1. There is no auth request flow, so for legacy userinfo project data was never attached to the token
2. For optimized userinfo, there is no client ID that maps to an application. The client ID for client credentials is the machine user's name. There we can't obtain a project ID. When the project ID remained empty, we always ignored the roleAudience.
This PR fixes situation 2, by always taking the roleAudience into account, even when the projectID is empty. The code responsible for the bug is also refactored to be more readable and understandable, including additional godoc.
The fix only applies to the optimized userinfo code introduced in #7706 and released in v2.50 (currently in RC). Therefore it can't be back-ported to earlier versions.
Fixes #6662
* chore(deps): update all go deps (#7764)
This change updates all go modules, including oidc, a major version of go-jose and the go 1.22 release.
* Revert "chore(deps): update all go deps" (#7772)
Revert "chore(deps): update all go deps (#7764)"
This reverts commit 6893e7d060a953d595a18ff8daa979834c4324d5.
---------
Co-authored-by: Livio Spring <livio.a@gmail.com>
2024-04-16 13:02:38 +00:00
// If roles are requested take the audience for those from the scopes,
// when currentProjectOnly is not set.
if ! currentProjectOnly && ( len ( requestedRoles ) > 0 || slices . Contains ( scope , ScopeProjectsRoles ) ) {
roleAudience = domain . AddAudScopeToAudience ( ctx , roleAudience , scope )
2023-11-21 12:11:38 +00:00
}
fix(oidc): roles in userinfo for client credentials token (#7763)
* fix(oidc): roles in userinfo for client credentials token
When tokens were obtained using the client credentials grant,
with audience and role scopes, userinfo would not return the role claims. This had multiple causes:
1. There is no auth request flow, so for legacy userinfo project data was never attached to the token
2. For optimized userinfo, there is no client ID that maps to an application. The client ID for client credentials is the machine user's name. There we can't obtain a project ID. When the project ID remained empty, we always ignored the roleAudience.
This PR fixes situation 2, by always taking the roleAudience into account, even when the projectID is empty. The code responsible for the bug is also refactored to be more readable and understandable, including additional godoc.
The fix only applies to the optimized userinfo code introduced in #7706 and released in v2.50 (currently in RC). Therefore it can't be back-ported to earlier versions.
Fixes #6662
* chore(deps): update all go deps (#7764)
This change updates all go modules, including oidc, a major version of go-jose and the go 1.22 release.
* Revert "chore(deps): update all go deps" (#7772)
Revert "chore(deps): update all go deps (#7764)"
This reverts commit 6893e7d060a953d595a18ff8daa979834c4324d5.
---------
Co-authored-by: Livio Spring <livio.a@gmail.com>
2024-04-16 13:02:38 +00:00
// When either:
// - Project role assertion is set;
// - Roles for the current project (only) are requested;
// - There is already a roleAudience requested through scope;
// - There are requested roles through the scope;
// and the projectID is not empty, projectID must be part of the roleAudience.
if ( projectRoleAssertion || currentProjectOnly || len ( roleAudience ) > 0 || len ( requestedRoles ) > 0 ) && projectID != "" && ! slices . Contains ( roleAudience , projectID ) {
2023-11-21 12:11:38 +00:00
roleAudience = append ( roleAudience , projectID )
}
return roleAudience , requestedRoles
}
2024-05-31 10:10:18 +00:00
func userInfoToOIDC ( user * query . OIDCUserInfo , userInfoAssertion bool , scope [ ] string , assetPrefix string ) * oidc . UserInfo {
2024-09-12 12:36:33 +00:00
out := & oidc . UserInfo {
Subject : user . User . ID ,
}
2023-11-21 12:11:38 +00:00
for _ , s := range scope {
switch s {
case oidc . ScopeEmail :
2024-05-31 10:10:18 +00:00
if ! userInfoAssertion {
continue
}
2023-11-21 12:11:38 +00:00
out . UserInfoEmail = userInfoEmailToOIDC ( user . User )
case oidc . ScopeProfile :
2024-05-31 10:10:18 +00:00
if ! userInfoAssertion {
continue
}
2023-11-21 12:11:38 +00:00
out . UserInfoProfile = userInfoProfileToOidc ( user . User , assetPrefix )
case oidc . ScopePhone :
2024-05-31 10:10:18 +00:00
if ! userInfoAssertion {
continue
}
2023-11-21 12:11:38 +00:00
out . UserInfoPhone = userInfoPhoneToOIDC ( user . User )
case oidc . ScopeAddress :
2024-05-31 10:10:18 +00:00
if ! userInfoAssertion {
continue
}
// TODO: handle address for human users as soon as implemented
2023-11-21 12:11:38 +00:00
case ScopeUserMetaData :
setUserInfoMetadata ( user . Metadata , out )
case ScopeResourceOwner :
setUserInfoOrgClaims ( user , out )
default :
if claim , ok := strings . CutPrefix ( s , domain . OrgDomainPrimaryScope ) ; ok {
out . AppendClaims ( domain . OrgDomainPrimaryClaim , claim )
}
if claim , ok := strings . CutPrefix ( s , domain . OrgIDScope ) ; ok {
out . AppendClaims ( domain . OrgIDClaim , claim )
setUserInfoOrgClaims ( user , out )
}
}
}
2024-05-31 10:10:18 +00:00
return out
}
2023-11-21 12:11:38 +00:00
2024-05-31 13:06:59 +00:00
func assertRoles ( projectID string , user * query . OIDCUserInfo , roleAudience , requestedRoles [ ] string , assertion bool , info * oidc . UserInfo ) {
2024-05-31 10:10:18 +00:00
if ! assertion {
2024-05-31 13:06:59 +00:00
return
2024-05-31 10:10:18 +00:00
}
2023-11-21 12:11:38 +00:00
// prevent returning obtained grants if none where requested
if ( projectID != "" && len ( requestedRoles ) > 0 ) || len ( roleAudience ) > 0 {
2024-05-31 13:06:59 +00:00
setUserInfoRoleClaims ( info , newProjectRoles ( projectID , user . UserGrants , requestedRoles ) )
2023-11-21 12:11:38 +00:00
}
}
func userInfoEmailToOIDC ( user * query . User ) oidc . UserInfoEmail {
if human := user . Human ; human != nil {
return oidc . UserInfoEmail {
Email : string ( human . Email ) ,
EmailVerified : oidc . Bool ( human . IsEmailVerified ) ,
}
}
return oidc . UserInfoEmail { }
}
func userInfoProfileToOidc ( user * query . User , assetPrefix string ) oidc . UserInfoProfile {
if human := user . Human ; human != nil {
return oidc . UserInfoProfile {
Name : human . DisplayName ,
GivenName : human . FirstName ,
FamilyName : human . LastName ,
Nickname : human . NickName ,
Picture : domain . AvatarURL ( assetPrefix , user . ResourceOwner , user . Human . AvatarKey ) ,
Gender : getGender ( human . Gender ) ,
Locale : oidc . NewLocale ( human . PreferredLanguage ) ,
UpdatedAt : oidc . FromTime ( user . ChangeDate ) ,
PreferredUsername : user . PreferredLoginName ,
}
}
if machine := user . Machine ; machine != nil {
return oidc . UserInfoProfile {
Name : machine . Name ,
UpdatedAt : oidc . FromTime ( user . ChangeDate ) ,
PreferredUsername : user . PreferredLoginName ,
}
}
return oidc . UserInfoProfile { }
}
func userInfoPhoneToOIDC ( user * query . User ) oidc . UserInfoPhone {
if human := user . Human ; human != nil {
return oidc . UserInfoPhone {
PhoneNumber : string ( human . Phone ) ,
PhoneNumberVerified : human . IsPhoneVerified ,
}
}
return oidc . UserInfoPhone { }
}
func setUserInfoMetadata ( metadata [ ] query . UserMetadata , out * oidc . UserInfo ) {
if len ( metadata ) == 0 {
return
}
mdmap := make ( map [ string ] string , len ( metadata ) )
for _ , md := range metadata {
mdmap [ md . Key ] = base64 . RawURLEncoding . EncodeToString ( md . Value )
}
out . AppendClaims ( ClaimUserMetaData , mdmap )
}
func setUserInfoOrgClaims ( user * query . OIDCUserInfo , out * oidc . UserInfo ) {
if org := user . Org ; org != nil {
2024-03-20 10:18:46 +00:00
out . AppendClaims ( ClaimResourceOwnerID , org . ID )
out . AppendClaims ( ClaimResourceOwnerName , org . Name )
out . AppendClaims ( ClaimResourceOwnerPrimaryDomain , org . PrimaryDomain )
2023-11-21 12:11:38 +00:00
}
}
func setUserInfoRoleClaims ( userInfo * oidc . UserInfo , roles * projectsRoles ) {
if roles != nil && len ( roles . projects ) > 0 {
if roles , ok := roles . projects [ roles . requestProjectID ] ; ok {
userInfo . AppendClaims ( ClaimProjectRoles , roles )
}
for projectID , roles := range roles . projects {
userInfo . AppendClaims ( fmt . Sprintf ( ClaimProjectRolesFormat , projectID ) , roles )
}
}
}
2024-05-31 10:10:18 +00:00
func ( s * Server ) userinfoFlows ( ctx context . Context , qu * query . OIDCUserInfo , userInfo * oidc . UserInfo , triggerType domain . TriggerType ) ( err error ) {
2024-04-10 15:05:13 +00:00
ctx , span := tracing . NewSpan ( ctx )
defer func ( ) { span . EndWithError ( err ) } ( )
2024-05-31 10:10:18 +00:00
queriedActions , err := s . query . GetActiveActionsByFlowAndTriggerType ( ctx , domain . FlowTypeCustomiseToken , triggerType , qu . User . ResourceOwner )
2023-11-21 12:11:38 +00:00
if err != nil {
return err
}
ctxFields := actions . SetContextFields (
actions . SetFields ( "v1" ,
actions . SetFields ( "claims" , userinfoClaims ( userInfo ) ) ,
actions . SetFields ( "getUser" , func ( c * actions . FieldConfig ) interface { } {
return func ( call goja . FunctionCall ) goja . Value {
return object . UserFromQuery ( c , qu . User )
}
} ) ,
actions . SetFields ( "user" ,
actions . SetFields ( "getMetadata" , func ( c * actions . FieldConfig ) interface { } {
return func ( goja . FunctionCall ) goja . Value {
return object . UserMetadataListFromSlice ( c , qu . Metadata )
}
} ) ,
actions . SetFields ( "grants" , func ( c * actions . FieldConfig ) interface { } {
2024-04-22 11:34:23 +00:00
return object . UserGrantsFromSlice ( ctx , s . query , c , qu . UserGrants )
2023-11-21 12:11:38 +00:00
} ) ,
) ,
2024-01-26 08:56:10 +00:00
actions . SetFields ( "org" ,
actions . SetFields ( "getMetadata" , func ( c * actions . FieldConfig ) interface { } {
return func ( goja . FunctionCall ) goja . Value {
2024-04-22 11:34:23 +00:00
return object . GetOrganizationMetadata ( ctx , s . query , c , qu . User . ResourceOwner )
2024-01-26 08:56:10 +00:00
}
} ) ,
) ,
2023-11-21 12:11:38 +00:00
) ,
)
for _ , action := range queriedActions {
actionCtx , cancel := context . WithTimeout ( ctx , action . Timeout ( ) )
claimLogs := [ ] string { }
apiFields := actions . WithAPIFields (
actions . SetFields ( "v1" ,
actions . SetFields ( "userinfo" ,
actions . SetFields ( "setClaim" , func ( key string , value interface { } ) {
2024-03-27 07:26:14 +00:00
if strings . HasPrefix ( key , ClaimPrefix ) {
return
}
2023-11-21 12:11:38 +00:00
if userInfo . Claims [ key ] == nil {
userInfo . AppendClaims ( key , value )
return
}
claimLogs = append ( claimLogs , fmt . Sprintf ( "key %q already exists" , key ) )
} ) ,
actions . SetFields ( "appendLogIntoClaims" , func ( entry string ) {
claimLogs = append ( claimLogs , entry )
} ) ,
) ,
actions . SetFields ( "claims" ,
actions . SetFields ( "setClaim" , func ( key string , value interface { } ) {
2024-03-27 07:26:14 +00:00
if strings . HasPrefix ( key , ClaimPrefix ) {
return
}
2023-11-21 12:11:38 +00:00
if userInfo . Claims [ key ] == nil {
userInfo . AppendClaims ( key , value )
return
}
claimLogs = append ( claimLogs , fmt . Sprintf ( "key %q already exists" , key ) )
} ) ,
actions . SetFields ( "appendLogIntoClaims" , func ( entry string ) {
claimLogs = append ( claimLogs , entry )
} ) ,
) ,
actions . SetFields ( "user" ,
actions . SetFields ( "setMetadata" , func ( call goja . FunctionCall ) goja . Value {
if len ( call . Arguments ) != 2 {
panic ( "exactly 2 (key, value) arguments expected" )
}
key := call . Arguments [ 0 ] . Export ( ) . ( string )
val := call . Arguments [ 1 ] . Export ( )
value , err := json . Marshal ( val )
if err != nil {
logging . WithError ( err ) . Debug ( "unable to marshal" )
panic ( err )
}
metadata := & domain . Metadata {
Key : key ,
Value : value ,
}
if _ , err = s . command . SetUserMetadata ( ctx , metadata , userInfo . Subject , qu . User . ResourceOwner ) ; err != nil {
logging . WithError ( err ) . Info ( "unable to set md in action" )
panic ( err )
}
return nil
} ) ,
) ,
) ,
)
err = actions . Run (
actionCtx ,
ctxFields ,
apiFields ,
action . Script ,
action . Name ,
append ( actions . ActionToOptions ( action ) , actions . WithHTTP ( actionCtx ) , actions . WithUUID ( actionCtx ) ) ... ,
)
cancel ( )
if err != nil {
return err
}
if len ( claimLogs ) > 0 {
userInfo . AppendClaims ( fmt . Sprintf ( ClaimActionLogFormat , action . Name ) , claimLogs )
}
}
return nil
}