carnet api
This commit is contained in:
parent
9b6eed1b37
commit
7102090ccf
@ -3,6 +3,31 @@ import { getServerSession } from 'next-auth';
|
||||
import { authOptions } from '@/app/api/auth/[...nextauth]/route';
|
||||
import { DOMParser } from '@xmldom/xmldom';
|
||||
|
||||
async function establishNextcloudSession(nextcloudUrl: string, keycloakToken: string) {
|
||||
// First, try to establish a session with Nextcloud
|
||||
const sessionResponse = await fetch(`${nextcloudUrl}/index.php/apps/oauth2/api/v1/token`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
'Authorization': `Bearer ${keycloakToken}`,
|
||||
},
|
||||
body: new URLSearchParams({
|
||||
grant_type: 'authorization_code',
|
||||
code: keycloakToken,
|
||||
}).toString(),
|
||||
});
|
||||
|
||||
if (!sessionResponse.ok) {
|
||||
const errorText = await sessionResponse.text();
|
||||
console.error('Failed to establish Nextcloud session:', errorText);
|
||||
throw new Error('Failed to establish Nextcloud session');
|
||||
}
|
||||
|
||||
// Get the session cookie from the response
|
||||
const cookies = sessionResponse.headers.getSetCookie();
|
||||
return cookies.join('; ');
|
||||
}
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
const session = await getServerSession(authOptions);
|
||||
@ -34,13 +59,16 @@ export async function GET() {
|
||||
}
|
||||
|
||||
try {
|
||||
// Get user's folders using WebDAV with Keycloak token
|
||||
// Establish Nextcloud session
|
||||
const sessionCookie = await establishNextcloudSession(nextcloudUrl, session.accessToken);
|
||||
|
||||
// Get user's folders using WebDAV with session cookie
|
||||
const webdavUrl = `${nextcloudUrl}/remote.php/dav/files/${encodeURIComponent(session.user.email)}/`;
|
||||
console.log('Requesting WebDAV URL:', webdavUrl);
|
||||
|
||||
const foldersResponse = await fetch(webdavUrl, {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${session.accessToken}`,
|
||||
'Cookie': sessionCookie,
|
||||
'Depth': '1',
|
||||
'Content-Type': 'application/xml',
|
||||
},
|
||||
|
||||
Loading…
Reference in New Issue
Block a user