Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
PollOAuth2FlowResult,
PollOAuth2LocalFlowResult,
PollOAuth2GlobalFlowResult,
IJWT,
ISecurity,
JwtMintResult,
} from "./types";
import { CallSender } from "penpal/lib/types";

Expand Down Expand Up @@ -105,6 +108,27 @@
}
}

class JWT implements IJWT {
constructor(
private client: IDeskproClient,
private strategyName: string
) { }

async mint(options: { context: Context }): Promise<JwtMintResult> {
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 = {
Expand Down Expand Up @@ -172,6 +196,9 @@
// Deskpro UI
public sendDeskproUIMessage: (message: DeskproUIMessage) => Promise<void>;

// Security
public securityJwtMint: (strategyName: string, context: Context) => Promise<JwtMintResult>;

constructor(
private readonly parent: <T extends object = CallSender>(options?: object) => Connection<T>,
private readonly options: DeskproClientOptions
Expand Down Expand Up @@ -221,6 +248,8 @@

this.sendDeskproUIMessage = async () => { };

this.securityJwtMint = async () => ({ token: "", expiresAt: new Date() } as JwtMintResult);

if (this.options.runAfterPageLoad) {
window.addEventListener("load", () => this.run());
}
Expand Down Expand Up @@ -401,6 +430,21 @@
if (parent._sendDeskproUIMessage) {
this.sendDeskproUIMessage = (message: DeskproUIMessage) => parent._sendDeskproUIMessage(message);
}

// Security
if (parent._securityJwtMint) {
this.securityJwtMint = async (strategyName: string, context: Context) => {
try {

Check failure on line 437 in src/client/client.ts

View workflow job for this annotation

GitHub Actions / Test / Build

Unnecessary try/catch wrapper
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 {
Expand Down Expand Up @@ -551,6 +595,10 @@
return new DeskproUI(this);
}

public security(): ISecurity {
return new Security(this);
}

public getParentMethods(): ChildMethods {
return this.parentMethods;
}
Expand Down
16 changes: 16 additions & 0 deletions src/client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ export interface CoreCallSender {
_setAdminSetting: (value: string) => void;
_setAdminSettingInvalid: (message: string, settingName?: string) => void;
_sendDeskproUIMessage: (message: DeskproUIMessage) => Promise<void>;
_securityJwtMint: (strategyName: string, context: Context) => Promise<JwtMintResult>;
}

export type DeskproCallSender = CoreCallSender & TicketSidebarDeskproCallSender;
Expand Down Expand Up @@ -339,6 +340,7 @@ export interface IDeskproClient {
setAdminSetting: (value: string) => void;
setAdminSettingInvalid: (message: string, settingName?: string) => void;
sendDeskproUIMessage: (message: DeskproUIMessage) => Promise<void>;
securityJwtMint: (strategyName: string, context: Context) => Promise<JwtMintResult>;
getEntityAssociation(name: string, entityId: string): IEntityAssociation;
startOauth2Local(
authorizeUrlFn: (data: { state: string, callbackUrl: string, codeChallenge: string }) => string,
Expand All @@ -351,6 +353,7 @@ export interface IDeskproClient {
options?: { timeout?: number, pollInterval?: number },
): Promise<IOAuth2>;
deskpro(): IDeskproUI;
security(): ISecurity;
}

export interface IOAuth2 {
Expand Down Expand Up @@ -469,3 +472,16 @@ export interface IDeskproUI {
alertError: (text: string, duration?: number) => Promise<void>;
alertDismiss: () => Promise<void>;
}

export interface JwtMintResult {
token: string;
expiresAt: Date;
}

export interface IJWT {
mint: (options: { context: Context }) => Promise<JwtMintResult>;
}

export interface ISecurity {
jwt: (strategyName: string) => IJWT;
}
Loading