Make mobile auth work again

This commit is contained in:
Regalijan 2023-10-28 22:23:33 -04:00
parent 0bc68ef1d2
commit 82f2438231
Signed by: regalijan
GPG Key ID: 5D4196DA269EF520
2 changed files with 21 additions and 11 deletions

View File

@ -42,6 +42,13 @@ export async function onRequestGet(context: RequestContext) {
"", "",
); );
const oauthCredentials = await context.env.DATA.get(
`oauthcredentials_${currentUser.id}`,
{ type: "json" },
);
if (!oauthCredentials) return jsonError("No credentials found for you", 500);
await context.env.DATA.put( await context.env.DATA.put(
`auth_${btoa( `auth_${btoa(
String.fromCharCode( String.fromCharCode(
@ -58,7 +65,7 @@ export async function onRequestGet(context: RequestContext) {
.replaceAll("=", "")}`, .replaceAll("=", "")}`,
JSON.stringify(currentUser), JSON.stringify(currentUser),
{ {
expirationTtl: currentUser.expires_in + 1209600, expirationTtl: 2419200,
}, },
); );
@ -66,7 +73,7 @@ export async function onRequestGet(context: RequestContext) {
JSON.stringify({ JSON.stringify({
email: currentUser.email, email: currentUser.email,
email_verified: true, email_verified: true,
exp: Math.floor(Date.now() / 1000) + currentUser.expires_in, exp: Math.floor(Date.now() / 1000) + 2419200,
iat: Math.floor(Date.now() / 1000), iat: Math.floor(Date.now() / 1000),
iss: "https://carcrushers.cc/auth/mobile/token", iss: "https://carcrushers.cc/auth/mobile/token",
jti: tokenId, jti: tokenId,
@ -107,17 +114,20 @@ export async function onRequestGet(context: RequestContext) {
.replaceAll("/", "_") .replaceAll("/", "_")
.replaceAll("=", ""); .replaceAll("=", "");
return new Response(`<!DOCTYPE html> return new Response(
`<!DOCTYPE html>
<html> <html>
<body> <body>
<p>You were logged in successfully. If this page does not close in a few seconds, please click done.</p> <p>You were logged in successfully. If this page does not close in a few seconds, please click done.</p>
</body> </body>
</html> </html>
`, { `,
{
headers: { headers: {
"content-type": "text/html", "content-type": "text/html",
location: `com.carcrushers.app://login-callback?token=${header}.${claimSet}.${encodedSignature}` location: `com.carcrushers.app://login-callback?token=${header}.${claimSet}.${encodedSignature}`,
}, },
status: 302, status: 302,
}); },
);
} }

View File

@ -130,14 +130,14 @@ export async function onRequestGet(context: RequestContext) {
const tokenHash = await generateTokenHash(authToken); const tokenHash = await generateTokenHash(authToken);
await context.env.DATA.put(`auth_${tokenHash}`, JSON.stringify(userData), { await context.env.DATA.put(`auth_${tokenHash}`, JSON.stringify(userData), {
expirationTtl: tokenData.expires_in, expirationTtl: 2419200,
}); });
await context.env.DATA.put( await context.env.DATA.put(
`oauthcredentials_${userData.id}`, `oauthcredentials_${userData.id}`,
JSON.stringify(oauthData), JSON.stringify(oauthData),
{ {
expirationTtl: 1209600000, expirationTtl: 1209600,
}, },
); );