-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #119 from bcgov/develop/alex-GRAD2-2410
Develop/alex grad2 2410
- Loading branch information
Showing
12 changed files
with
313 additions
and
70 deletions.
There are no files selected for viewing
This file contains 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 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
24 changes: 24 additions & 0 deletions
24
api/src/main/java/ca/bc/gov/educ/api/gradbusiness/config/TokenUtilsConfig.java
This file contains 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,24 @@ | ||
package ca.bc.gov.educ.api.gradbusiness.config; | ||
|
||
import ca.bc.gov.educ.api.gradbusiness.model.dto.ResponseObjCache; | ||
import ca.bc.gov.educ.api.gradbusiness.util.EducGraduationApiConstants; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class TokenUtilsConfig { | ||
|
||
EducGraduationApiConstants constants; | ||
|
||
@Autowired | ||
public TokenUtilsConfig(EducGraduationApiConstants constants) { | ||
this.constants = constants; | ||
} | ||
|
||
@Bean | ||
public ResponseObjCache createResponseObjCache() { | ||
return new ResponseObjCache(constants.getTokenExpiryOffset()); | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
api/src/main/java/ca/bc/gov/educ/api/gradbusiness/model/dto/ResponseObj.java
This file contains 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,10 @@ | ||
package ca.bc.gov.educ.api.gradbusiness.model.dto; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class ResponseObj { | ||
|
||
private String access_token; | ||
private String refresh_token; | ||
} |
42 changes: 42 additions & 0 deletions
42
api/src/main/java/ca/bc/gov/educ/api/gradbusiness/model/dto/ResponseObjCache.java
This file contains 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,42 @@ | ||
package ca.bc.gov.educ.api.gradbusiness.model.dto; | ||
|
||
import org.json.JSONObject; | ||
|
||
import java.util.Base64; | ||
|
||
public class ResponseObjCache { | ||
|
||
private long tokenExpiry = 0; | ||
private ResponseObj responseObj; | ||
|
||
// tokenExpiry-[seconds] provides a slight offset, if token WILL expire in | ||
// [seconds], obtain a new one | ||
private int offset; | ||
|
||
public ResponseObjCache(int offset) { | ||
this.offset = offset; | ||
} | ||
|
||
public ResponseObj getResponseObj() { | ||
return responseObj; | ||
} | ||
|
||
public void setResponseObj(ResponseObj responseObj) { | ||
this.setTokenExpiry(responseObj); | ||
this.responseObj = responseObj; | ||
} | ||
|
||
public boolean isExpired(){ | ||
// tokenExpiry-[seconds] provides a slight offset, if token WILL expire in | ||
// 10 seconds, obtain a new one | ||
return (responseObj == null) || (tokenExpiry-offset) < (System.currentTimeMillis() / 1000); | ||
} | ||
|
||
private void setTokenExpiry(ResponseObj responseObj){ | ||
String[] parts = responseObj.getAccess_token().split("\\."); | ||
JSONObject payload = new JSONObject(new String(Base64.getUrlDecoder().decode(parts[1]))); | ||
this.tokenExpiry = payload.getLong("exp"); | ||
} | ||
|
||
|
||
} |
This file contains 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 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.