Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Make mobile auth work again
  • Loading branch information
regalijan committed Oct 29, 2023
1 parent 0bc68ef commit 82f2438
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
28 changes: 19 additions & 9 deletions functions/api/auth/mobile/token.ts
Expand Up @@ -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(
`auth_${btoa(
String.fromCharCode(
Expand All @@ -58,15 +65,15 @@ export async function onRequestGet(context: RequestContext) {
.replaceAll("=", "")}`,
JSON.stringify(currentUser),
{
expirationTtl: currentUser.expires_in + 1209600,
expirationTtl: 2419200,
},
);

const claimSet = btoa(
JSON.stringify({
email: currentUser.email,
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),
iss: "https://carcrushers.cc/auth/mobile/token",
jti: tokenId,
Expand Down Expand Up @@ -107,17 +114,20 @@ export async function onRequestGet(context: RequestContext) {
.replaceAll("/", "_")
.replaceAll("=", "");

return new Response(`<!DOCTYPE html>
return new Response(
`<!DOCTYPE html>
<html>
<body>
<p>You were logged in successfully. If this page does not close in a few seconds, please click done.</p>
</body>
</html>
`, {
headers: {
"content-type": "text/html",
location: `com.carcrushers.app://login-callback?token=${header}.${claimSet}.${encodedSignature}`
`,
{
headers: {
"content-type": "text/html",
location: `com.carcrushers.app://login-callback?token=${header}.${claimSet}.${encodedSignature}`,
},
status: 302,
},
status: 302,
});
);
}
4 changes: 2 additions & 2 deletions functions/api/auth/session.ts
Expand Up @@ -130,14 +130,14 @@ export async function onRequestGet(context: RequestContext) {
const tokenHash = await generateTokenHash(authToken);

await context.env.DATA.put(`auth_${tokenHash}`, JSON.stringify(userData), {
expirationTtl: tokenData.expires_in,
expirationTtl: 2419200,
});

await context.env.DATA.put(
`oauthcredentials_${userData.id}`,
JSON.stringify(oauthData),
{
expirationTtl: 1209600000,
expirationTtl: 1209600,
},
);

Expand Down

0 comments on commit 82f2438

Please sign in to comment.