0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2025-01-20 22:52:46 -05:00
verdaccio/packages/utils/src/auth-utils.ts

121 lines
2.8 KiB
TypeScript
Raw Normal View History

import { ROLES, TIME_EXPIRATION_7D, DEFAULT_MIN_LIMIT_PASSWORD } from '@verdaccio/dev-commons';
import {
RemoteUser,
AllowAccess,
PackageAccess,
Security,
APITokenOptions,
JWTOptions,
} from '@verdaccio/types';
2020-03-08 09:19:12 +01:00
import { VerdaccioError } from '@verdaccio/commons-api';
2020-03-03 23:59:19 +01:00
export interface CookieSessionToken {
expires: Date;
}
export function validatePassword(
password: string,
minLength: number = DEFAULT_MIN_LIMIT_PASSWORD
): boolean {
return typeof password === 'string' && password.length >= minLength;
}
2020-03-08 09:19:12 +01:00
/**
* All logged users will have by default the group $all and $authenticate
*/
export const defaultLoggedUserRoles = [
ROLES.$ALL,
ROLES.$AUTH,
ROLES.DEPRECATED_ALL,
ROLES.DEPRECATED_AUTH,
ROLES.ALL,
];
2020-03-08 09:19:12 +01:00
/**
*
*/
export const defaultNonLoggedUserRoles = [
ROLES.$ALL,
ROLES.$ANONYMOUS,
// groups without '$' are going to be deprecated eventually
ROLES.DEPRECATED_ALL,
ROLES.DEPRECATED_ANONYMOUS,
2020-03-08 09:19:12 +01:00
];
/**
* Create a RemoteUser object
* @return {Object} { name: xx, pluginGroups: [], real_groups: [] }
*/
export function createRemoteUser(name: string, pluginGroups: string[]): RemoteUser {
2018-09-21 17:34:12 +02:00
const isGroupValid: boolean = Array.isArray(pluginGroups);
2020-03-08 09:19:12 +01:00
const groups = (isGroupValid ? pluginGroups : []).concat([...defaultLoggedUserRoles]);
return {
name,
groups,
real_groups: pluginGroups,
};
}
/**
* Builds an anonymous remote user in case none is logged in.
* @return {Object} { name: xx, groups: [], real_groups: [] }
*/
export function createAnonymousRemoteUser(): RemoteUser {
return {
name: undefined,
2020-03-08 09:19:12 +01:00
groups: [...defaultNonLoggedUserRoles],
real_groups: [],
};
}
2018-07-17 20:33:51 +02:00
2020-03-08 09:19:12 +01:00
export type AllowActionCallbackResponse = boolean | undefined;
export type AllowActionCallback = (
error: VerdaccioError | null,
allowed?: AllowActionCallbackResponse
) => void;
export type AllowAction = (
user: RemoteUser,
pkg: AuthPackageAllow,
callback: AllowActionCallback
) => void;
2020-03-08 09:19:12 +01:00
export interface AuthPackageAllow extends PackageAccess, AllowAccess {
// TODO: this should be on @verdaccio/types
unpublish: boolean | string[];
}
export function createSessionToken(): CookieSessionToken {
const tenHoursTime = 10 * 60 * 60 * 1000;
return {
// npmjs.org sets 10h expire
expires: new Date(Date.now() + tenHoursTime),
};
}
const defaultWebTokenOptions: JWTOptions = {
sign: {
// The expiration token for the website is 7 days
expiresIn: TIME_EXPIRATION_7D,
},
verify: {},
};
const defaultApiTokenConf: APITokenOptions = {
legacy: true,
};
export const defaultSecurity: Security = {
web: defaultWebTokenOptions,
api: defaultApiTokenConf,
};
export function getAuthenticatedMessage(user: string): string {
return `you are authenticated as '${user}'`;
}
export function buildUserBuffer(name: string, password: string): Buffer {
return Buffer.from(`${name}:${password}`, 'utf8');
}