|
| 1 | +package com.dropbox.core; |
| 2 | + |
| 3 | +import com.dropbox.core.http.HttpRequestor; |
| 4 | +import com.dropbox.core.util.LangUtil; |
| 5 | +import com.dropbox.core.v2.DbxRawClientV2; |
| 6 | + |
| 7 | +import java.io.Serializable; |
| 8 | +import java.io.UnsupportedEncodingException; |
| 9 | +import java.security.MessageDigest; |
| 10 | +import java.security.NoSuchAlgorithmException; |
| 11 | +import java.security.SecureRandom; |
| 12 | +import java.util.HashMap; |
| 13 | +import java.util.Map; |
| 14 | + |
| 15 | +import static com.dropbox.core.util.StringUtil.urlSafeBase64Encode; |
| 16 | + |
| 17 | +/** |
| 18 | + * This class should be lib/jar private. We make it public so that Android related code can use it. |
| 19 | + * |
| 20 | + * <b>Beta</b>: This feature is not available to all developers. Please do NOT use it unless you are |
| 21 | + * early access partner of this feature. The function signature is subjected to change |
| 22 | + * in next minor version release. |
| 23 | + * |
| 24 | + * This class does code verifier and code challenge generation in Proof Key for Code Exchange(PKCE). |
| 25 | + * @see <a href="https://tools.ietf.org/html/rfc7636">https://tools.ietf.org/html/rfc7636</a> |
| 26 | + */ |
| 27 | +public class DbxPKCEManager implements Serializable { |
| 28 | + public static final long serialVersionUID = 0; |
| 29 | + public static final String CODE_CHALLENGE_METHODS = "S256"; |
| 30 | + public static final int CODE_VERIFIER_SIZE = 128; |
| 31 | + |
| 32 | + private static final SecureRandom RAND = new SecureRandom(); |
| 33 | + private static final String CODE_VERIFIER_CHAR_SET = |
| 34 | + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~"; |
| 35 | + |
| 36 | + private String codeVerifier; |
| 37 | + private String codeChallenge; |
| 38 | + |
| 39 | + /** |
| 40 | + * This class has state. Each instance has a randomly generated codeVerifier in it. Just |
| 41 | + * like we shouldn't re-use the same code verifier in PKCE, we shouldn't re-use the same |
| 42 | + * DbxPKCEManager instance in different OAuth flow. |
| 43 | + */ |
| 44 | + public DbxPKCEManager() { |
| 45 | + this.codeVerifier = generateCodeVerifier(); |
| 46 | + this.codeChallenge = generateCodeChallenge(); |
| 47 | + } |
| 48 | + |
| 49 | + String generateCodeVerifier() { |
| 50 | + StringBuilder sb = new StringBuilder(); |
| 51 | + |
| 52 | + for (int i = 0; i < CODE_VERIFIER_SIZE; i++) { |
| 53 | + sb.append(CODE_VERIFIER_CHAR_SET.charAt(RAND.nextInt(CODE_VERIFIER_CHAR_SET.length()))); |
| 54 | + } |
| 55 | + |
| 56 | + return sb.toString(); |
| 57 | + } |
| 58 | + |
| 59 | + String generateCodeChallenge() { |
| 60 | + return generateCodeChallenge(this.codeVerifier); |
| 61 | + } |
| 62 | + |
| 63 | + static String generateCodeChallenge(String codeVerifier) { |
| 64 | + try { |
| 65 | + MessageDigest digest = MessageDigest.getInstance("SHA-256"); |
| 66 | + byte[] signiture = digest.digest(codeVerifier.getBytes("US-ASCII")); |
| 67 | + return urlSafeBase64Encode(signiture).replaceAll("=+$", ""); // remove trailing equal |
| 68 | + } catch (NoSuchAlgorithmException e) { |
| 69 | + throw LangUtil.mkAssert("Impossible", e); |
| 70 | + } catch (UnsupportedEncodingException e) { |
| 71 | + throw LangUtil.mkAssert("Impossible", e); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @return The randomly generate code verifier in this instance. |
| 77 | + */ |
| 78 | + public String getCodeVerifier() { |
| 79 | + return this.codeVerifier; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @return The code challenge, which is a hashed code verifier. |
| 84 | + */ |
| 85 | + public String getCodeChallenge() { |
| 86 | + return this.codeChallenge; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Make oauth2/token request to exchange code for oauth2 access token. Client secret is not |
| 91 | + * requried. |
| 92 | + * @param requestConfig Default attributes to use for each request. |
| 93 | + * @param oauth2Code OAuth2 code defined in OAuth2 code flow. |
| 94 | + * @param appKey Client Key |
| 95 | + * @param redirectUri The same redirect_uri that's used in preivous oauth2/authorize call. |
| 96 | + * @param host Only used for testing when you don't want to make request against production. |
| 97 | + * @return OAuth2 result, including oauth2 access token, and optionally expiration time and |
| 98 | + * refresh token. |
| 99 | + * @throws DbxException If reqeust is invalid, or code expired, or server error. |
| 100 | + */ |
| 101 | + public DbxAuthFinish makeTokenRequest(DbxRequestConfig requestConfig, |
| 102 | + String oauth2Code, |
| 103 | + String appKey, |
| 104 | + String redirectUri, |
| 105 | + DbxHost host) throws DbxException { |
| 106 | + Map<String, String> params = new HashMap<String, String>(); |
| 107 | + params.put("grant_type", "authorization_code"); |
| 108 | + params.put("code", oauth2Code); |
| 109 | + params.put("locale", requestConfig.getUserLocale()); |
| 110 | + params.put("client_id", appKey); |
| 111 | + params.put("code_verifier", this.codeVerifier); |
| 112 | + |
| 113 | + if (redirectUri != null) { |
| 114 | + params.put("redirect_uri", redirectUri); |
| 115 | + } |
| 116 | + |
| 117 | + return DbxRequestUtil.doPostNoAuth( |
| 118 | + requestConfig, |
| 119 | + DbxRawClientV2.USER_AGENT_ID, |
| 120 | + host.getApi(), |
| 121 | + "oauth2/token", |
| 122 | + DbxRequestUtil.toParamsArray(params), |
| 123 | + null, |
| 124 | + new DbxRequestUtil.ResponseHandler<DbxAuthFinish>() { |
| 125 | + @Override |
| 126 | + public DbxAuthFinish handle(HttpRequestor.Response response) throws DbxException { |
| 127 | + if (response.getStatusCode() != 200) { |
| 128 | + throw DbxRequestUtil.unexpectedStatus(response); |
| 129 | + } |
| 130 | + return DbxRequestUtil.readJsonFromResponse(DbxAuthFinish.Reader, response); |
| 131 | + } |
| 132 | + } |
| 133 | + ); |
| 134 | + } |
| 135 | +} |
0 commit comments