emulate 0.1.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +125 -5
- package/dist/api.d.ts +23 -0
- package/dist/api.js +713 -0
- package/dist/api.js.map +1 -0
- package/dist/chunk-D6EKRYGP.js +1615 -0
- package/dist/chunk-D6EKRYGP.js.map +1 -0
- package/dist/chunk-TEPNEZ63.js +2143 -0
- package/dist/chunk-TEPNEZ63.js.map +1 -0
- package/dist/dist-BKXG6HVH.js +3641 -0
- package/dist/dist-BKXG6HVH.js.map +1 -0
- package/dist/dist-DSSB3LYT.js +788 -0
- package/dist/dist-DSSB3LYT.js.map +1 -0
- package/dist/dist-JYDZIVC6.js +2684 -0
- package/dist/dist-JYDZIVC6.js.map +1 -0
- package/dist/dist-O4KFIBVU.js +626 -0
- package/dist/dist-O4KFIBVU.js.map +1 -0
- package/dist/dist-OCDKIMRJ.js +10586 -0
- package/dist/dist-OCDKIMRJ.js.map +1 -0
- package/dist/dist-UZSUUE3Y.js +1287 -0
- package/dist/dist-UZSUUE3Y.js.map +1 -0
- package/dist/dist-VVXVP5EZ.js +1199 -0
- package/dist/dist-VVXVP5EZ.js.map +1 -0
- package/dist/fonts/GeistPixel-Square.woff2 +0 -0
- package/dist/fonts/geist-sans.woff2 +0 -0
- package/dist/index.js +615 -17326
- package/dist/index.js.map +1 -1
- package/package.json +21 -5
|
@@ -0,0 +1,626 @@
|
|
|
1
|
+
import {
|
|
2
|
+
SignJWT,
|
|
3
|
+
exportJWK,
|
|
4
|
+
generateKeyPair
|
|
5
|
+
} from "./chunk-D6EKRYGP.js";
|
|
6
|
+
import "./chunk-TEPNEZ63.js";
|
|
7
|
+
|
|
8
|
+
// ../@emulators/apple/dist/index.js
|
|
9
|
+
import { randomBytes } from "crypto";
|
|
10
|
+
import { randomBytes as randomBytes2 } from "crypto";
|
|
11
|
+
import { readFileSync } from "fs";
|
|
12
|
+
import { fileURLToPath } from "url";
|
|
13
|
+
import { dirname, join } from "path";
|
|
14
|
+
function getAppleStore(store) {
|
|
15
|
+
return {
|
|
16
|
+
users: store.collection("apple.users", ["uid", "email"]),
|
|
17
|
+
oauthClients: store.collection("apple.oauth_clients", ["client_id"])
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
function generateAppleUid() {
|
|
21
|
+
const prefix = randomBytes(3).toString("hex").toUpperCase();
|
|
22
|
+
const middle = randomBytes(16).toString("hex");
|
|
23
|
+
const suffix = randomBytes(2).toString("hex").toUpperCase();
|
|
24
|
+
return `${prefix}.${middle}.${suffix}`;
|
|
25
|
+
}
|
|
26
|
+
function generatePrivateRelayEmail() {
|
|
27
|
+
const id = randomBytes(12).toString("hex");
|
|
28
|
+
return `${id}@privaterelay.appleid.com`;
|
|
29
|
+
}
|
|
30
|
+
function createErrorHandler(documentationUrl) {
|
|
31
|
+
return async (c, next) => {
|
|
32
|
+
if (documentationUrl) {
|
|
33
|
+
c.set("docsUrl", documentationUrl);
|
|
34
|
+
}
|
|
35
|
+
await next();
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
var errorHandler = createErrorHandler();
|
|
39
|
+
var isDebug = typeof process !== "undefined" && (process.env.DEBUG === "1" || process.env.DEBUG === "true" || process.env.EMULATE_DEBUG === "1");
|
|
40
|
+
function debug(label, ...args) {
|
|
41
|
+
if (isDebug) {
|
|
42
|
+
console.log(`[${label}]`, ...args);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
var __dirname = dirname(fileURLToPath(import.meta.url));
|
|
46
|
+
var FONTS = {
|
|
47
|
+
"geist-sans.woff2": readFileSync(join(__dirname, "fonts", "geist-sans.woff2")),
|
|
48
|
+
"GeistPixel-Square.woff2": readFileSync(join(__dirname, "fonts", "GeistPixel-Square.woff2"))
|
|
49
|
+
};
|
|
50
|
+
function escapeHtml(s) {
|
|
51
|
+
return s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
|
52
|
+
}
|
|
53
|
+
function escapeAttr(s) {
|
|
54
|
+
return escapeHtml(s).replace(/'/g, "'");
|
|
55
|
+
}
|
|
56
|
+
var CSS = `
|
|
57
|
+
@font-face{
|
|
58
|
+
font-family:'Geist';font-style:normal;font-weight:100 900;font-display:swap;
|
|
59
|
+
src:url('/_emulate/fonts/geist-sans.woff2') format('woff2');
|
|
60
|
+
}
|
|
61
|
+
@font-face{
|
|
62
|
+
font-family:'Geist Pixel';font-style:normal;font-weight:400;font-display:swap;
|
|
63
|
+
src:url('/_emulate/fonts/GeistPixel-Square.woff2') format('woff2');
|
|
64
|
+
}
|
|
65
|
+
*{box-sizing:border-box;margin:0;padding:0}
|
|
66
|
+
body{
|
|
67
|
+
font-family:'Geist',-apple-system,BlinkMacSystemFont,sans-serif;
|
|
68
|
+
background:#000;color:#33ff00;min-height:100vh;
|
|
69
|
+
-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale;
|
|
70
|
+
}
|
|
71
|
+
.emu-bar{
|
|
72
|
+
border-bottom:1px solid #0a3300;padding:10px 20px;
|
|
73
|
+
display:flex;align-items:center;gap:10px;font-size:.8125rem;color:#1a8c00;
|
|
74
|
+
}
|
|
75
|
+
.emu-bar-title{font-weight:600;color:#33ff00;font-family:'Geist Pixel',monospace;}
|
|
76
|
+
.emu-bar-links{margin-left:auto;display:flex;gap:16px;}
|
|
77
|
+
.emu-bar-links a{
|
|
78
|
+
color:#1a8c00;font-size:.75rem;text-decoration:none;transition:color .15s;
|
|
79
|
+
}
|
|
80
|
+
.emu-bar-links a:hover{color:#33ff00;}
|
|
81
|
+
.emu-bar-links a .full{display:inline;}
|
|
82
|
+
.emu-bar-links a .short{display:none;}
|
|
83
|
+
@media(max-width:600px){
|
|
84
|
+
.emu-bar-links a .full{display:none;}
|
|
85
|
+
.emu-bar-links a .short{display:inline;}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
.content{
|
|
89
|
+
display:flex;align-items:center;justify-content:center;
|
|
90
|
+
min-height:calc(100vh - 42px);padding:24px 16px;
|
|
91
|
+
}
|
|
92
|
+
.content-inner{width:100%;max-width:420px;}
|
|
93
|
+
.card-title{
|
|
94
|
+
font-family:'Geist Pixel',monospace;
|
|
95
|
+
font-size:1.125rem;font-weight:600;margin-bottom:4px;color:#33ff00;
|
|
96
|
+
}
|
|
97
|
+
.card-subtitle{color:#1a8c00;font-size:.8125rem;margin-bottom:18px;line-height:1.45;}
|
|
98
|
+
.powered-by{
|
|
99
|
+
position:fixed;bottom:0;left:0;right:0;
|
|
100
|
+
text-align:center;padding:12px;font-size:.6875rem;color:#0a3300;
|
|
101
|
+
font-family:'Geist Pixel',monospace;
|
|
102
|
+
}
|
|
103
|
+
.powered-by a{color:#1a8c00;text-decoration:none;transition:color .15s;}
|
|
104
|
+
.powered-by a:hover{color:#33ff00;}
|
|
105
|
+
|
|
106
|
+
.error-title{
|
|
107
|
+
font-family:'Geist Pixel',monospace;
|
|
108
|
+
color:#ff4444;font-size:1.125rem;font-weight:600;margin-bottom:8px;
|
|
109
|
+
}
|
|
110
|
+
.error-msg{color:#1a8c00;font-size:.875rem;line-height:1.5;}
|
|
111
|
+
.error-card{text-align:center;}
|
|
112
|
+
|
|
113
|
+
.user-form{margin-bottom:8px;}
|
|
114
|
+
.user-form:last-of-type{margin-bottom:0;}
|
|
115
|
+
.user-btn{
|
|
116
|
+
width:100%;display:flex;align-items:center;gap:12px;
|
|
117
|
+
padding:10px 12px;border:1px solid #0a3300;border-radius:8px;
|
|
118
|
+
background:#000;color:inherit;cursor:pointer;text-align:left;
|
|
119
|
+
font:inherit;transition:border-color .15s;
|
|
120
|
+
}
|
|
121
|
+
.user-btn:hover{border-color:#33ff00;}
|
|
122
|
+
.avatar{
|
|
123
|
+
width:36px;height:36px;border-radius:50%;
|
|
124
|
+
background:#0a3300;color:#33ff00;font-weight:600;font-size:.875rem;
|
|
125
|
+
display:flex;align-items:center;justify-content:center;flex-shrink:0;
|
|
126
|
+
font-family:'Geist Pixel',monospace;
|
|
127
|
+
}
|
|
128
|
+
.user-text{min-width:0;}
|
|
129
|
+
.user-login{font-weight:600;font-size:.875rem;display:block;color:#33ff00;}
|
|
130
|
+
.user-meta{color:#1a8c00;font-size:.75rem;margin-top:1px;}
|
|
131
|
+
.user-email{font-size:.6875rem;color:#116600;word-break:break-all;margin-top:1px;}
|
|
132
|
+
|
|
133
|
+
.settings-layout{
|
|
134
|
+
max-width:920px;margin:0 auto;padding:28px 20px;
|
|
135
|
+
display:flex;gap:28px;
|
|
136
|
+
}
|
|
137
|
+
.settings-sidebar{width:200px;flex-shrink:0;}
|
|
138
|
+
.settings-sidebar a{
|
|
139
|
+
display:block;padding:6px 10px;border-radius:6px;color:#1a8c00;
|
|
140
|
+
text-decoration:none;font-size:.8125rem;transition:color .15s;
|
|
141
|
+
}
|
|
142
|
+
.settings-sidebar a:hover{color:#33ff00;}
|
|
143
|
+
.settings-sidebar a.active{color:#33ff00;font-weight:600;}
|
|
144
|
+
.settings-main{flex:1;min-width:0;}
|
|
145
|
+
|
|
146
|
+
.s-card{
|
|
147
|
+
padding:18px 0;margin-bottom:14px;border-bottom:1px solid #0a3300;
|
|
148
|
+
}
|
|
149
|
+
.s-card:last-child{border-bottom:none;}
|
|
150
|
+
.s-card-header{display:flex;align-items:center;gap:14px;margin-bottom:14px;}
|
|
151
|
+
.s-icon{
|
|
152
|
+
width:42px;height:42px;border-radius:8px;
|
|
153
|
+
background:#0a3300;display:flex;align-items:center;justify-content:center;
|
|
154
|
+
font-size:1.125rem;font-weight:700;color:#116600;flex-shrink:0;
|
|
155
|
+
font-family:'Geist Pixel',monospace;
|
|
156
|
+
}
|
|
157
|
+
.s-title{
|
|
158
|
+
font-family:'Geist Pixel',monospace;
|
|
159
|
+
font-size:1.25rem;font-weight:600;color:#33ff00;
|
|
160
|
+
}
|
|
161
|
+
.s-subtitle{font-size:.75rem;color:#1a8c00;margin-top:2px;}
|
|
162
|
+
.section-heading{
|
|
163
|
+
font-size:.9375rem;font-weight:600;margin-bottom:10px;color:#33ff00;
|
|
164
|
+
display:flex;align-items:center;justify-content:space-between;
|
|
165
|
+
}
|
|
166
|
+
.perm-list{list-style:none;}
|
|
167
|
+
.perm-list li{padding:5px 0;font-size:.8125rem;display:flex;align-items:center;gap:6px;color:#1a8c00;}
|
|
168
|
+
.check{color:#33ff00;}
|
|
169
|
+
.org-row{
|
|
170
|
+
display:flex;align-items:center;gap:8px;padding:7px 0;
|
|
171
|
+
border-bottom:1px solid #0a3300;font-size:.8125rem;
|
|
172
|
+
}
|
|
173
|
+
.org-row:last-child{border-bottom:none;}
|
|
174
|
+
.org-icon{
|
|
175
|
+
width:22px;height:22px;border-radius:4px;background:#0a3300;
|
|
176
|
+
display:flex;align-items:center;justify-content:center;
|
|
177
|
+
font-size:.625rem;font-weight:700;color:#116600;flex-shrink:0;
|
|
178
|
+
font-family:'Geist Pixel',monospace;
|
|
179
|
+
}
|
|
180
|
+
.org-name{font-weight:600;color:#33ff00;}
|
|
181
|
+
.badge{font-size:.6875rem;padding:1px 7px;border-radius:999px;font-weight:500;}
|
|
182
|
+
.badge-granted{background:#0a3300;color:#33ff00;}
|
|
183
|
+
.badge-denied{background:#1a0a0a;color:#ff4444;}
|
|
184
|
+
.badge-requested{background:#0a3300;color:#1a8c00;}
|
|
185
|
+
.btn-revoke{
|
|
186
|
+
display:inline-block;padding:5px 14px;border-radius:6px;
|
|
187
|
+
border:1px solid #0a3300;background:transparent;color:#ff4444;
|
|
188
|
+
font-size:.75rem;font-weight:600;cursor:pointer;transition:border-color .15s;
|
|
189
|
+
}
|
|
190
|
+
.btn-revoke:hover{border-color:#ff4444;}
|
|
191
|
+
.info-text{color:#1a8c00;font-size:.75rem;line-height:1.5;margin-top:10px;}
|
|
192
|
+
.app-link{
|
|
193
|
+
display:flex;align-items:center;gap:12px;padding:12px;
|
|
194
|
+
border:1px solid #0a3300;border-radius:8px;background:#000;
|
|
195
|
+
text-decoration:none;color:inherit;margin-bottom:8px;transition:border-color .15s;
|
|
196
|
+
}
|
|
197
|
+
.app-link:hover{border-color:#33ff00;}
|
|
198
|
+
.app-link-name{font-weight:600;font-size:.875rem;color:#33ff00;}
|
|
199
|
+
.app-link-scopes{font-size:.6875rem;color:#1a8c00;margin-top:1px;}
|
|
200
|
+
.empty{color:#1a8c00;text-align:center;padding:28px 0;font-size:.875rem;}
|
|
201
|
+
`;
|
|
202
|
+
var POWERED_BY = `<div class="powered-by">Powered by <a href="https://emulate.dev" target="_blank" rel="noopener">emulate</a></div>`;
|
|
203
|
+
function emuBar(service) {
|
|
204
|
+
const title = service ? `${escapeHtml(service)} Emulator` : "Emulator";
|
|
205
|
+
return `<div class="emu-bar">
|
|
206
|
+
<span class="emu-bar-title">${title}</span>
|
|
207
|
+
<nav class="emu-bar-links">
|
|
208
|
+
<a href="https://github.com/vercel-labs/emulate/issues" target="_blank" rel="noopener"><span class="full">Report Issue</span><span class="short">Report</span></a>
|
|
209
|
+
<a href="https://github.com/vercel-labs/emulate" target="_blank" rel="noopener"><span class="full">Source Code</span><span class="short">Source</span></a>
|
|
210
|
+
<a href="https://emulate.dev" target="_blank" rel="noopener"><span class="full">Learn More</span><span class="short">Learn</span></a>
|
|
211
|
+
</nav>
|
|
212
|
+
</div>`;
|
|
213
|
+
}
|
|
214
|
+
function head(title) {
|
|
215
|
+
return `<!DOCTYPE html>
|
|
216
|
+
<html lang="en">
|
|
217
|
+
<head>
|
|
218
|
+
<meta charset="utf-8"/>
|
|
219
|
+
<meta name="viewport" content="width=device-width,initial-scale=1"/>
|
|
220
|
+
<title>${escapeHtml(title)} | emulate</title>
|
|
221
|
+
<style>${CSS}</style>
|
|
222
|
+
</head>`;
|
|
223
|
+
}
|
|
224
|
+
function renderCardPage(title, subtitle, body, service) {
|
|
225
|
+
return `${head(title)}
|
|
226
|
+
<body>
|
|
227
|
+
${emuBar(service)}
|
|
228
|
+
<div class="content">
|
|
229
|
+
<div class="content-inner">
|
|
230
|
+
<div class="card-title">${escapeHtml(title)}</div>
|
|
231
|
+
<div class="card-subtitle">${subtitle}</div>
|
|
232
|
+
${body}
|
|
233
|
+
</div>
|
|
234
|
+
</div>
|
|
235
|
+
${POWERED_BY}
|
|
236
|
+
</body></html>`;
|
|
237
|
+
}
|
|
238
|
+
function renderErrorPage(title, message, service) {
|
|
239
|
+
return `${head(title)}
|
|
240
|
+
<body>
|
|
241
|
+
${emuBar(service)}
|
|
242
|
+
<div class="content">
|
|
243
|
+
<div class="content-inner error-card">
|
|
244
|
+
<div class="error-title">${escapeHtml(title)}</div>
|
|
245
|
+
<div class="error-msg">${escapeHtml(message)}</div>
|
|
246
|
+
</div>
|
|
247
|
+
</div>
|
|
248
|
+
${POWERED_BY}
|
|
249
|
+
</body></html>`;
|
|
250
|
+
}
|
|
251
|
+
function renderUserButton(opts) {
|
|
252
|
+
const hiddens = Object.entries(opts.hiddenFields).map(([k, v]) => `<input type="hidden" name="${escapeAttr(k)}" value="${escapeAttr(v)}"/>`).join("");
|
|
253
|
+
const nameLine = opts.name ? `<div class="user-meta">${escapeHtml(opts.name)}</div>` : "";
|
|
254
|
+
const emailLine = opts.email ? `<div class="user-email">${escapeHtml(opts.email)}</div>` : "";
|
|
255
|
+
return `<form class="user-form" method="post" action="${escapeAttr(opts.formAction)}">
|
|
256
|
+
${hiddens}
|
|
257
|
+
<button type="submit" class="user-btn">
|
|
258
|
+
<span class="avatar">${escapeHtml(opts.letter)}</span>
|
|
259
|
+
<span class="user-text">
|
|
260
|
+
<span class="user-login">${escapeHtml(opts.login)}</span>
|
|
261
|
+
${nameLine}${emailLine}
|
|
262
|
+
</span>
|
|
263
|
+
</button>
|
|
264
|
+
</form>`;
|
|
265
|
+
}
|
|
266
|
+
function normalizeUri(uri) {
|
|
267
|
+
try {
|
|
268
|
+
const u = new URL(uri);
|
|
269
|
+
return `${u.origin}${u.pathname.replace(/\/+$/, "")}`;
|
|
270
|
+
} catch {
|
|
271
|
+
return uri.replace(/\/+$/, "").split("?")[0];
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
function matchesRedirectUri(incoming, registered) {
|
|
275
|
+
const normalized = normalizeUri(incoming);
|
|
276
|
+
return registered.some((r) => normalizeUri(r) === normalized);
|
|
277
|
+
}
|
|
278
|
+
function bodyStr(v) {
|
|
279
|
+
if (typeof v === "string") return v;
|
|
280
|
+
if (Array.isArray(v) && typeof v[0] === "string") return v[0];
|
|
281
|
+
return "";
|
|
282
|
+
}
|
|
283
|
+
var keyPairPromise = generateKeyPair("RS256");
|
|
284
|
+
var KID = "emulate-apple-1";
|
|
285
|
+
var PENDING_CODE_TTL_MS = 5 * 60 * 1e3;
|
|
286
|
+
function getPendingCodes(store) {
|
|
287
|
+
let map = store.getData("apple.oauth.pendingCodes");
|
|
288
|
+
if (!map) {
|
|
289
|
+
map = /* @__PURE__ */ new Map();
|
|
290
|
+
store.setData("apple.oauth.pendingCodes", map);
|
|
291
|
+
}
|
|
292
|
+
return map;
|
|
293
|
+
}
|
|
294
|
+
function getRefreshTokens(store) {
|
|
295
|
+
let map = store.getData("apple.oauth.refreshTokens");
|
|
296
|
+
if (!map) {
|
|
297
|
+
map = /* @__PURE__ */ new Map();
|
|
298
|
+
store.setData("apple.oauth.refreshTokens", map);
|
|
299
|
+
}
|
|
300
|
+
return map;
|
|
301
|
+
}
|
|
302
|
+
function getFirstAuthTracker(store) {
|
|
303
|
+
let set = store.getData("apple.oauth.firstAuthTracker");
|
|
304
|
+
if (!set) {
|
|
305
|
+
set = /* @__PURE__ */ new Set();
|
|
306
|
+
store.setData("apple.oauth.firstAuthTracker", set);
|
|
307
|
+
}
|
|
308
|
+
return set;
|
|
309
|
+
}
|
|
310
|
+
function isPendingCodeExpired(p) {
|
|
311
|
+
return Date.now() - p.created_at > PENDING_CODE_TTL_MS;
|
|
312
|
+
}
|
|
313
|
+
var SERVICE_LABEL = "Apple";
|
|
314
|
+
async function createIdToken(user, clientId, nonce, baseUrl) {
|
|
315
|
+
const { privateKey } = await keyPairPromise;
|
|
316
|
+
const email = user.is_private_email && user.private_relay_email ? user.private_relay_email : user.email;
|
|
317
|
+
const now = Math.floor(Date.now() / 1e3);
|
|
318
|
+
const builder = new SignJWT({
|
|
319
|
+
sub: user.uid,
|
|
320
|
+
email,
|
|
321
|
+
email_verified: String(user.email_verified),
|
|
322
|
+
is_private_email: String(user.is_private_email),
|
|
323
|
+
real_user_status: user.real_user_status,
|
|
324
|
+
nonce_supported: true,
|
|
325
|
+
auth_time: now,
|
|
326
|
+
...nonce ? { nonce } : {}
|
|
327
|
+
}).setProtectedHeader({ alg: "RS256", kid: KID, typ: "JWT" }).setIssuer(baseUrl).setAudience(clientId).setIssuedAt(now).setExpirationTime("1h");
|
|
328
|
+
return builder.sign(privateKey);
|
|
329
|
+
}
|
|
330
|
+
function oauthRoutes({ app, store, baseUrl, tokenMap }) {
|
|
331
|
+
const as = getAppleStore(store);
|
|
332
|
+
app.get("/.well-known/openid-configuration", (c) => {
|
|
333
|
+
return c.json({
|
|
334
|
+
issuer: baseUrl,
|
|
335
|
+
authorization_endpoint: `${baseUrl}/auth/authorize`,
|
|
336
|
+
token_endpoint: `${baseUrl}/auth/token`,
|
|
337
|
+
revocation_endpoint: `${baseUrl}/auth/revoke`,
|
|
338
|
+
jwks_uri: `${baseUrl}/auth/keys`,
|
|
339
|
+
response_types_supported: ["code"],
|
|
340
|
+
response_modes_supported: ["query", "fragment", "form_post"],
|
|
341
|
+
subject_types_supported: ["pairwise"],
|
|
342
|
+
id_token_signing_alg_values_supported: ["RS256"],
|
|
343
|
+
scopes_supported: ["openid", "email", "name"],
|
|
344
|
+
token_endpoint_auth_methods_supported: ["client_secret_post"],
|
|
345
|
+
claims_supported: [
|
|
346
|
+
"aud",
|
|
347
|
+
"email",
|
|
348
|
+
"email_verified",
|
|
349
|
+
"exp",
|
|
350
|
+
"iat",
|
|
351
|
+
"is_private_email",
|
|
352
|
+
"iss",
|
|
353
|
+
"nonce",
|
|
354
|
+
"nonce_supported",
|
|
355
|
+
"real_user_status",
|
|
356
|
+
"sub",
|
|
357
|
+
"transfer_sub"
|
|
358
|
+
]
|
|
359
|
+
});
|
|
360
|
+
});
|
|
361
|
+
app.get("/auth/keys", async (c) => {
|
|
362
|
+
const { publicKey } = await keyPairPromise;
|
|
363
|
+
const jwk = await exportJWK(publicKey);
|
|
364
|
+
return c.json({
|
|
365
|
+
keys: [{
|
|
366
|
+
...jwk,
|
|
367
|
+
kid: KID,
|
|
368
|
+
use: "sig",
|
|
369
|
+
alg: "RS256"
|
|
370
|
+
}]
|
|
371
|
+
});
|
|
372
|
+
});
|
|
373
|
+
app.get("/auth/authorize", (c) => {
|
|
374
|
+
const client_id = c.req.query("client_id") ?? "";
|
|
375
|
+
const redirect_uri = c.req.query("redirect_uri") ?? "";
|
|
376
|
+
const scope = c.req.query("scope") ?? "";
|
|
377
|
+
const state = c.req.query("state") ?? "";
|
|
378
|
+
const nonce = c.req.query("nonce") ?? "";
|
|
379
|
+
const response_mode = c.req.query("response_mode") ?? "query";
|
|
380
|
+
const clientsConfigured = as.oauthClients.all().length > 0;
|
|
381
|
+
let clientName = "";
|
|
382
|
+
if (clientsConfigured) {
|
|
383
|
+
const client = as.oauthClients.findOneBy("client_id", client_id);
|
|
384
|
+
if (!client) {
|
|
385
|
+
return c.html(
|
|
386
|
+
renderErrorPage("Application not found", `The client_id '${client_id}' is not registered.`, SERVICE_LABEL),
|
|
387
|
+
400
|
|
388
|
+
);
|
|
389
|
+
}
|
|
390
|
+
if (redirect_uri && !matchesRedirectUri(redirect_uri, client.redirect_uris)) {
|
|
391
|
+
return c.html(
|
|
392
|
+
renderErrorPage("Redirect URI mismatch", "The redirect_uri is not registered for this application.", SERVICE_LABEL),
|
|
393
|
+
400
|
|
394
|
+
);
|
|
395
|
+
}
|
|
396
|
+
clientName = client.name;
|
|
397
|
+
}
|
|
398
|
+
const subtitleText = clientName ? `Sign in to <strong>${escapeHtml(clientName)}</strong> with your Apple ID.` : "Choose a seeded user to continue.";
|
|
399
|
+
const users = as.users.all();
|
|
400
|
+
const userButtons = users.map((user) => {
|
|
401
|
+
return renderUserButton({
|
|
402
|
+
letter: (user.email[0] ?? "?").toUpperCase(),
|
|
403
|
+
login: user.email,
|
|
404
|
+
name: user.name,
|
|
405
|
+
email: user.email,
|
|
406
|
+
formAction: "/auth/authorize/callback",
|
|
407
|
+
hiddenFields: {
|
|
408
|
+
email: user.email,
|
|
409
|
+
redirect_uri,
|
|
410
|
+
scope,
|
|
411
|
+
state,
|
|
412
|
+
nonce,
|
|
413
|
+
client_id,
|
|
414
|
+
response_mode
|
|
415
|
+
}
|
|
416
|
+
});
|
|
417
|
+
}).join("\n");
|
|
418
|
+
const body = users.length === 0 ? '<p class="empty">No users in the emulator store.</p>' : userButtons;
|
|
419
|
+
return c.html(renderCardPage("Sign in with Apple", subtitleText, body, SERVICE_LABEL));
|
|
420
|
+
});
|
|
421
|
+
app.post("/auth/authorize/callback", async (c) => {
|
|
422
|
+
const body = await c.req.parseBody();
|
|
423
|
+
const email = bodyStr(body.email);
|
|
424
|
+
const redirect_uri = bodyStr(body.redirect_uri);
|
|
425
|
+
const scope = bodyStr(body.scope);
|
|
426
|
+
const state = bodyStr(body.state);
|
|
427
|
+
const client_id = bodyStr(body.client_id);
|
|
428
|
+
const nonce = bodyStr(body.nonce);
|
|
429
|
+
const response_mode = bodyStr(body.response_mode) || "query";
|
|
430
|
+
const code = randomBytes2(20).toString("hex");
|
|
431
|
+
getPendingCodes(store).set(code, {
|
|
432
|
+
email,
|
|
433
|
+
scope,
|
|
434
|
+
redirectUri: redirect_uri,
|
|
435
|
+
clientId: client_id,
|
|
436
|
+
nonce: nonce || null,
|
|
437
|
+
responseMode: response_mode,
|
|
438
|
+
created_at: Date.now()
|
|
439
|
+
});
|
|
440
|
+
debug("apple.oauth", `[Apple callback] code=${code.slice(0, 8)}... email=${email}`);
|
|
441
|
+
const tracker = getFirstAuthTracker(store);
|
|
442
|
+
const pairKey = `${email}:${client_id}`;
|
|
443
|
+
const isFirstAuth = !tracker.has(pairKey);
|
|
444
|
+
if (isFirstAuth) {
|
|
445
|
+
tracker.add(pairKey);
|
|
446
|
+
}
|
|
447
|
+
let userJson;
|
|
448
|
+
if (isFirstAuth) {
|
|
449
|
+
const user = as.users.findOneBy("email", email);
|
|
450
|
+
if (user) {
|
|
451
|
+
userJson = JSON.stringify({
|
|
452
|
+
name: { firstName: user.given_name, lastName: user.family_name },
|
|
453
|
+
email: user.email
|
|
454
|
+
});
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
if (response_mode === "form_post") {
|
|
458
|
+
const html = `<!DOCTYPE html>
|
|
459
|
+
<html>
|
|
460
|
+
<head><title>Submit</title></head>
|
|
461
|
+
<body onload="document.forms[0].submit()">
|
|
462
|
+
<form method="POST" action="${escapeAttr(redirect_uri)}">
|
|
463
|
+
<input type="hidden" name="code" value="${escapeAttr(code)}" />
|
|
464
|
+
<input type="hidden" name="state" value="${escapeAttr(state)}" />${userJson ? `
|
|
465
|
+
<input type="hidden" name="user" value="${escapeAttr(userJson)}" />` : ""}
|
|
466
|
+
</form>
|
|
467
|
+
</body>
|
|
468
|
+
</html>`;
|
|
469
|
+
return c.html(html);
|
|
470
|
+
}
|
|
471
|
+
const url = new URL(redirect_uri);
|
|
472
|
+
url.searchParams.set("code", code);
|
|
473
|
+
if (state) url.searchParams.set("state", state);
|
|
474
|
+
if (userJson) url.searchParams.set("user", userJson);
|
|
475
|
+
return c.redirect(url.toString(), 302);
|
|
476
|
+
});
|
|
477
|
+
app.post("/auth/token", async (c) => {
|
|
478
|
+
const rawText = await c.req.text();
|
|
479
|
+
const body = Object.fromEntries(new URLSearchParams(rawText));
|
|
480
|
+
const grant_type = body.grant_type ?? "";
|
|
481
|
+
const code = body.code ?? "";
|
|
482
|
+
const client_id = body.client_id ?? "";
|
|
483
|
+
const refresh_token = body.refresh_token ?? "";
|
|
484
|
+
if (grant_type === "authorization_code") {
|
|
485
|
+
const pendingMap = getPendingCodes(store);
|
|
486
|
+
const pending = pendingMap.get(code);
|
|
487
|
+
if (!pending) {
|
|
488
|
+
return c.json({ error: "invalid_grant", error_description: "The code is incorrect or expired." }, 400);
|
|
489
|
+
}
|
|
490
|
+
if (isPendingCodeExpired(pending)) {
|
|
491
|
+
pendingMap.delete(code);
|
|
492
|
+
return c.json({ error: "invalid_grant", error_description: "The code is incorrect or expired." }, 400);
|
|
493
|
+
}
|
|
494
|
+
pendingMap.delete(code);
|
|
495
|
+
const user = as.users.findOneBy("email", pending.email);
|
|
496
|
+
if (!user) {
|
|
497
|
+
return c.json({ error: "invalid_grant", error_description: "User not found." }, 400);
|
|
498
|
+
}
|
|
499
|
+
const accessToken = "apple_" + randomBytes2(20).toString("base64url");
|
|
500
|
+
const refreshToken = "r_apple_" + randomBytes2(20).toString("base64url");
|
|
501
|
+
const scopes = pending.scope ? pending.scope.split(/\s+/).filter(Boolean) : [];
|
|
502
|
+
if (tokenMap) {
|
|
503
|
+
tokenMap.set(accessToken, { login: user.email, id: user.id, scopes });
|
|
504
|
+
}
|
|
505
|
+
getRefreshTokens(store).set(refreshToken, {
|
|
506
|
+
email: user.email,
|
|
507
|
+
clientId: pending.clientId,
|
|
508
|
+
scope: pending.scope,
|
|
509
|
+
nonce: pending.nonce
|
|
510
|
+
});
|
|
511
|
+
const idToken = await createIdToken(user, pending.clientId, pending.nonce, baseUrl);
|
|
512
|
+
debug("apple.oauth", `[Apple token] issued token for ${user.email}`);
|
|
513
|
+
return c.json({
|
|
514
|
+
access_token: accessToken,
|
|
515
|
+
token_type: "Bearer",
|
|
516
|
+
expires_in: 3600,
|
|
517
|
+
refresh_token: refreshToken,
|
|
518
|
+
id_token: idToken
|
|
519
|
+
});
|
|
520
|
+
}
|
|
521
|
+
if (grant_type === "refresh_token") {
|
|
522
|
+
const refreshMap = getRefreshTokens(store);
|
|
523
|
+
const stored = refreshMap.get(refresh_token);
|
|
524
|
+
if (!stored) {
|
|
525
|
+
return c.json({ error: "invalid_grant", error_description: "The refresh_token is invalid." }, 400);
|
|
526
|
+
}
|
|
527
|
+
const user = as.users.findOneBy("email", stored.email);
|
|
528
|
+
if (!user) {
|
|
529
|
+
return c.json({ error: "invalid_grant", error_description: "User not found." }, 400);
|
|
530
|
+
}
|
|
531
|
+
const accessToken = "apple_" + randomBytes2(20).toString("base64url");
|
|
532
|
+
const scopes = stored.scope ? stored.scope.split(/\s+/).filter(Boolean) : [];
|
|
533
|
+
if (tokenMap) {
|
|
534
|
+
tokenMap.set(accessToken, { login: user.email, id: user.id, scopes });
|
|
535
|
+
}
|
|
536
|
+
const idToken = await createIdToken(user, stored.clientId || client_id, stored.nonce, baseUrl);
|
|
537
|
+
debug("apple.oauth", `[Apple refresh] issued new token for ${user.email}`);
|
|
538
|
+
return c.json({
|
|
539
|
+
access_token: accessToken,
|
|
540
|
+
token_type: "Bearer",
|
|
541
|
+
expires_in: 3600,
|
|
542
|
+
id_token: idToken
|
|
543
|
+
});
|
|
544
|
+
}
|
|
545
|
+
return c.json({ error: "unsupported_grant_type", error_description: "Only authorization_code and refresh_token are supported." }, 400);
|
|
546
|
+
});
|
|
547
|
+
app.post("/auth/revoke", async (c) => {
|
|
548
|
+
const rawText = await c.req.text();
|
|
549
|
+
const params = new URLSearchParams(rawText);
|
|
550
|
+
const token = params.get("token") ?? "";
|
|
551
|
+
if (token && tokenMap) {
|
|
552
|
+
tokenMap.delete(token);
|
|
553
|
+
}
|
|
554
|
+
if (token) {
|
|
555
|
+
getRefreshTokens(store).delete(token);
|
|
556
|
+
}
|
|
557
|
+
return c.body(null, 200);
|
|
558
|
+
});
|
|
559
|
+
}
|
|
560
|
+
function seedDefaults(store, _baseUrl) {
|
|
561
|
+
const as = getAppleStore(store);
|
|
562
|
+
as.users.insert({
|
|
563
|
+
uid: generateAppleUid(),
|
|
564
|
+
email: "testuser@icloud.com",
|
|
565
|
+
name: "Test User",
|
|
566
|
+
given_name: "Test",
|
|
567
|
+
family_name: "User",
|
|
568
|
+
email_verified: true,
|
|
569
|
+
is_private_email: false,
|
|
570
|
+
private_relay_email: null,
|
|
571
|
+
real_user_status: 2
|
|
572
|
+
});
|
|
573
|
+
}
|
|
574
|
+
function seedFromConfig(store, _baseUrl, config) {
|
|
575
|
+
const as = getAppleStore(store);
|
|
576
|
+
if (config.users) {
|
|
577
|
+
for (const u of config.users) {
|
|
578
|
+
const existing = as.users.findOneBy("email", u.email);
|
|
579
|
+
if (existing) continue;
|
|
580
|
+
const nameParts = (u.name ?? "").split(/\s+/);
|
|
581
|
+
const isPrivate = u.is_private_email ?? false;
|
|
582
|
+
as.users.insert({
|
|
583
|
+
uid: generateAppleUid(),
|
|
584
|
+
email: u.email,
|
|
585
|
+
name: u.name ?? u.email.split("@")[0],
|
|
586
|
+
given_name: u.given_name ?? nameParts[0] ?? "",
|
|
587
|
+
family_name: u.family_name ?? nameParts.slice(1).join(" ") ?? "",
|
|
588
|
+
email_verified: true,
|
|
589
|
+
is_private_email: isPrivate,
|
|
590
|
+
private_relay_email: isPrivate ? generatePrivateRelayEmail() : null,
|
|
591
|
+
real_user_status: 2
|
|
592
|
+
});
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
if (config.oauth_clients) {
|
|
596
|
+
for (const client of config.oauth_clients) {
|
|
597
|
+
const existing = as.oauthClients.findOneBy("client_id", client.client_id);
|
|
598
|
+
if (existing) continue;
|
|
599
|
+
as.oauthClients.insert({
|
|
600
|
+
client_id: client.client_id,
|
|
601
|
+
team_id: client.team_id,
|
|
602
|
+
key_id: client.key_id ?? "TESTKEY001",
|
|
603
|
+
name: client.name,
|
|
604
|
+
redirect_uris: client.redirect_uris
|
|
605
|
+
});
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
var applePlugin = {
|
|
610
|
+
name: "apple",
|
|
611
|
+
register(app, store, webhooks, baseUrl, tokenMap) {
|
|
612
|
+
const ctx = { app, store, webhooks, baseUrl, tokenMap };
|
|
613
|
+
oauthRoutes(ctx);
|
|
614
|
+
},
|
|
615
|
+
seed(store, baseUrl) {
|
|
616
|
+
seedDefaults(store, baseUrl);
|
|
617
|
+
}
|
|
618
|
+
};
|
|
619
|
+
var index_default = applePlugin;
|
|
620
|
+
export {
|
|
621
|
+
applePlugin,
|
|
622
|
+
index_default as default,
|
|
623
|
+
getAppleStore,
|
|
624
|
+
seedFromConfig
|
|
625
|
+
};
|
|
626
|
+
//# sourceMappingURL=dist-O4KFIBVU.js.map
|