Skip to content

Commit

Permalink
Update crypto statement to fix vite issue
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoocasali committed May 14, 2024
1 parent 2f37b74 commit 27480d2
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ tenant_token_guide_generate_sdk_1: |-
const apiKeyUid = '85c3c2f9-bdd6-41f1-abd8-11fcf80e0f76'
const expiresAt = new Date('2025-12-20') // optional
const token = client.generateTenantToken(apiKeyUid, searchRules, {
const token = await client.generateTenantToken(apiKeyUid, searchRules, {
apiKey: apiKey,
expiresAt: expiresAt,
})
Expand Down
6 changes: 3 additions & 3 deletions src/clients/node-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ class MeiliSearch extends Client {
* @param options - Token options to customize some aspect of the token.
* @returns The token in JWT format.
*/
generateTenantToken(
async generateTenantToken(
apiKeyUid: string,
searchRules: TokenSearchRules,
options?: TokenOptions
): string {
): Promise<string> {
if (typeof window === 'undefined') {
return this.tokens.generateTenantToken(apiKeyUid, searchRules, options)
return await this.tokens.generateTenantToken(apiKeyUid, searchRules, options)
}
return super.generateTenantToken(apiKeyUid, searchRules, options)
}
Expand Down
11 changes: 6 additions & 5 deletions src/token.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Config, TokenSearchRules, TokenOptions } from './types'
import { createHmac } from 'crypto'
import { MeiliSearchError } from './errors'
import { validateUuid4 } from './utils'

Expand All @@ -15,7 +14,9 @@ function encode64(data: any) {
* @param encodedPayload - Payload of the token in base64.
* @returns The signature of the token in base64.
*/
function sign(apiKey: string, encodedHeader: string, encodedPayload: string) {
async function sign(apiKey: string, encodedHeader: string, encodedPayload: string) {
const { createHmac } = await import('crypto')

return createHmac('sha256', apiKey)
.update(`${encodedHeader}.${encodedPayload}`)
.digest('base64')
Expand Down Expand Up @@ -132,11 +133,11 @@ class Token {
* @param options - Token options to customize some aspect of the token.
* @returns The token in JWT format.
*/
generateTenantToken(
async generateTenantToken(
apiKeyUid: string,
searchRules: TokenSearchRules,
options?: TokenOptions
): string {
): Promise<string> {
const apiKey = options?.apiKey || this.config.apiKey || ''
const uid = apiKeyUid || ''
const expiresAt = options?.expiresAt
Expand All @@ -145,7 +146,7 @@ class Token {

const encodedHeader = createHeader()
const encodedPayload = createPayload({ searchRules, uid, expiresAt })
const signature = sign(apiKey, encodedHeader, encodedPayload)
const signature = await sign(apiKey, encodedHeader, encodedPayload)

return `${encodedHeader}.${encodedPayload}.${signature}`
}
Expand Down
29 changes: 15 additions & 14 deletions tests/token.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
config,
HOST,
} from './utils/meilisearch-test-utils'
import { createHmac } from 'crypto'
import crypto from 'crypto'
import MeiliSearch from '../src'
import { MeiliSearchError } from '../src/errors'

Expand Down Expand Up @@ -44,7 +44,7 @@ describe.each([{ permission: 'Admin' }])(
const client = await getClient(permission)
const apiKey = await getKey(permission)
const { uid } = await client.getKey(apiKey)
const token = client.generateTenantToken(uid, [], {})
const token = await client.generateTenantToken(uid, [], {})
const [header64] = token.split('.')

// header
Expand All @@ -57,11 +57,12 @@ describe.each([{ permission: 'Admin' }])(
const client = await getClient(permission)
const apiKey = await getKey(permission)
const { uid } = await client.getKey(apiKey)
const token = client.generateTenantToken(uid, [], {})
const token = await client.generateTenantToken(uid, [], {})
const [header64, payload64, signature64] = token.split('.')

// signature
const newSignature = createHmac('sha256', apiKey)
const newSignature = crypto
.createHmac('sha256', apiKey)
.update(`${header64}.${payload64}`)
.digest('base64')
.replace(/\+/g, '-')
Expand All @@ -75,7 +76,7 @@ describe.each([{ permission: 'Admin' }])(
const client = await getClient(permission)
const apiKey = await getKey(permission)
const { uid } = await client.getKey(apiKey)
const token = client.generateTenantToken(uid, [], {})
const token = await client.generateTenantToken(uid, [], {})
const [_, payload64] = token.split('.')

// payload
Expand All @@ -90,7 +91,7 @@ describe.each([{ permission: 'Admin' }])(
const client = await getClient(permission)
const apiKey = await getKey(permission)
const { uid } = await client.getKey(apiKey)
const token = client.generateTenantToken(uid, [UID])
const token = await client.generateTenantToken(uid, [UID])
const [_, payload64] = token.split('.')

// payload
Expand All @@ -105,7 +106,7 @@ describe.each([{ permission: 'Admin' }])(
const client = await getClient(permission)
const apiKey = await getKey(permission)
const { uid } = await client.getKey(apiKey)
const token = client.generateTenantToken(uid, { [UID]: {} })
const token = await client.generateTenantToken(uid, { [UID]: {} })
const [_, payload64] = token.split('.')

// payload
Expand All @@ -120,7 +121,7 @@ describe.each([{ permission: 'Admin' }])(
const apiKey = await getKey(permission)
const { uid } = await client.getKey(apiKey)

const token = client.generateTenantToken(uid, ['*'])
const token = await client.generateTenantToken(uid, ['*'])

const searchClient = new MeiliSearch({ host: HOST, apiKey: token })

Expand All @@ -137,7 +138,7 @@ describe.each([{ permission: 'Admin' }])(
indexes: [UID],
})
const client = await getClient(permission)
const token = client.generateTenantToken(uid, ['*'], {
const token = await client.generateTenantToken(uid, ['*'], {
apiKey: key,
})

Expand All @@ -152,7 +153,7 @@ describe.each([{ permission: 'Admin' }])(
const date = new Date('December 17, 4000 03:24:00')
const apiKey = await getKey(permission)
const { uid } = await client.getKey(apiKey)
const token = client.generateTenantToken(uid, ['*'], {
const token = await client.generateTenantToken(uid, ['*'], {
expiresAt: date,
})

Expand Down Expand Up @@ -182,7 +183,7 @@ describe.each([{ permission: 'Admin' }])(
const client = await getClient(permission)
const apiKey = await getKey(permission)
const { uid } = await client.getKey(apiKey)
const token = client.generateTenantToken(uid, {
const token = await client.generateTenantToken(uid, {
[UID]: null,
})

Expand All @@ -202,7 +203,7 @@ describe.each([{ permission: 'Admin' }])(
const client = await getClient(permission)
const apiKey = await getKey(permission)
const { uid } = await client.getKey(apiKey)
const token = client.generateTenantToken(uid, {
const token = await client.generateTenantToken(uid, {
[UID]: { filter: 'id = 2' },
})

Expand All @@ -216,7 +217,7 @@ describe.each([{ permission: 'Admin' }])(
const client = await getClient(permission)
const apiKey = await getKey(permission)
const { uid } = await client.getKey(apiKey)
const token = client.generateTenantToken(uid, [])
const token = await client.generateTenantToken(uid, [])

const searchClient = new MeiliSearch({ host: HOST, apiKey: token })

Expand All @@ -230,7 +231,7 @@ describe.each([{ permission: 'Admin' }])(
const client = await getClient(permission)
const apiKey = await getKey(permission)
const { uid } = await client.getKey(apiKey)
const token = client.generateTenantToken(uid, { misc: null })
const token = await client.generateTenantToken(uid, { misc: null })

const searchClient = new MeiliSearch({ host: HOST, apiKey: token })

Expand Down

0 comments on commit 27480d2

Please sign in to comment.