Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const { provider, authentication } = params;
const authService = app.defaultAuthentication(settings.service);
debug(`Running authenticate hook on '${path}'`);
if (type && type !== 'before') {
throw new NotAuthenticated('The authenticate hook must be used as a before hook');
}
if (!authService || typeof authService.authenticate !== 'function') {
throw new NotAuthenticated('Could not find a valid authentication service');
}
// @ts-ignore
if (service === authService) {
throw new NotAuthenticated('The authenticate hook does not need to be used on the authentication service');
}
if (params.authenticated === true) {
return context;
}
if (authentication) {
const authParams = omit(params, 'provider', 'authentication', 'query');
debug('Authenticating with', authentication, strategies);
const authResult = await authService.authenticate(authentication, authParams, ...strategies);
context.params = Object.assign({}, params, omit(authResult, 'accessToken'), { authenticated: true });
return context;
return async (context: HookContext) => {
const { app, params, type, path, service } = context;
const { strategies } = settings;
const { provider, authentication } = params;
const authService = app.defaultAuthentication(settings.service);
debug(`Running authenticate hook on '${path}'`);
if (type && type !== 'before') {
throw new NotAuthenticated('The authenticate hook must be used as a before hook');
}
if (!authService || typeof authService.authenticate !== 'function') {
throw new NotAuthenticated('Could not find a valid authentication service');
}
// @ts-ignore
if (service === authService) {
throw new NotAuthenticated('The authenticate hook does not need to be used on the authentication service');
}
if (params.authenticated === true) {
return context;
}
if (authentication) {
const authParams = omit(params, 'provider', 'authentication', 'query');
debug('Authenticating with', authentication, strategies);
async remove (id: string | null, params: Params) {
const { authentication } = params;
const { authStrategies } = this.configuration;
// When an id is passed it is expected to be the authentication `accessToken`
if (id !== null && id !== authentication.accessToken) {
throw new NotAuthenticated('Invalid access token');
}
debug('Verifying authentication strategy in remove');
return this.authenticate(authentication, params, ...authStrategies);
}
// If called internally or we are already authenticated skip
if (!hook.params.provider || hook.params.authenticated) {
return Promise.resolve(hook);
}
if (hook.type !== 'before') {
return Promise.reject(new Error(`The 'authenticate' hook should only be used as a 'before' hook.`));
}
hook.data = hook.data || {};
const strategy = hook.data.strategy || strategies[0];
if (strategies.indexOf(strategy) === -1) {
return Promise.reject(new NotAuthenticated(`Strategy ${strategy} is not permitted`));
}
// Handle the case where authenticate hook was registered without a passport strategy specified
if (!strategy) {
return Promise.reject(new errors.GeneralError(`You must provide an authentication 'strategy'`));
}
// The client must send a `strategy` name.
if (!app.passport._strategy(strategy)) {
return Promise.reject(new errors.BadRequest(`Authentication strategy '${strategy}' is not registered.`));
}
// NOTE (EK): Passport expects an express/connect
// like request object. So we need to create one.
let request = {
query: hook.data,
authenticate (authentication, params, ...allowed) {
debug('Running authenticate for strategies', allowed);
const strategies = this.getStrategies(...allowed)
.filter(current => current && typeof current.authenticate === 'function');
if (!authentication || strategies.length === 0) {
// If there are no valid strategies or `authentication` is not an object
return Promise.reject(
new NotAuthenticated(`No valid authentication strategy available`)
);
}
const { strategy } = authentication;
const authParams = Object.assign(params, { authentication: true });
// Throw an error is a `strategy` is indicated but not in the allowed strategies
if (strategy && !allowed.includes(strategy)) {
return Promise.reject(
new NotAuthenticated(`Invalid authentication strategy '${strategy}'`)
);
}
// Run all strategies and accumulate results and errors
const promise = strategies.reduce((acc, authStrategy) => {
return acc.then(({ result, error }) => {
async getEntity (id: string, params: Params) {
const { entity } = this.configuration;
const entityService = this.entityService;
debug('Getting entity', id);
if (entityService === null) {
throw new NotAuthenticated(`Could not find entity service`);
}
const result = await entityService.get(id, omit(params, 'provider'));
if (!params.provider) {
return result;
}
return entityService.get(id, { ...params, [entity]: result });
}
async getEntity (result: any, params: Params) {
const { entityService } = this;
const { entityId = entityService.id, entity } = this.configuration;
if (!entityId || result[entityId] === undefined) {
throw new NotAuthenticated('Could not get local entity');
}
if (!params.provider) {
return result;
}
return entityService.get(result[entityId], {
...params,
[entity]: result
});
}
return this.getAccessToken().then(accessToken => {
if (!accessToken) {
throw new NotAuthenticated('No accessToken found in storage');
}
return this.authenticate({
strategy: this.options.jwtStrategy,
accessToken
});
});
}
return new Promise(function (resolve, reject) {
var tokenLocation = options.storageKey || options.cookie;
if (hasValidToken(tokenLocation) && !window.doneSsr) {
feathersClient.authenticate()
.then(function (data) {
var payload = decode(data.accessToken);
return resolve(payload);
})
.catch(reject);
} else {
reject(new errors.NotAuthenticated('Not Authenticated'));
}
});
},
getCredentials = this.getJWT().then(accessToken => {
if (!accessToken) {
return Promise.reject(new errors.NotAuthenticated(`Could not find stored JWT and no authentication strategy was given`));
}
return { strategy: this.options.jwtStrategy, accessToken };
});
}