Skip to content

Commit e0b1e71

Browse files
committed
Update crypto statement to fix vite issue
1 parent 2f37b74 commit e0b1e71

File tree

6 files changed

+48
-48
lines changed

6 files changed

+48
-48
lines changed

.code-samples.meilisearch.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,7 @@ tenant_token_guide_generate_sdk_1: |-
709709
const apiKeyUid = '85c3c2f9-bdd6-41f1-abd8-11fcf80e0f76'
710710
const expiresAt = new Date('2025-12-20') // optional
711711
712-
const token = client.generateTenantToken(apiKeyUid, searchRules, {
712+
const token = await client.generateTenantToken(apiKeyUid, searchRules, {
713713
apiKey: apiKey,
714714
expiresAt: expiresAt,
715715
})

docker-compose.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
version: "3.8"
2-
31
services:
42
package:
5-
image: node:16
3+
image: node:18
64
tty: true
75
stdin_open: true
86
working_dir: /home/package

src/clients/client.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -475,11 +475,11 @@ class Client {
475475
_apiKeyUid: string,
476476
_searchRules: TokenSearchRules,
477477
_options?: TokenOptions
478-
): string {
478+
): Promise<string> {
479479
const error = new Error()
480-
throw new Error(
481-
`Meilisearch: failed to generate a tenant token. Generation of a token only works in a node environment \n ${error.stack}.`
482-
)
480+
error.message = `Meilisearch: failed to generate a tenant token. Generation of a token only works in a node environment \n ${error.stack}.`
481+
482+
return Promise.reject(error)
483483
}
484484
}
485485

src/clients/node-client.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,19 @@ class MeiliSearch extends Client {
1818
* @param options - Token options to customize some aspect of the token.
1919
* @returns The token in JWT format.
2020
*/
21-
generateTenantToken(
21+
async generateTenantToken(
2222
apiKeyUid: string,
2323
searchRules: TokenSearchRules,
2424
options?: TokenOptions
25-
): string {
25+
): Promise<string> {
2626
if (typeof window === 'undefined') {
27-
return this.tokens.generateTenantToken(apiKeyUid, searchRules, options)
27+
return await this.tokens.generateTenantToken(
28+
apiKeyUid,
29+
searchRules,
30+
options
31+
)
2832
}
29-
return super.generateTenantToken(apiKeyUid, searchRules, options)
33+
return await super.generateTenantToken(apiKeyUid, searchRules, options)
3034
}
3135
}
3236
export { MeiliSearch }

src/token.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Config, TokenSearchRules, TokenOptions } from './types'
2-
import { createHmac } from 'crypto'
32
import { MeiliSearchError } from './errors'
43
import { validateUuid4 } from './utils'
54

@@ -15,7 +14,13 @@ function encode64(data: any) {
1514
* @param encodedPayload - Payload of the token in base64.
1615
* @returns The signature of the token in base64.
1716
*/
18-
function sign(apiKey: string, encodedHeader: string, encodedPayload: string) {
17+
async function sign(
18+
apiKey: string,
19+
encodedHeader: string,
20+
encodedPayload: string
21+
) {
22+
const { createHmac } = await import('crypto')
23+
1924
return createHmac('sha256', apiKey)
2025
.update(`${encodedHeader}.${encodedPayload}`)
2126
.digest('base64')
@@ -132,11 +137,11 @@ class Token {
132137
* @param options - Token options to customize some aspect of the token.
133138
* @returns The token in JWT format.
134139
*/
135-
generateTenantToken(
140+
async generateTenantToken(
136141
apiKeyUid: string,
137142
searchRules: TokenSearchRules,
138143
options?: TokenOptions
139-
): string {
144+
): Promise<string> {
140145
const apiKey = options?.apiKey || this.config.apiKey || ''
141146
const uid = apiKeyUid || ''
142147
const expiresAt = options?.expiresAt
@@ -145,7 +150,7 @@ class Token {
145150

146151
const encodedHeader = createHeader()
147152
const encodedPayload = createPayload({ searchRules, uid, expiresAt })
148-
const signature = sign(apiKey, encodedHeader, encodedPayload)
153+
const signature = await sign(apiKey, encodedHeader, encodedPayload)
149154

150155
return `${encodedHeader}.${encodedPayload}.${signature}`
151156
}

tests/token.test.ts

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
} from './utils/meilisearch-test-utils'
1010
import { createHmac } from 'crypto'
1111
import MeiliSearch from '../src'
12-
import { MeiliSearchError } from '../src/errors'
1312

1413
const HASH_ALGORITHM = 'HS256'
1514
const TOKEN_TYP = 'JWT'
@@ -44,7 +43,7 @@ describe.each([{ permission: 'Admin' }])(
4443
const client = await getClient(permission)
4544
const apiKey = await getKey(permission)
4645
const { uid } = await client.getKey(apiKey)
47-
const token = client.generateTenantToken(uid, [], {})
46+
const token = await client.generateTenantToken(uid, [], {})
4847
const [header64] = token.split('.')
4948

5049
// header
@@ -57,7 +56,7 @@ describe.each([{ permission: 'Admin' }])(
5756
const client = await getClient(permission)
5857
const apiKey = await getKey(permission)
5958
const { uid } = await client.getKey(apiKey)
60-
const token = client.generateTenantToken(uid, [], {})
59+
const token = await client.generateTenantToken(uid, [], {})
6160
const [header64, payload64, signature64] = token.split('.')
6261

6362
// signature
@@ -75,7 +74,7 @@ describe.each([{ permission: 'Admin' }])(
7574
const client = await getClient(permission)
7675
const apiKey = await getKey(permission)
7776
const { uid } = await client.getKey(apiKey)
78-
const token = client.generateTenantToken(uid, [], {})
77+
const token = await client.generateTenantToken(uid, [], {})
7978
const [_, payload64] = token.split('.')
8079

8180
// payload
@@ -90,7 +89,7 @@ describe.each([{ permission: 'Admin' }])(
9089
const client = await getClient(permission)
9190
const apiKey = await getKey(permission)
9291
const { uid } = await client.getKey(apiKey)
93-
const token = client.generateTenantToken(uid, [UID])
92+
const token = await client.generateTenantToken(uid, [UID])
9493
const [_, payload64] = token.split('.')
9594

9695
// payload
@@ -105,7 +104,7 @@ describe.each([{ permission: 'Admin' }])(
105104
const client = await getClient(permission)
106105
const apiKey = await getKey(permission)
107106
const { uid } = await client.getKey(apiKey)
108-
const token = client.generateTenantToken(uid, { [UID]: {} })
107+
const token = await client.generateTenantToken(uid, { [UID]: {} })
109108
const [_, payload64] = token.split('.')
110109

111110
// payload
@@ -120,7 +119,7 @@ describe.each([{ permission: 'Admin' }])(
120119
const apiKey = await getKey(permission)
121120
const { uid } = await client.getKey(apiKey)
122121

123-
const token = client.generateTenantToken(uid, ['*'])
122+
const token = await client.generateTenantToken(uid, ['*'])
124123

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

@@ -137,7 +136,7 @@ describe.each([{ permission: 'Admin' }])(
137136
indexes: [UID],
138137
})
139138
const client = await getClient(permission)
140-
const token = client.generateTenantToken(uid, ['*'], {
139+
const token = await client.generateTenantToken(uid, ['*'], {
141140
apiKey: key,
142141
})
143142

@@ -152,7 +151,7 @@ describe.each([{ permission: 'Admin' }])(
152151
const date = new Date('December 17, 4000 03:24:00')
153152
const apiKey = await getKey(permission)
154153
const { uid } = await client.getKey(apiKey)
155-
const token = client.generateTenantToken(uid, ['*'], {
154+
const token = await client.generateTenantToken(uid, ['*'], {
156155
expiresAt: date,
157156
})
158157

@@ -171,18 +170,18 @@ describe.each([{ permission: 'Admin' }])(
171170
const { uid } = await client.getKey(apiKey)
172171
const date = new Date('December 17, 2000 03:24:00')
173172

174-
expect(() =>
175-
client.generateTenantToken(uid, ['*'], {
176-
expiresAt: date,
177-
})
178-
).toThrow()
173+
expect(
174+
client.generateTenantToken(uid, ['*'], { expiresAt: date })
175+
).rejects.toThrow(
176+
`Meilisearch: The expiresAt field must be a date in the future.`
177+
)
179178
})
180179

181180
test(`${permission} key: Search in tenant token with specific index set to null`, async () => {
182181
const client = await getClient(permission)
183182
const apiKey = await getKey(permission)
184183
const { uid } = await client.getKey(apiKey)
185-
const token = client.generateTenantToken(uid, {
184+
const token = await client.generateTenantToken(uid, {
186185
[UID]: null,
187186
})
188187

@@ -202,7 +201,7 @@ describe.each([{ permission: 'Admin' }])(
202201
const client = await getClient(permission)
203202
const apiKey = await getKey(permission)
204203
const { uid } = await client.getKey(apiKey)
205-
const token = client.generateTenantToken(uid, {
204+
const token = await client.generateTenantToken(uid, {
206205
[UID]: { filter: 'id = 2' },
207206
})
208207

@@ -216,7 +215,7 @@ describe.each([{ permission: 'Admin' }])(
216215
const client = await getClient(permission)
217216
const apiKey = await getKey(permission)
218217
const { uid } = await client.getKey(apiKey)
219-
const token = client.generateTenantToken(uid, [])
218+
const token = await client.generateTenantToken(uid, [])
220219

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

@@ -230,7 +229,7 @@ describe.each([{ permission: 'Admin' }])(
230229
const client = await getClient(permission)
231230
const apiKey = await getKey(permission)
232231
const { uid } = await client.getKey(apiKey)
233-
const token = client.generateTenantToken(uid, { misc: null })
232+
const token = await client.generateTenantToken(uid, { misc: null })
234233

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

@@ -246,38 +245,32 @@ describe.each([{ permission: 'Admin' }])(
246245
const { uid } = await client.getKey(apiKey)
247246
const date = new Date('December 17, 2000 03:24:00')
248247

249-
expect(() =>
248+
expect(
250249
client.generateTenantToken(
251250
uid,
252251
{},
253252
{
254253
expiresAt: date,
255254
}
256255
)
257-
).toThrowError(
258-
new MeiliSearchError(
259-
`Meilisearch: The expiresAt field must be a date in the future.`
260-
)
256+
).rejects.toThrow(
257+
`Meilisearch: The expiresAt field must be a date in the future.`
261258
)
262259
})
263260

264261
test(`${permission} key: Creates tenant token with wrong uid type throws an error`, async () => {
265262
const client = await getClient(permission)
266263

267-
expect(() => client.generateTenantToken('1234', ['*'])).toThrowError(
268-
new MeiliSearchError(
269-
`Meilisearch: The uid of your key is not a valid uuid4. To find out the uid of your key use getKey().`
270-
)
264+
expect(client.generateTenantToken('1234', ['*'])).rejects.toThrow(
265+
`Meilisearch: The uid of your key is not a valid uuid4. To find out the uid of your key use getKey().`
271266
)
272267
})
273268

274269
test(`${permission} key: Creates a tenant token with no api key in client and in parameters throws an error`, () => {
275270
const client = new MeiliSearch({ host: HOST })
276271

277-
expect(() => client.generateTenantToken('123', [])).toThrowError(
278-
new MeiliSearchError(
279-
`Meilisearch: The API key used for the token generation must exist and be of type string.`
280-
)
272+
expect(client.generateTenantToken('123', [])).rejects.toThrow(
273+
`Meilisearch: The API key used for the token generation must exist and be of type string.`
281274
)
282275
})
283276
}

0 commit comments

Comments
 (0)