-
Notifications
You must be signed in to change notification settings - Fork 0
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
BearerToken model #373
BearerToken model #373
Conversation
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.
Yes, this is pretty much what I was suggesting...with a little extra encapsulation for the nully state, which I've added some commentary for.
Curious what you think of it too.
*/ | ||
public static boolean isValid(String input) { | ||
return input != null; | ||
} |
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.
Now that you have the BearerToken
class, I think you also have a place to formalize what it means for the actual token to be null
, give that thing a name, and encapsulate that concern instead of having to do null-checks anywhere.
I don't know if I have the name right here...but maybe it's as simple as Unauthenticated
? 🤷
} | |
public static BearerToken unauthenticated() { | |
return new BearerToken(null); | |
} | |
public boolean isAuthenticated() { | |
return value != null; | |
} |
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.
Semantically, I want to be pretty clear about what we're actually providing here. What the current implementation offers is: "WDS found an Authentication: Bearer …
header, and saved its contents for later use". What I am trying to avoid is: "WDS has an OAuth token that is currently valid for use in Terra".
The latter implies that the token isn't expired, decodes as a JWT, has the right scopes, was issued by the right OAuth issuer, etc. I'm trying to keep WDS out of this business, and instead delegate to Sam and Azure b2c for this work.
The former makes very few promises. This keeps WDS simple: if it finds some opaque value in the http header, it will then pass that same http header onwards when it makes outbound requests.
Does this stance 1) make sense, and 2) change anything about the model you're suggesting?
Perhaps it's just a terminology thing, I could be satisfied with simply changing BearerToken.isAuthenticated()
to BearerToken.isEmpty()
.
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.
Totally agree with your separation of concerns here, keeping WDS lean and mean wrt to the assertions it makes about this token. I'm mainly motivated to drive null
s out the interaction here, and completely incapable of coming up with a good name for what you describe. If you're good with isEmpty
I am too!
public static String getToken(String initialValue) { | ||
return getToken(initialValue, () -> null); | ||
public static BearerToken getToken(String initialValue) { | ||
return getToken(new BearerToken(initialValue), () -> null); |
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.
Here you could potentially use () => BearerToken.unauthenticated()
public static String getToken(String initialValue, Supplier<String> orElse) { | ||
if (initialValue != null) { | ||
public static BearerToken getToken(BearerToken initialValue, Supplier<BearerToken> orElse) { | ||
if (initialValue != null && initialValue.value() != null) { |
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.
Here you could potentially use initialValue.isAuthenticated()
instead of checking initialValue.value() != null
(subject to that being a reasonable name/concept to formalize)
Kudos, SonarCloud Quality Gate passed! |
} | ||
|
||
/** indicates if this BearerToken is not empty; that is, has a non-null `value` */ | ||
public boolean nonEmpty() { |
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.
The double negative here isn't the easiest to read, but I don't have a better alternative.
|
||
/** returns a BearerToken with the specified `value` */ | ||
public static BearerToken of(String val) { | ||
return new BearerToken(val); |
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.
only thought I had is - would it be useful to check if the token actually contains "Bearer" (or contains more then one) and if it doesnt throw a specific error that format is not correct or something like that? Maybe a very rare occurrence but when I was testing cloning I had Bearer in the token twice and it took an embarrassing amount of time to figure out :D
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.
See my previous reply to Josh: #373 (comment) … I am hoping to stay away from anything beyond the most basic validation. That said, if I'm in the minority here, we can add it …
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.
thank you!
Incorporate a
BearerToken
class as a safer and more encapsulated option instead of using rawString
s.