@@ -2,10 +2,10 @@ import { MONERIUM_CONFIG } from './config';
22import constants from './constants' ;
33import {
44 cleanQueryString ,
5- getAuthFlowUrlAndStoreCodeVerifier ,
65 isAuthCode ,
76 isClientCredentials ,
87 isRefreshToken ,
8+ preparePKCEChallenge ,
99 queryParams ,
1010 rest ,
1111} from './helpers' ;
@@ -18,6 +18,7 @@ import type {
1818 AuthArgs ,
1919 AuthCodePayload ,
2020 AuthFlowOptions ,
21+ AuthFlowSIWEOptions ,
2122 AuthorizationCodeCredentials ,
2223 Balances ,
2324 BearerProfile ,
@@ -41,7 +42,8 @@ import type {
4142 OrderFilter ,
4243 OrderNotificationQueryParams ,
4344 OrdersResponse ,
44- PKCERequestArgs ,
45+ PKCERequest ,
46+ PKCESIWERequest ,
4547 Profile ,
4648 ProfilesQueryParams ,
4749 ProfilesResponse ,
@@ -152,41 +154,86 @@ export class MoneriumClient {
152154 }
153155
154156 /**
155- * Construct the url to the authorization code flow and redirects,
157+ * Constructs the url to the authorization code flow and redirects,
156158 * Code Verifier needed for the code challenge is stored in local storage
157159 * For automatic wallet link, add the following properties: `address`, `signature` & `chain`
158160 *
161+ * This authorization code is then used to request an access token via the token endpoint. (https://monerium.dev/api-docs#operation/auth-token)
162+ *
159163 * @group Authentication
160164 * @see {@link https://monerium.dev/api-docs-v2#tag/auth/operation/auth | API Documentation }
161165 * @param {AuthFlowOptions } [params] - the auth flow params
162- * @returns string
166+ * @returns void
163167 *
164168 */
165169 async authorize ( params ?: AuthFlowOptions ) {
166- const clientId =
167- params ?. clientId ||
168- ( this . #client as AuthorizationCodeCredentials ) ?. clientId ;
169- const redirectUri =
170- params ?. redirectUri ||
171- ( this . #client as AuthorizationCodeCredentials ) ?. redirectUri ;
172-
173- if ( ! clientId ) {
174- throw new Error ( 'Missing ClientId' ) ;
175- }
176-
177- const authFlowUrl = getAuthFlowUrlAndStoreCodeVerifier ( this . #env, {
178- client_id : clientId ,
179- redirect_uri : redirectUri ,
180- address : params ?. address ,
181- signature : params ?. signature ,
182- chain : params ?. chain ,
170+ const codeChallenge = preparePKCEChallenge ( ) ;
171+
172+ const autoLink = params ?. address
173+ ? {
174+ address : params ?. address ,
175+ signature : params ?. signature ,
176+ chain : params ?. chain
177+ ? parseChainBackwardsCompatible ( this . #env. name , params ?. chain )
178+ : undefined ,
179+ }
180+ : { } ;
181+
182+ const queryParams = urlEncoded ( {
183+ client_id : ( this . #client as AuthorizationCodeCredentials ) ?. clientId ,
184+ redirect_uri : ( this . #client as AuthorizationCodeCredentials ) ?. redirectUri ,
185+ code_challenge : codeChallenge ,
186+ code_challenge_method : 'S256' as PKCERequest [ 'code_challenge_method' ] ,
187+ response_type : 'code' as PKCERequest [ 'response_type' ] ,
183188 state : params ?. state ,
184- email : params ?. email ,
185189 skip_create_account : params ?. skipCreateAccount ,
186190 skip_kyc : params ?. skipKyc ,
191+ email : params ?. email ,
192+ ...autoLink ,
187193 } ) ;
188194
189- this . #debug( `Authorization URL: ${ authFlowUrl } ` ) ;
195+ const authFlowUrl = `${ this . #env. api } /auth?${ queryParams } ` ;
196+
197+ this . #debug( `Auth flow URL: ${ authFlowUrl } ` ) ;
198+ // Redirect to the authFlow
199+ window . location . assign ( authFlowUrl ) ;
200+ }
201+ /**
202+ * Constructs the url to the authorization code flow and redirects,
203+ * Code Verifier needed for the code challenge is stored in local storage
204+ *
205+ * "Sign in with Ethereum" (SIWE) flow can be used for existing Monerium customers.
206+ * In this case the payload must include a valid EIP-4361 (https://eips.ethereum.org/EIPS/eip-4361) message and signature.
207+ * On successful authorization the authorization code is returned at once.
208+ *
209+ * This authorization code is then used to request an access token via the token endpoint.
210+ *
211+ * https://monerium.com/siwe
212+ *
213+ * @group Authentication
214+ * @see {@link https://monerium.dev/api-docs-v2#tag/auth/operation/auth | API Documentation }
215+ * @param {AuthFlowSIWEOptions } [params] - the auth flow SIWE params
216+ * @returns void
217+ *
218+ */
219+ async siwe ( params : AuthFlowSIWEOptions ) {
220+ const codeChallenge = preparePKCEChallenge ( ) ;
221+
222+ const queryParams = urlEncoded ( {
223+ client_id : ( this . #client as AuthorizationCodeCredentials ) ?. clientId ,
224+ redirect_uri : ( this . #client as AuthorizationCodeCredentials ) ?. redirectUri ,
225+ message : params . message ,
226+ signature : params . signature ,
227+ code_challenge : codeChallenge ,
228+ code_challenge_method : 'S256' as PKCESIWERequest [ 'code_challenge_method' ] ,
229+ authentication_method : 'siwe' as PKCESIWERequest [ 'authentication_method' ] ,
230+ state : params ?. state ,
231+ } ) ;
232+
233+ const authFlowUrl = `${ this . #env. api } /auth?${ queryParams } ` ;
234+
235+ this . #debug( `Auth flow SIWE URL: ${ authFlowUrl } ` ) ;
236+
190237 // Redirect to the authFlow
191238 window . location . assign ( authFlowUrl ) ;
192239 }
@@ -791,15 +838,4 @@ export class MoneriumClient {
791838 * @hidden
792839 */
793840 getEnvironment = ( ) : Environment => this . #env;
794- /**
795- *
796- * @hidden
797- */
798- getAuthFlowURI = ( args : PKCERequestArgs ) : string => {
799- const url = getAuthFlowUrlAndStoreCodeVerifier (
800- this . #env,
801- mapChainIdToChain ( this . #env. name , args )
802- ) ;
803- return url ;
804- } ;
805841}
0 commit comments