-
Notifications
You must be signed in to change notification settings - Fork 155
[SDK-2622] Add expiresAt to token response #357
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6886933
Add tests for preserving deserialization of TokenHolder prior to refa…
jimmyjames 37bb0bd
Add custom deserializer
jimmyjames ab4c9f0
Add calculated expiresAt field to TokenHolder
jimmyjames 98d456a
Add test for default constructor
jimmyjames e437b3e
Updated test for improved coverage
jimmyjames bac6480
Review feedback - updated Javadocs, return null for expiresAt if expi…
jimmyjames 745b2fe
Merge branch 'master' into add-expires-at-to-token-response
jimmyjames 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
51 changes: 51 additions & 0 deletions
51
src/main/java/com/auth0/json/auth/TokenHolderDeserializer.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,51 @@ | ||
| package com.auth0.json.auth; | ||
|
|
||
| import com.fasterxml.jackson.core.JsonParser; | ||
| import com.fasterxml.jackson.core.JsonProcessingException; | ||
| import com.fasterxml.jackson.databind.DeserializationContext; | ||
| import com.fasterxml.jackson.databind.JsonNode; | ||
| import com.fasterxml.jackson.databind.deser.std.StdDeserializer; | ||
|
|
||
| import java.io.IOException; | ||
| import java.time.Instant; | ||
| import java.util.Date; | ||
|
|
||
| /** | ||
| * A custom Jackson Deserializer for the {@linkplain TokenHolder} object. It deserializes the JSON response and calculates | ||
| * an {@code expiresAt} date derived from the current time and the {@code expires_at} returned on the response. | ||
| */ | ||
| public class TokenHolderDeserializer extends StdDeserializer<TokenHolder> { | ||
|
|
||
| @SuppressWarnings("unused") | ||
| public TokenHolderDeserializer() { | ||
| this(null); | ||
| } | ||
|
|
||
| public TokenHolderDeserializer(Class<?> clazz) { | ||
| super(clazz); | ||
| } | ||
|
|
||
| @Override | ||
| public TokenHolder deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException { | ||
| JsonNode node = p.getCodec().readTree(p); | ||
|
|
||
| JsonNode accessTokenNode = node.get("access_token"); | ||
| JsonNode idTokenNode = node.get("id_token"); | ||
| JsonNode refreshTokenNode = node.get("refresh_token"); | ||
| JsonNode tokenTypeNode = node.get("token_type"); | ||
| JsonNode scopeNode = node.get("scope"); | ||
| JsonNode expiresInNode = node.get("expires_in"); | ||
|
|
||
| String accessToken = accessTokenNode != null ? accessTokenNode.asText() : null; | ||
| String idToken = idTokenNode != null ? idTokenNode.asText() : null; | ||
| String refreshToken = refreshTokenNode != null ? refreshTokenNode.asText() : null; | ||
| String tokenType = tokenTypeNode != null ? tokenTypeNode.asText() : null; | ||
| String scope = scopeNode != null ? scopeNode.asText() : null; | ||
|
|
||
| // As a primitive, if expires_in is not sent on the response, value will be 0 instead of null | ||
| long expiresIn = expiresInNode != null ? expiresInNode.asLong() : 0L; | ||
|
lbalmaceda marked this conversation as resolved.
|
||
| Date expiresAt = expiresInNode != null ? Date.from(Instant.now().plusSeconds(expiresIn)) : null; | ||
|
|
||
| return new TokenHolder(accessToken, idToken, refreshToken, tokenType, expiresIn, scope, expiresAt); | ||
| } | ||
| } | ||
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
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.