package com.perforce.workshop.tjuricek.p4oauth.models; import java.util.ArrayList; import java.util.Collection; import java.util.UUID; /** * The authorization code is generated after we know the user has given our * application the right credentials to p4d. */ public class AuthorizationCode { private String redirectUri; private String clientId; private String authorizationCode; private long created; private Collection<Token> tokens; public AuthorizationCode() { } public AuthorizationCode(String requestUri, String clientId, String authorizationCode, long created) { this(); this.redirectUri = requestUri; this.clientId = clientId; this.authorizationCode = authorizationCode; this.created = created; } /** * Generates a new authorization code for the redirectUri and client. * * @param requestUri * @param clientId * @return */ public static AuthorizationCode create(String requestUri, String clientId) { AuthorizationCode code = new AuthorizationCode(); code.setRedirectUri(requestUri); code.setClientId(clientId); code.setCreated(System.currentTimeMillis()); code.setAuthorizationCode(UUID.randomUUID().toString()); return code; } public Collection<Token> getTokens() { return tokens; } public void setTokens(Collection<Token> tokens) { this.tokens = tokens; } public void addToken(Token t) { if (this.tokens == null) { this.tokens = new ArrayList<>(); } this.tokens.add(t); } /** * This is the redirectUri used for the <em>originating</em> authorization * code request. */ public String getRedirectUri() { return redirectUri; } public void setRedirectUri(String redirectUri) { this.redirectUri = redirectUri; } public String getClientId() { return clientId; } public void setClientId(String clientId) { this.clientId = clientId; } public String getAuthorizationCode() { return authorizationCode; } public void setAuthorizationCode(String authorizationCode) { this.authorizationCode = authorizationCode; } public long getCreated() { return created; } public void setCreated(long created) { this.created = created; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; AuthorizationCode that = (AuthorizationCode) o; if (created != that.created) return false; if (authorizationCode != null ? !authorizationCode.equals(that.authorizationCode) : that.authorizationCode != null) return false; if (clientId != null ? !clientId.equals(that.clientId) : that.clientId != null) return false; if (redirectUri != null ? !redirectUri.equals(that.redirectUri) : that.redirectUri != null) return false; if (tokens != null ? !tokens.equals(that.tokens) : that.tokens != null) return false; return true; } @Override public int hashCode() { int result = redirectUri != null ? redirectUri.hashCode() : 0; result = 31 * result + (clientId != null ? clientId.hashCode() : 0); result = 31 * result + (authorizationCode != null ? authorizationCode.hashCode() : 0); result = 31 * result + (int) (created ^ (created >>> 32)); result = 31 * result + (tokens != null ? tokens.hashCode() : 0); return result; } @Override public String toString() { return "AuthorizationCode{" + "redirectUri='" + redirectUri + '\'' + ", clientId='" + clientId + '\'' + ", authorizationCode='" + authorizationCode + '\'' + ", created=" + created + ", tokens=" + tokens + '}'; } }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#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/AuthorizationCode.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. |