[java-idp-plugin-duo] branch main updated: Catch and throw exception on JWS creation

Phil Smart philip.smart at jisc.ac.uk
Tue Oct 6 16:16:26 UTC 2020


This is an automated email from the git hooks/post-receive script.

philsmart pushed a commit to branch main
in repository java-idp-plugin-duo.

View the commit online:
http://git.shibboleth.net/view/?p=java-idp-plugin-duo.git;a=commit;h=a7f6c332fc9f8cc10085830c1e202da8c34192f7

The following commit(s) were added to refs/heads/main by this push:
       new  a7f6c33   Catch and throw exception on JWS creation
a7f6c33 is described below

commit a7f6c332fc9f8cc10085830c1e202da8c34192f7
Author: Phil Smart <philip.smart at jisc.ac.uk>
AuthorDate: Tue Oct 6 17:16:20 2020 +0100

    Catch and throw exception on JWS creation
    
    Required because of a switch to the older Auth0 JWT lib for Duo SDK
    compatibility
---
 .../idp/plugin/authn/duo/nimbus/NimbusUtils.java   | 51 +++++++++++++---------
 1 file changed, 31 insertions(+), 20 deletions(-)

diff --git a/idp-duo-nimbus-client-impl/src/main/java/net/shibboleth/idp/plugin/authn/duo/nimbus/NimbusUtils.java b/idp-duo-nimbus-client-impl/src/main/java/net/shibboleth/idp/plugin/authn/duo/nimbus/NimbusUtils.java
index 119ad93..df55569 100644
--- a/idp-duo-nimbus-client-impl/src/main/java/net/shibboleth/idp/plugin/authn/duo/nimbus/NimbusUtils.java
+++ b/idp-duo-nimbus-client-impl/src/main/java/net/shibboleth/idp/plugin/authn/duo/nimbus/NimbusUtils.java
@@ -18,6 +18,7 @@
 
 package net.shibboleth.idp.plugin.authn.duo.nimbus;
 
+import java.io.UnsupportedEncodingException;
 import java.security.SecureRandom;
 import java.time.Duration;
 import java.util.Collections;
@@ -27,7 +28,9 @@ import javax.annotation.Nonnull;
 
 import com.auth0.jwt.JWT;
 import com.auth0.jwt.algorithms.Algorithm;
+import com.auth0.jwt.exceptions.JWTCreationException;
 
+import net.shibboleth.idp.plugin.authn.duo.DuoClientException;
 import net.shibboleth.idp.plugin.authn.duo.DuoOIDCIntegration;
 import net.shibboleth.utilities.java.support.logic.Constraint;
 
@@ -69,20 +72,24 @@ public final class NimbusUtils {
      */
     //TODO this method and the below should be nimbus, inside oidc-commons, and merged into a single API
     @Nonnull static String createJWSRequestObject(@Nonnull final DuoOIDCIntegration duoIntegration, 
-            @Nonnull final String state, @Nonnull final String username) {
+            @Nonnull final String state, @Nonnull final String username) throws DuoClientException{
         final Date expiration = new Date();
         expiration.setTime(expiration.getTime() + Duration.ofHours(1).toMillis());
 
-        return JWT.create()
-                    .withHeader(Collections.singletonMap("alg", "HS512"))
-                    .withExpiresAt(expiration)
-                    .withClaim("scope", "openid")
-                    .withClaim("client_id", duoIntegration.getClientId())
-                    .withClaim("redirect_uri", duoIntegration.getRedirectURI())
-                    .withClaim("state", state)
-                    .withClaim("duo_uname", username)
-                    .withClaim("response_type", "code")
-                    .sign(Algorithm.HMAC512(duoIntegration.getSecretKey()));
+        try {
+            return JWT.create()
+                        .withHeader(Collections.singletonMap("alg", "HS512"))
+                        .withExpiresAt(expiration)
+                        .withClaim("scope", "openid")
+                        .withClaim("client_id", duoIntegration.getClientId())
+                        .withClaim("redirect_uri", duoIntegration.getRedirectURI())
+                        .withClaim("state", state)
+                        .withClaim("duo_uname", username)
+                        .withClaim("response_type", "code")
+                        .sign(Algorithm.HMAC512(duoIntegration.getSecretKey()));
+        } catch (IllegalArgumentException | JWTCreationException | UnsupportedEncodingException e) {
+            throw new DuoClientException(e);
+        }
  
     }
     
@@ -97,19 +104,23 @@ public final class NimbusUtils {
      */
     //TODO: replace with nimbus method inside of commons 
     @Nonnull static String createJWS(@Nonnull final String aud, 
-            @Nonnull final DuoOIDCIntegration duoIntegration) {
+            @Nonnull final DuoOIDCIntegration duoIntegration) throws DuoClientException{
         
         final Date expiration = new Date();
         expiration.setTime(expiration.getTime() + Duration.ofHours(1).toMillis());
        
-        return com.auth0.jwt.JWT.create()
-                    .withHeader(Collections.singletonMap("alg", "HS512"))
-                    .withIssuer(duoIntegration.getClientId())
-                    .withSubject(duoIntegration.getClientId())
-                    .withAudience(aud)
-                    .withExpiresAt(expiration)
-                    .withJWTId(NimbusUtils.generateJWTId(32))
-                    .sign(Algorithm.HMAC512(duoIntegration.getSecretKey()));        
+        try {
+            return com.auth0.jwt.JWT.create()
+                        .withHeader(Collections.singletonMap("alg", "HS512"))
+                        .withIssuer(duoIntegration.getClientId())
+                        .withSubject(duoIntegration.getClientId())
+                        .withAudience(aud)
+                        .withExpiresAt(expiration)
+                        .withJWTId(NimbusUtils.generateJWTId(32))
+                        .sign(Algorithm.HMAC512(duoIntegration.getSecretKey()));
+        } catch (IllegalArgumentException | JWTCreationException | UnsupportedEncodingException e) {
+            throw new DuoClientException(e);
+        }        
         
     }
 

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list