diff --git a/src/client/client.ts b/src/client/client.ts index 31898e3..c763cdf 100644 --- a/src/client/client.ts +++ b/src/client/client.ts @@ -29,6 +29,9 @@ import { PollOAuth2FlowResult, PollOAuth2LocalFlowResult, PollOAuth2GlobalFlowResult, + IJWT, + ISecurity, + JwtMintResult, } from "./types"; import { CallSender } from "penpal/lib/types"; @@ -105,6 +108,27 @@ class EntityAssociation implements IEntityAssociation { } } +class JWT implements IJWT { + constructor( + private client: IDeskproClient, + private strategyName: string + ) { } + + async mint(options: { context: Context }): Promise { + return this.client.securityJwtMint(this.strategyName, options.context); + } +} + +class Security implements ISecurity { + constructor( + private client: IDeskproClient + ) { } + + jwt(strategyName: string): IJWT { + return new JWT(this.client, strategyName); + } +} + export class DeskproClient implements IDeskproClient { private parentMethods: ChildMethods = { @@ -172,6 +196,9 @@ export class DeskproClient implements IDeskproClient { // Deskpro UI public sendDeskproUIMessage: (message: DeskproUIMessage) => Promise; + // Security + public securityJwtMint: (strategyName: string, context: Context) => Promise; + constructor( private readonly parent: (options?: object) => Connection, private readonly options: DeskproClientOptions @@ -221,6 +248,8 @@ export class DeskproClient implements IDeskproClient { this.sendDeskproUIMessage = async () => { }; + this.securityJwtMint = async () => ({ token: "", expiresAt: new Date() } as JwtMintResult); + if (this.options.runAfterPageLoad) { window.addEventListener("load", () => this.run()); } @@ -401,6 +430,21 @@ export class DeskproClient implements IDeskproClient { if (parent._sendDeskproUIMessage) { this.sendDeskproUIMessage = (message: DeskproUIMessage) => parent._sendDeskproUIMessage(message); } + + // Security + if (parent._securityJwtMint) { + this.securityJwtMint = async (strategyName: string, context: Context) => { + try { + const result = await parent._securityJwtMint(strategyName, context); + return { + ...result, + expiresAt: result.expiresAt instanceof Date ? result.expiresAt : new Date(result.expiresAt), + }; + } catch (error) { + throw error; + } + }; + } } public onReady(cb: ChildMethod): void { @@ -551,6 +595,10 @@ export class DeskproClient implements IDeskproClient { return new DeskproUI(this); } + public security(): ISecurity { + return new Security(this); + } + public getParentMethods(): ChildMethods { return this.parentMethods; } diff --git a/src/client/types.ts b/src/client/types.ts index b7e8324..4a22b66 100644 --- a/src/client/types.ts +++ b/src/client/types.ts @@ -222,6 +222,7 @@ export interface CoreCallSender { _setAdminSetting: (value: string) => void; _setAdminSettingInvalid: (message: string, settingName?: string) => void; _sendDeskproUIMessage: (message: DeskproUIMessage) => Promise; + _securityJwtMint: (strategyName: string, context: Context) => Promise; } export type DeskproCallSender = CoreCallSender & TicketSidebarDeskproCallSender; @@ -339,6 +340,7 @@ export interface IDeskproClient { setAdminSetting: (value: string) => void; setAdminSettingInvalid: (message: string, settingName?: string) => void; sendDeskproUIMessage: (message: DeskproUIMessage) => Promise; + securityJwtMint: (strategyName: string, context: Context) => Promise; getEntityAssociation(name: string, entityId: string): IEntityAssociation; startOauth2Local( authorizeUrlFn: (data: { state: string, callbackUrl: string, codeChallenge: string }) => string, @@ -351,6 +353,7 @@ export interface IDeskproClient { options?: { timeout?: number, pollInterval?: number }, ): Promise; deskpro(): IDeskproUI; + security(): ISecurity; } export interface IOAuth2 { @@ -469,3 +472,16 @@ export interface IDeskproUI { alertError: (text: string, duration?: number) => Promise; alertDismiss: () => Promise; } + +export interface JwtMintResult { + token: string; + expiresAt: Date; +} + +export interface IJWT { + mint: (options: { context: Context }) => Promise; +} + +export interface ISecurity { + jwt: (strategyName: string) => IJWT; +}