1 |
|
2 |
|
3 |
|
4 |
|
5 | import { __spreadArrays } from "tslib";
|
6 | import { ClientConfigurationError } from "./error/ClientConfigurationError";
|
7 | import { Constants } from "./utils/Constants";
|
8 | var ScopeSet = (function () {
|
9 | function ScopeSet() {
|
10 | }
|
11 | |
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
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 |
|
30 |
|
31 |
|
32 |
|
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 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | ScopeSet.trimAndConvertToLowerCase = function (scope) {
|
46 | return scope.trim().toLowerCase();
|
47 | };
|
48 | |
49 |
|
50 |
|
51 |
|
52 | ScopeSet.trimAndConvertArrayToLowerCase = function (scopes) {
|
53 | var _this = this;
|
54 | return scopes.map(function (scope) { return _this.trimAndConvertToLowerCase(scope); });
|
55 | };
|
56 | |
57 |
|
58 |
|
59 |
|
60 | ScopeSet.trimScopes = function (scopes) {
|
61 | return scopes.map(function (scope) { return scope.trim(); });
|
62 | };
|
63 | |
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | ScopeSet.removeElement = function (scopes, scope) {
|
71 | var scopeVal = this.trimAndConvertToLowerCase(scope);
|
72 | return scopes.filter(function (value) { return value !== scopeVal; });
|
73 | };
|
74 | |
75 |
|
76 |
|
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 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
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 |
|
105 | if (!Array.isArray(scopes)) {
|
106 | throw ClientConfigurationError.createScopesNonArrayError(scopes);
|
107 | }
|
108 |
|
109 | if (scopes.length < 1 && scopesRequired) {
|
110 | throw ClientConfigurationError.createEmptyScopesArrayError(scopes.toString());
|
111 | }
|
112 | };
|
113 | |
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
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 |
|
132 |
|
133 |
|
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 |
|
144 | |
145 |
|
146 |
|
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 |
|
161 |
|
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 |
|
170 |
|
171 |
|
172 | ScopeSet.onlyContainsClientId = function (scopes, clientId) {
|
173 |
|
174 | return !!scopes && (scopes.indexOf(clientId) > -1 && scopes.length === 1);
|
175 | };
|
176 | |
177 |
|
178 |
|
179 |
|
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 |
|
193 |
|
194 |
|
195 | ScopeSet.removeDefaultScopes = function (scopes) {
|
196 | return scopes.filter(function (scope) {
|
197 | return (scope !== Constants.openidScope && scope !== Constants.profileScope);
|
198 | });
|
199 | };
|
200 | |
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 | ScopeSet.translateClientIdIfSingleScope = function (scopes, clientId) {
|
207 | return this.onlyContainsClientId(scopes, clientId) ? Constants.oidcScopes : scopes;
|
208 | };
|
209 | return ScopeSet;
|
210 | }());
|
211 | export { ScopeSet };
|
212 |
|
\ | No newline at end of file |