Skip to content

Commit

Permalink
feat: auth signature timestamp (#810)
Browse files Browse the repository at this point in the history
  • Loading branch information
manchuck authored Mar 23, 2023
1 parent 8b2834f commit 254386f
Show file tree
Hide file tree
Showing 41 changed files with 2,288 additions and 1,147 deletions.
1,980 changes: 1,219 additions & 761 deletions package-lock.json

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions packages/auth/__tests__/__dataSets__/basic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { AuthParams } from '../../lib/types/index';
import {
apiKey,
apiSecret,
} from '../common';

export default [
{
label: 'create basic auth',
method: 'createBasicHeader',
authParameters: {
apiKey,
apiSecret,
} as AuthParams,
parameters: [],
expected: `Basic MTIzNDU6QUJDREU=`,
},
];

23 changes: 23 additions & 0 deletions packages/auth/__tests__/__dataSets__/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import jwtTests from './jwt';
import basicTests from './basic';
import queryTests from './query';
import signatureTests from './signature';

export default [
{
label: 'JWT',
tests: jwtTests,
},
{
label: 'Basic Auth',
tests: basicTests,
},
{
label: 'Query Params',
tests: queryTests,
},
{
label: 'Signature Hash',
tests: signatureTests,
},
];
54 changes: 54 additions & 0 deletions packages/auth/__tests__/__dataSets__/jwt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { tokenGenerate } from '@vonage/jwt';
import { AuthParams } from '../../lib/types/index';
import {
applicationId,
privateKeyString,
privateKeyPath,
} from '../common';

jest.useFakeTimers({
now: 10907902800000,
});

export default [
{
label: 'create bearer header with private string',
method: 'createBearerHeader',
authParameters: {
privateKey: privateKeyString,
applicationId: applicationId,
jwtOptions: {
jti: 'foo',
},
} as AuthParams,
parameters: [],
expected: `Bearer ${tokenGenerate(applicationId, privateKeyString, { jti: 'foo' })}`,
},
{
label: 'create bearer header with private key file',
method: 'createBearerHeader',
authParameters: {
privateKey: privateKeyPath,
applicationId: applicationId,
jwtOptions: {
jti: 'foo',
},
} as AuthParams,
parameters: [],
expected: `Bearer ${tokenGenerate(applicationId, privateKeyString, { jti: 'foo' })}`,
},
{
label: 'create bearer header with custom claims',
method: 'createBearerHeader',
authParameters: {
privateKey: privateKeyPath,
applicationId: applicationId,
jwtOptions: {
jti: 'foo',
fizz: 'buzz',
},
} as AuthParams,
parameters: [],
expected: `Bearer ${tokenGenerate(applicationId, privateKeyString, { jti: 'foo', fizz: 'buzz' })}`,
},
];
59 changes: 59 additions & 0 deletions packages/auth/__tests__/__dataSets__/query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { AuthParams, AuthQueryParams } from '../../lib/types/index';
import {
apiKey,
apiSecret,
} from '../common';

export default [
{
label: 'add to query params',
method: 'getQueryParams',
authParameters: {
apiKey,
apiSecret,
} as AuthParams,
parameters: [],
expected: {
api_key: apiKey,
api_secret: apiSecret,
} as AuthQueryParams,
},
{
label: 'append to query params',
method: 'getQueryParams',
authParameters: {
apiKey,
apiSecret,
} as AuthParams,
parameters: [
{
fizz: 'buzz',
},
],
expected: {
fizz: 'buzz',
api_key: apiKey,
api_secret: apiSecret,
} as AuthQueryParams,
},
{
label: 'add key and secret without being overiddend',
method: 'getQueryParams',
authParameters: {
apiKey,
apiSecret,
} as AuthParams,
parameters: [
{
api_key: 'not my key',
api_secret: 'not my secret',
},
],
expected: {
api_key: apiKey,
api_secret: apiSecret,
} as AuthQueryParams,
},
];


156 changes: 156 additions & 0 deletions packages/auth/__tests__/__dataSets__/signature.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
import { AlgorithmTypes } from '../../lib/enums/index';
import { AuthParams, SignedHashParams } from '../../lib/types/index';
import {
apiKey,
apiSecret,
} from '../common';

export default [
{
label: `sign request using ${AlgorithmTypes.md5hash}`,
method: 'createSignatureHash',
authParameters: {
apiKey: apiKey,
signature: {
secret: apiSecret,
algorithm: AlgorithmTypes.md5hash,
} as SignedHashParams,
} as AuthParams,
parameters: [],
expected: {
api_key: apiKey,
timestamp: '10907902800',
sig: 'd7f81394898d98c876e782b3c98391b8',
},
},
{
label: `sign request using ${AlgorithmTypes.md5hmac}`,
method: 'createSignatureHash',
authParameters: {
apiKey: apiKey,
signature: {
secret: apiSecret,
algorithm: AlgorithmTypes.md5hmac,
} as SignedHashParams,
} as AuthParams,
parameters: [],
expected: {
api_key: apiKey,
timestamp: '10907902800',
sig: 'c3216bfa0866f77ff0b8cb2db6024a6e',
},
},
{
label: `sign request using ${AlgorithmTypes.sha1hmac}`,
method: 'createSignatureHash',
authParameters: {
apiKey: apiKey,
signature: {
secret: apiSecret,
algorithm: AlgorithmTypes.sha1hmac,
} as SignedHashParams,
} as AuthParams,
parameters: [],
expected: {
api_key: apiKey,
timestamp: '10907902800',
sig: '9c3aafe2377f54394366a483eae34ce32fdadfef',
},
},
{
label: `sign request using ${AlgorithmTypes.sha256hmac}`,
method: 'createSignatureHash',
authParameters: {
apiKey: apiKey,
signature: {
secret: apiSecret,
algorithm: AlgorithmTypes.sha256hmac,
} as SignedHashParams,
} as AuthParams,
parameters: [],
expected: {
api_key: apiKey,
timestamp: '10907902800',
sig: '19ce55eb9d54bf1a9d758f4b6b7dc6d9f86e15924edf4ddd104e2c1edcd57443',
},
},
{
label: `sign request using ${AlgorithmTypes.sha512hmac}`,
method: 'createSignatureHash',
authParameters: {
apiKey: apiKey,
signature: {
secret: apiSecret,
algorithm: AlgorithmTypes.sha512hmac,
} as SignedHashParams,
} as AuthParams,
parameters: [],
expected: {
api_key: apiKey,
timestamp: '10907902800',
sig: `a3d390830787461570689a4b892b6f619e25da77f89dc7ec87c024f774fa7b4f5252b33189483af56c6f395ba181f90d5fb27edac31911df17abb711f86dd1b5`,
},
},
{
label: `sign without overwriting timestamp`,
method: 'createSignatureHash',
authParameters: {
apiKey: apiKey,
signature: {
secret: apiSecret,
algorithm: AlgorithmTypes.sha512hmac,
} as SignedHashParams,
} as AuthParams,
parameters: [{
fizz: 'buzz',
api_key: 'not my api key',
timestamp: '1444924800000',
}],
expected: {
fizz: 'buzz',
api_key: apiKey,
timestamp: '1444924800000',
sig: `c192d25e4f106187dc20b6a5b3a2ec66aa011e1d8332122cb932040c944cf495d7f72e2fdb052438d309f1401d91e445c679a6f69b9537d6bf5fdac7016f8639`,
},
},
{
label: `sign without overwriting timestamp`,
method: 'createSignatureHash',
authParameters: {
apiKey: apiKey,
signature: {
secret: apiSecret,
algorithm: AlgorithmTypes.sha512hmac,
} as SignedHashParams,
} as AuthParams,
parameters: [{
fizz: 'buzz',
timestamp: '1444924800000',
}],
expected: {
fizz: 'buzz',
api_key: apiKey,
timestamp: '1444924800000',
sig: `c192d25e4f106187dc20b6a5b3a2ec66aa011e1d8332122cb932040c944cf495d7f72e2fdb052438d309f1401d91e445c679a6f69b9537d6bf5fdac7016f8639`,
},
},
{
label: `error with invalid algorithm`,
method: 'createSignatureHash',
authParameters: {
apiKey: apiKey,
signature: {
secret: apiSecret,
algorithm: 'foo',
},
},
parameters: [],
error: 'Cannot sign request! Invalid algorithm: foo',
expected: {
api_key: apiKey,
timestamp: '10907902800',
sig: `a3d390830787461570689a4b892b6f619e25da77f89dc7ec87c024f774fa7b4f5252b33189483af56c6f395ba181f90d5fb27edac31911df17abb711f86dd1b5`,
},
},
];

Loading

0 comments on commit 254386f

Please sign in to comment.