2025-01-01 21:41:31 -05:00
|
|
|
import { generateSessionToken } from "@server/auth/sessions/app";
|
2024-12-18 23:14:26 -05:00
|
|
|
import db from "@server/db";
|
|
|
|
import { resourceAccessToken, resources } from "@server/db/schema";
|
|
|
|
import HttpCode from "@server/types/HttpCode";
|
2025-01-01 21:41:31 -05:00
|
|
|
import response from "@server/lib/response";
|
2024-12-18 23:14:26 -05:00
|
|
|
import { eq, and } from "drizzle-orm";
|
|
|
|
import { NextFunction, Request, Response } from "express";
|
|
|
|
import createHttpError from "http-errors";
|
|
|
|
import { z } from "zod";
|
|
|
|
import { fromError } from "zod-validation-error";
|
|
|
|
import {
|
|
|
|
createResourceSession,
|
|
|
|
serializeResourceSessionCookie
|
2025-01-01 21:41:31 -05:00
|
|
|
} from "@server/auth/sessions/resource";
|
|
|
|
import config from "@server/lib/config";
|
2024-12-18 23:14:26 -05:00
|
|
|
import logger from "@server/logger";
|
2025-01-11 19:47:07 -05:00
|
|
|
import { verifyResourceAccessToken } from "@server/auth/verifyResourceAccessToken";
|
2024-12-18 23:14:26 -05:00
|
|
|
|
2024-12-21 21:01:12 -05:00
|
|
|
const authWithAccessTokenBodySchema = z
|
|
|
|
.object({
|
|
|
|
accessToken: z.string(),
|
|
|
|
accessTokenId: z.string()
|
|
|
|
})
|
|
|
|
.strict();
|
|
|
|
|
|
|
|
const authWithAccessTokenParamsSchema = z
|
|
|
|
.object({
|
|
|
|
resourceId: z
|
|
|
|
.string()
|
|
|
|
.transform(Number)
|
|
|
|
.pipe(z.number().int().positive())
|
|
|
|
})
|
|
|
|
.strict();
|
2024-12-18 23:14:26 -05:00
|
|
|
|
|
|
|
export type AuthWithAccessTokenResponse = {
|
|
|
|
session?: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export async function authWithAccessToken(
|
|
|
|
req: Request,
|
|
|
|
res: Response,
|
|
|
|
next: NextFunction
|
|
|
|
): Promise<any> {
|
|
|
|
const parsedBody = authWithAccessTokenBodySchema.safeParse(req.body);
|
|
|
|
|
|
|
|
if (!parsedBody.success) {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.BAD_REQUEST,
|
|
|
|
fromError(parsedBody.error).toString()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const parsedParams = authWithAccessTokenParamsSchema.safeParse(req.params);
|
|
|
|
|
|
|
|
if (!parsedParams.success) {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.BAD_REQUEST,
|
|
|
|
fromError(parsedParams.error).toString()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const { resourceId } = parsedParams.data;
|
2024-12-21 21:01:12 -05:00
|
|
|
const { accessToken, accessTokenId } = parsedBody.data;
|
2024-12-18 23:14:26 -05:00
|
|
|
|
|
|
|
try {
|
2025-01-11 19:47:07 -05:00
|
|
|
const [resource] = await db
|
2024-12-18 23:14:26 -05:00
|
|
|
.select()
|
2025-01-11 19:47:07 -05:00
|
|
|
.from(resources)
|
|
|
|
.where(eq(resources.resourceId, resourceId))
|
2024-12-18 23:14:26 -05:00
|
|
|
.limit(1);
|
|
|
|
|
|
|
|
if (!resource) {
|
|
|
|
return next(
|
2025-01-11 19:47:07 -05:00
|
|
|
createHttpError(HttpCode.NOT_FOUND, "Resource not found")
|
2024-12-18 23:14:26 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2025-01-11 19:47:07 -05:00
|
|
|
const { valid, error, tokenItem } = await verifyResourceAccessToken({
|
|
|
|
resource,
|
|
|
|
accessTokenId,
|
|
|
|
accessToken
|
|
|
|
});
|
2024-12-22 16:59:30 -05:00
|
|
|
|
2025-01-11 19:47:07 -05:00
|
|
|
if (!valid) {
|
2024-12-18 23:14:26 -05:00
|
|
|
return next(
|
2025-01-11 19:47:07 -05:00
|
|
|
createHttpError(
|
|
|
|
HttpCode.UNAUTHORIZED,
|
|
|
|
error || "Invalid access token"
|
|
|
|
)
|
2024-12-18 23:14:26 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2025-01-11 19:47:07 -05:00
|
|
|
if (!tokenItem || !resource) {
|
2024-12-18 23:14:26 -05:00
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.UNAUTHORIZED,
|
2025-01-11 19:47:07 -05:00
|
|
|
"Access token does not exist for resource"
|
2024-12-18 23:14:26 -05:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const token = generateSessionToken();
|
|
|
|
await createResourceSession({
|
|
|
|
resourceId,
|
|
|
|
token,
|
|
|
|
accessTokenId: tokenItem.accessTokenId,
|
|
|
|
sessionLength: tokenItem.sessionLength,
|
|
|
|
expiresAt: tokenItem.expiresAt,
|
2024-12-26 15:13:49 -05:00
|
|
|
doNotExtend: tokenItem.expiresAt ? true : false
|
2024-12-18 23:14:26 -05:00
|
|
|
});
|
2025-01-01 17:50:12 -05:00
|
|
|
const cookieName = `${config.getRawConfig().server.resource_session_cookie_name}_${resource.resourceId}`;
|
2024-12-18 23:14:26 -05:00
|
|
|
const cookie = serializeResourceSessionCookie(cookieName, token);
|
|
|
|
res.appendHeader("Set-Cookie", cookie);
|
|
|
|
|
|
|
|
return response<AuthWithAccessTokenResponse>(res, {
|
|
|
|
data: {
|
|
|
|
session: token
|
|
|
|
},
|
|
|
|
success: true,
|
|
|
|
error: false,
|
|
|
|
message: "Authenticated with resource successfully",
|
|
|
|
status: HttpCode.OK
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
logger.error(e);
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.INTERNAL_SERVER_ERROR,
|
|
|
|
"Failed to authenticate with resource"
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|