Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / common / extensions / docs / templates / articles / app_identity.html
blob7fd312db5e0b041be89573d083fba800c12f8264
1 <h1>User Authentication</h1>
3 <p>
4 Web authentication protocols utilize HTTP features,
5 but Chrome Apps run inside the app container;
6 they don’t load over HTTP and can’t perform redirects or set cookies.
7 </p>
9 <p>
10 Use the <a href="identity">Chrome Identity API</a>
11 to authenticate users:
12 the <code>getAuthToken</code> for users logged into their Google Account and
13 the <code>launchWebAuthFlow</code> for users logged into a non-Google account.
14 If your app uses its own server to authenticate users, you will need to use the latter.
15 </p>
17 <p class="note">
18 <b>API Samples: </b>
19 Want to play with the code?
20 Check out
21 <a href="https://github.com/GoogleChrome/chrome-app-samples#_feature_identity">these samples</a>,
22 in particular the
23 <a href="https://github.com/GoogleChrome/chrome-app-samples/tree/master/samples/identity#readme">identity sample</a>.
24 </p>
26 <h2 id="how">How it works</h2>
28 <p>
29 Chrome Apps users have a Google account associated with their
30 profile. Apps can get OAuth2 tokens for these users using
31 the <code>getAuthToken</code> API.
32 </p>
34 <p>
35 Apps that want to perform authentication with non-Google identity
36 providers must call <code>launchWebAuthFlow</code>. This method uses a
37 browser pop-up to show the provider pages and captures redirects to
38 the specific URL patterns. The redirect URLs are passed to the app
39 and the app extracts the token from the URL.
40 </p>
42 <h2 id="google">Google account authentication</h2>
44 <p>
45 Here are the five steps you need to complete:
46 </p>
48 <ol>
49 <li>Add permissions to your manifest and upload your app.</li>
50 <li>Copy key in the installed <code>manifest.json</code> to
51 your source manifest, so that your application ID will stay
52 constant during development.</li>
53 <li>Get an OAuth2 client ID for your Chrome App.</li>
54 <li>Update your manifest to include the client ID and scopes.</li>
55 <li>Get the authentication token.</li>
56 </ol>
58 <h3 id="add_permissions">Add permissions and upload app</h3>
60 <p>
61 You need to make sure the identity permission is in your manifest.
62 You can then upload your app to the apps and extensions management
63 page (see <a href="publish_app">Publish</a>).
64 </p>
66 <pre data-filename="manifest.json">
67 "permissions": [
68 "identity"
70 </pre>
72 <h3 id="copy_key">Copy key to your manifest</h3>
74 <p>
75 When you register your application in the Google OAuth console, you'll
76 provide your application's ID, which will be checked during token
77 requests. Therefore it's important to have a consistent application ID
78 during development.
79 </p>
81 <p>
82 To keep your application ID constant, you need to copy the key in the
83 installed
84 <code>manifest.json</code> to your source manifest.
85 It's not the most graceful task, but here's how it goes:
86 </p>
88 <ol>
89 <li>Go to your <a href="http://www.chromium.org/user-experience/user-data-directory">user data
90 directory</a>. Example on MacOs:
91 <code>~/Library/Application\ Support/Google/Chrome/Default/Extensions</code></li>
92 <li>List the installed apps and extensions and match your app ID on the apps and extensions
93 management page to the same ID here.</li>
94 <li>Go to the installed app directory (this will be a version within the app ID).
95 Open the installed <code>manifest.json</code> (pico is a quick way to open the file).</li>
96 <li>Copy the "key" in the installed <code>manifest.json</code> and paste it into your app's
97 source manifest file.</li>
98 </ol>
100 <h3 id="client_id">Get your OAuth2 client ID</h3>
103 You need to register your app
104 in the Google APIs Console
105 to get the client ID:
106 </p>
108 <ol>
109 <li>Login to the <a href="https://code.google.com/apis/console/">Google APIs Console</a>
110 using the same Google account used to upload your app to the Chrome Web Store. </li>
111 <li>Create a new project by expanding the drop-down menu in the top-left
112 corner and selecting the <strong>Create...</strong> menu item. </li>
113 <li> Once created and named, go to the "Services" navigation menu item and
114 turn on any Google services your app needs. </li>
115 <li> Go to the "API Access" navigation menu item and click on the
116 <strong>Create an OAuth 2.0 client ID...</strong> blue button. </li>
117 <li> Enter the requested branding information,
118 select the <strong>Installed application</strong> type. </li>
119 <li> Select <strong>Chrome Application</strong> and enter your application ID
120 (same ID displayed in the apps and extensions management page). </li>
121 </ol>
123 <p class="warning">
124 <strong>Warning: </strong>
125 If the app ID here does not match your app ID,
126 an error will occur when your app calls <a href="#token">getAuthToken()</a>.
127 </p>
129 <h3 id="update_manifest">Update your manifest with OAuth2 client ID and scopes</h3>
132 You need to update your manifest to include
133 the client ID and scopes.
134 Here's the sample "oauth2" for the
135 <a href="https://github.com/GoogleChrome/chrome-app-samples/tree/master/samples/gdrive">gdrive sample</a>:
136 </p>
138 <pre data-filename="manifest.json">
139 "oauth2": {
140 "client_id": "665859454684.apps.googleusercontent.com",
141 "scopes": [
142 "https://www.googleapis.com/auth/drive"
145 </pre>
147 <h3 id="token">Get access tokens</h3>
150 You are now ready to get the auth token by calling
151 $(ref:identity.getAuthToken).
152 </p>
154 <pre>
155 chrome.identity.getAuthToken({ 'interactive': true }, function(token) {
156 // Use the token.
158 </pre>
160 <h4 id="getAuthToken-prompts">User interaction</h4>
163 When calling <code>getAuthToken</code>, you can pass a flag
164 (<code>'interactive': true</code> in the example above) indicating
165 whether you want the API to be called in interactive mode or silent
166 mode. If you invoke the API in interactive mode, the user is shown
167 a sign in and/or approval UI when necessary, as shown in the
168 screenshot below:
169 </p>
170 <img src="{{static}}/images/identity-api-ui.png"
171 width="853"
172 height="514"
173 alt="screenshot showing UI when an app uses the Identity API
174 to authenticate a Google account">
177 If you invoke the API in silent mode, the API will only return a token
178 if it's possible to produce one without showing any UI. This is
179 useful in cases when an app is doing the flow at app startup, for
180 example, or in general in cases where there is no user gesture
181 involved.
182 </p>
185 The best practice we suggest is to use silent mode when there is no
186 user gesture involved and use interactive mode if there is a user
187 gesture (for example, the user clicked the Sign In button in your
188 app). Note that we do not enforce any gesture requirement.
189 </p>
191 <h4 id="getAuthToken-caching">Caching</h4>
194 Chrome has an in-memory cache of access tokens, so you can call
195 <code>getAuthToken</code> any time you need to use a token. Token
196 expiration is handled automatically by the cache.
197 </p>
200 You can see the current state of the token cache
201 on <code>chrome://identity-internals</code>.
202 </p>
205 There are some cases, such as when the user changes their password,
206 when non-expired access tokens will stop working. API calls using the
207 token will start returning with an HTTP status code 401. If you detect
208 that this has happened, you can remove the invalid token from Chrome's
209 cache by calling $(ref:identity.removeCachedAuthToken).
210 </p>
213 Example of <code>removeCachedAuthToken</code> usage:
214 </p>
216 <pre>
217 // callback = function (error, httpStatus, responseText);
218 function authenticatedXhr(method, url, callback) {
219 var retry = true;
220 function getTokenAndXhr() {
221 chrome.identity.getAuthToken({/* details */},
222 function (access_token) {
223 if (chrome.runtime.lastError) {
224 callback(chrome.runtime.lastError);
225 return;
228 var xhr = new XMLHttpRequest();
229 xhr.open(method, url);
230 xhr.setRequestHeader('Authorization',
231 'Bearer ' + access_token);
233 xhr.onload = function () {
234 if (this.status === 401 && retry) {
235 // This status may indicate that the cached
236 // access token was invalid. Retry once with
237 // a fresh token.
238 retry = false;
239 chrome.identity.removeCachedAuthToken(
240 { 'token': access_token },
241 getTokenAndXhr);
242 return;
245 callback(null, this.status, this.responseText);
250 </pre>
252 <h2 id="non">Non-Google account authentication</h2>
255 Here are the three steps you need to complete:
256 </p>
258 <ol>
259 <li>Register with the provider.</li>
260 <li>Add permissions for provider resources that your app will access.</li>
261 <li>Get the authentication token.</li>
262 </ol>
264 <h3 id="register_provider">Register with the provider</h3>
267 You need to register an OAuth2 client ID with the provider
268 and configure the client ID as a website.
269 For the redirect URI to be entered during registration,
270 use the URL of the form:
271 <code>https://&lt;extension-id&gt;.chromiumapp.org/&lt;anything-here&gt;</code>
272 </p>
275 For example, if you app ID
276 is <code>abcdefghijklmnopqrstuvwxyzabcdef</code> and you want
277 <code>provider_cb</code> to be the path, to distinguish it with
278 redirect URIs from other providers, you should use:
279 <code>https://abcdefghijklmnopqrstuvwxyzabcdef.chromiumapp.org/provider_cb</code>
280 </p>
282 <h3 id="permissions_provider">Add permissions for provider</h3>
285 To make cross-origin XHRs to the provider API endpoints,
286 you need to whitelist the appropriate patterns in the permissions:
287 </p>
289 <pre data-filename="manifest.json">
290 "permissions": [
292 "https://www.website-of-provider-with-user-photos.com/photos/*"
294 </pre>
296 <h3 id="token2">Get the token</h3>
299 To get the token:
300 </p>
302 <pre>
303 chrome.identity.launchWebAuthFlow(
304 {'url': '&lt;url-to-do-auth&gt;', 'interactive': true},
305 function(redirect_url) { /* Extract token from redirect_url */ });
306 </pre>
309 The &lt;url-to-do-auth&gt; is whatever the URL is to do auth to the provider from a website.
310 For example, let us say that you are performing OAuth2 flow with a provider
311 and have registered your app with client id 123456789012345 and
312 you want access to user’s photos on the provider’s website:
313 <code>https://www.website-of-provider-with-user-photos.com/dialog/oauth?client_id=123456789012345&amp;<br>redirect_uri=https://abcdefghijklmnopqrstuvwxyzabcdef.chromiumapp.org/provider_cb&amp;response_type=token&amp;scope=user_photos</code>
314 </p>
317 The provider will perform authentication and if appropriate,
318 will show login and/or approval UI to the user.
319 It will then redirect to
320 <code>https://abcdefghijklmnopqrstuvwxyzabcdef.chromiumapp.org/provider_cb#authToken=&lt;auth-token></code>
321 </p>
324 Chrome will capture that and invoke the callback
325 of the app with the full redirect URL.
326 The app should extract the token out of the URL.
327 </p>
329 <h3 id="launchWebAuthFlow-interactive">Interactive versus silent mode</h3>
332 When calling <code>launchWebAuthFlow</code>,
333 you can pass a flag (<code>'interactive': true</code> in the example above)
334 indicating whether you want the API to be called
335 in interactive mode or not (aka silent mode).
336 If you invoke the API in interactive mode,
337 the user is shown UI, if necessary,
338 to get the token (signin UI and/or approval UI;
339 or for that matter any provider specific UI).
340 </p>
343 If you invoke the API in silent mode,
344 the API will only return a token if the provider is able
345 to provide a token without showing any UI.
346 This is useful in cases when an app is doing the flow at app startup, for example,
347 or in general in cases where there is no user gesture involved.
348 </p>
351 The best practice we suggest is to use silent mode
352 when there is no user gesture involved and use interactive mode
353 if there is a user gesture (for example, the user clicked the Sign In button in your app).
354 Note that we do not enforce gesture requirement.
355 </p>
357 <p class="backtotop"><a href="#top">Back to top</a></p>