1
0
mirror of https://github.com/verdaccio/verdaccio.git synced 2025-02-21 07:29:37 +01:00

Refactor & minor fixes (#1009)

This commit is contained in:
Ayush Sharma 2018-09-21 17:34:12 +02:00 committed by GitHub
parent e92c680586
commit 32fce8eaea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 24 additions and 23 deletions

@ -58,7 +58,7 @@ export function validatePackage(req: $RequestExtend, res: $ResponseExtend, next:
export function media(expect: string) {
return function(req: $RequestExtend, res: $ResponseExtend, next: $NextFunctionVer) {
if (req.headers[HEADER_TYPE.CONTENT_TYPE] !== expect) {
next( ErrorCode.getCode(HTTP_STATUS.UNSUPORTED_MEDIA, 'wrong content-type, expect: ' + expect
next( ErrorCode.getCode(HTTP_STATUS.UNSUPPORTED_MEDIA, 'wrong content-type, expect: ' + expect
+ ', got: '+req.headers[HEADER_TYPE.CONTENT_TYPE]) );
} else {
next();

@ -1,7 +1,7 @@
// @flow
import _ from 'lodash';
import {convertPayloadToBase64, ErrorCode} from './utils';
import {API_ERROR, HTTP_STATUS, ROLES, TIME_EXPIRATION_7D, TOKEN_BASIC, TOKEN_BEARER} from './constants';
import {API_ERROR, HTTP_STATUS, ROLES, TIME_EXPIRATION_7D, TOKEN_BASIC, TOKEN_BEARER, CHARACTER_ENCODING} from './constants';
import type {
RemoteUser,
@ -22,7 +22,7 @@ import {aesDecrypt, verifyPayload} from './crypto-utils';
* @return {Object} { name: xx, pluginGroups: [], real_groups: [] }
*/
export function createRemoteUser(name: string, pluginGroups: Array<string>): RemoteUser {
const isGroupValid: boolean = _.isArray(pluginGroups);
const isGroupValid: boolean = Array.isArray(pluginGroups);
const groups = (isGroupValid ? pluginGroups : []).concat([
ROLES.$ALL,
ROLES.$AUTH,
@ -50,7 +50,7 @@ export function createAnonymousRemoteUser(): RemoteUser {
ROLES.$ALL,
ROLES.$ANONYMOUS,
ROLES.DEPRECATED_ALL,
ROLES.DEPRECATED_ANONUMOUS,
ROLES.DEPRECATED_ANONYMOUS,
],
real_groups: [],
};
@ -127,7 +127,7 @@ export function getAuthenticatedMessage(user: string): string {
}
export function buildUserBuffer(name: string, password: string) {
return Buffer.from(`${name}:${password}`, 'utf8');
return Buffer.from(`${name}:${password}`, CHARACTER_ENCODING.UTF8);
}
export function isAESLegacy(security: Security): boolean {
@ -152,8 +152,7 @@ export async function getApiToken(
// i am wiling to use here _.isNil but flow does not like it yet.
const {jwt} = security.api;
if (typeof jwt !== 'undefined' &&
typeof jwt.sign !== 'undefined') {
if (jwt && jwt.sign) {
return await auth.jwtEncrypt(remoteUser, jwt.sign);
} else {
return await new Promise((resolve) => {
@ -204,15 +203,15 @@ export function verifyJWTPayload(token: string, secret: string): RemoteUser {
const payload: RemoteUser = (verifyPayload(token, secret): RemoteUser);
return payload;
} catch (err) {
} catch (error) {
// #168 this check should be removed as soon AES encrypt is removed.
if (err.name === 'JsonWebTokenError') {
if (error.name === 'JsonWebTokenError') {
// it might be possible the jwt configuration is enabled and
// old tokens fails still remains in usage, thus
// we return an anonymous user to force log in.
return createAnonymousRemoteUser();
} else {
throw ErrorCode.getCode(HTTP_STATUS.UNAUTHORIZED, err.message);
throw ErrorCode.getCode(HTTP_STATUS.UNAUTHORIZED, error.message);
}
}
}

@ -7,6 +7,7 @@ import logger from './logger';
import mkdirp from 'mkdirp';
import {folderExists, fileExists} from './utils';
import {CHARACTER_ENCODING} from './constants';
const CONFIG_FILE = 'config.yaml';
const XDG = 'xdg';
@ -48,7 +49,7 @@ function createConfigFile(configLocation: any) {
}
function readDefaultConfig() {
return fs.readFileSync(require.resolve('../../conf/default.yaml'), 'utf8');
return fs.readFileSync(require.resolve('../../conf/default.yaml'), CHARACTER_ENCODING.UTF8);
}
function createConfigFolder(configLocation) {

@ -14,7 +14,7 @@ export const HEADERS = {
};
export const CHARACTER_ENCODING = {
UTF8: 'utf-8'
UTF8: 'utf8'
}
export const HEADER_TYPE = {
@ -40,7 +40,7 @@ export const ROLES = {
$ANONYMOUS: '$anonymous',
DEPRECATED_ALL: '@all',
DEPRECATED_AUTH: '@authenticated',
DEPRECATED_ANONUMOUS: '@anonymous',
DEPRECATED_ANONYMOUS: '@anonymous',
};
export const HTTP_STATUS = {
@ -53,7 +53,7 @@ export const HTTP_STATUS = {
FORBIDDEN: 403,
NOT_FOUND: 404,
CONFLICT: 409,
UNSUPORTED_MEDIA: 415,
UNSUPPORTED_MEDIA: 415,
BAD_DATA: 422,
INTERNAL_ERROR: 500,
SERVICE_UNAVAILABLE: 503,

@ -8,7 +8,7 @@ import Stream from 'stream';
import URL from 'url';
import {parseInterval, isObject, ErrorCode, buildToken} from './utils';
import {ReadTarball} from '@verdaccio/streams';
import {ERROR_CODE, TOKEN_BASIC, TOKEN_BEARER, HEADERS, HTTP_STATUS, API_ERROR, HEADER_TYPE} from './constants';
import {ERROR_CODE, TOKEN_BASIC, TOKEN_BEARER, HEADERS, HTTP_STATUS, API_ERROR, HEADER_TYPE, CHARACTER_ENCODING} from './constants';
import type {
Config,
UpLinkConf,
@ -158,7 +158,7 @@ class ProxyStorage implements IProxy {
if (options.json && res.statusCode < 300) {
try {
// $FlowFixMe
body = JSON.parse(body.toString('utf8'));
body = JSON.parse(body.toString(CHARACTER_ENCODING.UTF8));
} catch (_err) {
body = {};
err = _err;

@ -1,6 +1,6 @@
import zlib from 'zlib';
import {readFile} from '../lib/test.utils';
import {HEADER_TYPE, HEADERS, HTTP_STATUS} from "../../../src/lib/constants";
import {HEADER_TYPE, HEADERS, HTTP_STATUS, CHARACTER_ENCODING} from "../../../src/lib/constants";
export default function(server, express) {
const PKG_NAME = 'testexp_gzip';
@ -13,7 +13,7 @@ export default function(server, express) {
beforeAll(function() {
express.get(`/${PKG_NAME}`, function(req, res) {
const pkg = JSON.parse(readFile('../fixtures/publish.json5')
.toString('utf8')
.toString(CHARACTER_ENCODING.UTF8)
.replace(/__NAME__/g, PKG_NAME)
.replace(/__VERSION__/g, PKG_VERSION));
@ -69,7 +69,7 @@ export default function(server, express) {
.then(async function(body) {
// should fails since is zipped
expect(function() {
JSON.parse(body.toString('utf8'));
JSON.parse(body.toString(CHARACTER_ENCODING.UTF8));
}).toThrow(/Unexpected/);
// we unzip content and check content

@ -1,5 +1,5 @@
import {readFile} from '../lib/test.utils';
import {API_ERROR, HTTP_STATUS} from "../../../src/lib/constants";
import {API_ERROR, HTTP_STATUS, CHARACTER_ENCODING} from "../../../src/lib/constants";
const readTags = () => readFile('../fixtures/publish.json5');
@ -18,7 +18,7 @@ export default function(server) {
describe('should test add tag to a package', () => {
beforeAll(function() {
return server.putPackage(PKG_NAME,
JSON.parse(readTags().toString('utf8').replace(/__NAME__/g, PKG_NAME)
JSON.parse(readTags().toString(CHARACTER_ENCODING.UTF8).replace(/__NAME__/g, PKG_NAME)
.replace(/__VERSION__/g, PKG_VERSION))
).status(HTTP_STATUS.CREATED);
});

@ -2,6 +2,7 @@
import _ from 'lodash';
import Auth from '../../../src/lib/auth';
import {CHARACTER_ENCODING} from '../../../src/lib/constants';
// $FlowFixMe
import configExample from '../partials/config/index';
import AppConfig from '../../../src/lib/config';
@ -70,7 +71,7 @@ describe('Auth utilities', () => {
};
const verifyAES = (token: string, user: string, password: string, secret: string) => {
const payload = aesDecrypt(convertPayloadToBase64(token), secret).toString('utf8');
const payload = aesDecrypt(convertPayloadToBase64(token), secret).toString(CHARACTER_ENCODING.UTF8);
const content = payload.split(':');
expect(content[0]).toBe(user);
@ -86,7 +87,7 @@ describe('Auth utilities', () => {
expect(_.isString(token)).toBeTruthy();
});
test('should sign token with aes and security emtpy', async () => {
test('should sign token with aes and security empty', async () => {
const token = await signCredentials('security-empty',
'test', 'test', '123456', 'aesEncrypt', 'jwtEncrypt');