Skip to content

Commit

Permalink
refactor: avoid Buffer in workers environment
Browse files Browse the repository at this point in the history
  • Loading branch information
hmnd committed Mar 17, 2022
1 parent ab3c96e commit 99a60b8
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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");
Expand Down
14 changes: 14 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -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);
}
5 changes: 3 additions & 2 deletions src/providers/oauth2.base.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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);

Expand All @@ -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}=`))
Expand Down
3 changes: 2 additions & 1 deletion src/providers/reddit.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { base64Encode } from "../helpers";
import { OAuth2Provider, OAuth2ProviderConfig } from "./oauth2";

export interface RedditProfile {
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 99a60b8

Please sign in to comment.