@@ -31,9 +31,9 @@ type KeyCloakConnector struct {
3131
3232func EntityResolution (ctx context.Context ,
3333 req * authorization.IdpPluginRequest , config * authorization.IdpConfig ) (* authorization.IdpPluginResponse , error ) {
34- slog . Info ( fmt . Sprintf ( "req: %+v" , req ))
35- slog .Info ( fmt .Sprintf ("config: %+v" , config ))
36- jsonString , err := json .Marshal (config .Config .AsMap ())
34+ // note this only logs when run in test not when running in the OPE engine.
35+ slog .DebugContext ( ctx , "EntityResolution" , "req" , fmt .Sprintf ("%+v" , req ), " config" , fmt . Sprintf ( " %+v" , config ))
36+ jsonString , err := json .Marshal (config .GetConfig () .AsMap ())
3737 if err != nil {
3838 slog .Error ("Error marshalling keycloak config!" , "error" , err )
3939 return nil , err
@@ -52,26 +52,55 @@ func EntityResolution(ctx context.Context,
5252 payload := req .GetEntities ()
5353
5454 var resolvedEntities []* authorization.IdpEntityRepresentation
55- slog .Debug ( "EntityResolution invoked with " , "payload" , payload )
55+ slog .DebugContext ( ctx , "EntityResolution invoked" , "payload" , payload )
5656
57- for i , ident := range payload {
58- slog .Debug ( "Lookup" , "entity" , ident .GetEntityType ())
57+ for _ , ident := range payload {
58+ slog .DebugContext ( ctx , "Lookup" , "entity" , ident .GetEntityType ())
5959 var keycloakEntities []* gocloak.User
6060 var getUserParams gocloak.GetUsersParams
61-
6261 exactMatch := true
63- switch ident .EntityType .(type ) {
62+ switch ident .GetEntityType ().(type ) {
63+ case * authorization.Entity_ClientId :
64+ slog .DebugContext (ctx , "GetClient" , "client_id" , ident .GetClientId ())
65+ clientID := ident .GetClientId ()
66+ clients , err := connector .client .GetClients (ctx , connector .token .AccessToken , kcConfig .Realm , gocloak.GetClientsParams {
67+ ClientID : & clientID ,
68+ })
69+ if err != nil {
70+ slog .Error (err .Error ())
71+ return & authorization.IdpPluginResponse {},
72+ status .Error (codes .Internal , services .ErrGetRetrievalFailed )
73+ }
74+ var jsonEntities []* structpb.Struct
75+ for _ , client := range clients {
76+ json , err := typeToGenericJSONMap (client )
77+ if err != nil {
78+ slog .Error ("Error serializing entity representation!" , "error" , err )
79+ return & authorization.IdpPluginResponse {},
80+ status .Error (codes .Internal , services .ErrCreationFailed )
81+ }
82+ var mystruct , struct_err = structpb .NewStruct (json )
83+ if struct_err != nil {
84+ slog .Error ("Error making struct!" , "error" , err )
85+ return & authorization.IdpPluginResponse {},
86+ status .Error (codes .Internal , services .ErrCreationFailed )
87+ }
88+ jsonEntities = append (jsonEntities , mystruct )
89+ }
90+ resolvedEntities = append (
91+ resolvedEntities ,
92+ & authorization.IdpEntityRepresentation {
93+ OriginalId : ident .GetId (),
94+ AdditionalProps : jsonEntities ,
95+ },
96+ )
97+ return & authorization.IdpPluginResponse {
98+ EntityRepresentations : resolvedEntities ,
99+ }, nil
64100 case * authorization.Entity_EmailAddress :
65- getUserParams = gocloak.GetUsersParams {Email : func () * string { t := payload [ i ] .GetEmailAddress (); return & t }(), Exact : & exactMatch }
101+ getUserParams = gocloak.GetUsersParams {Email : func () * string { t := ident .GetEmailAddress (); return & t }(), Exact : & exactMatch }
66102 case * authorization.Entity_UserName :
67- getUserParams = gocloak.GetUsersParams {Username : func () * string { t := payload [i ].GetUserName (); return & t }(), Exact : & exactMatch }
68- // case "":
69- // return &authorization.IdpPluginResponse{},
70- // status.Error(codes.InvalidArgument, services.ErrNotFound)
71- default :
72- typeErr := fmt .Errorf ("Unsupported/unknown type for entity %s" , ident .String ())
73- return & authorization.IdpPluginResponse {},
74- status .Error (codes .InvalidArgument , typeErr .Error ())
103+ getUserParams = gocloak.GetUsersParams {Username : func () * string { t := ident .GetUserName (); return & t }(), Exact : & exactMatch }
75104 }
76105
77106 users , err := connector .client .GetUsers (ctx , connector .token .AccessToken , kcConfig .Realm , getUserParams )
@@ -93,7 +122,7 @@ func EntityResolution(ctx context.Context,
93122 ctx ,
94123 connector .token .AccessToken ,
95124 kcConfig .Realm ,
96- gocloak.GetGroupsParams {Search : func () * string { t := payload [ i ] .GetEmailAddress (); return & t }()},
125+ gocloak.GetGroupsParams {Search : func () * string { t := ident .GetEmailAddress (); return & t }()},
97126 )
98127 if groupErr != nil {
99128 slog .Error ("Error getting group" , "group" , groupErr )
@@ -153,7 +182,7 @@ func EntityResolution(ctx context.Context,
153182 OriginalId : ident .GetId (),
154183 AdditionalProps : jsonEntities },
155184 )
156- slog .Debug ( "Entities" , "resolved" , fmt .Sprintf ("%+v" , resolvedEntities ))
185+ slog .DebugContext ( ctx , "Entities" , "resolved" , fmt .Sprintf ("%+v" , resolvedEntities ))
157186 }
158187
159188 return & authorization.IdpPluginResponse {
0 commit comments