UNPKG

8.37 kBJavaScriptView Raw
1/*
2 * Copyright (c) Microsoft Corporation. All rights reserved.
3 * Licensed under the MIT License.
4 */
5import { __spreadArrays } from "tslib";
6import { ClientConfigurationError } from "./error/ClientConfigurationError";
7import { Constants } from "./utils/Constants";
8var ScopeSet = /** @class */ (function () {
9 function ScopeSet() {
10 }
11 /**
12 * Check if there are dup scopes in a given request
13 *
14 * @param cachedScopes
15 * @param scopes
16 */
17 // TODO: Rename this, intersecting scopes isn't a great name for duplicate checker
18 ScopeSet.isIntersectingScopes = function (cachedScopes, scopes) {
19 var convertedCachedScopes = this.trimAndConvertArrayToLowerCase(__spreadArrays(cachedScopes));
20 var requestScopes = this.trimAndConvertArrayToLowerCase(__spreadArrays(scopes));
21 for (var i = 0; i < requestScopes.length; i++) {
22 if (convertedCachedScopes.indexOf(requestScopes[i].toLowerCase()) > -1) {
23 return true;
24 }
25 }
26 return false;
27 };
28 /**
29 * Check if a given scope is present in the request
30 *
31 * @param cachedScopes
32 * @param scopes
33 */
34 ScopeSet.containsScope = function (cachedScopes, scopes) {
35 var convertedCachedScopes = this.trimAndConvertArrayToLowerCase(__spreadArrays(cachedScopes));
36 var requestScopes = this.trimAndConvertArrayToLowerCase(__spreadArrays(scopes));
37 return requestScopes.every(function (value) { return convertedCachedScopes.indexOf(value.toString().toLowerCase()) >= 0; });
38 };
39 /**
40 * Trims and converts string to lower case
41 *
42 * @param scopes
43 */
44 // TODO: Rename this, too generic name for a function that only deals with scopes
45 ScopeSet.trimAndConvertToLowerCase = function (scope) {
46 return scope.trim().toLowerCase();
47 };
48 /**
49 * Performs trimAndConvertToLowerCase on string array
50 * @param scopes
51 */
52 ScopeSet.trimAndConvertArrayToLowerCase = function (scopes) {
53 var _this = this;
54 return scopes.map(function (scope) { return _this.trimAndConvertToLowerCase(scope); });
55 };
56 /**
57 * Trims each scope in scopes array
58 * @param scopes
59 */
60 ScopeSet.trimScopes = function (scopes) {
61 return scopes.map(function (scope) { return scope.trim(); });
62 };
63 /**
64 * Remove one element from a scope array
65 *
66 * @param scopes
67 * @param scope
68 */
69 // TODO: Rename this, too generic name for a function that only deals with scopes
70 ScopeSet.removeElement = function (scopes, scope) {
71 var scopeVal = this.trimAndConvertToLowerCase(scope);
72 return scopes.filter(function (value) { return value !== scopeVal; });
73 };
74 /**
75 * Parse the scopes into a formatted scopeList
76 * @param scopes
77 */
78 ScopeSet.parseScope = function (scopes) {
79 var scopeList = "";
80 if (scopes) {
81 for (var i = 0; i < scopes.length; ++i) {
82 scopeList += (i !== scopes.length - 1) ? scopes[i] + " " : scopes[i];
83 }
84 }
85 return scopeList;
86 };
87 /**
88 * @hidden
89 *
90 * Used to validate the scopes input parameter requested by the developer.
91 * @param {Array<string>} scopes - Developer requested permissions. Not all scopes are guaranteed to be included in the access token returned.
92 * @param {boolean} scopesRequired - Boolean indicating whether the scopes array is required or not
93 * @ignore
94 */
95 ScopeSet.validateInputScope = function (scopes, scopesRequired) {
96 if (!scopes) {
97 if (scopesRequired) {
98 throw ClientConfigurationError.createScopesRequiredError(scopes);
99 }
100 else {
101 return;
102 }
103 }
104 // Check that scopes is an array object (also throws error if scopes == null)
105 if (!Array.isArray(scopes)) {
106 throw ClientConfigurationError.createScopesNonArrayError(scopes);
107 }
108 // Check that scopes is not an empty array
109 if (scopes.length < 1 && scopesRequired) {
110 throw ClientConfigurationError.createEmptyScopesArrayError(scopes.toString());
111 }
112 };
113 /**
114 * @hidden
115 *
116 * Extracts scope value from the state sent with the authentication request.
117 * @param {string} state
118 * @returns {string} scope.
119 * @ignore
120 */
121 ScopeSet.getScopeFromState = function (state) {
122 if (state) {
123 var splitIndex = state.indexOf(Constants.resourceDelimiter);
124 if (splitIndex > -1 && splitIndex + 1 < state.length) {
125 return state.substring(splitIndex + 1);
126 }
127 }
128 return "";
129 };
130 /**
131 * @ignore
132 * Appends extraScopesToConsent if passed
133 * @param {@link AuthenticationParameters}
134 */
135 ScopeSet.appendScopes = function (reqScopes, reqExtraScopesToConsent) {
136 if (reqScopes) {
137 var convertedExtraScopes = reqExtraScopesToConsent ? this.trimAndConvertArrayToLowerCase(__spreadArrays(reqExtraScopesToConsent)) : null;
138 var convertedReqScopes = this.trimAndConvertArrayToLowerCase(__spreadArrays(reqScopes));
139 return convertedExtraScopes ? __spreadArrays(convertedReqScopes, convertedExtraScopes) : convertedReqScopes;
140 }
141 return null;
142 };
143 // #endregion
144 /**
145 * @ignore
146 * Returns true if the scopes array only contains openid and/or profile
147 */
148 ScopeSet.onlyContainsOidcScopes = function (scopes) {
149 var scopesCount = scopes.length;
150 var oidcScopesFound = 0;
151 if (scopes.indexOf(Constants.openidScope) > -1) {
152 oidcScopesFound += 1;
153 }
154 if (scopes.indexOf(Constants.profileScope) > -1) {
155 oidcScopesFound += 1;
156 }
157 return (scopesCount > 0 && scopesCount === oidcScopesFound);
158 };
159 /**
160 * @ignore
161 * Returns true if the scopes array only contains openid and/or profile
162 */
163 ScopeSet.containsAnyOidcScopes = function (scopes) {
164 var containsOpenIdScope = scopes.indexOf(Constants.openidScope) > -1;
165 var containsProfileScope = scopes.indexOf(Constants.profileScope) > -1;
166 return (containsOpenIdScope || containsProfileScope);
167 };
168 /**
169 * @ignore
170 * Returns true if the clientId is the only scope in the array
171 */
172 ScopeSet.onlyContainsClientId = function (scopes, clientId) {
173 // Double negation to force false value returned in case scopes is null
174 return !!scopes && (scopes.indexOf(clientId) > -1 && scopes.length === 1);
175 };
176 /**
177 * @ignore
178 * Adds missing OIDC scopes to scopes array without duplication. Since STS requires OIDC scopes for
179 * all implicit flow requests, 'openid' and 'profile' should always be included in the final request
180 */
181 ScopeSet.appendDefaultScopes = function (scopes) {
182 var extendedScopes = scopes;
183 if (extendedScopes.indexOf(Constants.openidScope) === -1) {
184 extendedScopes.push(Constants.openidScope);
185 }
186 if (extendedScopes.indexOf(Constants.profileScope) === -1) {
187 extendedScopes.push(Constants.profileScope);
188 }
189 return extendedScopes;
190 };
191 /**
192 * @ignore
193 * Removes present OIDC scopes from scopes array.
194 */
195 ScopeSet.removeDefaultScopes = function (scopes) {
196 return scopes.filter(function (scope) {
197 return (scope !== Constants.openidScope && scope !== Constants.profileScope);
198 });
199 };
200 /**
201 * @ignore
202 * Removes clientId from scopes array if included as only scope. If it's not the only scope, it is treated as a resource scope.
203 * @param scopes Array<string>: Pre-normalized scopes array
204 * @param clientId string: The application's clientId that is searched for in the scopes array
205 */
206 ScopeSet.translateClientIdIfSingleScope = function (scopes, clientId) {
207 return this.onlyContainsClientId(scopes, clientId) ? Constants.oidcScopes : scopes;
208 };
209 return ScopeSet;
210}());
211export { ScopeSet };
212//# sourceMappingURL=ScopeSet.js.map
\No newline at end of file