refactor: avoid Buffer in workers environment

This commit is contained in:
David Roizenman 2022-03-16 19:09:37 -07:00
parent 8bfdcebe1f
commit 9ccbca903f
No known key found for this signature in database
GPG Key ID: CD7B405D13E241B6
4 changed files with 21 additions and 4 deletions

View File

@ -3,6 +3,7 @@ import type { EndpointOutput } from "@sveltejs/kit/types/endpoint";
import { RequestEvent } from "@sveltejs/kit/types/hooks";
import cookie from "cookie";
import { JWTPayload, jwtVerify, KeyLike, SignJWT } from "jose";
import { base64Encode } from "./helpers";
import type { JWT, Session } from "./interfaces";
import { join } from "./path";
import type { Provider } from "./providers";
@ -40,7 +41,7 @@ export class Auth {
if (this.config?.providers?.length) {
const provs = this.config?.providers?.map((provider) => provider.id).join("+");
return encoder.encode(provs);
return encoder.encode(base64Encode(provs));
}
return encoder.encode("svelte_auth_secret");

View File

@ -1,3 +1,17 @@
export function ucFirst(val: string) {
return val.charAt(0).toUpperCase() + val.slice(1);
}
export function base64Encode(str: string): string {
if (typeof btoa === 'undefined') {
return Buffer.from(str).toString('base64');
}
return btoa(str);
}
export function base64Decode(str: string): string {
if (typeof atob === 'undefined') {
return Buffer.from(str, 'base64').toString();
}
return atob(str);
}

View File

@ -1,5 +1,6 @@
import type { EndpointOutput } from "@sveltejs/kit/types/endpoint";
import { RequestEvent } from "@sveltejs/kit/types/hooks";
import { base64Encode, base64Decode } from "../helpers";
import type { Auth } from "../auth";
import type { CallbackResult } from "../types";
import { Provider, ProviderConfig } from "./base";
@ -39,7 +40,7 @@ export abstract class OAuth2BaseProvider<
const state = [
`redirect=${url.searchParams.get("redirect") ?? this.getUri(auth, "/", url.host)}`,
].join(",");
const base64State = Buffer.from(state).toString("base64");
const base64State = base64Encode(state);
const nonce = Math.round(Math.random() * 1000).toString(); // TODO: Generate random based on user values
const authUrl = await this.getAuthorizationUrl(event, auth, base64State, nonce);
@ -61,7 +62,7 @@ export abstract class OAuth2BaseProvider<
getStateValue(query: URLSearchParams, name: string) {
if (query.get("state")) {
const state = Buffer.from(query.get("state")!, "base64").toString();
const state = base64Decode(query.get("state")!);
return state
.split(",")
.find((state) => state.startsWith(`${name}=`))

View File

@ -1,3 +1,4 @@
import { base64Encode } from "../helpers";
import { OAuth2Provider, OAuth2ProviderConfig } from "./oauth2";
export interface RedditProfile {
@ -228,7 +229,7 @@ export class RedditOAuth2Provider extends OAuth2Provider<
headers: {
...config.headers,
Authorization:
"Basic " + Buffer.from(`${config.apiKey}:${config.apiSecret}`).toString("base64"),
"Basic " + base64Encode(`${config.apiKey}:${config.apiSecret}`),
},
authorizationParams: {
...config.authorizationParams,