Skip to content

Commit

Permalink
feat: add an helper to decode the JWT
Browse files Browse the repository at this point in the history
  • Loading branch information
marcomontalbano committed Mar 21, 2024
1 parent 5da8de6 commit ad98ce2
Show file tree
Hide file tree
Showing 8 changed files with 483 additions and 38 deletions.
8 changes: 7 additions & 1 deletion examples/bun/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { authenticate, AuthenticateOptions, GrantType } from '@commercelayer/js-auth'
import { authenticate, AuthenticateOptions, GrantType, jwtDecode, jwtIsSalesChannel } from '@commercelayer/js-auth'

const grantType: GrantType = 'client_credentials'

Expand All @@ -10,3 +10,9 @@ const options: AuthenticateOptions<'client_credentials'> = {
const auth = await authenticate(grantType, options)

console.log(auth)

const parsedJWT = jwtDecode(auth.accessToken)

if (jwtIsSalesChannel(parsedJWT.payload)) {
console.log(parsedJWT.payload)
}
20 changes: 11 additions & 9 deletions examples/esm/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
// @ts-check

import { authenticate } from '@commercelayer/js-auth'
import { authenticate, jwtDecode, jwtIsSalesChannel } from '@commercelayer/js-auth'

async function run() {
const auth = await authenticate('client_credentials', {
clientId: 'BISG8bb3GWpC8_D7Nt1SuWWdieS5bJq831A50LgB_Ig',
scope: 'market:id:KoaJYhMVVj'
})
const auth = await authenticate('client_credentials', {
clientId: 'BISG8bb3GWpC8_D7Nt1SuWWdieS5bJq831A50LgB_Ig',
scope: 'stock_location:id:DGzAouppwn'
})

console.log(auth)
}
console.log(auth)

const parsedJWT = jwtDecode(auth.accessToken)

run()
if (jwtIsSalesChannel(parsedJWT.payload)) {
console.log(parsedJWT.payload)
}
File renamed without changes.
24 changes: 0 additions & 24 deletions packages/js-auth/specs/provisioning.spec.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { authenticate } from '../src/index.js'
import { authenticate } from './index.js'

const clientId = process.env.VITE_TEST_CLIENT_ID
const integrationClientId = process.env.VITE_TEST_INTEGRATION_CLIENT_ID
Expand All @@ -8,12 +8,13 @@ const scope = process.env.VITE_TEST_SCOPE
const username = process.env.VITE_TEST_USERNAME
const password = process.env.VITE_TEST_PASSWORD

describe('Authentication', () => {
describe('Organization auth', () => {
it('Get a sales channel token', async () => {
const res = await authenticate('client_credentials', {
clientId,
domain
})

expect(res).toHaveProperty('accessToken')
expect(res).toHaveProperty('tokenType')
expect(res).toHaveProperty('expiresIn')
Expand All @@ -23,12 +24,14 @@ describe('Authentication', () => {
expect(res.expires).toBeInstanceOf(Date)
expect(res.expires.getTime()).toBeGreaterThan(Date.now())
})

it('Get an error requesting a sales channel token', async () => {
const res = await authenticate('client_credentials', {
clientId: 'wrong-client-id',
domain,
scope
})

expect(res).toHaveProperty('errors')
expect(res.errors).toBeInstanceOf(Array)
expect(res.errors?.[0]).toMatchObject({
Expand All @@ -38,24 +41,28 @@ describe('Authentication', () => {
status: 401,
title: 'invalid_client'
})

expect(res).not.toHaveProperty('accessToken')
expect(res).not.toHaveProperty('tokenType')
expect(res).not.toHaveProperty('expiresIn')
expect(res).not.toHaveProperty('scope')
expect(res).not.toHaveProperty('createdAt')
})

it('Get a integration token', async () => {
const res = await authenticate('client_credentials', {
clientId: integrationClientId,
clientSecret,
domain
})

expect(res).toHaveProperty('accessToken')
expect(res).toHaveProperty('tokenType')
expect(res).toHaveProperty('expiresIn')
expect(res).toHaveProperty('scope')
expect(res).toHaveProperty('createdAt')
})

it('Get a customer token', async () => {
const res = await authenticate('password', {
clientId,
Expand All @@ -64,6 +71,7 @@ describe('Authentication', () => {
password,
scope
})

expect(res).toHaveProperty('accessToken')
expect(res).toHaveProperty('tokenType')
expect(res).toHaveProperty('expiresIn')
Expand All @@ -73,6 +81,7 @@ describe('Authentication', () => {
expect(res).toHaveProperty('ownerType')
expect(res).toHaveProperty('refreshToken')
})

it('Refresh a customer token', async () => {
const res = await authenticate('password', {
clientId,
Expand All @@ -81,6 +90,7 @@ describe('Authentication', () => {
password,
scope
})

expect(res).toHaveProperty('accessToken')
expect(res).toHaveProperty('tokenType')
expect(res).toHaveProperty('expiresIn')
Expand All @@ -89,12 +99,14 @@ describe('Authentication', () => {
expect(res).toHaveProperty('ownerId')
expect(res).toHaveProperty('ownerType')
expect(res).toHaveProperty('refreshToken')

const res2 = await authenticate('refresh_token', {
clientId,
domain,
refreshToken: res.refreshToken,
scope
})

expect(res2).toHaveProperty('accessToken')
expect(res2).toHaveProperty('tokenType')
expect(res2).toHaveProperty('expiresIn')
Expand All @@ -104,6 +116,7 @@ describe('Authentication', () => {
expect(res2).toHaveProperty('ownerType')
expect(res2).toHaveProperty('refreshToken')
})

it('Set a custom header', async () => {
const res = await authenticate('password', {
clientId,
Expand All @@ -115,6 +128,7 @@ describe('Authentication', () => {
'X-My-Header': 'My-Value'
}
})

expect(res).toHaveProperty('accessToken')
expect(res).toHaveProperty('tokenType')
expect(res).toHaveProperty('expiresIn')
Expand All @@ -125,3 +139,22 @@ describe('Authentication', () => {
expect(res).toHaveProperty('refreshToken')
})
})

describe('Provisioning auth', () => {
it('Get a provisioning token', async () => {
const res = await authenticate('client_credentials', {
domain: process.env.VITE_TEST_PROVISIONING_DOMAIN,
clientId: process.env.VITE_TEST_PROVISIONING_CLIENT_ID,
clientSecret: process.env.VITE_TEST_PROVISIONING_CLIENT_SECRET
})

expect(res).toHaveProperty('accessToken')
expect(res).toHaveProperty('tokenType')
expect(res).toHaveProperty('expiresIn')
expect(res).toHaveProperty('scope')
expect(res).toHaveProperty('createdAt')
expect(res).toHaveProperty('expires')
expect(res.expires).toBeInstanceOf(Date)
expect(res.expires.getTime()).toBeGreaterThan(Date.now())
})
})
12 changes: 10 additions & 2 deletions packages/js-auth/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
export { authenticate } from './authenticate.js'
export {
jwtDecode,
jwtIsDashboard,
jwtIsIntegration,
jwtIsProvisioning,
jwtIsSalesChannel,
jwtIsWebApp
} from './jwtDecode.js'

export type {
GrantType,
AuthenticateOptions,
AuthenticateReturn
AuthenticateReturn,
GrantType
} from './types/index.js'
Loading

0 comments on commit ad98ce2

Please sign in to comment.