package com.perforce.workshop.tjuricek.p4oauth.models; import java.util.Collection; import java.util.Optional; import java.util.UUID; /** * When we "grant" access, we generate one of these. We save the redirectUri * and login, so that we can fetch the correct p4d Token associated with the * access key. */ public class AccessToken { private String id; private long created; private Collection<Token> tokens; public AccessToken() { created = System.currentTimeMillis(); } public static AccessToken forAuthCode(AuthorizationCode authCode) { AccessToken accessToken = new AccessToken(); accessToken.setId(UUID.randomUUID().toString()); accessToken.setTokens(authCode.getTokens()); return accessToken; } public String getId() { return id; } public void setId(String id) { this.id = id; } public long getCreated() { return created; } public void setCreated(long created) { this.created = created; } public Collection<Token> getTokens() { return tokens; } public void setTokens(Collection<Token> tokens) { this.tokens = tokens; } public Optional<Token> findToken(String redirectUri) { if (this.tokens == null) return Optional.empty(); return this.tokens.stream() .filter(t -> t.getRedirectUri().equals(redirectUri)) .findFirst(); } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; AccessToken that = (AccessToken) o; if (created != that.created) return false; if (id != null ? !id.equals(that.id) : that.id != null) return false; if (tokens != null ? !tokens.equals(that.tokens) : that.tokens != null) return false; return true; } @Override public int hashCode() { int result = id != null ? id.hashCode() : 0; result = 31 * result + (int) (created ^ (created >>> 32)); result = 31 * result + (tokens != null ? tokens.hashCode() : 0); return result; } @Override public String toString() { return "AccessToken{" + "id='" + id + '\'' + ", created=" + created + ", tokens=" + tokens + '}'; } }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#2 | 9180 | tjuricek |
0.1.1 Adding mechanism for fetching the p4 token given the bearer token on an Authorization header. This is probably secure since we can't use these tokens except for the restricted hosts. |
||
#1 | 9095 | tjuricek | Added some basic test data and renamed "workspace" to "workshop" in package name | ||
//guest/tjuricek/p4oauth/src/main/java/com/perforce/workspace/tjuricek/p4oauth/models/AccessToken.java | |||||
#1 | 9089 | tjuricek |
Moving some code that worked via some manual validation to the workshop. This just implements a basic code authorization grant scheme. Automated tests are forthcoming, awating some gradle plugin work that should sit outside of this project. |