-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: signature package * feat: signature package
- Loading branch information
1 parent
399cf9c
commit ddb6a22
Showing
27 changed files
with
298 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
'@verdaccio/auth': minor | ||
'@verdaccio/config': minor | ||
'@verdaccio/signature': minor | ||
'@verdaccio/ui-components': minor | ||
--- | ||
|
||
feat: signature package |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,2 @@ | ||
export { Auth, TokenEncryption } from './auth'; | ||
export { Auth } from './auth'; | ||
export * from './utils'; | ||
export * from './legacy-token'; | ||
export * from './jwt-token'; | ||
export * from './token'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,9 @@ | |
{ | ||
"path": "../loaders" | ||
}, | ||
{ | ||
"path": "../signature" | ||
}, | ||
{ | ||
"path": "../logger/logger" | ||
}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "../../.babelrc" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const config = require('../../jest/config'); | ||
|
||
module.exports = Object.assign({}, config, {}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
{ | ||
"name": "@verdaccio/signature", | ||
"version": "6.0.0-6-next.1", | ||
"description": "verdaccio signature utils", | ||
"main": "./build/index.js", | ||
"types": "build/index.d.ts", | ||
"author": { | ||
"name": "Juan Picado", | ||
"email": "[email protected]" | ||
}, | ||
"repository": { | ||
"type": "https", | ||
"url": "https://github.com/verdaccio/verdaccio" | ||
}, | ||
"license": "MIT", | ||
"homepage": "https://verdaccio.org", | ||
"keywords": [ | ||
"private", | ||
"package", | ||
"repository", | ||
"registry", | ||
"enterprise", | ||
"modules", | ||
"proxy", | ||
"server", | ||
"verdaccio" | ||
], | ||
"engines": { | ||
"node": ">=12" | ||
}, | ||
"scripts": { | ||
"clean": "rimraf ./build", | ||
"test": "jest", | ||
"type-check": "tsc --noEmit -p tsconfig.build.json", | ||
"build:types": "tsc --emitDeclarationOnly -p tsconfig.build.json", | ||
"build:js": "babel src/ --out-dir build/ --copy-files --extensions \".ts,.tsx\" --source-maps", | ||
"watch": "pnpm build:js -- --watch", | ||
"build": "pnpm run build:js && pnpm run build:types" | ||
}, | ||
"dependencies": { | ||
"jsonwebtoken": "9.0.0", | ||
"debug": "4.3.4", | ||
"lodash": "4.17.21" | ||
}, | ||
"devDependencies": { | ||
"@verdaccio/config": "workspace:6.0.0-6-next.62", | ||
"@verdaccio/types": "workspace:11.0.0-6-next.21" | ||
}, | ||
"funding": { | ||
"type": "opencollective", | ||
"url": "https://opencollective.com/verdaccio" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export { | ||
aesDecryptDeprecated, | ||
aesEncryptDeprecated, | ||
generateRandomSecretKeyDeprecated, | ||
} from './legacy-signature'; | ||
export { aesDecrypt, aesEncrypt } from './signature'; | ||
export { signPayload, verifyPayload } from './jwt-token'; | ||
export * as utils from './utils'; | ||
export * as types from './types'; | ||
export { parseBasicPayload } from './token'; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { createCipher, createDecipher } from 'crypto'; | ||
|
||
import { generateRandomHexString } from '../utils'; | ||
|
||
export const defaultAlgorithm = 'aes192'; | ||
export const defaultTarballHashAlgorithm = 'sha1'; | ||
|
||
/** | ||
* | ||
* @param buf | ||
* @param secret | ||
* @returns | ||
*/ | ||
export function aesEncryptDeprecated(buf: Buffer, secret: string): Buffer { | ||
// deprecated (it will be removed in Verdaccio 6), it is a breaking change | ||
// https://nodejs.org/api/crypto.html#crypto_crypto_createcipher_algorithm_password_options | ||
// https://www.grainger.xyz/changing-from-cipher-to-cipheriv/ | ||
const c = createCipher(defaultAlgorithm, secret); | ||
const b1 = c.update(buf); | ||
const b2 = c.final(); | ||
return Buffer.concat([b1, b2]); | ||
} | ||
|
||
/** | ||
* | ||
* @param buf | ||
* @param secret | ||
* @returns | ||
*/ | ||
export function aesDecryptDeprecated(buf: Buffer, secret: string): Buffer { | ||
try { | ||
// https://nodejs.org/api/crypto.html#crypto_crypto_createdecipher_algorithm_password_options | ||
// https://www.grainger.xyz/changing-from-cipher-to-cipheriv/ | ||
const c = createDecipher(defaultAlgorithm, secret); | ||
const b1 = c.update(buf); | ||
const b2 = c.final(); | ||
return Buffer.concat([b1, b2]); | ||
} catch (_) { | ||
return Buffer.alloc(0); | ||
} | ||
} | ||
|
||
export const TOKEN_VALID_LENGTH_DEPRECATED = 64; | ||
|
||
/** | ||
* Genrate a secret key of 64 characters. | ||
* @deprecated keys should be length max of 64 | ||
*/ | ||
export function generateRandomSecretKeyDeprecated(): string { | ||
return generateRandomHexString(6); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export interface AESPayload { | ||
user: string; | ||
password: string; | ||
} | ||
|
||
export type BasicPayload = AESPayload | void; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { Hash, createHash, pseudoRandomBytes, randomBytes } from 'crypto'; | ||
|
||
export const defaultTarballHashAlgorithm = 'sha1'; | ||
|
||
/** | ||
* | ||
* @returns | ||
*/ | ||
export function createTarballHash(algorithm = defaultTarballHashAlgorithm): Hash { | ||
return createHash(algorithm); | ||
} | ||
|
||
/** | ||
* Express doesn't do ETAGS with requests <= 1024b | ||
* we use md5 here, it works well on 1k+ bytes, but with fewer data | ||
* could improve performance using crc32 after benchmarks. | ||
* @param {Object} data | ||
* @return {String} | ||
*/ | ||
export function stringToMD5(data: Buffer | string): string { | ||
return createHash('md5').update(data).digest('hex'); | ||
} | ||
|
||
/** | ||
* | ||
* @param length | ||
* @returns | ||
*/ | ||
export function generateRandomHexString(length = 8): string { | ||
return pseudoRandomBytes(length).toString('hex'); | ||
} | ||
|
||
export const TOKEN_VALID_LENGTH = 32; | ||
|
||
/** | ||
* Generate a secret of 32 characters. | ||
*/ | ||
export function generateRandomSecretKey(): string { | ||
return randomBytes(TOKEN_VALID_LENGTH).toString('base64').substring(0, TOKEN_VALID_LENGTH); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { createRemoteUser } from '@verdaccio/config'; | ||
|
||
import { signPayload, verifyPayload } from '../src'; | ||
|
||
describe('verifyJWTPayload', () => { | ||
test('should verify the token and return a remote user', async () => { | ||
const remoteUser = createRemoteUser('foo', []); | ||
const token = await signPayload(remoteUser, '12345'); | ||
const verifiedToken = verifyPayload(token, '12345'); | ||
expect(verifiedToken.groups).toEqual(remoteUser.groups); | ||
expect(verifiedToken.name).toEqual(remoteUser.name); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { | ||
aesDecryptDeprecated, | ||
aesEncryptDeprecated, | ||
generateRandomSecretKeyDeprecated, | ||
} from '../src'; | ||
|
||
describe('test deprecated crypto utils', () => { | ||
test('decrypt payload flow', () => { | ||
const secret = generateRandomSecretKeyDeprecated(); | ||
const payload = 'juan:password'; | ||
const token = aesEncryptDeprecated(Buffer.from(payload), secret); | ||
const data = aesDecryptDeprecated(token, secret); | ||
|
||
expect(data.toString()).toEqual(payload.toString()); | ||
}); | ||
|
||
test('crypt fails if secret is incorrect', () => { | ||
const payload = 'juan:password'; | ||
expect(aesEncryptDeprecated(Buffer.from(payload), 'fake_token').toString()).not.toEqual( | ||
Buffer.from(payload) | ||
); | ||
}); | ||
}); |
2 changes: 1 addition & 1 deletion
2
packages/auth/test/legacy-token.spec.ts → packages/signature/test/legacy-token.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { | ||
TOKEN_VALID_LENGTH, | ||
createTarballHash, | ||
generateRandomSecretKey, | ||
stringToMD5, | ||
} from '../src/utils'; | ||
|
||
test('token generation length is valid', () => { | ||
expect(generateRandomSecretKey()).toHaveLength(TOKEN_VALID_LENGTH); | ||
}); | ||
|
||
test('string to md5 has valid length', () => { | ||
expect(stringToMD5(Buffer.from('foo'))).toHaveLength(32); | ||
}); | ||
|
||
test('create a hash of content', () => { | ||
expect(typeof createTarballHash().update('1').digest('hex')).toEqual('string'); | ||
}); |
Oops, something went wrong.