-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor id token verifier classes for brevity
- Loading branch information
1 parent
24729be
commit a7418c4
Showing
7 changed files
with
88 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 0 additions & 12 deletions
12
auth0/src/main/java/com/auth0/android/provider/NoSignatureVerifier.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 50 additions & 16 deletions
66
auth0/src/main/java/com/auth0/android/provider/SignatureVerifier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,62 @@ | ||
package com.auth0.android.provider; | ||
|
||
import android.support.annotation.CallSuper; | ||
import android.support.annotation.NonNull; | ||
|
||
import com.auth0.android.authentication.AuthenticationAPIClient; | ||
import com.auth0.android.authentication.AuthenticationException; | ||
import com.auth0.android.callback.AuthenticationCallback; | ||
import com.auth0.android.callback.BaseCallback; | ||
import com.auth0.android.jwt.JWT; | ||
|
||
//TODO: Make pkg private | ||
abstract class SignatureVerifier { | ||
|
||
private final String expectedAlgorithm; | ||
import java.security.InvalidKeyException; | ||
import java.security.PublicKey; | ||
import java.util.Map; | ||
|
||
SignatureVerifier(String expectedAlgorithm) { | ||
this.expectedAlgorithm = expectedAlgorithm; | ||
} | ||
/** | ||
* Abstract class meant to verify tokens signed with HS256 and RS256 signatures. | ||
*/ | ||
abstract class SignatureVerifier { | ||
|
||
private final void checkAlgorithm(JWT token) throws TokenValidationException { | ||
/** | ||
* Verifies that the given token's signature is valid, deeming the payload inside it authentic | ||
* | ||
* @param token the ID token to have its signature validated | ||
* @throws TokenValidationException if the signature is not valid | ||
*/ | ||
abstract void verifySignature(@NonNull JWT token) throws TokenValidationException; | ||
|
||
/** | ||
* Validates the algorithm of the given token is supported and creates a new instance of a SignatureVerifier | ||
* | ||
* @param token the ID token to create a signature verifier for | ||
* @param apiClient api client instance to fetch the JWKS keys, if necessary | ||
* @param callback the callback to receive the result in | ||
*/ | ||
static void forToken(@NonNull JWT token, @NonNull AuthenticationAPIClient apiClient, @NonNull final BaseCallback<SignatureVerifier, TokenValidationException> callback) { | ||
String algorithmName = token.getHeader().get("alg"); | ||
if (!expectedAlgorithm.equals(algorithmName)) { | ||
throw new TokenValidationException(String.format("Signature algorithm of \"%s\" is not supported. Expected \"%s\".", algorithmName, expectedAlgorithm)); | ||
if ("RS256".equals(algorithmName)) { | ||
final String keyId = token.getHeader().get("kid"); | ||
apiClient.fetchJsonWebKeys().start(new AuthenticationCallback<Map<String, PublicKey>>() { | ||
@Override | ||
public void onSuccess(Map<String, PublicKey> jwks) { | ||
PublicKey publicKey = jwks.get(keyId); | ||
try { | ||
callback.onSuccess(new AsymmetricSignatureVerifier(publicKey)); | ||
} catch (InvalidKeyException e) { | ||
callback.onFailure(new TokenValidationException(String.format("Could not find a public key for kid \"%s\"", keyId))); | ||
} | ||
} | ||
|
||
@Override | ||
public void onFailure(AuthenticationException error) { | ||
callback.onFailure(new TokenValidationException(String.format("Could not find a public key for kid \"%s\"", keyId))); | ||
} | ||
}); | ||
} else if ("HS256".equals(algorithmName)) { | ||
callback.onSuccess(new SymmetricSignatureVerifier()); | ||
} else { | ||
callback.onFailure(new TokenValidationException(String.format("Signature algorithm of \"%s\" is not supported. Expected either \"RS256\" or \"HS256\".", algorithmName))); | ||
} | ||
} | ||
|
||
@CallSuper | ||
void verifySignature(JWT token) throws TokenValidationException { | ||
checkAlgorithm(token); | ||
} | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
auth0/src/main/java/com/auth0/android/provider/SimpleAuthCallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
* AuthCallback.java | ||
* SimpleAuthCallback.java | ||
* | ||
* Copyright (c) 2016 Auth0 (http://auth0.com) | ||
* | ||
|
17 changes: 17 additions & 0 deletions
17
auth0/src/main/java/com/auth0/android/provider/SymmetricSignatureVerifier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.auth0.android.provider; | ||
|
||
import android.support.annotation.NonNull; | ||
|
||
import com.auth0.android.jwt.JWT; | ||
|
||
/** | ||
* Token signature verifier for HS256 algorithms. | ||
*/ | ||
class SymmetricSignatureVerifier extends SignatureVerifier { | ||
|
||
@Override | ||
void verifySignature(@NonNull JWT token) throws TokenValidationException { | ||
//NO-OP | ||
//HS256 (symmetric) signatures cannot be calculated on non-confidential clients | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters