1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | Object.defineProperty(exports, "__esModule", { value: true });
|
7 | exports.RequestUtils = void 0;
|
8 | var tslib_1 = require("tslib");
|
9 | var Constants_1 = require("./Constants");
|
10 | var ClientConfigurationError_1 = require("../error/ClientConfigurationError");
|
11 | var ScopeSet_1 = require("../ScopeSet");
|
12 | var StringUtils_1 = require("./StringUtils");
|
13 | var CryptoUtils_1 = require("./CryptoUtils");
|
14 | var TimeUtils_1 = require("./TimeUtils");
|
15 | var ClientAuthError_1 = require("../error/ClientAuthError");
|
16 |
|
17 |
|
18 |
|
19 | var RequestUtils = (function () {
|
20 | function RequestUtils() {
|
21 | }
|
22 | |
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | RequestUtils.validateRequest = function (request, isLoginCall, clientId, interactionType) {
|
33 |
|
34 | if (!isLoginCall && !request) {
|
35 | throw ClientConfigurationError_1.ClientConfigurationError.createEmptyRequestError();
|
36 | }
|
37 | var scopes;
|
38 | var extraQueryParameters;
|
39 | if (request) {
|
40 |
|
41 | scopes = isLoginCall ? ScopeSet_1.ScopeSet.appendScopes(request.scopes, request.extraScopesToConsent) : request.scopes;
|
42 | ScopeSet_1.ScopeSet.validateInputScope(scopes, !isLoginCall);
|
43 | scopes = ScopeSet_1.ScopeSet.translateClientIdIfSingleScope(scopes, clientId);
|
44 |
|
45 | this.validatePromptParameter(request.prompt);
|
46 |
|
47 | extraQueryParameters = this.validateEQParameters(request.extraQueryParameters, request.claimsRequest);
|
48 |
|
49 | this.validateClaimsRequest(request.claimsRequest);
|
50 | }
|
51 |
|
52 | var state = this.validateAndGenerateState(request && request.state, interactionType);
|
53 | var correlationId = this.validateAndGenerateCorrelationId(request && request.correlationId);
|
54 | var validatedRequest = tslib_1.__assign(tslib_1.__assign({}, request), { extraQueryParameters: extraQueryParameters,
|
55 | scopes: scopes,
|
56 | state: state,
|
57 | correlationId: correlationId });
|
58 | return validatedRequest;
|
59 | };
|
60 | |
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | RequestUtils.validatePromptParameter = function (prompt) {
|
67 | if (prompt) {
|
68 | if ([Constants_1.PromptState.LOGIN, Constants_1.PromptState.SELECT_ACCOUNT, Constants_1.PromptState.CONSENT, Constants_1.PromptState.NONE].indexOf(prompt) < 0) {
|
69 | throw ClientConfigurationError_1.ClientConfigurationError.createInvalidPromptError(prompt);
|
70 | }
|
71 | }
|
72 | };
|
73 | |
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 | RequestUtils.validateEQParameters = function (extraQueryParameters, claimsRequest) {
|
80 | var eQParams = tslib_1.__assign({}, extraQueryParameters);
|
81 | if (!eQParams) {
|
82 | return null;
|
83 | }
|
84 | if (claimsRequest) {
|
85 |
|
86 | delete eQParams[Constants_1.Constants.claims];
|
87 | }
|
88 | Constants_1.DisallowedEQParams.forEach(function (param) {
|
89 | if (eQParams[param]) {
|
90 |
|
91 | delete eQParams[param];
|
92 | }
|
93 | });
|
94 | return eQParams;
|
95 | };
|
96 | |
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 | RequestUtils.validateClaimsRequest = function (claimsRequest) {
|
104 | if (!claimsRequest) {
|
105 | return;
|
106 | }
|
107 | try {
|
108 | JSON.parse(claimsRequest);
|
109 | }
|
110 | catch (e) {
|
111 | throw ClientConfigurationError_1.ClientConfigurationError.createClaimsRequestParsingError(e);
|
112 | }
|
113 | };
|
114 | |
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | RequestUtils.validateAndGenerateState = function (userState, interactionType) {
|
122 | return !StringUtils_1.StringUtils.isEmpty(userState) ? "" + RequestUtils.generateLibraryState(interactionType) + Constants_1.Constants.resourceDelimiter + userState : RequestUtils.generateLibraryState(interactionType);
|
123 | };
|
124 | |
125 |
|
126 |
|
127 |
|
128 |
|
129 | RequestUtils.generateLibraryState = function (interactionType) {
|
130 | var stateObject = {
|
131 | id: CryptoUtils_1.CryptoUtils.createNewGuid(),
|
132 | ts: TimeUtils_1.TimeUtils.now(),
|
133 | method: interactionType
|
134 | };
|
135 | var stateString = JSON.stringify(stateObject);
|
136 | return CryptoUtils_1.CryptoUtils.base64Encode(stateString);
|
137 | };
|
138 | |
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 | RequestUtils.parseLibraryState = function (state) {
|
145 | var libraryState = decodeURIComponent(state).split(Constants_1.Constants.resourceDelimiter)[0];
|
146 | if (CryptoUtils_1.CryptoUtils.isGuid(libraryState)) {
|
147 |
|
148 | return {
|
149 | id: libraryState,
|
150 | ts: TimeUtils_1.TimeUtils.now(),
|
151 | method: Constants_1.Constants.interactionTypeRedirect
|
152 | };
|
153 | }
|
154 | try {
|
155 | var stateString = CryptoUtils_1.CryptoUtils.base64Decode(libraryState);
|
156 | var stateObject = JSON.parse(stateString);
|
157 | return stateObject;
|
158 | }
|
159 | catch (e) {
|
160 | throw ClientAuthError_1.ClientAuthError.createInvalidStateError(state, null);
|
161 | }
|
162 | };
|
163 | |
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 | RequestUtils.validateAndGenerateCorrelationId = function (correlationId) {
|
170 |
|
171 | if (correlationId && !CryptoUtils_1.CryptoUtils.isGuid(correlationId)) {
|
172 | throw ClientConfigurationError_1.ClientConfigurationError.createInvalidCorrelationIdError();
|
173 | }
|
174 | return CryptoUtils_1.CryptoUtils.isGuid(correlationId) ? correlationId : CryptoUtils_1.CryptoUtils.createNewGuid();
|
175 | };
|
176 | |
177 |
|
178 |
|
179 |
|
180 | RequestUtils.createRequestSignature = function (request) {
|
181 | return "" + request.scopes.join(" ").toLowerCase() + Constants_1.Constants.resourceDelimiter + request.authority;
|
182 | };
|
183 | return RequestUtils;
|
184 | }());
|
185 | exports.RequestUtils = RequestUtils;
|
186 |
|
\ | No newline at end of file |