-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sdk-core): migrate middleware auth plugin to ES6 crypto base (#2113
) ## Proposed change feat(sdk-core): migrate middleware auth plugin to ES6 crypto base ## Related issues - 🚀 Feature resolves #2110 <!-- Please make sure to follow the contributing guidelines on https://github.com/amadeus-digital/Otter/blob/main/CONTRIBUTING.md -->
- Loading branch information
Showing
4 changed files
with
66 additions
and
44 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
30 changes: 30 additions & 0 deletions
30
packages/@ama-sdk/core/src/plugins/mgw-mdw-auth/mgw-mdw-auth.helpers.ts
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,30 @@ | ||
/** | ||
* Computes the SHA256 digest of the given string | ||
* @param value Value to hash | ||
*/ | ||
export async function sha256(value: string) { | ||
const utf8 = new TextEncoder().encode(value); | ||
const hashBuffer = await crypto.subtle.digest('SHA-256', utf8); | ||
const hashArray = Array.from(new Uint8Array(hashBuffer)); | ||
const hashHex = hashArray | ||
.map((bytes) => bytes.toString(16).padStart(2, '0')) | ||
.join(''); | ||
return hashHex; | ||
} | ||
|
||
/** | ||
* Generates hash-based message authentication code using cryptographic hash function HmacSHA256 and the provided | ||
* secret key | ||
* Should only be in a NodeJS MDW context | ||
* @param value Value to hash | ||
* @param secretKey Secret cryptographic key | ||
*/ | ||
export async function hmacSHA256(value: string, secretKey: string) { | ||
const enc = new TextEncoder(); | ||
const algorithm = { name: 'HMAC', hash: 'SHA-256' }; | ||
|
||
const key = await crypto.subtle.importKey('raw', enc.encode(secretKey), algorithm, false, ['sign', 'verify']); | ||
const signature = await crypto.subtle.sign(algorithm.name, key, enc.encode(value)); | ||
const digest = btoa(String.fromCharCode(...new Uint8Array(signature))); | ||
return digest; | ||
} |
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