Skip to content

Commit

Permalink
chore: add test for getClaims
Browse files Browse the repository at this point in the history
  • Loading branch information
kangmingtay committed Feb 6, 2025
1 parent 98f166b commit 7f788d3
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 7 deletions.
45 changes: 38 additions & 7 deletions infra/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ services:
GOTRUE_SMTP_ADMIN_EMAIL: admin@email.com
GOTRUE_MAILER_SUBJECTS_CONFIRMATION: 'Please confirm'
GOTRUE_EXTERNAL_PHONE_ENABLED: 'true'
GOTRUE_SMS_PROVIDER: "twilio"
GOTRUE_SMS_TWILIO_ACCOUNT_SID: "${GOTRUE_SMS_TWILIO_ACCOUNT_SID}"
GOTRUE_SMS_TWILIO_AUTH_TOKEN: "${GOTRUE_SMS_TWILIO_AUTH_TOKEN}"
GOTRUE_SMS_TWILIO_MESSAGE_SERVICE_SID: "${GOTRUE_SMS_TWILIO_MESSAGE_SERVICE_SID}"
GOTRUE_SMS_PROVIDER: 'twilio'
GOTRUE_SMS_TWILIO_ACCOUNT_SID: '${GOTRUE_SMS_TWILIO_ACCOUNT_SID}'
GOTRUE_SMS_TWILIO_AUTH_TOKEN: '${GOTRUE_SMS_TWILIO_AUTH_TOKEN}'
GOTRUE_SMS_TWILIO_MESSAGE_SERVICE_SID: '${GOTRUE_SMS_TWILIO_MESSAGE_SERVICE_SID}'
GOTRUE_SMS_AUTOCONFIRM: 'false'
GOTRUE_COOKIE_KEY: "sb"
GOTRUE_COOKIE_KEY: 'sb'
depends_on:
- db
restart: on-failure
Expand All @@ -47,6 +47,7 @@ services:
- '9998:9998'
environment:
GOTRUE_JWT_SECRET: '37c304f8-51aa-419a-a1af-06154e63707a'
GOTRUE_JWT_KEYS: '[{"kty":"oct","k":"Z7-AyPyChGNcQsX16cPBV-pPBo4q-zckDxkq1VZjATo","kid":"12580317-221c-49b6-894a-f4473b8afe39","key_ops":["sign", "verify"],"alg":"HS256"},{"kty":"RSA","n":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jeng5Hk4kJHnXUtjls6VKEfj5DaiaKj2YgdI_-oEsf3ylUu9yLxloYjN4BVvgzFiBtiJzI3exyOEmzsqj1Bhe1guiGkvcvMj2nJ0fP9e1zNKM5UfPHQMjOh3tigXCLst0-_JZT55BnbNuw1YAytiFSU2_755xoLR-U","dq":"dCP7V-bJ6p1X_FLpOGau9wy262OKi_0_4mj-Mk-Q1tUhGRg4jeEdQRDdc6lN7Rilz-ZZGkVs2FGkD0MVd3PisXYmk2m6pfMhoe0K-WxkNy8Ce7Vq99jLVwgHMIenyS6zZjMTRYAZgPSShu2fVe-rU2VVLyz7r5RpzOzuibRIVfE","qi":"i7ND2teiVLkbaAs6rHfo5DiD1nlsORNYnn8Y_FjF6utb5OUljZ6-5WyEDJN9oIUX8o_Il9E6js-z7nhvPfFZHQN7ZWuYI0rO5qmsCDS9jWJ4GR61SgzZuLT7Jpp_KtwjW70x5wZ1Y-GugOP1Wct1YZWHn5YyLhvO6X_vttSmcS0","kid":"638c54b8-28c2-4b12-9598-ba12ef610a29","key_ops":["verify"],"alg":"RS256"}]'
GOTRUE_JWT_EXP: 3600
GOTRUE_DB_DRIVER: postgres
DB_NAMESPACE: auth
Expand All @@ -66,7 +67,37 @@ services:
GOTRUE_SMTP_USER: GOTRUE_SMTP_USER
GOTRUE_SMTP_PASS: GOTRUE_SMTP_PASS
GOTRUE_SMTP_ADMIN_EMAIL: admin@email.com
GOTRUE_COOKIE_KEY: "sb"
GOTRUE_COOKIE_KEY: 'sb'
depends_on:
- db
restart: on-failure
autoconfirm_with_asymmetric_keys: # Signup enabled, autoconfirm on
image: supabase/auth:v2.169.0
ports:
- '9996:9996'
environment:
GOTRUE_JWT_SECRET: 'Z7-AyPyChGNcQsX16cPBV-pPBo4q-zckDxkq1VZjATo'
GOTRUE_JWT_KEYS: '[{"kty":"oct","k":"Z7-AyPyChGNcQsX16cPBV-pPBo4q-zckDxkq1VZjATo","kid":"12580317-221c-49b6-894a-f4473b8afe39","key_ops":["verify"],"al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0K3VM","dp":"KJ4LHcQnAjeng5Hk4kJHnXUtjls6VKEfj5DaiaKj2YgdI_-oEsf3ylUu9yLxloYjN4BVvgzFiBtiJzI3exyOEmzsqj1Bhe1guiGkvcvMj2nJ0fP9e1zNKM5UfPHQMjOh3tigXCLst0-_JZT55BnbNuw1YAytiFSU2_755xoLR-U","dq":"dCP7V-bJ6p1X_FLpOGau9wy262OKi_0_4mj-Mk-Q1tUhGRg4jeEdQRDdc6lN7Rilz-ZZGkVs2FGkD0MVd3PisXYmk2m6pfMhoe0K-WxkNy8Ce7Vq99jLVwgHMIenyS6zZjMTRYAZgPSShu2fVe-rU2VVLyz7r5RpzOzuibRIVfE","qi":"i7ND2teiVLkbaAs6rHfo5DiD1nlsORNYnn8Y_FjF6utb5OUljZ6-5WyEDJN9oIUX8o_Il9E6js-z7nhvPfFZHQN7ZWuYI0rO5qmsCDS9jWJ4GR61SgzZuLT7Jpp_KtwjW70x5wZ1Y-GugOP1Wct1YZWHn5YyLhvO6X_vttSmcS0","kid":"638c54b8-28c2-4b12-9598-ba12ef610a29","key_ops":["sign","verify"],"alg":"RS256"}]'
GOTRUE_JWT_EXP: 3600
GOTRUE_DB_DRIVER: postgres
DB_NAMESPACE: auth
GOTRUE_API_HOST: 0.0.0.0
PORT: 9996
GOTRUE_DISABLE_SIGNUP: 'false'
API_EXTERNAL_URL: http://localhost:9996
GOTRUE_SITE_URL: http://localhost:9996
GOTRUE_MAILER_AUTOCONFIRM: 'true'
GOTRUE_SMS_AUTOCONFIRM: 'true'
GOTRUE_LOG_LEVEL: DEBUG
GOTRUE_OPERATOR_TOKEN: super-secret-operator-token
DATABASE_URL: 'postgres://postgres:postgres@db:5432/postgres?sslmode=disable'
GOTRUE_EXTERNAL_PHONE_ENABLED: 'true'
GOTRUE_SMTP_HOST: mail
GOTRUE_SMTP_PORT: 2500
GOTRUE_SMTP_USER: GOTRUE_SMTP_USER
GOTRUE_SMTP_PASS: GOTRUE_SMTP_PASS
GOTRUE_SMTP_ADMIN_EMAIL: admin@email.com
GOTRUE_COOKIE_KEY: 'sb'
depends_on:
- db
restart: on-failure
Expand Down Expand Up @@ -95,7 +126,7 @@ services:
GOTRUE_SMTP_USER: GOTRUE_SMTP_USER
GOTRUE_SMTP_PASS: GOTRUE_SMTP_PASS
GOTRUE_SMTP_ADMIN_EMAIL: admin@email.com
GOTRUE_COOKIE_KEY: "sb"
GOTRUE_COOKIE_KEY: 'sb'
depends_on:
- db
restart: on-failure
Expand Down
65 changes: 65 additions & 0 deletions test/GoTrueClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ import GoTrueClient from '../src/GoTrueClient'
import {
authClient as auth,
authClientWithSession as authWithSession,
authClientWithAsymmetricSession as authWithAsymmetricSession,
authSubscriptionClient,
clientApiAutoConfirmOffSignupsEnabledClient as phoneClient,
clientApiAutoConfirmDisabledClient as signUpDisabledClient,
clientApiAutoConfirmEnabledClient as signUpEnabledClient,
authAdminApiAutoConfirmEnabledClient,
GOTRUE_URL_SIGNUP_ENABLED_AUTO_CONFIRM_ON,
authClient,
authClientWithAsymmetricSession,
GOTRUE_URL_SIGNUP_ENABLED_ASYMMETRIC_AUTO_CONFIRM_ON,
} from './lib/clients'
import { mockUserCredentials } from './lib/utils'
import { Session } from '../src'
Expand Down Expand Up @@ -918,6 +922,67 @@ describe('MFA', () => {
})
})

