From 9ade39a2643c93898171489f9357481012881079 Mon Sep 17 00:00:00 2001 From: "Juan Picado @jotadeveloper" Date: Tue, 17 Jul 2018 20:33:51 +0200 Subject: [PATCH] refactor: add types on auth files --- src/lib/auth-utils.js | 12 ++++++++---- src/lib/auth.js | 12 ++++++------ 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/lib/auth-utils.js b/src/lib/auth-utils.js index 67e7142dc..aa3586cf1 100644 --- a/src/lib/auth-utils.js +++ b/src/lib/auth-utils.js @@ -1,8 +1,12 @@ +// @flow + import {ErrorCode} from './utils'; import {API_ERROR} from './constants'; -export function allow_action(action) { - return function(user, pkg, callback) { +import type {RemoteUser, Package, Callback} from '@verdaccio/types'; + +export function allow_action(action: string) { + return function(user: RemoteUser, pkg: Package, callback: Callback) { const {name, groups} = user; const hasPermission = pkg[action].some((group) => name === group || groups.includes(group)); @@ -20,11 +24,11 @@ export function allow_action(action) { export function getDefaultPlugins() { return { - authenticate(user, password, cb) { + authenticate(user: string, password: string, cb: Callback) { cb(ErrorCode.getForbidden(API_ERROR.BAD_USERNAME_PASSWORD)); }, - add_user(user, password, cb) { + add_user(user: string, password: string, cb: Callback) { return cb(ErrorCode.getConflict(API_ERROR.BAD_USERNAME_PASSWORD)); }, diff --git a/src/lib/auth.js b/src/lib/auth.js index d15656d2e..bb82c3c87 100644 --- a/src/lib/auth.js +++ b/src/lib/auth.js @@ -126,11 +126,11 @@ class Auth implements IAuth { (function next() { const plugin = plugins.shift(); - if (typeof(plugin.allow_access) !== 'function') { + if (_.isFunction(plugin.allow_access) === false) { return next(); } - plugin.allow_access(user, pkg, function(err, ok) { + plugin.allow_access(user, pkg, function(err, ok: boolean) { if (err) { return callback(err); } @@ -155,11 +155,11 @@ class Auth implements IAuth { (function next() { const plugin = plugins.shift(); - if (typeof(plugin.allow_publish) !== 'function') { + if (_.isFunction(plugin.allow_publish) === false) { return next(); } - plugin.allow_publish(user, pkg, (err, ok) => { + plugin.allow_publish(user, pkg, (err, ok: boolean) => { if (err) { return callback(err); } @@ -188,7 +188,8 @@ class Auth implements IAuth { return _next(); }; - if (req.remote_user != null && req.remote_user.name !== undefined) { + if (_.isUndefined(req.remote_user) === false + && _.isUndefined(req.remote_user.name) === false) { return next(); } req.remote_user = buildAnonymousUser(); @@ -204,7 +205,6 @@ class Auth implements IAuth { } const credentials = this._parseCredentials(parts); - if (!credentials) { return next(); }