@@ -615,23 +615,12 @@ func (a *Authenticator) AuthenticateToken(ctx context.Context, token string) (*a
615
615
}
616
616
617
617
if a .celMapper .UserValidationRules != nil {
618
- userInfo := & authenticationv1.UserInfo {
619
- Extra : make (map [string ]authenticationv1.ExtraValue ),
620
- Groups : info .GetGroups (),
621
- UID : info .GetUID (),
622
- Username : info .GetName (),
623
- }
624
- // Convert the extra information in the user object
625
- for key , val := range info .GetExtra () {
626
- userInfo .Extra [key ] = authenticationv1 .ExtraValue (val )
627
- }
628
-
629
618
// Convert the user info to unstructured so that we can evaluate the CEL expressions
630
619
// against the user info. This is done once here so that we don't have to convert
631
620
// the user info to unstructured multiple times in the CEL mapper for each mapping.
632
- userInfoUnstructured , err := convertObjectToUnstructured ( userInfo )
621
+ userInfoUnstructured , err := convertUserInfoToUnstructured ( info )
633
622
if err != nil {
634
- return nil , false , fmt .Errorf ("oidc: could not convert user info to unstructured: %v " , err )
623
+ return nil , false , fmt .Errorf ("oidc: could not convert user info to unstructured: %w " , err )
635
624
}
636
625
637
626
evalResult , err := a .celMapper .UserValidationRules .EvalUser (ctx , userInfoUnstructured )
@@ -944,3 +933,40 @@ func convertObjectToUnstructured(obj interface{}) (*unstructured.Unstructured, e
944
933
}
945
934
return & unstructured.Unstructured {Object : ret }, nil
946
935
}
936
+
937
+ func convertUserInfoToUnstructured (info user.Info ) (* unstructured.Unstructured , error ) {
938
+ userInfo := & authenticationv1.UserInfo {
939
+ Extra : make (map [string ]authenticationv1.ExtraValue ),
940
+ Groups : info .GetGroups (),
941
+ UID : info .GetUID (),
942
+ Username : info .GetName (),
943
+ }
944
+ // Convert the extra information in the user object
945
+ for key , val := range info .GetExtra () {
946
+ userInfo .Extra [key ] = authenticationv1 .ExtraValue (val )
947
+ }
948
+
949
+ // Convert the user info to unstructured so that we can evaluate the CEL expressions
950
+ // against the user info. This is done once here so that we don't have to convert
951
+ // the user info to unstructured multiple times in the CEL mapper for each mapping.
952
+ userInfoUnstructured , err := convertObjectToUnstructured (userInfo )
953
+ if err != nil {
954
+ return nil , err
955
+ }
956
+
957
+ // check if the user info contains the required fields. If not, set them to empty values.
958
+ // This is done because the CEL expressions expect these fields to be present.
959
+ if userInfoUnstructured .Object ["username" ] == nil {
960
+ userInfoUnstructured .Object ["username" ] = ""
961
+ }
962
+ if userInfoUnstructured .Object ["uid" ] == nil {
963
+ userInfoUnstructured .Object ["uid" ] = ""
964
+ }
965
+ if userInfoUnstructured .Object ["groups" ] == nil {
966
+ userInfoUnstructured .Object ["groups" ] = []string {}
967
+ }
968
+ if userInfoUnstructured .Object ["extra" ] == nil {
969
+ userInfoUnstructured .Object ["extra" ] = map [string ]authenticationv1.ExtraValue {}
970
+ }
971
+ return userInfoUnstructured , nil
972
+ }
0 commit comments