Skip to content

Commit

Permalink
Use Cognito JWT to extract auth data
Browse files Browse the repository at this point in the history
  • Loading branch information
max-konin committed May 17, 2023
1 parent 5e6f266 commit 739e27b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
2 changes: 2 additions & 0 deletions packages/server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export async function createServer({ defaultQuery, lambdaHandler, port, schema,
try { prismaAppSyncHeader = JSON.parse(request?.headers?.['x-prisma-appsync']) }
catch { prismaAppSyncHeader = {} }

const authorizationHeader = request?.headers?.authorization
const authorization = prismaAppSyncHeader?.authorization || Authorizations.AWS_IAM || null
const signature = prismaAppSyncHeader?.signature || {}

Expand All @@ -102,6 +103,7 @@ export async function createServer({ defaultQuery, lambdaHandler, port, schema,
username: 'johndoe',
sub: 'xxxxxx',
resolverContext: {},
jwt: authorizationHeader,
},
...signature,
})
Expand Down
8 changes: 5 additions & 3 deletions packages/server/src/utils/useLambdaIdentity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ export default function useLambdaIdentity(identity: Authorization, opts?: mockOp
return mock
}
else if (identity === Authorizations.AMAZON_COGNITO_USER_POOLS) {
const decodedJWTToken = opts?.jwt ? JSON.parse(Buffer.from(opts?.jwt?.split('.')[1], 'base64').toString()) : {}
const mock: AMAZON_COGNITO_USER_POOLS = {
sub: opts?.sub || 'undefined',
sub: decodedJWTToken?.sub || 'undefined',
issuer: 'string',
username: opts?.username || 'undefined',
claims: {},
username: decodedJWTToken?.['cognito:username'] || 'undefined',
claims: Object.fromEntries(Object.entries(decodedJWTToken).filter(([key]) => key.includes('custom:'))),
sourceIp: [opts?.sourceIp || 'undefined'],
defaultAuthStrategy: 'string',
groups: ['admin', 'member'],
Expand Down Expand Up @@ -70,4 +71,5 @@ interface mockOptions {
username: string
sourceIp: string
resolverContext: any
jwt: string
}

0 comments on commit 739e27b

Please sign in to comment.