-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: auth signature timestamp (#810)
- Loading branch information
Showing
41 changed files
with
2,288 additions
and
1,147 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,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=`, | ||
}, | ||
]; | ||
|
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 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, | ||
}, | ||
]; |
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,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' })}`, | ||
}, | ||
]; |
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,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, | ||
}, | ||
]; | ||
|
||
|
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,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`, | ||
}, | ||
}, | ||
]; | ||
|
Oops, something went wrong.