Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use auth data from Cognito JWT #143

Merged
merged 3 commits into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: decodedJWTToken,
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
}
2 changes: 1 addition & 1 deletion scripts/test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ await $`npx prisma generate --schema tests/prisma/schema.prisma`

// unit tests
console.log(chalk.blue('\nTest :: Client\n'))
max-konin marked this conversation as resolved.
Show resolved Hide resolved
await $`vitest run tests/**/*.spec.ts`
await $`vitest run tests`
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed this one because tests/**/*.spec.ts does not run tests in subfolders. Vitest already run files match .spec.ts pattern


50 changes: 50 additions & 0 deletions tests/server/utils/useLambdaIdentity.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import useLambdaIdentity from '@appsync-server/utils/useLambdaIdentity'
import { Authorizations } from '@client'
import { describe, expect, it } from 'vitest'

const toBase64 = (str: string) => Buffer.from(str).toString('base64')

describe('Server | Utils', () => {
describe('#useLambdaIdentity', () => {
describe('when the identity equals to AMAZON_COGNITO_USER_POOLS', () => {
it('should extract data from JWT token', () => {
const header = {
alg: 'RS256',
}
const data = {
'sub': 'sub',
'email_verified': true,
'iss': 'https://cognito-idp.us-east-1.amazonaws.com/user_pool_id',
'cognito:username': 'username',
'origin_jti': 'origin_jti',
'custom:variable': 'custom variable',
'aud': 'aud',
'event_id': 'event_id',
'token_use': 'id',
'auth_time': 1683789834,
'exp': 1684737597,
'iat': 1684733997,
'jti': 'jti',
'email': 'user@example.com',
}
const token = `${toBase64(JSON.stringify(header))}.${toBase64(JSON.stringify(data))}.fakesignature`

expect(useLambdaIdentity(Authorizations.AMAZON_COGNITO_USER_POOLS, {
jwt: token,
sub: 'mockSub',
sourceIp: 'sourceId',
username: 'mockUsername',
resolverContext: {},
})).toEqual({
sub: data.sub,
issuer: 'string',
username: data['cognito:username'],
claims: data,
sourceIp: ['sourceId'],
defaultAuthStrategy: 'string',
groups: ['admin', 'member'],
})
})
})
})
})