This repository has been archived by the owner on Apr 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 390
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] - Function to authenticate flow requests
- Loading branch information
1 parent
b583318
commit 09b8469
Showing
4 changed files
with
108 additions
and
0 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,11 @@ | ||
import {ConfigInterface} from '../base-types'; | ||
|
||
import {validateFactory} from './validate'; | ||
|
||
export function shopifyFlow(config: ConfigInterface) { | ||
return { | ||
validate: validateFactory(config), | ||
}; | ||
} | ||
|
||
export type ShopifyFlow = ReturnType<typeof shopifyFlow>; |
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,20 @@ | ||
import {AdapterArgs} from '../../runtime/types'; | ||
|
||
export interface FlowValidateParams extends AdapterArgs { | ||
rawBody: string; | ||
} | ||
|
||
export enum FlowValidationErrorReason { | ||
MissingBody = 'missing_body', | ||
MissingHmac = 'missing_hmac', | ||
InvalidHmac = 'invalid_hmac', | ||
} | ||
|
||
export interface FlowValidationInvalid { | ||
valid: false; | ||
reason: FlowValidationErrorReason; | ||
} | ||
|
||
export interface FlowValidationValid { | ||
valid: true; | ||
} |
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,74 @@ | ||
import {abstractConvertRequest, getHeader} from '../../runtime/http'; | ||
import {createSHA256HMAC} from '../../runtime/crypto'; | ||
import {HashFormat} from '../../runtime/crypto/types'; | ||
import {ConfigInterface} from '../base-types'; | ||
import {safeCompare} from '../auth/oauth/safe-compare'; | ||
import {logger} from '../logger'; | ||
|
||
import { | ||
FlowValidateParams, | ||
FlowValidationInvalid, | ||
FlowValidationValid, | ||
FlowValidationErrorReason, | ||
} from './types'; | ||
|
||
export function validateFactory(config: ConfigInterface) { | ||
return async function validate({ | ||
rawBody, | ||
...adapterArgs | ||
}: FlowValidateParams): Promise<FlowValidationInvalid | FlowValidationValid> { | ||
const request = await abstractConvertRequest(adapterArgs); | ||
|
||
if (!rawBody.length) { | ||
fail(FlowValidationErrorReason.MissingBody, config); | ||
} | ||
|
||
const hmac = getHeader(request.headers, 'hmac'); | ||
|
||
if (!hmac) { | ||
return fail(FlowValidationErrorReason.MissingHmac, config); | ||
} | ||
|
||
if (await hmacIsValid(config.apiSecretKey, rawBody, hmac)) { | ||
return succeed(config); | ||
} | ||
|
||
return fail(FlowValidationErrorReason.InvalidHmac, config); | ||
}; | ||
} | ||
|
||
async function fail( | ||
reason: FlowValidationErrorReason, | ||
config: ConfigInterface, | ||
): Promise<FlowValidationInvalid> { | ||
const log = logger(config); | ||
await log.debug('Flow request is not valid'); | ||
|
||
return { | ||
valid: false, | ||
reason, | ||
}; | ||
} | ||
|
||
async function succeed(config: ConfigInterface): Promise<FlowValidationValid> { | ||
const log = logger(config); | ||
await log.debug('Flow request is not valid'); | ||
|
||
return { | ||
valid: true, | ||
}; | ||
} | ||
|
||
async function hmacIsValid( | ||
secret: string, | ||
rawBody: string, | ||
hmac: string, | ||
): Promise<boolean> { | ||
const generatedHash = await createSHA256HMAC( | ||
secret, | ||
rawBody, | ||
HashFormat.Base64, | ||
); | ||
|
||
return safeCompare(generatedHash, hmac); | ||
} |
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