11package ee .ria .sso .oidc ;
22
3- import ee .ria .sso .authentication .AuthenticationType ;
43import ee .ria .sso .authentication .principal .TaraPrincipal ;
4+ import ee .ria .sso .authentication .principal .TaraPrincipalFactory ;
55import lombok .Getter ;
66import lombok .extern .slf4j .Slf4j ;
77import org .apache .commons .codec .digest .MessageDigestAlgorithms ;
1818import org .apereo .cas .support .oauth .OAuth20ResponseTypes ;
1919import org .apereo .cas .support .oauth .services .OAuthRegisteredService ;
2020import org .apereo .cas .ticket .accesstoken .AccessToken ;
21- import org .apereo .cas .util .CollectionUtils ;
2221import org .apereo .cas .util .DigestUtils ;
2322import org .apereo .cas .util .EncodingUtils ;
2423import org .apereo .cas .util .Pac4jUtils ;
3332import javax .servlet .http .HttpServletRequest ;
3433import javax .servlet .http .HttpServletResponse ;
3534import java .util .*;
36- import java .util .stream .Collectors ;
3735
3836import static ee .ria .sso .authentication .principal .TaraPrincipal .Attribute .*;
3937
@@ -47,9 +45,9 @@ public class TaraOidcIdTokenGeneratorService extends OidcIdTokenGeneratorService
4745 public static final String CLAIM_EMAIL = "email" ;
4846 public static final String CLAIM_EMAIL_VERIFIED = "email_verified" ;
4947
50- private static final List <TaraPrincipal .Attribute > validProfileAttributesToClaimsList = Arrays .asList (
48+ public static final List <TaraPrincipal .Attribute > validProfileAttributesToClaimsList = Collections . unmodifiableList ( Arrays .asList (
5149 FAMILY_NAME , GIVEN_NAME , DATE_OF_BIRTH
52- );
50+ )) ;
5351
5452 public TaraOidcIdTokenGeneratorService (final CasConfigurationProperties casProperties ,
5553 final OidcIdTokenSigningAndEncryptionService signingService ,
@@ -105,10 +103,7 @@ protected JwtClaims produceIdTokenClaims(final HttpServletRequest request,
105103 claims .setJwtId (UUID .randomUUID ().toString ());
106104 claims .setIssuer (oidc .getIssuer ());
107105 claims .setAudience (service .getClientId ());
108-
109- final NumericDate expirationDate = NumericDate .now ();
110- expirationDate .addSeconds (timeoutInSeconds );
111- claims .setExpirationTime (expirationDate );
106+ claims .setExpirationTime ( getExpirationDate (timeoutInSeconds ));
112107 claims .setIssuedAtToNow ();
113108 claims .setNotBeforeMinutesInThePast (oidc .getSkew ());
114109
@@ -117,59 +112,43 @@ protected JwtClaims produceIdTokenClaims(final HttpServletRequest request,
117112 claims .setClaim (OAuth20Constants .STATE , authentication .getAttributes ().get (OAuth20Constants .STATE ));
118113 claims .setClaim (OAuth20Constants .NONCE , authentication .getAttributes ().get (OAuth20Constants .NONCE ));
119114 claims .setClaim (OidcConstants .CLAIM_AT_HASH , generateAccessTokenHash (accessTokenId ));
120-
121115 return claims ;
122116 }
123117
124- private void setTaraClaims (AccessToken accessTokenId , JwtClaims claims ) {
125- Assert .notNull (accessTokenId .getTicketGrantingTicket (), "No TGT associated with this access token!" );
126- Assert .notNull (accessTokenId .getTicketGrantingTicket ().getAuthentication (), "No authentication associated with this TGT!" );
127118
128- Principal taraPrincipal = accessTokenId .getTicketGrantingTicket ().getAuthentication ().getPrincipal ();
129- claims .setSubject (getMandatoryPrincipalAttribute (PRINCIPAL_CODE , taraPrincipal ));
119+ private void setTaraClaims (AccessToken accessToken , JwtClaims claims ) {
120+ Assert .notNull (accessToken .getTicketGrantingTicket (), "No TGT associated with this access token!" );
121+ Assert .notNull (accessToken .getTicketGrantingTicket ().getAuthentication (), "No authentication associated with this TGT!" );
122+ Principal taraPrincipal = TaraPrincipalFactory .createPrincipal (accessToken .getTicketGrantingTicket ());
130123
131- if (isEmailClaimsRequested (taraPrincipal )) {
132- claims .setStringClaim (CLAIM_EMAIL , getMandatoryPrincipalAttribute (TaraPrincipal .Attribute .EMAIL , taraPrincipal ));
133- claims .setClaim (CLAIM_EMAIL_VERIFIED , getMandatoryPrincipalAttribute (TaraPrincipal .Attribute .EMAIL_VERIFIED , taraPrincipal , Boolean .class ));
124+ claims .setSubject (getAttributeValue (SUB , taraPrincipal ));
125+
126+ if (taraPrincipal .getAttributes ().containsKey (EMAIL .name ()) && taraPrincipal .getAttributes ().containsKey (EMAIL_VERIFIED .name ())) {
127+ claims .setStringClaim (CLAIM_EMAIL , getAttributeValue (EMAIL , taraPrincipal ));
128+ claims .setClaim (CLAIM_EMAIL_VERIFIED , getAttributeValue (EMAIL_VERIFIED , taraPrincipal , Boolean .class ));
134129 }
135130
136131 claims .setClaim (CLAIM_PROFILE_ATTRIBUTES , getProfileAttributesMap (taraPrincipal ));
137- claims .setStringListClaim (OidcConstants .AMR , getAmrValuesList ( taraPrincipal ));
132+ claims .setStringListClaim (OidcConstants .AMR , getAttributeValue ( AMR , taraPrincipal , List . class ));
138133
139- if (isOfAuthenticationType (taraPrincipal , AuthenticationType .eIDAS )) {
140- String levelOfAssurance = getMandatoryPrincipalAttribute (LEVEL_OF_ASSURANCE , taraPrincipal );
141- claims .setStringClaim (OidcConstants .ACR , levelOfAssurance );
134+ if (taraPrincipal .getAttributes ().containsKey (ACR .name ())) {
135+ claims .setStringClaim (OidcConstants .ACR , getAttributeValue (ACR , taraPrincipal ));
142136 }
143137 }
144138
145- private boolean isEmailClaimsRequested (Principal taraPrincipal ) {
146- return taraPrincipal .getAttributes ().get (TaraPrincipal .Attribute .EMAIL .name ()) != null ;
147- }
148-
149139 private Map <String , Object > getProfileAttributesMap (Principal principal ) {
150140 final Map <String , Object > principalAttributes = principal .getAttributes ();
151141 final Map <String , Object > profileAttributes = new TreeMap (String .CASE_INSENSITIVE_ORDER );
152142
153143 validProfileAttributesToClaimsList .forEach (key -> {
154144 Object value = principalAttributes .get (key .name ().toLowerCase ());
155145 if (value != null )
156- profileAttributes .put (key .name ().toLowerCase (), (( List ) value ). get ( 0 ) );
146+ profileAttributes .put (key .name ().toLowerCase (), value );
157147 });
158148
159149 return profileAttributes ;
160150 }
161151
162- private static boolean isOfAuthenticationType (Principal principal , AuthenticationType type ) {
163- String authenticationType = getMandatoryPrincipalAttribute (AUTHENTICATION_TYPE , principal );
164- return type .getAmrName ().equals (authenticationType );
165- }
166-
167- private List <String > getAmrValuesList (Principal principal ) {
168- String authenticationType = getMandatoryPrincipalAttribute (AUTHENTICATION_TYPE , principal );
169- return CollectionUtils .toCollection (authenticationType ).stream ()
170- .map (e -> e .toString ()).collect (Collectors .toList ());
171- }
172-
173152 private String generateAccessTokenHash (final AccessToken accessTokenId ) {
174153 final byte [] tokenBytes = accessTokenId .getId ().getBytes ();
175154 final String hashAlg ;
@@ -189,18 +168,20 @@ private String generateAccessTokenHash(final AccessToken accessTokenId) {
189168 return EncodingUtils .encodeBase64 (hashBytesLeftHalf );
190169 }
191170
192- private static String getMandatoryPrincipalAttribute (TaraPrincipal .Attribute attribute , Principal principal ) {
193- return getMandatoryPrincipalAttribute (attribute , principal , String .class );
171+ private static String getAttributeValue (TaraPrincipal .Attribute attribute , Principal principal ) {
172+ return getAttributeValue (attribute , principal , String .class );
194173 }
195174
196- private static <T > T getMandatoryPrincipalAttribute (TaraPrincipal .Attribute attribute , Principal principal , Class <T > clazz ) {
175+ private static <T > T getAttributeValue (TaraPrincipal .Attribute attribute , Principal principal , Class <T > clazz ) {
197176 String attributeName = attribute .name ();
198177 Assert .notNull (principal .getAttributes ().get (attributeName ), "Mandatory attribute " + attributeName + " not found when generating OIDC token" );
199- List list = ((List )(principal .getAttributes ().get (attributeName )));
200- Assert .isTrue (list .size () == 1 , "Expected a single profile attribute. Found " + list .size ());
201- Object o = list .get (0 );
202- Assert .isTrue (o .getClass ().isAssignableFrom (clazz ), "Cannot assign principal value of type " + o .getClass () + " to " + clazz );
203- return (T )o ;
178+ return (T )principal .getAttributes ().get (attributeName );
179+ }
180+
181+ private NumericDate getExpirationDate (long timeoutInSeconds ) {
182+ final NumericDate expirationDate = NumericDate .now ();
183+ expirationDate .addSeconds (timeoutInSeconds );
184+ return expirationDate ;
204185 }
205186}
206187
0 commit comments