-
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.
fix(auth): add signed requests and req updates
- Loading branch information
1 parent
a845782
commit 887a776
Showing
6 changed files
with
164 additions
and
34 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 |
---|---|---|
@@ -1,18 +1,87 @@ | ||
import { AuthConstructor, AuthInterface, AuthOpts, AuthQueryParams } from './common' | ||
import { createHash, createHmac } from 'crypto'; | ||
import { AuthConstructor, AuthInterface, AuthOpts, AuthQueryParams, SignedHashParams, AuthSignedParams, AlgorithmTypes } from './types' | ||
|
||
export const Auth: AuthConstructor = class Auth implements AuthInterface{ | ||
apiKey: string | ||
apiSecret: string | ||
export const Auth: AuthConstructor = class Auth implements AuthInterface { | ||
|
||
apiKey: string | ||
apiSecret: string | ||
signature: SignedHashParams | ||
constructor(opts?: AuthOpts) { | ||
// add additional methods to find auth | ||
// also needs to handle private key, signatures, etc | ||
// also needs to handle private key, etc | ||
|
||
this.apiKey = opts?.apiKey || '' | ||
this.apiSecret = opts?.apiSecret || '' | ||
this.signature = opts?.signature || null | ||
} | ||
|
||
getQueryParams(): AuthQueryParams { | ||
return {api_key: this.apiKey, api_secret: this.apiSecret} | ||
getQueryParams = <T>(params: T): AuthQueryParams & T => { | ||
return { api_key: this.apiKey, api_secret: this.apiSecret, ...params } | ||
} | ||
} | ||
|
||
createSignatureHash = <T>(params: T): AuthSignedParams & T => { | ||
let returnParams = Object.assign({ api_key: this.apiKey }, params); | ||
|
||
// Add the current timestamp to the parameters list with the key 'timestamp'. | ||
// This should be an integer containing the number of seconds since the epoch (UNIX time)) | ||
returnParams['timestamp'] = Math.floor(Date.now() / 1000).toString(); | ||
|
||
|
||
// Loop through each of the parameters, sorted by key. | ||
// For every value in the parameter list, replace all instances of & and = with an underscore _. | ||
let keys = Object.keys(returnParams); | ||
let stringifiedParamsforSigning = keys.sort().map((keyName) => { | ||
// Generate a string consisting of &akey=value | ||
return `&${keyName}=${returnParams[keyName].toString().replace(/(&|=)/ig, '_')}` | ||
}, []).join(''); | ||
|
||
// For hash | ||
// Add signature secret to the end of the string, directly after the last value. | ||
// It should now look something like this: '&akey=value&bkey=value${your_signature_secret}' | ||
// Now run the string through an md5 hash function | ||
// convert the resulting bytes to a string of hexadecimal digits. | ||
// This is your MD5 hash signature, | ||
// Should be added to the HTTP parameters of your request as the 'sig' parameter. | ||
if (this.signature.algorithm === AlgorithmTypes.md5hash) { | ||
returnParams['sig'] = createHash('md5').update(stringifiedParamsforSigning + this.signature.secret).digest('hex') | ||
} | ||
|
||
// For HMAC | ||
// Create an HMAC generator with your desired algorithm and your signature secret as the key. | ||
// Now run the string through an hmac generator | ||
// convert the resulting bytes to a string of hexadecimal digits. | ||
// This is your HMAC signature, | ||
// Should be added to the HTTP parameters of your request as the sig parameter | ||
if (this.signature.algorithm === AlgorithmTypes.md5hmac) { | ||
returnParams['sig'] = createHmac('md5', this.signature.secret).update(stringifiedParamsforSigning).digest('hex'); | ||
} | ||
if (this.signature.algorithm === AlgorithmTypes.sha1hmac) { | ||
returnParams['sig'] = createHmac('sha1', this.signature.secret).update(stringifiedParamsforSigning).digest('hex'); | ||
} | ||
if (this.signature.algorithm === AlgorithmTypes.sha256hmac) { | ||
returnParams['sig'] = createHmac('sha256', this.signature.secret).update(stringifiedParamsforSigning).digest('hex'); | ||
} | ||
if (this.signature.algorithm === AlgorithmTypes.sha512hmac) { | ||
returnParams['sig'] = createHmac('sha512', this.signature.secret).update(stringifiedParamsforSigning).digest('hex'); | ||
} | ||
|
||
|
||
|
||
return returnParams; | ||
} | ||
} | ||
|
||
|
||
|
||
// loop through ordered data keys here, | ||
|
||
// keys.on('readable', () => { | ||
// // Only one element is going to be produced by the | ||
// // hash stream. | ||
// const data = input.read(); | ||
// if (data) | ||
// hash.update(data); | ||
// else { | ||
// console.log(`${hash.digest('hex')} ${filename}`); | ||
// } | ||
// }); |
This file was deleted.
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
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,41 @@ | ||
export interface AuthConstructor { | ||
new(opts?: AuthOpts): AuthInterface | ||
} | ||
|
||
// MD5 HASH, MD5 HMAC, SHA1 HMAC, SHA-256 HMAC and SHA-512 HMAC. | ||
export enum AlgorithmTypes { | ||
md5hash = "MD5HASH", | ||
md5hmac = "MD5HMAC", | ||
sha1hmac = "SHA1HMAC", | ||
sha256hmac = "SHA256HMAC", | ||
sha512hmac = "SHA512HMAC" | ||
} | ||
export interface AuthOpts { | ||
apiKey?: string | ||
apiSecret?: string | ||
file?: string, | ||
signature?: SignedHashParams | ||
} | ||
|
||
export interface SignedHashParams { | ||
secret: string, | ||
algorithm: AlgorithmTypes | ||
} | ||
|
||
export interface AuthSignedParams { | ||
api_key: string | ||
sig?: string | ||
} | ||
|
||
export interface AuthQueryParams { | ||
api_key: string | ||
api_secret: string | ||
} | ||
|
||
export interface AuthInterface { | ||
apiKey: string | ||
apiSecret: string | ||
signature?: SignedHashParams | ||
getQueryParams<T>(params: T): AuthQueryParams & T | ||
createSignatureHash<T>(params: T): AuthSignedParams & T | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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