describe('getClaims', () => {
test('getClaims returns nothing if there is no session present', async () => {
const { data, error } = await authClient.getClaims()
expect(data).toBeNull()
expect(error).toBeNull()
})

test('getClaims calls getUser if symmetric jwt is present', async () => {
const { email, password } = mockUserCredentials()
jest.spyOn(authWithSession, 'getUser')
const {
data: { user },
error: initialError,
} = await authWithSession.signUp({
email,
password,
})
expect(initialError).toBeNull()
expect(user).not.toBeNull()

const { data, error } = await authWithSession.getClaims()
expect(error).toBeNull()
expect(data?.claims.email).toEqual(user?.email)
expect(authWithSession.getUser).toHaveBeenCalled()
})

test('getClaims fetches JWKS to verify asymmetric jwt', async () => {
const fetchedUrls: any[] = []
const fetchedResponse: any[] = []

// override fetch to inspect fetchJwk called within getClaims
authWithAsymmetricSession['fetch'] = async (url: RequestInfo | URL, options = {}) => {
fetchedUrls.push(url)
const response = await globalThis.fetch(url, options)
const clonedResponse = response.clone()
fetchedResponse.push(await clonedResponse.json())
return response
}
const { email, password } = mockUserCredentials()
const {
data: { user },
error: initialError,
} = await authWithAsymmetricSession.signUp({
email,
password,
})
expect(initialError).toBeNull()
expect(user).not.toBeNull()

const { data, error } = await authWithAsymmetricSession.getClaims()
expect(error).toBeNull()
expect(data?.claims.email).toEqual(user?.email)
expect(fetchedUrls).toContain(
GOTRUE_URL_SIGNUP_ENABLED_ASYMMETRIC_AUTO_CONFIRM_ON + '/.well-known/jwks.json'
)

// contains the response for getSession and fetchJwk
expect(fetchedResponse).toHaveLength(2)
})
})

