UNPKG

@privy-io/js-sdk-core

Version:

Vanilla JS client for the Privy Auth API

2 lines (1 loc) 1.18 kB
import{RecoveryOAuthInit as t,RecoveryOAuthAuthenticate as e}from"@privy-io/routes";import{PrivyClientError as r}from"../../Error.mjs";import{createCodeVerifier as i,createStateCode as o,deriveCodeChallengeFromCodeVerifier as a,CODE_VERIFIER_KEY as s,STATE_CODE_KEY as h}from"../../pkce.mjs";import"jose";class c{async generateURL(e){let r=i(),c=o(),_=await a({codeVerifier:r,digest:this._crypto?.digest});return await Promise.all([this._storage.put(s,r),this._storage.put(h,c)]),this._privyInternal.fetch(t,{body:{redirect_to:e,code_challenge:_,state_code:c}})}async authorize(t,i){let[o,a]=await Promise.all([this._storage.get(s),this._storage.get(h)]);if(a!==i)throw this._privyInternal.createAnalyticsEvent("possible_phishing_attempt",{flow:"recovery_oauth",storedStateCode:a??"",returnedStateCode:i??""}),new r({code:"pkce_state_code_mismatch",error:"Unexpected auth flow. This may be a phishing attempt."});let c=await this._privyInternal.fetch(e,{body:{authorization_code:t,state_code:a,code_verifier:o}});return await Promise.all([this._storage.del(s),this._storage.del(h)]),c}constructor(t,e,r){this._privyInternal=t,this._storage=e,this._crypto=r}}export{c as default};