Skip to content

Commit

Permalink
feat: add logging output for each action
Browse files Browse the repository at this point in the history
  • Loading branch information
@jotadeveloper authored and sergiohgz committed Jul 31, 2019
1 parent 41f0ae1 commit 66f183c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
19 changes: 18 additions & 1 deletion plugins/auth-memory/src/Memory.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { PluginOptions, Callback, PackageAccess, IPluginAuth, RemoteUser, Logger } from '@verdaccio/types';
import { VerdaccioMemoryConfig, Users, UserMemory } from '../types/index';
import { getConflict, getForbidden, getNotFound, getUnauthorized } from '@verdaccio/commons-api/lib';

import { VerdaccioMemoryConfig, Users, UserMemory } from '../types/index';

export default class Memory implements IPluginAuth<VerdaccioMemoryConfig> {
public _logger: Logger;
public _users: Users;
Expand All @@ -19,22 +20,26 @@ export default class Memory implements IPluginAuth<VerdaccioMemoryConfig> {
const userCredentials = this._users[user];

if (!userCredentials) {
this._logger.debug({user}, '[VerdaccioMemory] user @{user} does not exist');
return done(null, false);
}

if (password !== userCredentials.password) {
const err = getUnauthorized("i don't like your password");
this._logger.info({user}, '[VerdaccioMemory] password invalid for: @{user}');

return done(err);
}

// authentication succeeded!
// return all usergroups this user has access to;
this._logger.info({user}, '[VerdaccioMemory] authentication succeeded for @{user}');
return done(null, [user]);
}

adduser(user: string, password: string, done: Callback) {
if (this._users[user]) {
this._logger.debug({user}, '[VerdaccioMemory] user @{user} already exist');
return done(null, true);
}

Expand All @@ -48,50 +53,61 @@ export default class Memory implements IPluginAuth<VerdaccioMemoryConfig> {

this._users[user] = { name: user, password: password };

this._logger.info({ user }, '[VerdaccioMemory] user added succeeded for @{user}');
done(null, user);
}

changePassword(username: string, password: string, newPassword: string, cb: Callback) {
const user: UserMemory = this._users[username];
this._logger.debug({ user: user.name }, 'user: @{user} init change password');

if (user && user.password === password) {
user.password = newPassword;
this._users[username] = user;

this._logger.info({ user }, '[VerdaccioMemory] user changed password succeeded for @{user}');
cb(null, user);
} else {
const err = getNotFound('user not found');
this._logger.debug({ user: user.name }, 'change password user @{user} not found');

return cb(err);
}
}

allow_access(user: RemoteUser, pkg: PackageAccess, cb: Callback) {
if (pkg.access!.includes('$all') || pkg.access!.includes('$anonymous')) {
this._logger.debug({ user: user.name }, '[VerdaccioMemory] user: @{user} has been granted access');

return cb(null, true);
}

if (!user.name) {
const err = getForbidden('not allowed to access package');
this._logger.debug({ user: user.name }, 'user: @{user} not allowed to access package');
return cb(err);
}

if (pkg.access!.includes(user.name) || pkg.access!.includes('$authenticated')) {
this._logger.debug({ user: user.name }, '[VerdaccioMemory] user: @{user} has been granted access');
return cb(null, true);
}

const err = getForbidden('not allowed to access package');

this._logger.debug({ user: user.name }, '[VerdaccioMemory] user: @{user} not allowed to access package');
return cb(err);
}

allow_publish(user: RemoteUser, pkg: PackageAccess, cb: Callback) {
if (pkg.publish!.includes('$all') || pkg.publish!.includes('$anonymous')) {
this._logger.debug({ user: user.name }, '[VerdaccioMemory] user: @{user} has been granted to publish');
return cb(null, true);
}

if (!user.name) {
const err = getForbidden('not allowed to publish package');
this._logger.debug({ user: user.name }, 'user: @{user} not allowed to publish package');

return cb(err);
}
Expand All @@ -101,6 +117,7 @@ export default class Memory implements IPluginAuth<VerdaccioMemoryConfig> {
}

const err = getForbidden('not allowed to publish package');
this._logger.debug({ user: user.name }, '[VerdaccioMemory] user: @{user} not allowed to publish package');

return cb(err);
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/auth-memory/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"noImplicitAny": true,
"strict": true,
"outDir": "lib",
"allowSyntheticDefaultImports": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"typeRoots": [
"./node_modules/@verdaccio/types/lib/verdaccio",
Expand Down

0 comments on commit 66f183c

Please sign in to comment.