-
Notifications
You must be signed in to change notification settings - Fork 2
AB#268288 Federated Authentication #87
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
graciecooper
wants to merge
7
commits into
master
Choose a base branch
from
feature/268288-federated-auth
base: master
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 all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
33558ba
Squash Commits
graciecooper 6575554
add headers
graciecooper 0fc027a
auth failure no longer aborts the batch & clean up constructor
graciecooper eed58de
fix broken tests
graciecooper 7ceccc4
address PR comments
graciecooper e5526ad
address comments
graciecooper 496d0f0
address comments
graciecooper 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
Some comments aren't visible on the classic Files Changed page.
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
64 changes: 64 additions & 0 deletions
64
OptimoveSDK/optimove-sdk/src/main/java/com/optimove/android/AuthJwtResolver.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,64 @@ | ||
| package com.optimove.android; | ||
|
|
||
| import androidx.annotation.Nullable; | ||
|
|
||
| import com.optimove.android.main.tools.opti_logger.OptiLoggerStreamsContainer; | ||
|
|
||
| import java.util.concurrent.CountDownLatch; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
|
|
||
| public final class AuthJwtResolver { | ||
|
|
||
| private AuthJwtResolver() { | ||
| } | ||
|
|
||
| /** | ||
| * When federated auth is configured and {@code userId} is non-empty, a JWT is required before sending | ||
| * user-identified requests. Returns true if {@code jwt} is missing after {@link #blockingJwt} (failure, | ||
| * timeout, or empty token). | ||
| */ | ||
| public static boolean isMissingRequiredJwt( | ||
| @Nullable AuthManager authManager, | ||
| @Nullable String userId, | ||
| @Nullable String jwt) { | ||
| if (authManager == null) { | ||
| return false; | ||
| } | ||
| if (userId == null || userId.trim().isEmpty()) { | ||
| return false; | ||
| } | ||
| return jwt == null || jwt.isEmpty(); | ||
| } | ||
|
|
||
| @Nullable | ||
| public static String blockingJwt(@Nullable AuthManager authManager, @Nullable String userId, long timeoutMs) { | ||
| if (authManager == null || userId == null || userId.isEmpty()) { | ||
| return null; | ||
| } | ||
| CountDownLatch latch = new CountDownLatch(1); | ||
| AtomicReference<String> tokenRef = new AtomicReference<>(); | ||
| AtomicReference<Exception> errorRef = new AtomicReference<>(); | ||
| authManager.getToken(userId, (token, error) -> { | ||
| tokenRef.set(token); | ||
| errorRef.set(error); | ||
| latch.countDown(); | ||
| }); | ||
| try { | ||
| if (!latch.await(timeoutMs, TimeUnit.MILLISECONDS)) { | ||
| OptiLoggerStreamsContainer.warn("JWT fetch timed out for user '%s' after %d ms", userId, timeoutMs); | ||
| return null; | ||
| } | ||
| } catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
| OptiLoggerStreamsContainer.warn("JWT fetch interrupted for user '%s'", userId); | ||
| return null; | ||
| } | ||
| Exception error = errorRef.get(); | ||
| if (error != null) { | ||
| OptiLoggerStreamsContainer.warn("JWT fetch failed for user '%s': %s", userId, error.getMessage()); | ||
| return null; | ||
| } | ||
| return tokenRef.get(); | ||
| } | ||
| } | ||
32 changes: 32 additions & 0 deletions
32
OptimoveSDK/optimove-sdk/src/main/java/com/optimove/android/AuthManager.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,32 @@ | ||
| package com.optimove.android; | ||
|
|
||
| import androidx.annotation.NonNull; | ||
| import androidx.annotation.Nullable; | ||
|
|
||
| public final class AuthManager { | ||
|
k-antipochkin marked this conversation as resolved.
|
||
|
|
||
| private final @NonNull AuthTokenProvider provider; | ||
|
|
||
| public AuthManager(@NonNull AuthTokenProvider provider) { | ||
| this.provider = provider; | ||
| } | ||
|
|
||
| public void getToken(@Nullable String userId, @NonNull AuthTokenProvider.Callback completion) { | ||
| if (userId == null) { | ||
| completion.onComplete(null, new AuthTokenException(AuthTokenException.Kind.NO_USER_ID)); | ||
| return; | ||
| } | ||
| String id = userId.trim(); | ||
| if (id.isEmpty()) { | ||
| completion.onComplete(null, new AuthTokenException(AuthTokenException.Kind.NO_USER_ID)); | ||
| return; | ||
| } | ||
| provider.getToken(id, (token, error) -> { | ||
| if (token != null) { | ||
| completion.onComplete(token, null); | ||
| } else { | ||
| completion.onComplete(null, error != null ? error : new AuthTokenException(AuthTokenException.Kind.TOKEN_FETCH_FAILED)); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
28 changes: 28 additions & 0 deletions
28
OptimoveSDK/optimove-sdk/src/main/java/com/optimove/android/AuthTokenException.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,28 @@ | ||
| package com.optimove.android; | ||
|
|
||
| import androidx.annotation.NonNull; | ||
|
|
||
| public final class AuthTokenException extends Exception { | ||
|
|
||
| public enum Kind { | ||
| TOKEN_FETCH_FAILED("Failed to fetch auth token from provider."), | ||
| NO_USER_ID("No userId available for auth token request."); | ||
|
|
||
| private final String message; | ||
|
|
||
| Kind(String message) { | ||
| this.message = message; | ||
| } | ||
| } | ||
|
|
||
| private final @NonNull Kind kind; | ||
|
|
||
| public AuthTokenException(@NonNull Kind kind) { | ||
| super(kind.message); | ||
| this.kind = kind; | ||
| } | ||
|
|
||
| public @NonNull Kind getKind() { | ||
| return kind; | ||
| } | ||
| } |
15 changes: 15 additions & 0 deletions
15
OptimoveSDK/optimove-sdk/src/main/java/com/optimove/android/AuthTokenProvider.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,15 @@ | ||
| package com.optimove.android; | ||
|
|
||
| import androidx.annotation.NonNull; | ||
| import androidx.annotation.Nullable; | ||
|
|
||
|
|
||
| public interface AuthTokenProvider { | ||
|
|
||
| void getToken(@NonNull String userId, @NonNull Callback callback); | ||
|
|
||
| @FunctionalInterface | ||
| interface Callback { | ||
| void onComplete(@Nullable String token, @Nullable Exception error); | ||
| } | ||
| } |
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
13 changes: 13 additions & 0 deletions
13
OptimoveSDK/optimove-sdk/src/main/java/com/optimove/android/OptimoveAuthHeaders.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,13 @@ | ||
| package com.optimove.android; | ||
|
|
||
| public final class OptimoveAuthHeaders { | ||
|
|
||
| public static final String USER_JWT = "X-User-JWT"; | ||
| public static final String AUTH_CAPABLE = "X-Optimove-Auth-Capable"; | ||
| public static final String AUTH_CAPABLE_VALUE = "1"; | ||
| public static final String PLATFORM = "X-Optimove-Platform"; | ||
| public static final String PLATFORM_VALUE = "android"; | ||
|
|
||
| private OptimoveAuthHeaders() { | ||
| } | ||
| } |
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.
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.