-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathCedarlingClient.ts
More file actions
59 lines (51 loc) · 1.46 KB
/
CedarlingClient.ts
File metadata and controls
59 lines (51 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import initWasm, { init, Cedarling, AuthorizeResult } from '@janssenproject/cedarling_wasm'
import type {
ICedarlingClient,
BootStrapConfig,
AuthorizationResponse,
TokenAuthorizationRequest,
} from '@/cedarling'
let cedarling: Cedarling | null = null
let cedarlingInitialized: boolean = false
let initializationPromise: Promise<void> | null = null
const initialize = async (bootStrapConfig: BootStrapConfig): Promise<void> => {
if (cedarlingInitialized) {
return Promise.resolve()
}
if (initializationPromise) {
return initializationPromise
}
initializationPromise = (async () => {
try {
await initWasm()
cedarling = await init(bootStrapConfig)
cedarlingInitialized = true
} catch (err) {
console.error('Error during Cedarling init:', err)
initializationPromise = null // Reset on error to allow retry
throw err
}
})()
return initializationPromise
}
const token_authorize = async (
request: TokenAuthorizationRequest,
): Promise<AuthorizationResponse> => {
if (!cedarlingInitialized || !cedarling) {
throw new Error('Cedarling not initialized')
}
try {
const result: AuthorizeResult = await cedarling.authorize(request)
return {
...result,
decision: result.decision,
} as AuthorizationResponse
} catch (error) {
console.error('Error during authorization:', error)
throw error
}
}
export const cedarlingClient: ICedarlingClient = {
initialize,
token_authorize,
}