Commit 185afaf9 authored by Hidde-Jan Jongsma's avatar Hidde-Jan Jongsma

Add callback url setting and params

parent cc912fb9
......@@ -5,7 +5,9 @@ import { sign } from "jsonwebtoken";
const ID = "my";
const SECRET = "secret";
function newClient(options?: SSIClientOptions) {
function newClient(
options: SSIClientOptions = { callbackUrl: "http://localhost?response=" }
) {
return new SSIClient(ID, SECRET, options);
}
......@@ -59,6 +61,23 @@ test("It handles issue responses", () => {
expect(response).toMatchObject(payload);
});
test("It handles missing callback parameters", () => {
const client = newClient({});
expect(() => client.issueUrl("MyType", { my: "data" }, "12345")).toThrow();
});
test("It accepts callback urls per request", () => {
const client = newClient({});
const url = client.issueUrl(
"MyType",
{ my: "data" },
"12345",
"myCallback?token="
);
expect(url).toBeDefined();
expect(url).toMatch(/issue\?token=/);
});
test("It handles malformed issue responses", () => {
const client = newClient();
const payload = { some: "body" };
......
......@@ -3,9 +3,10 @@ import { sign, verify, SignOptions, VerifyOptions } from "jsonwebtoken";
export interface SSIClientOptions {
url?: string;
name?: string;
callbackUrl?: string;
}
export type SSIData = Record<string, string | number | boolean | null>;
export type SSIData = Record<string, string | number | boolean>;
export type SSIFunction = "verify" | "issue";
......@@ -31,6 +32,7 @@ export type CredentialIssueResponse = CredentialResponse;
export default class SSIClient {
private url = "https://ssi-provider.sensorlab.tno.nl/";
private name = "ssi-service-provider";
private callbackUrl?: string;
constructor(
private clientId: string,
......@@ -48,19 +50,32 @@ export default class SSIClient {
if (options.name) {
this.name = options.name;
}
if (options.callbackUrl) {
this.callbackUrl = options.callbackUrl;
}
}
verifyUrl(type: string, requestId: string): string {
verifyUrl(type: string, requestId: string, callbackUrl?: string): string {
const callback = this.getCallbackUrl(callbackUrl);
const token = this.encodeJWT(
{ type },
{ type, callbackUrl: callback },
{ subject: "credential-verify-request", jwtid: requestId }
);
return this.constructRequestUrl("verify", token);
}
issueUrl(type: string, data: SSIData, requestId: string): string {
issueUrl(
type: string,
data: SSIData,
requestId: string,
callbackUrl?: string
): string {
const callback = this.getCallbackUrl(callbackUrl);
const token = this.encodeJWT(
{ type, data },
{ type, data, callbackUrl: callback },
{ subject: "credential-issue-request", jwtid: requestId }
);
return this.constructRequestUrl("issue", token);
......@@ -92,6 +107,18 @@ export default class SSIClient {
};
}
private getCallbackUrl(callbackUrl?: string) {
const callback = callbackUrl || this.callbackUrl;
if (!callback) {
throw new Error(
"Please specify a callback url either through the client instance or per request."
);
}
return callback;
}
private constructRequestUrl(endpoint: SSIFunction, token: string) {
const url = new URL(endpoint, this.url);
url.search = `?token=${token}`;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment