-
Notifications
You must be signed in to change notification settings - Fork 19
feat(auth): send from: on connect and reuse its challenge #410
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
gkc
wants to merge
2
commits into
trunk
Choose a base branch
from
gkc-from-first
base: trunk
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
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
100 changes: 100 additions & 0 deletions
100
at_client/src/main/java/org/atsign/client/api/AtCommandExecutorContext.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,100 @@ | ||
| package org.atsign.client.api; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
|
|
||
| /** | ||
| * The authentication context an {@link AtCommandExecutor} carries for the life of a connection: the | ||
| * identity it authenticates as ({@link #getAtSign() atSign}, {@link #getKeys() keys}, | ||
| * {@link #getConfig() config}) together with the single-use challenge from the {@code from:} the | ||
| * executor issues as its first command once ready. | ||
| * | ||
| * <p> | ||
| * Grouping these behind {@link AtCommandExecutor#getContext()} lets the authentication commands | ||
| * take | ||
| * exactly what they need from one accessor rather than the executor interface growing a separate | ||
| * method per field. | ||
| * | ||
| * <p> | ||
| * The identity fields are fixed for the life of the context. The challenge is session state: the | ||
| * executor {@link #setChallenge(String) retains} it once the initial {@code from:} completes, | ||
| * callers {@link #consumeChallenge() consume} it at most once (the server's {@code from:} challenge | ||
| * is single-use), and the executor {@link #clearChallenge() clears} it on disconnect — a challenge | ||
| * is only valid for the server session that issued it. | ||
| */ | ||
| public class AtCommandExecutorContext { | ||
|
|
||
| /** | ||
| * A context with no identity and no challenge, returned by executors that were not configured with | ||
| * an atSign (and so issue no initial {@code from:}). {@link #consumeChallenge()} always yields | ||
| * {@code null}, so authentication falls back to sending its own {@code from:}. | ||
| */ | ||
| public static final AtCommandExecutorContext EMPTY = new AtCommandExecutorContext(null, null, null); | ||
|
|
||
| private final AtSign atSign; | ||
|
|
||
| private final AtKeys keys; | ||
|
|
||
| private final Map<String, Object> config; | ||
|
|
||
| private final AtomicReference<String> challenge = new AtomicReference<>(); | ||
|
|
||
| public AtCommandExecutorContext(AtSign atSign, AtKeys keys, Map<String, Object> config) { | ||
| this.atSign = atSign; | ||
| this.keys = keys; | ||
| this.config = config; | ||
| } | ||
|
|
||
| /** | ||
| * @return the atSign this executor authenticates as, or {@code null} if none was configured | ||
| */ | ||
| public AtSign getAtSign() { | ||
| return atSign; | ||
| } | ||
|
|
||
| /** | ||
| * @return the keys this executor authenticates with, or {@code null} if none were configured (e.g. | ||
| * an onboarding executor whose keys are generated mid-flow and supplied to the command | ||
| * directly) | ||
| */ | ||
| public AtKeys getKeys() { | ||
| return keys; | ||
| } | ||
|
|
||
| /** | ||
| * @return the config sent in the {@code from:} command, or {@code null} if none was configured | ||
| */ | ||
| public Map<String, Object> getConfig() { | ||
| return config; | ||
| } | ||
|
|
||
| /** | ||
| * Retains the challenge returned by the initial {@code from:}. Called by the executor once that | ||
| * command completes on the ready thread. | ||
| * | ||
| * @param challenge the challenge from the server's {@code from:} response | ||
| */ | ||
| public void setChallenge(String challenge) { | ||
| this.challenge.set(challenge); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the retained {@code from:} challenge and clears it, so it is consumed at most once. | ||
| * Whichever authentication (CRAM or PKAM) sends its digest first consumes the challenge; a second | ||
| * authentication on the same connection (e.g. onboarding, which does CRAM then PKAM) gets | ||
| * {@code null} and must issue its own {@code from:}. | ||
| * | ||
| * @return the retained challenge, or {@code null} if none is available | ||
| */ | ||
| public String consumeChallenge() { | ||
| return challenge.getAndSet(null); | ||
| } | ||
|
|
||
| /** | ||
| * Clears any retained challenge. Called by the executor on disconnect — a challenge is only valid | ||
| * for the server session that issued it, so it must not survive into the next connection. | ||
| */ | ||
| public void clearChallenge() { | ||
| challenge.set(null); | ||
| } | ||
| } |
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
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we are using Lombok, I would recommend for this class too (as will remove boiler plate). Something like this...