MCPcopy
hub / github.com/hapijs/hapi / _setupRoute

Method _setupRoute

lib/auth.js:153–210  ·  view source on GitHub ↗
(options, path)

Source from the content-addressed store, hash-verified

151 }
152
153 _setupRoute(options, path) {
154
155 if (!options) {
156 return options; // Preserve the difference between undefined and false
157 }
158
159 if (typeof options === 'string') {
160 options = { strategies: [options] };
161 }
162 else if (options.strategy) {
163 options.strategies = [options.strategy];
164 delete options.strategy;
165 }
166
167 if (path &&
168 !options.strategies) {
169
170 Hoek.assert(this.settings.default, 'Route missing authentication strategy and no default defined:', path);
171 options = Hoek.applyToDefaults(this.settings.default, options);
172 }
173
174 path = path ?? 'default strategy';
175 Hoek.assert(options.strategies?.length, 'Missing authentication strategy:', path);
176
177 options.mode = options.mode ?? 'required';
178
179 if (options.entity !== undefined || // Backwards compatibility with <= 11.x.x
180 options.scope !== undefined) {
181
182 options.access = [{ entity: options.entity, scope: options.scope }];
183 delete options.entity;
184 delete options.scope;
185 }
186
187 if (options.access) {
188 for (const access of options.access) {
189 access.scope = internals.setupScope(access);
190 }
191 }
192
193 if (options.payload === true) {
194 options.payload = 'required';
195 }
196
197 let hasAuthenticatePayload = false;
198 for (const name of options.strategies) {
199 const strategy = this.#strategies[name];
200 Hoek.assert(strategy, 'Unknown authentication strategy', name, 'in', path);
201
202 Hoek.assert(strategy.methods.payload || options.payload !== 'required', 'Payload validation can only be required when all strategies support it in', path);
203 hasAuthenticatePayload = hasAuthenticatePayload || strategy.methods.payload;
204 Hoek.assert(!strategy.methods.options.payload || options.payload === undefined || options.payload === 'required', 'Cannot set authentication payload to', options.payload, 'when a strategy requires payload validation in', path);
205 }
206
207 Hoek.assert(!options.payload || hasAuthenticatePayload, 'Payload authentication requires at least one strategy with payload support in', path);
208
209 return options;
210 }

Callers 2

defaultMethod · 0.80
constructorMethod · 0.80

Calls

no outgoing calls

Tested by

no test coverage detected