/
auth.js
176 lines (146 loc) · 4.69 KB
/
auth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*!
* Copyright (c) 2015-2019 Cisco Systems, Inc. See LICENSE file.
*/
import {Interceptor} from '@webex/http-core';
/**
* @class
*/
export default class AuthInterceptor extends Interceptor {
/**
* @returns {AuthInterceptor}
*/
static create() {
return new AuthInterceptor({webex: this});
}
/**
* @see {@link Interceptor#onRequest}
* @param {Object} options
* @returns {Object}
*/
onRequest(options) {
options.headers = options.headers || {};
// If Authorizations is already set, don't overwrite it
if ('authorization' in options.headers || 'auth' in options) {
// If Authorization is set to null, false, or undefined, delete it to
// prevent a CORS preflight.
if (!options.headers.authorization) {
Reflect.deleteProperty(options.headers, 'authorization');
}
return Promise.resolve(options);
}
return this.requiresCredentials(options)
.then((requires) => {
if (!requires) {
return options;
}
return this.webex.credentials.getUserToken()
.then((token) => {
options.headers.authorization = token.toString();
return options;
});
});
}
/**
* Determines if the provided options object needs an authorization header.
*
* @param {Object} options
* @returns {Promise<boolean>}
*/
requiresCredentials(options) {
// Validate that authorization is necessary.
if (options.addAuthHeader === false) {
return Promise.resolve(false);
}
// Validate that the services plugin has been loaded before proceeding.
if (!this.webex.internal.services) {
return Promise.resolve(false);
}
// Destructure webex instance to isolate services plugin.
const {services} = this.webex.internal;
// Store the current service details if available and destructure details.
const details = services.getServiceFromUrl(options.uri || '');
const {resource, service, uri} = options;
const {name} = details || {};
// Unique validation for the u2c service.
if ((service && service === 'u2c') || (name && name === 'u2c')) {
if (
(resource && resource.includes('limited')) ||
(uri && uri.includes('limited'))
) {
return Promise.resolve(false);
}
return Promise.resolve(true);
}
// Validate that the service exists in the service catalog.
if (details || (service && services.hasService(service))) {
return Promise.resolve(true);
}
// Validate that the allowed domains can be utilized.
if (!services.validateDomains) {
Promise.resolve(false);
}
// Validate that the domain of the uri is allowed.
if (services.hasAllowedDomains() &&
(uri && services.isAllowedDomainUrl(uri))) {
return Promise.resolve(true);
}
return Promise.resolve(false);
}
/**
* @see {@link Interceptor#onResponseError}
* @param {Object} options
* @param {Error} reason
* @returns {Object}
*/
onResponseError(options, reason) {
return this.shouldAttemptReauth(reason, options)
.then((shouldAttemptReauth) => {
if (shouldAttemptReauth) {
this.webex.logger.info('auth: received 401, attempting to reauthenticate');
if (reason.options.headers) {
Reflect.deleteProperty(reason.options.headers, 'authorization');
}
if (this.webex.credentials.canRefresh) {
return this.webex.credentials.refresh()
.then(() => this.replay(options));
}
}
return Promise.reject(reason);
});
}
/**
* Replays the request
* @param {Object} options
* @returns {Object}
*/
replay(options) {
if (options.replayCount) {
options.replayCount += 1;
}
else {
options.replayCount = 1;
}
if (options.replayCount > this.webex.config.maxAuthenticationReplays) {
this.webex.logger.error(`auth: failed after ${this.webex.config.maxAuthenticationReplays} replay attempts`);
return Promise.reject(new Error(`Failed after ${this.webex.config.maxAuthenticationReplays} replay attempts`));
}
this.webex.logger.info(`auth: replaying request ${options.replayCount} time`);
return this.webex.request(options);
}
/**
* Indicates whether or not the current request should refresh its access
* token in event of a 401
* @param {Error} reason
* @param {Object} options
* @returns {Promise<boolean>}
*/
shouldAttemptReauth(reason, options) {
if (options && options.shouldRefreshAccessToken === false) {
return Promise.resolve(false);
}
if (reason.statusCode === 401) {
return Promise.resolve(true);
}
return Promise.resolve(false);
}
}