From 3bf845cac60bb8a2d5ee7a9dbfa56c76f6996178 Mon Sep 17 00:00:00 2001 From: Einar Alexander Eymundsson Date: Thu, 16 Feb 2023 17:40:29 +0000 Subject: [PATCH] chore: define properties for automatic wallet link on auth flow (#19) --- README.md | 22 ++++++++++++++++------ src/client.ts | 13 ++++++------- src/types.ts | 21 +++++++++++++++++++-- 3 files changed, 41 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index bcfdc2c..0ced7b3 100644 --- a/README.md +++ b/README.md @@ -22,17 +22,21 @@ yarn add @monerium/sdk - `watch`: Run Vite in watch mode to detect changes to files during development - `build`: Run Vite to build a production release distributable -### Getting started +## Getting started If you are new here, we recommend starting in the [Developer Portal](https://monerium.dev/docs/welcome). There you will more about `client_id`'s and ways of authenticating. +### Import the SDK and initialize a client + ```ts -import { MoneriumClient } from '@monerium/sdk' +import { MoneriumClient } from "@monerium/sdk"; const client = new MoneriumClient(); +``` -/** Authenticate using client credentials */ +### Authenticate using client credentials +```ts await client.auth({ client_id: "your_client_credentials_uuid" client_secret: "your_client_secret" @@ -40,14 +44,20 @@ await client.auth({ // User is now authenticated, get authentication data await client.getAuthContext() +``` -/*************************************/ -/** Or, authenticate using auth flow */ +### Authenticate using auth flow +```ts // Construct the authFlowUrl for your application and redirect your customer. let authFlowUrl = client.getAuthFlowURI({ client_id: "your_client_authflow_uuid" - redirect_uri: "http://your-webpage.com/monerium-integration" + // optional automatic wallet selection: + network: "mumbai", + chain: "polygon", + address: "0xValidAddress72413Fa92980B889A1eCE84dD", + signature: "0xValidSignature0df2b6c9e0fc067ab29bdbf322bec30aad7c46dcd97f62498a91ef7795957397e0f49426e000b0f500c347219ddd98dc5080982563055e918031c" + }) // Redirecting to the Monerium onboarding / Authentication flow. window.location.replace(authFlowUrl) diff --git a/src/client.ts b/src/client.ts index 9a452ae..50dddc3 100644 --- a/src/client.ts +++ b/src/client.ts @@ -31,8 +31,6 @@ export class MoneriumClient { codeVerifier?: string; bearerProfile?: BearerProfile; - clientId?: string; - constructor(env: "production" | "sandbox" = "sandbox") { this.#env = MONERIUM_CONFIG.environments[env]; } @@ -51,7 +49,7 @@ export class MoneriumClient { } else { throw new Error("Authentication method could not be detected."); } - this.clientId = args.client_id; + this.bearerProfile = (await this.#api( "post", `auth/token`, @@ -65,16 +63,16 @@ export class MoneriumClient { /** * Construct the url to the authorization code flow, * the code verifier is needed afterwards to obtain an access token and is therefore stored in `this.codeVerifier` + * For automatic wallet link, add the following properties: `address`, `signature`, `chain` & `network` * @returns string */ - getAuthFlowURI(args: PKCERequestArgs): string { this.codeVerifier = wordArray.random(64).toString(); const challenge = encodeBase64Url.stringify(SHA256(this.codeVerifier)); const params: PKCERequest = { ...args, - client_id: this.clientId || args?.client_id, + client_id: args?.client_id, code_challenge: challenge, code_challenge_method: "S256", response_type: "code", @@ -82,10 +80,11 @@ export class MoneriumClient { return `${this.#env.api}/auth?${new URLSearchParams(params)}`; } + /** - * @deprecated since v2.0.7, use getAuthFlowURI instead. + * @deprecated since v2.0.7, use {@link getAuthFlowURI} instead. */ - pkceRequest = this.getAuthFlowURI; + pkceRequest = (args: PKCERequestArgs) => this.getAuthFlowURI(args); // -- Read Methods diff --git a/src/types.ts b/src/types.ts index 83b9442..2d00957 100644 --- a/src/types.ts +++ b/src/types.ts @@ -58,20 +58,37 @@ export interface ClientCredentials { // -- pkceRequest +/** + * @returns A {@link PKCERequest} object with properties omitted that are automatically computed in by the SDK. + */ export type PKCERequestArgs = Omit< PKCERequest, "code_challenge" | "code_challenge_method" | "response_type" >; export type PKCERequest = { + /** the authentication flow client id of the application */ client_id: string; + /** the code challenge automatically generated by the SDK */ code_challenge: string; - code_challenge_method: string; - response_type: string; + /** the code challenge method for the authentication flow , handled by the SDK */ + code_challenge_method: "S256"; + /** the response type of the authentication flow, handled by the SDK */ + response_type: "code"; + /** the state of the application */ state?: string; + /** the redirect uri of the application */ redirect_uri?: string; + /** the scope of the application */ scope?: string; + /** the address of the wallet to automatically link */ address?: string; + /** the signature of the wallet to automatically link */ + signature?: string; + /** the network of the wallet to automatically link */ + network?: Network; + /** the chain of the wallet to automatically link */ + chain?: Chain; }; // -- authContext