Skip to content

Commit

Permalink
fix(auth): add signed requests and req updates
Browse files Browse the repository at this point in the history
  • Loading branch information
kellyjandrews committed Mar 7, 2022
1 parent a845782 commit 887a776
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 34 deletions.
87 changes: 78 additions & 9 deletions packages/auth/lib/auth.ts
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}`);
// }
// });
20 changes: 0 additions & 20 deletions packages/auth/lib/common.ts

This file was deleted.

6 changes: 5 additions & 1 deletion packages/auth/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ export {
AuthInterface,
AuthQueryParams,
AuthOpts,
} from './common'
AlgorithmTypes,
SignedHashParams,
AuthSignedParams

} from './types'

export { Auth };
41 changes: 41 additions & 0 deletions packages/auth/lib/types.ts
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
}
29 changes: 29 additions & 0 deletions packages/auth/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 11 additions & 4 deletions packages/auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,18 @@
"prepare": "npm run build",
"test-watch": "../../node_modules/.bin/jest --watch"
},
"devDependencies": {
"typescript": "^4.3.5"
},
"bugs": {
"url": "https://github.com/Vonage/vonage-node-sdk/issues"
},
"gitHead": "3d01d2557edea29b54610d7ff7b07d186affb0f8"
"gitHead": "3d01d2557edea29b54610d7ff7b07d186affb0f8",
"dependencies": {
"crypto": "^1.0.1"
},
"devDependencies": {
"@types/jest": "^27.4.0",
"@types/node": "^17.0.21",
"nock": "^13.2.4",
"ts-jest": "^27.1.3",
"typescript": "^4.3.5"
}
}

0 comments on commit 887a776

Please sign in to comment.