82
82
import java .net .Proxy ;
83
83
import java .net .URLEncoder ;
84
84
import java .nio .charset .StandardCharsets ;
85
+ import java .security .MessageDigest ;
85
86
import java .util .HashMap ;
86
87
import java .util .LinkedList ;
87
88
import java .util .List ;
@@ -108,7 +109,7 @@ public class AzureSecurityRealm extends SecurityRealm {
108
109
private static final String TIMESTAMP_ATTRIBUTE = AzureSecurityRealm .class .getName () + ".beginTime" ;
109
110
private static final String NONCE_ATTRIBUTE = AzureSecurityRealm .class .getName () + ".nonce" ;
110
111
private static final Logger LOGGER = Logger .getLogger (AzureSecurityRealm .class .getName ());
111
- private static final int NONCE_LENGTH = 10 ;
112
+ private static final int NONCE_LENGTH = 16 ;
112
113
public static final String CALLBACK_URL = "/securityRealm/finishLogin" ;
113
114
private static final String CONVERTER_NODE_CLIENT_ID = "clientid" ;
114
115
private static final String CONVERTER_NODE_CLIENT_SECRET = "clientsecret" ;
@@ -458,7 +459,12 @@ public HttpResponse doFinishLogin(StaplerRequest request)
458
459
JwtClaims validateIdToken (String expectedNonce , String idToken ) throws InvalidJwtException {
459
460
JwtClaims claims = getJwtConsumer ().processToClaims (idToken );
460
461
final String responseNonce = (String ) claims .getClaimValue ("nonce" );
461
- if (StringUtils .isAnyEmpty (expectedNonce , responseNonce ) || !expectedNonce .equals (responseNonce )) {
462
+ if (StringUtils .isAnyEmpty (expectedNonce , responseNonce )
463
+ || !MessageDigest .isEqual (
464
+ expectedNonce .getBytes (StandardCharsets .UTF_8 ),
465
+ responseNonce .getBytes (StandardCharsets .UTF_8 )
466
+ )
467
+ ) {
462
468
throw new IllegalStateException (String .format ("Invalid nonce in the response, "
463
469
+ "expected: %s actual: %s" , expectedNonce , responseNonce ));
464
470
}
0 commit comments