Chromium Code Reviews| Index: remoting/webapp/crd/js/credentials_provider.js |
| diff --git a/remoting/webapp/crd/js/credentials_provider.js b/remoting/webapp/crd/js/credentials_provider.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b1d9e20c0f2a71e1000dfd1e8d3e1c7dfe26a233 |
| --- /dev/null |
| +++ b/remoting/webapp/crd/js/credentials_provider.js |
| @@ -0,0 +1,100 @@ |
| +// Copyright 2015 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. |
| + |
| +'use strict'; |
| + |
| +var remoting = remoting || {}; |
| + |
| +/** @typedef {{id: string, secret: string}} */ |
| +remoting.PairingInfo; |
| + |
| +/** @typedef {{token: string, secret: string}} */ |
| +remoting.ThirdPartyToken; |
| + |
| +/** |
| + * Parameters for the remoting.CredentialsProvider constructor. |
| + * |
| + * fetchPin: Called by Me2Me connections when a PIN needs to be obtained |
| + * interactively. |
| + * |
| + * pairingInfo: The pairing info for Me2Me Connections. |
| + * |
| + * accessCode: It2Me access code. If present, the |fetchPin| callback will be |
| + * ignored. |
| + * |
| + * fetchThirdPartyToken: Called when a third party authentication token |
| + * is needed |
| + * |
| + * @typedef {{ |
| + * accessCode: (string|undefined), |
| + * fetchPin: (function(boolean,function(string): void)|undefined), |
| + * pairingInfo: (remoting.PairingInfo|undefined), |
| + * fetchThirdPartyToken: |
| + * (function(string ,string , string, |
| + * function(string, string):void) | undefined) |
| + * }} |
| + */ |
| +remoting.CredentialsProviderParams; |
| + |
| +/** |
| + * @param {remoting.CredentialsProviderParams} args |
| + * @constructor |
| + */ |
| +remoting.CredentialsProvider = function(args) { |
| + /** @private */ |
| + this.fetchPin_ = (args.accessCode) ? this.getAccessCode_ : args.fetchPin; |
|
Jamie
2015/02/28 00:41:12
Assert that exactly one of accessCode or fetchPin
|
| + /** @private */ |
| + this.pairingInfo_ = args.pairingInfo; |
| + /** @private */ |
| + this.accessCode_ = args.accessCode; |
| + /** @private */ |
| + this.fetchThirdPartyToken_ = args.fetchThirdPartyToken; |
| +}; |
| + |
| +/** @returns {void} */ |
| +remoting.CredentialsProvider.prototype.getAccessCode_ = function( |
| + /** boolean */ supportsPairing, /** Function */ callback) { |
| + callback(this.accessCode_); |
| +}; |
| + |
| +/** @returns {remoting.PairingInfo} */ |
| +remoting.CredentialsProvider.prototype.getPairingInfo = function() { |
| + return this.pairingInfo_ || { id: '', secret: ''}; |
| +}; |
| + |
| +/** |
| + * @param {boolean} pairingSupported Whether pairing is supported by the host. |
| + * @returns {Promise<string>} |
| + */ |
| +remoting.CredentialsProvider.prototype.getPIN = function(pairingSupported) { |
| + var that = this; |
| + if (!this.fetchPin_) { |
| + Promise.resolve(''); |
| + } |
| + return new Promise(function(/** function(string) */ resolve) { |
| + that.fetchPin_(pairingSupported, resolve); |
| + }); |
| +}; |
| + |
| +/** |
| + * @param {string} tokenUrl Token-issue URL received from the host. |
| + * @param {string} hostPublicKey Host public key (DER and Base64 encoded). |
| + * @param {string} scope OAuth scope to request the token for. |
| + * |
| + * @returns {Promise<remoting.ThirdPartyToken>} |
| + */ |
| +remoting.CredentialsProvider.prototype.getThirdPartyToken = function( |
| + tokenUrl, hostPublicKey, scope) { |
| + var that = this; |
| + if (!this.fetchThirdPartyToken_) { |
| + Promise.resolve({token: '', secret: ''}); |
| + } |
| + return new Promise(function(/** Function */ resolve) { |
| + var onTokenFetched = function(/** string */ token, /** string */ secret) { |
| + resolve({token: token, secret: secret}); |
| + }; |
| + that.fetchThirdPartyToken_(tokenUrl, hostPublicKey, scope, onTokenFetched); |
| + }); |
| +}; |
| + |