Skip to content

Commit 6555f78

Browse files
committed
Merge pull-request #22
2 parents 0129ee4 + 2278ba3 commit 6555f78

File tree

13 files changed

+445
-172
lines changed

13 files changed

+445
-172
lines changed

Examples/swift-demo-wallet/example-server/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import cors from "cors";
33
import dotenv from "dotenv";
44
import bodyParser from "body-parser";
55
import {
6-
getSubOrgId,
76
sendOtp,
87
verifyOtp,
98
createSubOrg,
9+
oAuth,
1010
} from "./src/handler.js";
1111

1212
dotenv.config();
@@ -32,16 +32,16 @@ async function handleRequest<T>(
3232
}
3333
}
3434

35-
app.post("/auth/getSubOrgId", (req, res) =>
36-
handleRequest(req, res, getSubOrgId),
35+
app.post("/auth/createSubOrg", (req, res) =>
36+
handleRequest(req, res, createSubOrg),
3737
);
38+
3839
app.post("/auth/sendOtp", (req, res) =>
3940
handleRequest(req, res, sendOtp),
4041
);
4142
app.post("/auth/verifyOtp", (req, res) => handleRequest(req, res, verifyOtp));
42-
app.post("/auth/createSubOrg", (req, res) =>
43-
handleRequest(req, res, createSubOrg),
44-
);
43+
44+
app.post("/auth/oAuth", (req, res) => handleRequest(req, res, oAuth));
4545

4646
app.listen(PORT, () =>
4747
console.log(`✅ Server running on http://localhost:${PORT}`),

Examples/swift-demo-wallet/example-server/src/handler.ts

Lines changed: 53 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import dotenv from "dotenv";
33
import { DEFAULT_ETHEREUM_ACCOUNTS, Turnkey } from "@turnkey/sdk-server";
44
import { decodeJwt } from "./util.js";
55
import {
6-
GetSubOrgIdParams,
7-
GetSubOrgIdResponse,
86
CreateSubOrgParams,
97
CreateSubOrgResponse,
108
SendOtpParams,
@@ -13,6 +11,8 @@ import {
1311
VerifyOtpResponse,
1412
VerifyOtpParams,
1513
SendOtpResponse,
14+
OAuthParams,
15+
OAuthResponse,
1616
} from "./types.js";
1717

1818
dotenv.config();
@@ -26,20 +26,6 @@ export const turnkeyConfig = {
2626

2727
const turnkey = new Turnkey(turnkeyConfig).apiClient();
2828

29-
export async function getSubOrgId(
30-
req: Request<{}, {}, GetSubOrgIdParams>
31-
): Promise<GetSubOrgIdResponse> {
32-
const { filterType, filterValue } = req.body;
33-
const { organizationIds } = await turnkey.getSubOrgIds({
34-
filterType,
35-
filterValue,
36-
});
37-
38-
return {
39-
organizationId: organizationIds[0] || turnkeyConfig.defaultOrganizationId,
40-
};
41-
}
42-
4329
export async function sendOtp(
4430
req: Request<{}, {}, SendOtpParams>
4531
): Promise<SendOtpResponse> {
@@ -103,7 +89,7 @@ export async function verifyOtp(
10389
export async function createSubOrg(
10490
req: Request<{}, {}, CreateSubOrgParams>
10591
): Promise<CreateSubOrgResponse> {
106-
const { email, phone, passkey, apiKeys } = req.body;
92+
const { email, phone, passkey, oauth, apiKeys } = req.body;
10793

10894
const authenticators = passkey
10995
? [
@@ -115,8 +101,17 @@ export async function createSubOrg(
115101
]
116102
: [];
117103

118-
let userEmail = email;
119-
const userPhoneNumber = phone;
104+
const oauthProviders = oauth
105+
? [
106+
{
107+
providerName: oauth.providerName,
108+
oidcToken: oauth.oidcToken,
109+
},
110+
]
111+
: [];
112+
113+
let userEmail = email;
114+
const userPhoneNumber = phone;
120115

121116
const subOrganizationName = `Sub Org - ${new Date().toISOString()}`;
122117

@@ -129,8 +124,8 @@ export async function createSubOrg(
129124
userEmail,
130125
userPhoneNumber,
131126
authenticators,
127+
oauthProviders: oauthProviders,
132128
apiKeys: apiKeys ?? [],
133-
oauthProviders: [],
134129
},
135130
],
136131
rootQuorumThreshold: 1,
@@ -142,3 +137,41 @@ export async function createSubOrg(
142137

143138
return { subOrganizationId: result.subOrganizationId };
144139
}
140+
141+
export async function oAuth(
142+
req: Request<{}, {}, OAuthParams>
143+
): Promise<OAuthResponse> {
144+
const { publicKey, providerName, oidcToken, expirationSeconds } = req.body;
145+
146+
let organizationId = turnkeyConfig.defaultOrganizationId;
147+
148+
const { organizationIds } = await turnkey.getSubOrgIds({
149+
filterType: "OIDC_TOKEN",
150+
filterValue: oidcToken,
151+
});
152+
153+
if (organizationIds.length > 0) {
154+
organizationId = organizationIds[0];
155+
} else {
156+
const createSubOrgParams = {
157+
oauth: {
158+
providerName,
159+
oidcToken,
160+
},
161+
};
162+
163+
const subOrgResponse = await createSubOrg({
164+
body: createSubOrgParams,
165+
} as Request);
166+
organizationId = subOrgResponse.subOrganizationId;
167+
}
168+
169+
const sessionResponse = await turnkey.oauthLogin({
170+
publicKey,
171+
organizationId,
172+
oidcToken,
173+
expirationSeconds,
174+
});
175+
176+
return { token: sessionResponse.session };
177+
}

Examples/swift-demo-wallet/example-server/src/types.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,6 @@ export enum FilterType {
1010
PhoneNumber = "PHONE_NUMBER",
1111
}
1212

13-
export type GetSubOrgIdParams = {
14-
filterType:
15-
| "NAME"
16-
| "USERNAME"
17-
| "EMAIL"
18-
| "PHONE_NUMBER"
19-
| "CREDENTIAL_ID"
20-
| "PUBLIC_KEY"
21-
| "OIDC_TOKEN";
22-
filterValue: string;
23-
};
24-
25-
export type GetSubOrgIdResponse = {
26-
organizationId: string;
27-
};
28-
2913
export type SendOtpParams = {
3014
otpType: "OTP_TYPE_EMAIL" | "OTP_TYPE_SMS";
3115
contact: string;
@@ -44,6 +28,10 @@ export type CreateSubOrgParams = {
4428
challenge: string;
4529
attestation: Attestation;
4630
};
31+
oauth?: {
32+
providerName: string;
33+
oidcToken: string;
34+
};
4735
apiKeys?: {
4836
apiKeyName: string;
4937
publicKey: string;
@@ -69,5 +57,16 @@ export type VerifyOtpResponse = {
6957
token?: string;
7058
};
7159

60+
export type OAuthParams = {
61+
publicKey: string;
62+
providerName: string;
63+
oidcToken: string;
64+
expirationSeconds: string;
65+
};
66+
67+
export type OAuthResponse = {
68+
token: string;
69+
}
70+
7271
export type Attestation = TurnkeyApiTypes["v1Attestation"];
7372
export type ApiKeyCurveType = TurnkeyApiTypes["v1ApiKeyCurve"];
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"images" : [
3+
{
4+
"filename" : "google-icon.png",
5+
"idiom" : "universal",
6+
"scale" : "1x"
7+
},
8+
{
9+
"idiom" : "universal",
10+
"scale" : "2x"
11+
},
12+
{
13+
"idiom" : "universal",
14+
"scale" : "3x"
15+
}
16+
],
17+
"info" : {
18+
"author" : "xcode",
19+
"version" : 1
20+
}
21+
}
6.41 KB
Loading

0 commit comments

Comments
 (0)