Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(212)

Unified Diff: components/devtools_bridge/android/java/src/org/chromium/components/devtools_bridge/apiary/OAuthClient.java

Issue 677843006: Registration of DevTools bridge in GCD. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@gcm-signaling
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/devtools_bridge/android/java/src/org/chromium/components/devtools_bridge/apiary/OAuthClient.java
diff --git a/components/devtools_bridge/android/java/src/org/chromium/components/devtools_bridge/apiary/OAuthClient.java b/components/devtools_bridge/android/java/src/org/chromium/components/devtools_bridge/apiary/OAuthClient.java
new file mode 100644
index 0000000000000000000000000000000000000000..7ddb38151a7314eab36a6efd79668beaf47302c7
--- /dev/null
+++ b/components/devtools_bridge/android/java/src/org/chromium/components/devtools_bridge/apiary/OAuthClient.java
@@ -0,0 +1,102 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.components.devtools_bridge.apiary;
+
+import android.util.JsonReader;
+
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.StringEntity;
+
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
+
+/**
+ * Google authentication client. Fetches a pair of refresh/access tokens for a
+ * secret received while registering the instance in GCD.
+ */
+public class OAuthClient {
+ public static final String API_BASE = "https://accounts.google.com/o/oauth2";
+ public static final String ENCODING = "UTF-8";
+ public static final String CONTENT_TYPE = "application/x-www-form-urlencoded";
+
+ private final HttpClient mHttpClient;
+ private final String mScope;
+ private final String mClientId;
+ private final String mClientSecret;
+
+ OAuthClient(HttpClient httpClient, String scope, String clientId, String clientSecret) {
+ assert httpClient != null;
+ assert scope != null;
+ assert clientId != null;
+ assert clientSecret != null;
+
+ mHttpClient = httpClient;
+ mScope = scope;
+ mClientId = clientId;
+ mClientSecret = clientSecret;
+ }
+
+ public OAuthResult authenticate(String secret) throws IOException {
+ final long startTimeMs = System.currentTimeMillis();
+
+ String content =
+ "client_id=" + urlEncode(mClientId)
+ + "&client_secret=" + urlEncode(mClientSecret)
+ + "&scope=" + urlEncode(mScope)
+ + "&code=" + urlEncode(secret)
+ + "&redirect_uri=oob"
+ + "&grant_type=authorization_code";
+
+ return mHttpClient.execute(
+ newHttpPost("/token", content),
+ new JsonResponseHandler<OAuthResult>() {
+ @Override
+ public OAuthResult readResponse(JsonReader reader) throws IOException {
+ return readResponse(reader, startTimeMs);
+ }
+ });
+ }
+
+ private HttpPost newHttpPost(String path, String content) throws UnsupportedEncodingException {
+ HttpPost request = new HttpPost(API_BASE + "/token");
+ request.setEntity(new StringEntity(content, ENCODING));
+ request.addHeader("Content-Type", CONTENT_TYPE);
+ return request;
+ }
+
+ private static String urlEncode(String value) {
+ try {
+ return URLEncoder.encode(value, ENCODING);
+ } catch (UnsupportedEncodingException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ static OAuthResult readResponse(JsonReader reader, long startTimeMs) throws IOException {
+ String refreshToken = null;
+ String accessToken = null;
+ long expiresInS = 0; // In seconds.
+
+ reader.beginObject();
+ while (reader.hasNext()) {
+ String name = reader.nextName();
+ if (name.equals("refresh_token")) {
+ refreshToken = reader.nextString();
+ } else if (name.equals("access_token")) {
+ accessToken = reader.nextString();
+ } else if (name.equals("expires_in")) {
+ expiresInS = reader.nextLong();
+ } else {
+ reader.skipValue();
+ }
+ }
+ reader.endObject();
+
+ return OAuthResult.create(
+ refreshToken, accessToken, startTimeMs + expiresInS * 1000);
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698