Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / google-auth-library / build / src / auth / refreshclient.js
1 "use strict";
2 /**
3  * Copyright 2015 Google Inc. All Rights Reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
18     return new (P || (P = Promise))(function (resolve, reject) {
19         function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
20         function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
21         function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
22         step((generator = generator.apply(thisArg, _arguments || [])).next());
23     });
24 };
25 Object.defineProperty(exports, "__esModule", { value: true });
26 const oauth2client_1 = require("./oauth2client");
27 class UserRefreshClient extends oauth2client_1.OAuth2Client {
28     constructor(optionsOrClientId, clientSecret, refreshToken, eagerRefreshThresholdMillis) {
29         const opts = (optionsOrClientId && typeof optionsOrClientId === 'object') ?
30             optionsOrClientId :
31             {
32                 clientId: optionsOrClientId,
33                 clientSecret,
34                 refreshToken,
35                 eagerRefreshThresholdMillis
36             };
37         super({
38             clientId: opts.clientId,
39             clientSecret: opts.clientSecret,
40             eagerRefreshThresholdMillis: opts.eagerRefreshThresholdMillis
41         });
42         this._refreshToken = opts.refreshToken;
43     }
44     /**
45      * Refreshes the access token.
46      * @param refreshToken An ignored refreshToken..
47      * @param callback Optional callback.
48      */
49     refreshTokenNoCache(refreshToken) {
50         const _super = Object.create(null, {
51             refreshTokenNoCache: { get: () => super.refreshTokenNoCache }
52         });
53         return __awaiter(this, void 0, void 0, function* () {
54             return _super.refreshTokenNoCache.call(this, this._refreshToken);
55         });
56     }
57     /**
58      * Create a UserRefreshClient credentials instance using the given input
59      * options.
60      * @param json The input object.
61      */
62     fromJSON(json) {
63         if (!json) {
64             throw new Error('Must pass in a JSON object containing the user refresh token');
65         }
66         if (json.type !== 'authorized_user') {
67             throw new Error('The incoming JSON object does not have the "authorized_user" type');
68         }
69         if (!json.client_id) {
70             throw new Error('The incoming JSON object does not contain a client_id field');
71         }
72         if (!json.client_secret) {
73             throw new Error('The incoming JSON object does not contain a client_secret field');
74         }
75         if (!json.refresh_token) {
76             throw new Error('The incoming JSON object does not contain a refresh_token field');
77         }
78         this._clientId = json.client_id;
79         this._clientSecret = json.client_secret;
80         this._refreshToken = json.refresh_token;
81         this.credentials.refresh_token = json.refresh_token;
82     }
83     fromStream(inputStream, callback) {
84         if (callback) {
85             this.fromStreamAsync(inputStream).then(r => callback(), callback);
86         }
87         else {
88             return this.fromStreamAsync(inputStream);
89         }
90     }
91     fromStreamAsync(inputStream) {
92         return __awaiter(this, void 0, void 0, function* () {
93             return new Promise((resolve, reject) => {
94                 if (!inputStream) {
95                     return reject(new Error('Must pass in a stream containing the user refresh token.'));
96                 }
97                 let s = '';
98                 inputStream.setEncoding('utf8')
99                     .on('error', reject)
100                     .on('data', (chunk) => s += chunk)
101                     .on('end', () => {
102                     try {
103                         const data = JSON.parse(s);
104                         this.fromJSON(data);
105                         return resolve();
106                     }
107                     catch (err) {
108                         return reject(err);
109                     }
110                 });
111             });
112         });
113     }
114 }
115 exports.UserRefreshClient = UserRefreshClient;
116 //# sourceMappingURL=refreshclient.js.map