describe('GoTrueClient with storageisServer = true', () => {
const originalWarn = console.warn
let warnings: any[][] = []
Expand Down
9 changes: 9 additions & 0 deletions test/lib/clients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ export const SIGNUP_ENABLED_AUTO_CONFIRM_OFF_PORT = 9999

export const SIGNUP_ENABLED_AUTO_CONFIRM_ON_PORT = 9998
export const SIGNUP_DISABLED_AUTO_CONFIRM_OFF_PORT = 9997
export const SIGNUP_ENABLED_ASYMMETRIC_AUTO_CONFIRM_ON_PORT = 9996

export const GOTRUE_URL_SIGNUP_ENABLED_AUTO_CONFIRM_OFF = `http://localhost:${SIGNUP_ENABLED_AUTO_CONFIRM_OFF_PORT}`
export const GOTRUE_URL_SIGNUP_ENABLED_AUTO_CONFIRM_ON = `http://localhost:${SIGNUP_ENABLED_AUTO_CONFIRM_ON_PORT}`
export const GOTRUE_URL_SIGNUP_ENABLED_ASYMMETRIC_AUTO_CONFIRM_ON = `http://localhost:${SIGNUP_ENABLED_ASYMMETRIC_AUTO_CONFIRM_ON_PORT}`
export const GOTRUE_URL_SIGNUP_DISABLED_AUTO_CONFIRM_OFF = `http://localhost:${SIGNUP_DISABLED_AUTO_CONFIRM_OFF_PORT}`

export const GOTRUE_JWT_SECRET = '37c304f8-51aa-419a-a1af-06154e63707a'
Expand Down Expand Up @@ -50,6 +52,13 @@ export const authClientWithSession = new GoTrueClient({
storage: new MemoryStorage(),
})

export const authClientWithAsymmetricSession = new GoTrueClient({
url: GOTRUE_URL_SIGNUP_ENABLED_ASYMMETRIC_AUTO_CONFIRM_ON,
autoRefreshToken: false,
persistSession: true,
storage: new MemoryStorage(),
})

export const authSubscriptionClient = new GoTrueClient({
url: GOTRUE_URL_SIGNUP_ENABLED_AUTO_CONFIRM_ON,
autoRefreshToken: false,
Expand Down

0 comments on commit 7f788d3

Please sign in to comment.