-
Notifications
You must be signed in to change notification settings - Fork 631
feat(webauthn): allow Android native origin #1196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
JeanLuX
wants to merge
2
commits into
supertokens:11.3
Choose a base branch
from
JeanLuX:feat/WebAuthn-Support-Android-Native-Origin
base: 11.3
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
272 changes: 272 additions & 0 deletions
272
src/test/java/io/supertokens/test/webauthn/api/TestAndroidOriginValidation.java
This file contains hidden or 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,272 @@ | ||
| package io.supertokens.test.webauthn.api; | ||
|
|
||
| import com.google.gson.JsonObject; | ||
| import io.supertokens.ProcessState; | ||
| import io.supertokens.pluginInterface.STORAGE_TYPE; | ||
| import io.supertokens.storageLayer.StorageLayer; | ||
| import io.supertokens.test.TestingProcessManager; | ||
| import io.supertokens.test.Utils; | ||
| import io.supertokens.test.httpRequest.HttpRequestForTesting; | ||
| import io.supertokens.test.httpRequest.HttpResponseException; | ||
| import io.supertokens.utils.SemVer; | ||
| import org.junit.AfterClass; | ||
| import org.junit.Before; | ||
| import org.junit.Rule; | ||
| import org.junit.Test; | ||
| import org.junit.rules.TestRule; | ||
|
|
||
| import static org.junit.Assert.*; | ||
|
|
||
| public class TestAndroidOriginValidation { | ||
| @Rule | ||
| public TestRule watchman = Utils.getOnFailure(); | ||
|
|
||
| @Rule | ||
| public TestRule retryFlaky = Utils.retryFlakyTest(); | ||
|
|
||
| @AfterClass | ||
| public static void afterTesting() { | ||
| Utils.afterTesting(); | ||
| } | ||
|
|
||
| @Before | ||
| public void beforeEach() { | ||
| Utils.reset(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testValidAndroidOrigin() throws Exception { | ||
| String[] args = {"../"}; | ||
|
|
||
| TestingProcessManager.TestingProcess process = TestingProcessManager.start(args); | ||
| assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED)); | ||
|
|
||
| if (StorageLayer.getStorage(process.getProcess()).getType() != STORAGE_TYPE.SQL) { | ||
| return; | ||
| } | ||
|
|
||
| JsonObject req = new JsonObject(); | ||
| req.addProperty("email", "[email protected]"); | ||
| req.addProperty("relyingPartyName", "Example"); | ||
| req.addProperty("relyingPartyId", "example.com"); | ||
| req.addProperty("origin", "android:apk-key-hash:47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU"); | ||
|
|
||
| try { | ||
| JsonObject resp = HttpRequestForTesting.sendJsonPOSTRequest(process.getProcess(), "", | ||
| "http://localhost:3567/recipe/webauthn/options/register", req, 1000, 1000, null, | ||
| SemVer.v5_3.get(), "webauthn"); | ||
| assertEquals("OK", resp.get("status").getAsString()); | ||
| } catch (HttpResponseException e) { | ||
| fail("Valid Android origin should be accepted: " + e.getMessage()); | ||
| } | ||
|
|
||
| process.kill(); | ||
| assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testValidAndroidOriginWithAlternativeHash() throws Exception { | ||
| String[] args = {"../"}; | ||
|
|
||
| TestingProcessManager.TestingProcess process = TestingProcessManager.start(args); | ||
| assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED)); | ||
|
|
||
| if (StorageLayer.getStorage(process.getProcess()).getType() != STORAGE_TYPE.SQL) { | ||
| return; | ||
| } | ||
|
|
||
| JsonObject req = new JsonObject(); | ||
| req.addProperty("email", "[email protected]"); | ||
| req.addProperty("relyingPartyName", "Example"); | ||
| req.addProperty("relyingPartyId", "example.com"); | ||
| req.addProperty("origin", "android:apk-key-hash:sYUC8p5I9SxqFernBPHmDxz_YVZXmVJdW8s-m3RTTqE"); | ||
|
|
||
| try { | ||
| JsonObject resp = HttpRequestForTesting.sendJsonPOSTRequest(process.getProcess(), "", | ||
| "http://localhost:3567/recipe/webauthn/options/register", req, 1000, 1000, null, | ||
| SemVer.v5_3.get(), "webauthn"); | ||
| assertEquals("OK", resp.get("status").getAsString()); | ||
| } catch (HttpResponseException e) { | ||
| fail("Valid Android origin with alternative hash should be accepted: " + e.getMessage()); | ||
| } | ||
|
|
||
| process.kill(); | ||
| assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testAndroidOriginWithEmptyHash() throws Exception { | ||
| String[] args = {"../"}; | ||
|
|
||
| TestingProcessManager.TestingProcess process = TestingProcessManager.start(args); | ||
| assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED)); | ||
|
|
||
| if (StorageLayer.getStorage(process.getProcess()).getType() != STORAGE_TYPE.SQL) { | ||
| return; | ||
| } | ||
|
|
||
| JsonObject req = new JsonObject(); | ||
| req.addProperty("email", "[email protected]"); | ||
| req.addProperty("relyingPartyName", "Example"); | ||
| req.addProperty("relyingPartyId", "example.com"); | ||
| req.addProperty("origin", "android:apk-key-hash:"); | ||
|
|
||
| JsonObject resp = HttpRequestForTesting.sendJsonPOSTRequest(process.getProcess(), "", | ||
| "http://localhost:3567/recipe/webauthn/options/register", req, 1000, 1000, null, | ||
| SemVer.v5_3.get(), "webauthn"); | ||
| assertEquals("INVALID_OPTIONS_ERROR", resp.get("status").getAsString()); | ||
| assertTrue(resp.get("reason").getAsString().contains("Android origin must contain a valid base64 hash")); | ||
|
|
||
| process.kill(); | ||
| assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testAndroidOriginWithInvalidCharacters() throws Exception { | ||
| String[] args = {"../"}; | ||
|
|
||
| TestingProcessManager.TestingProcess process = TestingProcessManager.start(args); | ||
| assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED)); | ||
|
|
||
| if (StorageLayer.getStorage(process.getProcess()).getType() != STORAGE_TYPE.SQL) { | ||
| return; | ||
| } | ||
|
|
||
| JsonObject req = new JsonObject(); | ||
| req.addProperty("email", "[email protected]"); | ||
| req.addProperty("relyingPartyName", "Example"); | ||
| req.addProperty("relyingPartyId", "example.com"); | ||
| req.addProperty("origin", "android:apk-key-hash:invalid@hash#with$special!"); | ||
|
|
||
| JsonObject resp = HttpRequestForTesting.sendJsonPOSTRequest(process.getProcess(), "", | ||
| "http://localhost:3567/recipe/webauthn/options/register", req, 1000, 1000, null, | ||
| SemVer.v5_3.get(), "webauthn"); | ||
| assertEquals("INVALID_OPTIONS_ERROR", resp.get("status").getAsString()); | ||
| assertTrue(resp.get("reason").getAsString().contains("Android origin hash must be valid URL-safe base64")); | ||
|
|
||
| process.kill(); | ||
| assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testAndroidOriginWithInvalidLength() throws Exception { | ||
| String[] args = {"../"}; | ||
|
|
||
| TestingProcessManager.TestingProcess process = TestingProcessManager.start(args); | ||
| assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED)); | ||
|
|
||
| if (StorageLayer.getStorage(process.getProcess()).getType() != STORAGE_TYPE.SQL) { | ||
| return; | ||
| } | ||
|
|
||
| JsonObject req = new JsonObject(); | ||
| req.addProperty("email", "[email protected]"); | ||
| req.addProperty("relyingPartyName", "Example"); | ||
| req.addProperty("relyingPartyId", "example.com"); | ||
| req.addProperty("origin", "android:apk-key-hash:abc"); | ||
|
|
||
| JsonObject resp = HttpRequestForTesting.sendJsonPOSTRequest(process.getProcess(), "", | ||
| "http://localhost:3567/recipe/webauthn/options/register", req, 1000, 1000, null, | ||
| SemVer.v5_3.get(), "webauthn"); | ||
| assertEquals("INVALID_OPTIONS_ERROR", resp.get("status").getAsString()); | ||
| assertTrue(resp.get("reason").getAsString().contains("Android origin hash must be 43 characters")); | ||
|
|
||
| process.kill(); | ||
| assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testAndroidOriginForSignInOptions() throws Exception { | ||
| String[] args = {"../"}; | ||
|
|
||
| TestingProcessManager.TestingProcess process = TestingProcessManager.start(args); | ||
| assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED)); | ||
|
|
||
| if (StorageLayer.getStorage(process.getProcess()).getType() != STORAGE_TYPE.SQL) { | ||
| return; | ||
| } | ||
|
|
||
| JsonObject req = new JsonObject(); | ||
| req.addProperty("relyingPartyName", "Example"); | ||
| req.addProperty("relyingPartyId", "example.com"); | ||
| req.addProperty("origin", "android:apk-key-hash:47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU"); | ||
| req.addProperty("timeout", 10000); | ||
| req.addProperty("userVerification", "preferred"); | ||
| req.addProperty("userPresence", false); | ||
|
|
||
| try { | ||
| JsonObject resp = HttpRequestForTesting.sendJsonPOSTRequest(process.getProcess(), "", | ||
| "http://localhost:3567/recipe/webauthn/options/signin", req, 1000, 1000, null, | ||
| SemVer.v5_3.get(), "webauthn"); | ||
| assertEquals("OK", resp.get("status").getAsString()); | ||
| } catch (HttpResponseException e) { | ||
| fail("Valid Android origin should be accepted for signin options: " + e.getMessage()); | ||
| } | ||
|
|
||
| process.kill(); | ||
| assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testMixedOriginsSupport() throws Exception { | ||
| String[] args = {"../"}; | ||
|
|
||
| TestingProcessManager.TestingProcess process = TestingProcessManager.start(args); | ||
| assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED)); | ||
|
|
||
| if (StorageLayer.getStorage(process.getProcess()).getType() != STORAGE_TYPE.SQL) { | ||
| return; | ||
| } | ||
|
|
||
| // Test that regular HTTP origins still work | ||
| JsonObject req1 = new JsonObject(); | ||
| req1.addProperty("email", "[email protected]"); | ||
| req1.addProperty("relyingPartyName", "Example"); | ||
| req1.addProperty("relyingPartyId", "example.com"); | ||
| req1.addProperty("origin", "http://example.com"); | ||
|
|
||
| try { | ||
| JsonObject resp = HttpRequestForTesting.sendJsonPOSTRequest(process.getProcess(), "", | ||
| "http://localhost:3567/recipe/webauthn/options/register", req1, 1000, 1000, null, | ||
| SemVer.v5_3.get(), "webauthn"); | ||
| assertEquals("OK", resp.get("status").getAsString()); | ||
| } catch (HttpResponseException e) { | ||
| fail("Regular HTTP origin should still work: " + e.getMessage()); | ||
| } | ||
|
|
||
| // Test that HTTPS origins still work | ||
| JsonObject req2 = new JsonObject(); | ||
| req2.addProperty("email", "[email protected]"); | ||
| req2.addProperty("relyingPartyName", "Example"); | ||
| req2.addProperty("relyingPartyId", "example.com"); | ||
| req2.addProperty("origin", "https://example.com"); | ||
|
|
||
| try { | ||
| JsonObject resp = HttpRequestForTesting.sendJsonPOSTRequest(process.getProcess(), "", | ||
| "http://localhost:3567/recipe/webauthn/options/register", req2, 1000, 1000, null, | ||
| SemVer.v5_3.get(), "webauthn"); | ||
| assertEquals("OK", resp.get("status").getAsString()); | ||
| } catch (HttpResponseException e) { | ||
| fail("Regular HTTPS origin should still work: " + e.getMessage()); | ||
| } | ||
|
|
||
| // Test that Android origins work | ||
| JsonObject req3 = new JsonObject(); | ||
| req3.addProperty("email", "[email protected]"); | ||
| req3.addProperty("relyingPartyName", "Example"); | ||
| req3.addProperty("relyingPartyId", "example.com"); | ||
| req3.addProperty("origin", "android:apk-key-hash:47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU"); | ||
|
|
||
| try { | ||
| JsonObject resp = HttpRequestForTesting.sendJsonPOSTRequest(process.getProcess(), "", | ||
| "http://localhost:3567/recipe/webauthn/options/register", req3, 1000, 1000, null, | ||
| SemVer.v5_3.get(), "webauthn"); | ||
| assertEquals("OK", resp.get("status").getAsString()); | ||
| } catch (HttpResponseException e) { | ||
| fail("Android origin should work alongside HTTP(S) origins: " + e.getMessage()); | ||
| } | ||
|
|
||
| process.kill(); | ||
| assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED)); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.