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

Side by Side Diff: remoting/webapp/me2mom/oauth2.js

Issue 7046012: Basic OAuth2 support using the native app flow. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: last one Created 9 years, 7 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « remoting/webapp/me2mom/manifest.json ('k') | remoting/webapp/me2mom/remoting.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Declare an OAuth2 class to handle retrieval/storage of an OAuth2 token.
6 //
7 // Ideally, this should implement the OAuth2 PostMessage flow to avoid needing
8 // to copy and paste a code, but that does not support extension URL schemes
9 // quite yet. Instead, we currently use the native app flow with an
10 // authorization code that the user must cut/paste.
11 function OAuth2() {
12 this.OAUTH2_REFRESH_TOKEN_NAME = 'oauth2_refresh_token';
13
14 this.client_id = encodeURIComponent(
15 '440925447803-m890isgsr23kdkcu2erd4mirnrjalf98.' +
16 'apps.googleusercontent.com');
17 this.client_secret = encodeURIComponent('TgKrL73H2kJe6Ir0ufp7bf6e');
18 this.scope = encodeURIComponent(
19 'https://www.googleapis.com/auth/chromoting ' +
20 'https://www.googleapis.com/auth/googletalk');
21 this.redirect_uri = encodeURIComponent('urn:ietf:wg:oauth:2.0:oob');
22 }
23
24 OAuth2.prototype.isAuthenticated = function() {
25 if(this.getRefreshToken()) {
26 return true;
27 }
28 return false;
29 }
30
31 OAuth2.prototype.clear = function() {
32 remoting.removeItem(this.OAUTH2_REFRESH_TOKEN_NAME);
33 delete this.access_token;
34 delete this.access_token_expiration;
35 }
36
37 OAuth2.prototype.setRefreshToken = function(token) {
38 remoting.setItem(this.OAUTH2_REFRESH_TOKEN_NAME, token);
39 }
40
41 OAuth2.prototype.getRefreshToken = function(token) {
42 return remoting.getItem(this.OAUTH2_REFRESH_TOKEN_NAME);
43 }
44
45 OAuth2.prototype.setAccessToken = function(token, expiration) {
46 this.access_token = token;
47 // Offset by 30 seconds to account for RTT issues.
48 // TODO(ajwong): See if this is necessary, or of the protocol already
49 // accounts for RTT.
50 this.access_token_expiration = expiration - 30000;
51 }
52
53 OAuth2.prototype.needsNewAccessToken = function() {
54 if (!this.isAuthenticated()) {
55 throw "Not Authenticated.";
56 }
57 if (!this.access_token) {
58 return true;
59 }
60 if (Date.now() > this.access_token_expiration) {
61 return true;
62 }
63 return false;
64 }
65
66 OAuth2.prototype.getAccessToken = function() {
67 if (this.needsNewAccessToken()) {
68 throw "Access Token expired.";
69 }
70 return this.access_token;
71 }
72
73 OAuth2.prototype.refreshAccessToken = function(on_done) {
74 if (!this.isAuthenticated()) {
75 throw "Not Authenticated.";
76 }
77 var xhr = new XMLHttpRequest();
78 var that = this;
79 xhr.onreadystatechange = function() {
80 if (xhr.readyState != 4) {
81 return;
82 }
83 if (xhr.status == 200) {
84 tokens = JSON.parse(xhr.responseText);
85 that.setAccessToken(tokens['access_token'],
86 tokens['expires_in'] * 1000 + Date.now());
87 } else {
88 console.log("Refresh access token failed. Status: " + xhr.status +
89 " response: " + xhr.responseText);
90 }
91 on_done();
92 };
93 xhr.open('POST', 'https://accounts.google.com/o/oauth2/token', true);
94 xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
95 var post_data = 'client_id=' + this.client_id
96 + '&client_secret=' + this.client_secret
97 + '&refresh_token=' + encodeURIComponent(this.getRefreshToken())
98 + '&grant_type=refresh_token';
99 xhr.send(post_data);
100 }
101
102 OAuth2.prototype.openOAuth2Window = function() {
103 var GET_CODE_URL = 'https://accounts.google.com/o/oauth2/auth?'
104 + 'client_id=' + this.client_id
105 + '&redirect_uri=' + this.redirect_uri
106 + '&scope=' + this.scope
107 + '&response_type=code';
108 window.open(GET_CODE_URL);
109 }
110
111 OAuth2.prototype.exchangeCodeForToken = function(code, on_done) {
112 var xhr = new XMLHttpRequest();
113 var that = this;
114 xhr.onreadystatechange = function() {
115 if (xhr.readyState != 4) {
116 return;
117 }
118 if (xhr.status == 200) {
119 tokens = JSON.parse(xhr.responseText);
120 that.setRefreshToken(tokens['refresh_token']);
121 that.setAccessToken(tokens['access_token'],
122 tokens['expires_in'] + Date.now());
123 } else {
124 console.log("Code exchnage failed. Status: " + xhr.status +
125 " response: " + xhr.responseText);
126 }
127 on_done();
128 };
129 xhr.open('POST', 'https://accounts.google.com/o/oauth2/token', true);
130 xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
131 var post_data = 'client_id=' + this.client_id
132 + '&client_secret=' + this.client_secret
133 + '&redirect_uri=' + this.redirect_uri
134 + '&code=' + encodeURIComponent(code)
135 + '&grant_type=authorization_code';
136 xhr.send(post_data);
137 }
138
OLDNEW
« no previous file with comments | « remoting/webapp/me2mom/manifest.json ('k') | remoting/webapp/me2mom/remoting.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698