Skip to content
Merged
Prev Previous commit
Next Next commit
jwt
  • Loading branch information
ikethirdweb committed Oct 18, 2023
commit 28345e4590b8378ccfe22b4d1b985470341d9b73
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
getConnectedEmail,
saveConnectedEmail,
} from "./embedded/helpers/storage/local";
import { AuthProvider } from "@paperxyz/embedded-wallet-service-sdk";

export class EmbeddedWalletConnector extends Connector<EmbeddedWalletConnectionArgs> {
private options: EmbeddedWalletConnectorOptions;
Expand All @@ -36,16 +37,38 @@ export class EmbeddedWalletConnector extends Connector<EmbeddedWalletConnectionA
this.email = getConnectedEmail();
}

async connect(
options?: { chainId?: number } & Omit<
EmbeddedWalletConnectionArgs,
"email"
>,
) {
async connect(options?: { chainId?: number } & EmbeddedWalletConnectionArgs) {
const connected = await this.isConnected();

if (!connected) {
// const;
if (connected) {
return this.getAddress();
}

switch (options?.loginType) {
case "headless_google_oauth":
{
await socialLogin(
{
provider: AuthProvider.GOOGLE,
redirectUrl: options.redirectUrl,
},
this.options.clientId,
);
}
break;
case "headless_email_otp_verification": {
await this.validateEmailOtp(options.otp);
break;
}
case "custom_jwt": {
await this.customJwt({
jwtToken: options.jwtToken,
encryptionKey: options.encryptionKey,
});
break;
}
default:
throw new Error("Invalid login type");
}

if (options?.chainId) {
Expand Down Expand Up @@ -125,7 +148,7 @@ export class EmbeddedWalletConnector extends Connector<EmbeddedWalletConnectionA
return { success: true };
}

async verifyAuth(authOptions: AuthOptions) {
async customJwt(authOptions: AuthOptions) {
try {
const resp = await customJwt(authOptions, this.options.clientId);
this.email = resp.email;
Expand Down Expand Up @@ -183,12 +206,7 @@ export class EmbeddedWalletConnector extends Connector<EmbeddedWalletConnectionA
return this.signer;
}

let signer;
try {
signer = await getEthersSigner(this.options.clientId);
} catch (error) {
console.error(`Error while getting the signer: ${error}`);
}
const signer = await getEthersSigner(this.options.clientId);

if (!signer) {
throw new Error("Error fetching the signer");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,14 +201,14 @@ export async function socialLogin(oauthOptions: OauthOption, clientId: string) {
}

export async function customJwt(authOptions: AuthOptions, clientId: string) {
const { jwt, authProvider } = authOptions;
const { jwtToken } = authOptions;

const resp = await fetch(ROUTE_AUTH_JWT_CALLBACK, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
jwt,
authProvider,
jwtToken,
authProvider: AuthProvider.CUSTOM_JWT,
developerClientId: clientId,
}),
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const keys = {
keys: [
{
kty: "RSA",
n: "tuYJTS0bXLO629xwe6LpGs_es-I6jbCwpEXmYfzRcWYGd0Z4l0czwmv7FJ8eycOaNFt2Hjb3nhwHZGcjbLd1SWXiiRpVnmL-1Y01AAPOWECqxifO3C5Jco5En5SO7p8tDsdx9LD8mxMnplyv3X8pBzYDrNlTlpLTPksS3yPqA01MXHlH_xhEY2G7g7sUHt9jb9uPWALofH3TeztjPPiU6kQLQ8VvlTkz0V2DMZYPnvLkRzt6myguwwNEQwzEzzH4ELtf8tez0lx5RE6qGGAezLIH_cnPmRevOEcxoQP3u0bvZcl0P5M7eOLPolw2B2_pFBpaqGMFxr7yCcwmde0HRw",
e: "AQAB",
kid: "0",
},
],
};
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,24 @@ export interface EmbeddedWalletConnectorOptions {

export interface AuthOptions {
jwtToken: string;
authProvider: string; // AuthProvider.CustomJwt
recoveryCode?: string;
encryptionKey: string;
}

export interface EmbeddedWalletConnectionArgs {
email?: string;
authOptions?: AuthOptions;
}
export type EmbeddedWalletConnectionArgs = {
chainId?: number;
} & (
| {
loginType: "headless_google_oauth";
redirectUrl: string;
}
| {
loginType: "headless_email_otp_verification";
email: string;
otp: string;
}
| {
loginType: "custom_jwt";
jwtToken: string;
encryptionKey: string;
}
);
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,13 @@ export const EmbeddedConnectionUI: React.FC<ConnectUIProps<EmbeddedWallet>> = ({

setTimeout(() => {
(selectionData.emailWallet as EmbeddedWallet)
.validateEmailOTP(otp)
.connect({
loginType: "headless_email_otp_verification",
otp,
email: selectionData.email,
})
.then(async (response) => {
if (response?.success) {
if (response) {
if (onLocallyConnected) {
onLocallyConnected(selectionData.emailWallet);
} else {
Expand All @@ -75,7 +79,7 @@ export const EmbeddedConnectionUI: React.FC<ConnectUIProps<EmbeddedWallet>> = ({
}
} else {
clearCode();
setErrorMessage(response?.error || "Error validating the code");
setErrorMessage(response || "Error validating the code");
setCheckingOtp(false);
setFocusedIndex(undefined);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ export const EmbeddedSocialConnection: React.FC<

setTimeout(() => {
(selectionData.emailWallet as EmbeddedWallet)
.socialLogin(selectionData?.oauthOptions)
.connect({
loginType: "headless_google_oauth",
redirectUrl: selectionData.oauthOptions?.redirectUrl,
})
.then(async (response) => {
if (response?.success) {
if (response) {
if (onLocallyConnected) {
onLocallyConnected(selectionData.emailWallet);
} else {
Expand All @@ -34,7 +37,7 @@ export const EmbeddedSocialConnection: React.FC<
}
} else {
setErrorMessage(
response?.error || "Error login in. Please try again later.",
response || "Error login in. Please try again later.",
);
}
})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { useAddress } from "@thirdweb-dev/react-core";
import React, { useState } from "react";
import { ActivityIndicator } from "react-native";
import { ConnectWalletHeader } from "../../../components/ConnectWalletFlow/ConnectingWallet/ConnectingWalletHeader";
import { Box, BaseButton, Text } from "../../../components/base";
import { shortenWalletAddress } from "../../../utils/addresses";
import { useGlobalTheme } from "../../../providers/ui-context-provider";
import { PasswordInput } from "../../../components/PasswordInput";

export type EnterPasswordProps = {
goBack: () => void;
close: () => void;
email: string;
};

export const EnterPassword = ({ close, goBack, email }: EnterPasswordProps) => {
const theme = useGlobalTheme();
const [errorMessage, setErrorMessage] = useState<string>();
const [checkingPass, setCheckingPass] = useState<boolean>(false);
const [password, setPassword] = useState<string>("");

const onNextPress = async () => {};

const onForgotPress = () => {};

return (
<Box marginHorizontal="xl">
<ConnectWalletHeader
middleContent={<Text variant="header">Enter password</Text>}
subHeaderText={`Enter the password for email: ${email}`}
onBackPress={goBack}
onClose={close}
/>
<Box mt="sm" flexDirection="column" marginTop="xl" mb="md">
<PasswordInput onChangeText={setPassword} />

<BaseButton
mt="sm"
borderWidth={1}
borderBottomColor="linkPrimary"
onPress={onForgotPress}
>
<Text variant="bodySmallSecondary" color="linkPrimary">
Forgot password
</Text>
</BaseButton>
</Box>
{errorMessage ? (
<Text variant="error" numberOfLines={1}>
{errorMessage}
</Text>
) : (
<Box height={20} />
)}
<BaseButton
mt="sm"
alignItems="center"
height={theme.textVariants.bodySmallSecondary.fontSize}
onPress={onNextPress}
>
{checkingPass ? (
<ActivityIndicator size={"small"} color={theme.colors.linkPrimary} />
) : (
<Text variant="bodySmallSecondary" color="linkPrimary">
Next
</Text>
)}
</BaseButton>
</Box>
);
};