updating svelte-kit types to 259 and patching RequestHandler for RequestEvent

This commit is contained in:
Landon Noss 2022-02-02 19:41:30 -07:00
parent ac77764b3d
commit dfcd60768a
8 changed files with 437 additions and 453 deletions

View File

@ -1,13 +1,15 @@
import type { Handle } from "@sveltejs/kit"; import type { Handle } from "@sveltejs/kit";
import { appAuth } from "$lib/appAuth"; import { appAuth } from "$lib/appAuth";
export const handle: Handle = async ({ request, render }) => { export const handle: Handle = async ({ event, resolve }) => {
// TODO https://github.com/sveltejs/kit/issues/1046 // TODO https://github.com/sveltejs/kit/issues/1046
if (request.query.has("_method")) {
request.method = request.query.get("_method").toUpperCase();
if (event.request.query.has("_method")) {
event.request.method = event.request.query.get("_method").toUpperCase();
} }
const response = await render(request); const response = await resolve(event);
return response; return response;
}; };

View File

@ -47,7 +47,7 @@
}, },
"devDependencies": { "devDependencies": {
"@rollup/plugin-typescript": "^8.2.1", "@rollup/plugin-typescript": "^8.2.1",
"@sveltejs/kit": "^1.0.0-next.211", "@sveltejs/kit": "^1.0.0-next.259",
"@types/jsonwebtoken": "^8.5.1", "@types/jsonwebtoken": "^8.5.1",
"@typescript-eslint/eslint-plugin": "^4.23.0", "@typescript-eslint/eslint-plugin": "^4.23.0",
"@typescript-eslint/parser": "^4.23.0", "@typescript-eslint/parser": "^4.23.0",

View File

@ -1,7 +1,7 @@
import type { GetSession, RequestHandler } from "@sveltejs/kit"; import type { GetSession, RequestHandler } from "@sveltejs/kit";
import type { EndpointOutput } from "@sveltejs/kit/types/endpoint"; import type { EndpointOutput } from "@sveltejs/kit/types/endpoint";
import { RequestHeaders } from "@sveltejs/kit/types/helper"; import { ResponseHeaders } from "@sveltejs/kit/types/helper";
import { ServerRequest } from "@sveltejs/kit/types/hooks"; import { RequestEvent } from "@sveltejs/kit/types/hooks";
import cookie from "cookie"; import cookie from "cookie";
import * as jsonwebtoken from "jsonwebtoken"; import * as jsonwebtoken from "jsonwebtoken";
import type { JWT, Session } from "./interfaces"; import type { JWT, Session } from "./interfaces";
@ -45,7 +45,7 @@ export class Auth {
return "svelte_auth_secret"; return "svelte_auth_secret";
} }
async getToken(headers: RequestHeaders) { async getToken(headers: any) {
if (!headers.cookie) { if (!headers.cookie) {
return null; return null;
} }
@ -86,7 +86,7 @@ export class Auth {
return new URL(pathname, this.getBaseUrl(host)).href; return new URL(pathname, this.getBaseUrl(host)).href;
} }
setToken(headers: RequestHeaders, newToken: JWT | any) { setToken(headers: any, newToken: JWT | any) {
const originalToken = this.getToken(headers); const originalToken = this.getToken(headers);
return { return {
@ -114,11 +114,12 @@ export class Auth {
} }
async handleProviderCallback( async handleProviderCallback(
request: ServerRequest, event: RequestEvent,
provider: Provider, provider: Provider,
): Promise<EndpointOutput> { ): Promise<EndpointOutput> {
const { headers, url } = request; const { headers } = event.request;
const [profile, redirectUrl] = await provider.callback(request, this); const {url} = event;
const [profile, redirectUrl] = await provider.callback(event, this);
let token = (await this.getToken(headers)) ?? { user: {} }; let token = (await this.getToken(headers)) ?? { user: {} };
if (this.config?.callbacks?.jwt) { if (this.config?.callbacks?.jwt) {
@ -139,11 +140,12 @@ export class Auth {
}; };
} }
async handleEndpoint(request: ServerRequest): Promise<EndpointOutput> { async handleEndpoint(event: RequestEvent): Promise<EndpointOutput> {
const { headers, method, url } = request; const { headers, method } = event.request;
const { url } = event;
if (url.pathname === this.getPath("signout")) { if (url.pathname === this.getPath("signout")) {
const token = this.setToken(headers, {}); const token = this.setToken(event.request.headers, {});
const jwt = this.signToken(token); const jwt = this.signToken(token);
if (method === "POST") { if (method === "POST") {
@ -177,9 +179,9 @@ export class Auth {
); );
if (provider) { if (provider) {
if (match.groups.method === "signin") { if (match.groups.method === "signin") {
return await provider.signin(request, this); return await provider.signin(event, this);
} else { } else {
return await this.handleProviderCallback(request, provider); return await this.handleProviderCallback(event, provider);
} }
} }
} }
@ -190,13 +192,13 @@ export class Auth {
}; };
} }
get: RequestHandler = async (request) => { get: RequestHandler = async (event: RequestEvent): Promise<any> => {
const { url } = request; const { url } = event;
if (url.pathname === this.getPath("csrf")) { if (url.pathname === this.getPath("csrf")) {
return { body: "1234" }; // TODO: Generate real token return { body: "1234" }; // TODO: Generate real token
} else if (url.pathname === this.getPath("session")) { } else if (url.pathname === this.getPath("session")) {
const session = await this.getSession(request); const session = await this.getSession(event);
return { return {
body: { body: {
session, session,
@ -204,15 +206,16 @@ export class Auth {
}; };
} }
return await this.handleEndpoint(request); return await this.handleEndpoint(event);
}; };
post: RequestHandler = async (request) => { post: RequestHandler = async (event: RequestEvent) => {
return await this.handleEndpoint(request); return await this.handleEndpoint(event);
}; };
getSession: GetSession = async ({ headers }) => { getSession: GetSession = async (event: RequestEvent) => {
const token = await this.getToken(headers); const {request} = event;
const token = await this.getToken(request.headers);
if (token) { if (token) {
if (this.config?.callbacks?.session) { if (this.config?.callbacks?.session) {

View File

@ -1,5 +1,5 @@
import type { EndpointOutput } from "@sveltejs/kit"; import type { EndpointOutput } from "@sveltejs/kit";
import { ServerRequest } from "@sveltejs/kit/types/hooks"; import { RequestEvent } from "@sveltejs/kit/types/hooks";
import type { Auth } from "../auth"; import type { Auth } from "../auth";
import type { CallbackResult } from "../types"; import type { CallbackResult } from "../types";
@ -28,12 +28,12 @@ export abstract class Provider<T extends ProviderConfig = ProviderConfig> {
} }
abstract signin<Locals extends Record<string, any> = Record<string, any>, Body = unknown>( abstract signin<Locals extends Record<string, any> = Record<string, any>, Body = unknown>(
request: ServerRequest<Locals, Body>, event: RequestEvent,
svelteKitAuth: Auth, svelteKitAuth: Auth,
): EndpointOutput | Promise<EndpointOutput>; ): EndpointOutput | Promise<EndpointOutput>;
abstract callback<Locals extends Record<string, any> = Record<string, any>, Body = unknown>( abstract callback<Locals extends Record<string, any> = Record<string, any>, Body = unknown>(
request: ServerRequest<Locals, Body>, event: RequestEvent,
svelteKitAuth: Auth, svelteKitAuth: Auth,
): CallbackResult | Promise<CallbackResult>; ): CallbackResult | Promise<CallbackResult>;
} }

View File

@ -1,5 +1,5 @@
import type { EndpointOutput } from "@sveltejs/kit/types/endpoint"; import type { EndpointOutput } from "@sveltejs/kit/types/endpoint";
import { ServerRequest } from "@sveltejs/kit/types/hooks"; import { RequestEvent } from "@sveltejs/kit/types/hooks";
import type { Auth } from "../auth"; import type { Auth } from "../auth";
import type { CallbackResult } from "../types"; import type { CallbackResult } from "../types";
import { Provider, ProviderConfig } from "./base"; import { Provider, ProviderConfig } from "./base";
@ -25,7 +25,7 @@ export abstract class OAuth2BaseProvider<
T extends OAuth2BaseProviderConfig, T extends OAuth2BaseProviderConfig,
> extends Provider<T> { > extends Provider<T> {
abstract getAuthorizationUrl( abstract getAuthorizationUrl(
request: ServerRequest, event: RequestEvent,
auth: Auth, auth: Auth,
state: string, state: string,
nonce: string, nonce: string,
@ -33,14 +33,15 @@ export abstract class OAuth2BaseProvider<
abstract getTokens(code: string, redirectUri: string): TokensType | Promise<TokensType>; abstract getTokens(code: string, redirectUri: string): TokensType | Promise<TokensType>;
abstract getUserProfile(tokens: any): ProfileType | Promise<ProfileType>; abstract getUserProfile(tokens: any): ProfileType | Promise<ProfileType>;
async signin(request: ServerRequest, auth: Auth): Promise<EndpointOutput> { async signin(event: RequestEvent, auth: Auth): Promise<EndpointOutput> {
const { method, url } = request; const { method } = event.request;
const { url } = event;
const state = [ const state = [
`redirect=${url.searchParams.get("redirect") ?? this.getUri(auth, "/", url.host)}`, `redirect=${url.searchParams.get("redirect") ?? this.getUri(auth, "/", url.host)}`,
].join(","); ].join(",");
const base64State = Buffer.from(state).toString("base64"); const base64State = Buffer.from(state).toString("base64");
const nonce = Math.round(Math.random() * 1000).toString(); // TODO: Generate random based on user values const nonce = Math.round(Math.random() * 1000).toString(); // TODO: Generate random based on user values
const authUrl = await this.getAuthorizationUrl(request, auth, base64State, nonce); const authUrl = await this.getAuthorizationUrl(event, auth, base64State, nonce);
if (method === "POST") { if (method === "POST") {
return { return {
@ -68,7 +69,8 @@ export abstract class OAuth2BaseProvider<
} }
} }
async callback({ url }: ServerRequest, auth: Auth): Promise<CallbackResult> { async callback(event: RequestEvent, auth: Auth): Promise<any> {
const {request, url} = event;
const code = url.searchParams.get("code"); const code = url.searchParams.get("code");
const redirect = this.getStateValue(url.searchParams, "redirect"); const redirect = this.getStateValue(url.searchParams, "redirect");

View File

@ -1,4 +1,4 @@
import { ServerRequest } from "@sveltejs/kit/types/hooks"; import { RequestEvent } from "@sveltejs/kit/types/hooks";
import type { Auth } from "../auth"; import type { Auth } from "../auth";
import { ucFirst } from "../helpers"; import { ucFirst } from "../helpers";
import { OAuth2BaseProvider, OAuth2BaseProviderConfig, OAuth2Tokens } from "./oauth2.base"; import { OAuth2BaseProvider, OAuth2BaseProviderConfig, OAuth2Tokens } from "./oauth2.base";
@ -37,7 +37,7 @@ export class OAuth2Provider<
}); });
} }
getAuthorizationUrl({ url }: ServerRequest, auth: Auth, state: string, nonce: string) { getAuthorizationUrl({ url }: RequestEvent, auth: Auth, state: string, nonce: string) {
const data = { const data = {
state, state,
nonce, nonce,

View File

@ -1,4 +1,4 @@
import { ServerRequest } from "@sveltejs/kit/types/hooks"; import { RequestEvent } from "@sveltejs/kit/types/hooks";
import type { Auth } from "../auth"; import type { Auth } from "../auth";
import type { CallbackResult } from "../types"; import type { CallbackResult } from "../types";
import { OAuth2BaseProvider, OAuth2BaseProviderConfig } from "./oauth2.base"; import { OAuth2BaseProvider, OAuth2BaseProviderConfig } from "./oauth2.base";
@ -38,7 +38,7 @@ export class TwitterAuthProvider extends OAuth2BaseProvider<any, any, TwitterAut
}; };
} }
async getAuthorizationUrl({ url }: ServerRequest, auth: Auth, state: string, nonce: string) { async getAuthorizationUrl({ url }: RequestEvent, auth: Auth, state: string, nonce: string) {
const endpoint = "https://api.twitter.com/oauth/authorize"; const endpoint = "https://api.twitter.com/oauth/authorize";
const { oauthToken } = await this.getRequestToken(auth, url.host); const { oauthToken } = await this.getRequestToken(auth, url.host);
@ -71,7 +71,8 @@ export class TwitterAuthProvider extends OAuth2BaseProvider<any, any, TwitterAut
return await res.json(); return await res.json();
} }
async callback({ url }: ServerRequest, auth: Auth): Promise<CallbackResult> { async callback(event: RequestEvent, auth: Auth): Promise<any> {
const {url} = event;
const oauthToken = url.searchParams.get("oauth_token"); const oauthToken = url.searchParams.get("oauth_token");
const oauthVerifier = url.searchParams.get("oauth_verifier"); const oauthVerifier = url.searchParams.get("oauth_verifier");
const redirect = this.getStateValue(url.searchParams, "redirect"); const redirect = this.getStateValue(url.searchParams, "redirect");

804
yarn.lock

File diff suppressed because it is too large Load Diff