Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / remoting / webapp / base / js / credentials_provider.js
blob7818f816b69eb00fe65dde8a026b463a30331426
1 // Copyright 2015 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.
5 'use strict';
7 var remoting = remoting || {};
9 /** @typedef {{clientId: string, sharedSecret: string}} */
10 remoting.PairingInfo;
12 /** @typedef {{token: string, secret: string}} */
13 remoting.ThirdPartyToken;
15 /**
16  * Parameters for the remoting.CredentialsProvider constructor.
17  *
18  * fetchPin: Called by Me2Me connections when a PIN needs to be obtained
19  *     interactively.
20  *
21  * pairingInfo: The pairing info for Me2Me Connections.
22  *
23  * accessCode: It2Me access code. If present, the |fetchPin| callback will be
24  *     ignored.
25  *
26  * fetchThirdPartyToken: Called when a third party authentication token
27  *     is needed
28  *
29  * @typedef {{
30  *   accessCode: (string|undefined),
31  *   fetchPin: (function(boolean,function(string): void)|undefined),
32  *   pairingInfo: (remoting.PairingInfo|undefined),
33  *   fetchThirdPartyToken:
34  *      (function(string ,string , string,
35  *                function(string, string):void) | undefined)
36  * }}
37  */
38 remoting.CredentialsProviderParams;
40 /**
41  * @param {remoting.CredentialsProviderParams} args
42  * @constructor
43  */
44 remoting.CredentialsProvider = function(args) {
45   /** @private */
46   this.fetchPin_ = (args.accessCode) ? this.getAccessCode_ : args.fetchPin;
47   /** @private */
48   this.pairingInfo_ = args.pairingInfo;
49   /** @private */
50   this.accessCode_ = args.accessCode;
51   /** @private */
52   this.fetchThirdPartyToken_ = args.fetchThirdPartyToken;
55 /** @returns {void}  */
56 remoting.CredentialsProvider.prototype.getAccessCode_ = function(
57   /** boolean */ supportsPairing, /** Function */ callback) {
58   callback(this.accessCode_);
61 /** @returns {remoting.PairingInfo}  */
62 remoting.CredentialsProvider.prototype.getPairingInfo = function() {
63   return this.pairingInfo_ || { clientId: '', sharedSecret: ''};
66 /**
67  * @param {boolean} pairingSupported Whether pairing is supported by the host.
68  * @returns {Promise<string>}
69  */
70 remoting.CredentialsProvider.prototype.getPIN = function(pairingSupported) {
71   var that = this;
72   if (!this.fetchPin_) {
73     Promise.resolve('');
74   }
75   return new Promise(function(/** function(string) */ resolve) {
76     that.fetchPin_(pairingSupported, resolve);
77   });
80 /**
81  * @param {string} tokenUrl Token-issue URL received from the host.
82  * @param {string} hostPublicKey Host public key (DER and Base64 encoded).
83  * @param {string} scope OAuth scope to request the token for.
84  *
85  * @returns {Promise<remoting.ThirdPartyToken>}
86  */
87 remoting.CredentialsProvider.prototype.getThirdPartyToken = function(
88     tokenUrl, hostPublicKey, scope) {
89   var that = this;
90   if (!this.fetchThirdPartyToken_) {
91     Promise.resolve({token: '', secret: ''});
92   }
93   return new Promise(function(/** Function */ resolve) {
94     var onTokenFetched = function(/** string */ token, /** string */ secret) {
95       resolve({token: token, secret: secret});
96     };
97     that.fetchThirdPartyToken_(tokenUrl, hostPublicKey, scope, onTokenFetched);
98   });