Use cookie for the jwt for images or videos

This commit is contained in:
Zoe Roux 2024-02-04 21:15:25 +01:00
parent 8b92d0525f
commit f4dc4c315d
4 changed files with 56 additions and 41 deletions

View File

@ -18,6 +18,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
using System.Threading.Tasks;
using Autofac; using Autofac;
using Kyoo.Abstractions.Controllers; using Kyoo.Abstractions.Controllers;
using Kyoo.Authentication.Models; using Kyoo.Authentication.Models;
@ -86,6 +87,14 @@ namespace Kyoo.Authentication
.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options => .AddJwtBearer(options =>
{ {
options.Events = new()
{
OnMessageReceived = (ctx) =>
{
ctx.Token ??= ctx.Request.Cookies["X-Bearer"];
return Task.CompletedTask;
}
};
options.TokenValidationParameters = new TokenValidationParameters options.TokenValidationParameters = new TokenValidationParameters
{ {
ValidateIssuer = false, ValidateIssuer = false,

View File

@ -35,7 +35,7 @@ const writeAccounts = (accounts: Account[]) => {
}; };
export const setCookie = (key: string, val?: unknown) => { export const setCookie = (key: string, val?: unknown) => {
let value = JSON.stringify(val); let value = typeof val !== "string" ? JSON.stringify(val) : val;
// Remove illegal values from json. There should not be one in the account anyways. // Remove illegal values from json. There should not be one in the account anyways.
value = value?.replaceAll(";", ""); value = value?.replaceAll(";", "");
const d = new Date(); const d = new Date();

View File

@ -38,7 +38,7 @@ export const TokenP = z.object({
}); });
export type Token = z.infer<typeof TokenP>; export type Token = z.infer<typeof TokenP>;
export const AccountP = UserP.merge( export const AccountP = UserP.and(
z.object({ z.object({
// set it optional for accounts logged in before the kind was present // set it optional for accounts logged in before the kind was present
kind: z.literal("user").optional(), kind: z.literal("user").optional(),
@ -126,7 +126,10 @@ export const AccountProvider = ({
oldSelectedId.current = selected?.id; oldSelectedId.current = selected?.id;
// update cookies for ssr (needs to contains token, theme, language...) // update cookies for ssr (needs to contains token, theme, language...)
if (Platform.OS === "web") setCookie("account", selected); if (Platform.OS === "web") {
setCookie("account", selected);
setCookie("X-Bearer", selected?.token.access_token);
}
}, [selected, queryClient]); }, [selected, queryClient]);
return ( return (

View File

@ -20,8 +20,10 @@
import { z } from "zod"; import { z } from "zod";
import { ResourceP } from "../traits/resource"; import { ResourceP } from "../traits/resource";
import { imageFn } from "../traits/images";
export const UserP = ResourceP("user").extend({ export const UserP = ResourceP("user")
.extend({
/** /**
* The name of this user. * The name of this user.
*/ */
@ -58,6 +60,7 @@ export const UserP = ResourceP("user").extend({
}) })
// keep a default for older versions of the api // keep a default for older versions of the api
.default({}), .default({}),
}); })
.transform((x) => ({ ...x, logo: imageFn(`/user/${x.slug}/logo`) }));
export type User = z.infer<typeof UserP>; export type User = z.infer<typeof UserP>;