-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b0043a2
commit 5a05381
Showing
9 changed files
with
953 additions
and
71 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
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,48 @@ | ||
import { GetParameterCommand, SSMClient } from '@aws-sdk/client-ssm'; | ||
import { awsConfig } from '@modules/aws/config'; | ||
import type { Stage } from '@modules/stage'; | ||
import { getIdentityIdSchema } from './schemas'; | ||
|
||
export async function getIdentityClientAccessToken() { | ||
const ssmClient = new SSMClient(awsConfig); | ||
|
||
const params = { | ||
Name: '/CODE/support/press-reader-entitlements/identity-client-access-token', | ||
WithDecryption: true, | ||
}; | ||
|
||
const command = new GetParameterCommand(params); | ||
|
||
const response = await ssmClient.send(command); | ||
return response.Parameter?.Value; | ||
} | ||
|
||
export async function getIdentityId(stage: Stage, userId: string) { | ||
const identityHost = | ||
stage === 'CODE' | ||
? 'https://idapi.code.dev-theguardian.com' | ||
: 'https://idapi.theguardian.com'; | ||
const clientAccessToken = await getIdentityClientAccessToken(); | ||
if (clientAccessToken == undefined) { | ||
throw new Error('Client access token not found'); | ||
} | ||
const response = await fetch(`${identityHost}/user/braze-uuid/${userId}`, { | ||
headers: { | ||
'X-GU-ID-Client-Access-Token': `Bearer ${clientAccessToken}`, | ||
}, | ||
method: 'GET', | ||
}) | ||
.then(async (res) => { | ||
const json = await res.json(); | ||
console.log(`Identity returned ${JSON.stringify(json)}`); | ||
return json; | ||
}) | ||
.then((json) => getIdentityIdSchema.parse(json)); | ||
|
||
if (response.status === 'ok') { | ||
return response.id; | ||
} | ||
throw new Error( | ||
`Failed to get identity id because of ${JSON.stringify(response)}`, | ||
); | ||
} |
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,21 @@ | ||
import { z } from 'zod'; | ||
|
||
const ok = z.literal('ok'); | ||
const error = z.literal('error'); | ||
|
||
const successfulGetIdentityIdResponse = z.object({ | ||
status: ok, | ||
id: z.string(), | ||
}); | ||
|
||
const failedGetIdentityIdResponse = z.object({ | ||
status: error, | ||
errors: z.array(z.object({ message: z.string(), description: z.string() })), | ||
}); | ||
|
||
export const getIdentityIdSchema = z.discriminatedUnion('status', [ | ||
successfulGetIdentityIdResponse, | ||
failedGetIdentityIdResponse, | ||
]); | ||
|
||
export type GetIdentityIdResponse = z.infer<typeof getIdentityIdSchema>; |
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
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
Oops, something went wrong.