fix: authentication fixes in proxies

Signed-off-by: Varun Raj <mailme@varunraj.in>
This commit is contained in:
Varun Raj committed 2024-09-04 19:49:44 +05:30
1 parent 1013231f00
commit 1465486de4
9 files changed
+42 -22

No files matched your search

+2 -2
View File
@@ -36,9 +36,9 @@ export default function ProfileInfo() {
{user && ( {user && (
<> <>
<p className="text-sm">{user?.name}</p> <p className="text-sm">{user?.name}</p>
<Button variant="destructive" className="mx-4" onClick={handleLogout}> {!user.isUsingAPIKey && <Button variant="destructive" className="mx-4" onClick={handleLogout}>
Log Out Log Out
</Button> </Button>}
</> </>
)} )}
</div> </div>
+6 -1
View File
@@ -15,7 +15,12 @@ export const getCurrentUserFromAPIKey = () => {
}, },
}).then((res) => { }).then((res) => {
if (res.ok) { if (res.ok) {
return res.json() return res.json().then((user) => {
return {
...user,
isUsingAPIKey: true,
}
})
} }
return null return null
}) })
+21
View File
@@ -0,0 +1,21 @@
import { ENV } from "@/config/environment"
import { IUser } from "@/types/user"
export const getUserHeaders = (user: IUser, otherHeaders?: {
'Content-Type': string
}) => {
let headers: {
'Content-Type': string;
'x-api-key'?: string;
'Authorization'?: string
} = {
'Content-Type': 'application/octet-stream',
}
if (user.isUsingAPIKey) {
headers['x-api-key'] = ENV.IMMICH_API_KEY
}
else {
headers['Authorization'] = `Bearer ${user.accessToken}`
}
return {...headers, ...otherHeaders}
}
+4 -5
View File
@@ -1,6 +1,8 @@
// pages/api/proxy/[...path].ts // pages/api/proxy/[...path].ts
import { ENV } from '@/config/environment'; import { ENV } from '@/config/environment';
import { getCurrentUser } from '@/handlers/serverUtils/user.utils';
import { getUserHeaders } from '@/helpers/user.helper';
import { NextApiRequest, NextApiResponse } from 'next' import { NextApiRequest, NextApiResponse } from 'next'
export default async function handler(req: NextApiRequest, res: NextApiResponse) { export default async function handler(req: NextApiRequest, res: NextApiResponse) {
@@ -8,14 +10,11 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
const targetUrl = `${ENV.IMMICH_URL}/api${proxyPath}`; const targetUrl = `${ENV.IMMICH_URL}/api${proxyPath}`;
const currentUser = await getCurrentUser(req);
try { try {
const response = await fetch(targetUrl, { const response = await fetch(targetUrl, {
method: req.method, method: req.method,
headers: { headers: getUserHeaders(currentUser),
'x-api-key': ENV.IMMICH_API_KEY,
'Content-Type': 'application/json',
},
body: req.method !== 'GET' && req.method !== 'HEAD' ? JSON.stringify(req.body) : null, body: req.method !== 'GET' && req.method !== 'HEAD' ? JSON.stringify(req.body) : null,
}) })
@@ -2,6 +2,7 @@
import { ENV } from '@/config/environment'; import { ENV } from '@/config/environment';
import { getCurrentUser } from '@/handlers/serverUtils/user.utils'; import { getCurrentUser } from '@/handlers/serverUtils/user.utils';
import { getUserHeaders } from '@/helpers/user.helper';
import { NextApiRequest, NextApiResponse } from 'next' import { NextApiRequest, NextApiResponse } from 'next'
export const config = { export const config = {
@@ -19,6 +20,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
const targetUrl = `${ENV.IMMICH_URL}/api/assets/${id}/thumbnail?size=${size || 'thumbnail'}`; const targetUrl = `${ENV.IMMICH_URL}/api/assets/${id}/thumbnail?size=${size || 'thumbnail'}`;
const user = await getCurrentUser(req); const user = await getCurrentUser(req);
if (!user) { if (!user) {
return res.status(403).json({ message: 'Unauthorized' }) return res.status(403).json({ message: 'Unauthorized' })
} }
@@ -26,10 +28,9 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
// Forward the request to the target API // Forward the request to the target API
const response = await fetch(targetUrl, { const response = await fetch(targetUrl, {
method: 'GET', method: 'GET',
headers: { headers: getUserHeaders(user, {
'Content-Type': 'application/octet-stream', 'Content-Type': 'application/octet-stream',
'Authorization': `Bearer ${user.accessToken}`, }),
},
}) })
if (!response.ok) { if (!response.ok) {
+3 -3
View File
@@ -2,6 +2,7 @@
import { ENV } from '@/config/environment'; import { ENV } from '@/config/environment';
import { getCurrentUser } from '@/handlers/serverUtils/user.utils'; import { getCurrentUser } from '@/handlers/serverUtils/user.utils';
import { getUserHeaders } from '@/helpers/user.helper';
import { NextApiRequest, NextApiResponse } from 'next' import { NextApiRequest, NextApiResponse } from 'next'
export const config = { export const config = {
@@ -28,10 +29,9 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
// Forward the request to the target API // Forward the request to the target API
const response = await fetch(targetUrl, { const response = await fetch(targetUrl, {
method: 'GET', method: 'GET',
headers: { headers: getUserHeaders(currentUser, {
'Content-Type': 'application/octet-stream', 'Content-Type': 'application/octet-stream',
Authorization: `Bearer ${currentUser.accessToken}`, }),
},
}) })
if (!response.ok) { if (!response.ok) {
@@ -26,12 +26,6 @@ export default async function handler(
try { try {
const { const {
id, id,
page = 1,
perPage = 60,
nameLessOnly = false,
minimumAssetCount = 0,
sort = "assetCount",
sortOrder = "desc",
} = req.query as any as IQuery; } = req.query as any as IQuery;
const currentUser = await getCurrentUser(req); const currentUser = await getCurrentUser(req);
-2
View File
@@ -86,8 +86,6 @@ export default async function handler(
} }
const people = await query.limit(perPage).offset((page - 1) * perPage); const people = await query.limit(perPage).offset((page - 1) * perPage);
return res.status(200).json({ return res.status(200).json({
people, people,
total: 10, total: 10,
+2
View File
@@ -15,4 +15,6 @@ export interface IUser {
quotaUsageInBytes: number; quotaUsageInBytes: number;
status: string; status: string;
license: null; license: null;
isUsingAPIKey?: boolean;
accessToken?: string;
} }