Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if (userId) {
user = await User.getById(userId);
} else if (customUserId) {
user = await User.getByCustomUserId(customUserId);
}
if (pubKey) {
key = await Key.getByPubKey(pubKey, user && user.get('id'), !user);
if (!key) {
throw new NotFoundKeyError();
}
if (!user) {
user = key.get('user');
if (user.get('mode') === 'esign') {
throw new Unauthorized('Cannot use e-signature with an admin token.');
}
}
}
if ((userId || customUserId) && !user) {
throw new NotFoundUserError();
}
// Key and user are specified, need to check that the user is the owner of a key.
if (key && user) {
if (key.get('userId') !== user.get('id')) {
throw new KeyOwnerMismatchError();
}
}
// If the pubkey is not specified, need to put the value by default.
module.exports = (opts, accessToken) => {
debug('verifyToken opts', opts);
// Throw error if no publication URL provided
if (!opts.me) {
throw new HttpError.InternalServerError('No publication URL provided');
}
// Throw error if no access token provided
if (!accessToken) {
throw new HttpError.Unauthorized('No access token provided in request');
}
// Throw error if access token does not contain a `me` value
if (!accessToken.me) {
throw new HttpError.Unauthorized('There was a problem with this access token');
}
// Normalize publication and token URLs before comparing
const accessTokenMe = normalizeUrl(accessToken.me);
const publicationMe = normalizeUrl(opts.me);
const isAuthenticated = accessTokenMe === publicationMe;
debug('Verified token URL: %s', accessTokenMe);
debug('Publication URL: %s', publicationMe);
// Publication URL does not match that provided by access token
async getUser(data, next) {
const user = this.userSockets.getBySocket(data.get('client'))
if (!user) throw new errors.Unauthorized('authorization required')
const newData = data.set('user', user)
return await next(newData)
}
function checkIfIsTrusted (result, callback) {
if (!trusted) {
callback(null, result)
} else {
const maybePromise = trusted(request, result)
if (maybePromise && maybePromise.then) {
maybePromise
.then(trusted => trusted ? callback(null, result) : callback(new Unauthorized(messagesOptions.authorizationTokenUntrusted)))
} else if (maybePromise) {
callback(null, maybePromise)
} else {
callback(new Unauthorized(messagesOptions.authorizationTokenUntrusted))
}
}
}
], function (err, result) {
.then(trusted => trusted ? callback(null, result) : callback(new Unauthorized(messagesOptions.authorizationTokenUntrusted)))
} else if (maybePromise) {
const infoUpdatePassword = ctx.request.body;
let user;
if (!infoUpdatePassword.email) {
throw new BadRequest('Need to send the email address.');
}
if (!infoUpdatePassword.token) {
throw new BadRequest('Need to send the reset token.');
}
if (!infoUpdatePassword.password) {
throw new BadRequest('Need to send the new password.');
}
try {
user = await updatePassword(infoUpdatePassword);
} catch (err) {
throw new Unauthorized('Invalid token.');
}
event.register({
type: 'user.edit',
authorizedUserId: null,
associatedTokenId: null,
associatedUserId: user.id,
associatedKeyId: null,
data: hidePassword(user)
});
ctx.body = serializeUser(user);
});
switch (token.status) {
case 'active':
return next();
case 'expired':
throw new Unauthorized('Token expired');
case 'blocked':
throw new Unauthorized('Token blocked');
}
}
}
throw new Unauthorized('Invalid token');
}
}
throw new Unauthorized('Missing token');
}
ensureIsLobbyHost(lobby, player) {
if (player.id !== lobby.host.id) {
throw new errors.Unauthorized('must be a lobby host')
}
}
export async function user(ctx: Context, next) {
if (!(ctx.session && ctx.session.user)) {
throw new Unauthorized();
}
return next();
}