-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from auth0/improve-tests
Improve tests coverage
- Loading branch information
Showing
28 changed files
with
1,521 additions
and
93 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
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
63 changes: 63 additions & 0 deletions
63
auth0/src/main/java/com/auth0/android/provider/AlgorithmHelper.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,63 @@ | ||
package com.auth0.android.provider; | ||
|
||
import android.support.annotation.NonNull; | ||
import android.util.Base64; | ||
import android.util.Log; | ||
|
||
import java.io.UnsupportedEncodingException; | ||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.SecureRandom; | ||
|
||
class AlgorithmHelper { | ||
|
||
private static final String TAG = AlgorithmHelper.class.getSimpleName(); | ||
|
||
private static final String US_ASCII = "US-ASCII"; | ||
private static final String SHA_256 = "SHA-256"; | ||
|
||
private String getBase64String(byte[] source) { | ||
return Base64.encodeToString(source, Base64.URL_SAFE | Base64.NO_WRAP | Base64.NO_PADDING); | ||
} | ||
|
||
byte[] getASCIIBytes(String value) { | ||
byte[] input; | ||
try { | ||
input = value.getBytes(US_ASCII); | ||
} catch (UnsupportedEncodingException e) { | ||
Log.e(TAG, "Could not convert string to an ASCII byte array", e); | ||
throw new IllegalStateException("Could not convert string to an ASCII byte array", e); | ||
} | ||
return input; | ||
} | ||
|
||
byte[] getSHA256(byte[] input) { | ||
byte[] signature; | ||
try { | ||
MessageDigest md = MessageDigest.getInstance(SHA_256); | ||
md.update(input, 0, input.length); | ||
signature = md.digest(); | ||
} catch (NoSuchAlgorithmException e) { | ||
Log.e(TAG, "Failed to get SHA-256 signature", e); | ||
throw new IllegalStateException("Failed to get SHA-256 signature", e); | ||
} | ||
return signature; | ||
} | ||
|
||
public String generateCodeVerifier() { | ||
SecureRandom sr = new SecureRandom(); | ||
byte[] code = new byte[32]; | ||
sr.nextBytes(code); | ||
String verifier = Base64.encodeToString(code, Base64.URL_SAFE | Base64.NO_WRAP | Base64.NO_PADDING); | ||
Log.d(TAG, "Generated code verifier is " + verifier); | ||
return verifier; | ||
} | ||
|
||
public String generateCodeChallenge(@NonNull String codeVerifier) { | ||
byte[] input = getASCIIBytes(codeVerifier); | ||
byte[] signature = getSHA256(input); | ||
String challenge = getBase64String(signature); | ||
Log.d(TAG, "Generated code challenge is " + challenge); | ||
return challenge; | ||
} | ||
} |
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
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
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
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
Oops, something went wrong.