carnet api
This commit is contained in:
parent
7102090ccf
commit
9458a0488a
@ -4,7 +4,7 @@ import { authOptions } from '@/app/api/auth/[...nextauth]/route';
|
|||||||
import { DOMParser } from '@xmldom/xmldom';
|
import { DOMParser } from '@xmldom/xmldom';
|
||||||
|
|
||||||
async function establishNextcloudSession(nextcloudUrl: string, keycloakToken: string) {
|
async function establishNextcloudSession(nextcloudUrl: string, keycloakToken: string) {
|
||||||
// First, try to establish a session with Nextcloud
|
// First, try to establish a session with Nextcloud using the Keycloak token
|
||||||
const sessionResponse = await fetch(`${nextcloudUrl}/index.php/apps/oauth2/api/v1/token`, {
|
const sessionResponse = await fetch(`${nextcloudUrl}/index.php/apps/oauth2/api/v1/token`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
@ -12,8 +12,11 @@ async function establishNextcloudSession(nextcloudUrl: string, keycloakToken: st
|
|||||||
'Authorization': `Bearer ${keycloakToken}`,
|
'Authorization': `Bearer ${keycloakToken}`,
|
||||||
},
|
},
|
||||||
body: new URLSearchParams({
|
body: new URLSearchParams({
|
||||||
grant_type: 'authorization_code',
|
grant_type: 'urn:ietf:params:oauth:grant-type:token-exchange',
|
||||||
code: keycloakToken,
|
subject_token: keycloakToken,
|
||||||
|
subject_token_type: 'urn:ietf:params:oauth:token-type:access_token',
|
||||||
|
client_id: process.env.NEXTCLOUD_CLIENT_ID || '',
|
||||||
|
client_secret: process.env.NEXTCLOUD_CLIENT_SECRET || '',
|
||||||
}).toString(),
|
}).toString(),
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -23,9 +26,8 @@ async function establishNextcloudSession(nextcloudUrl: string, keycloakToken: st
|
|||||||
throw new Error('Failed to establish Nextcloud session');
|
throw new Error('Failed to establish Nextcloud session');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the session cookie from the response
|
const sessionData = await sessionResponse.json();
|
||||||
const cookies = sessionResponse.headers.getSetCookie();
|
return sessionData.access_token;
|
||||||
return cookies.join('; ');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function GET() {
|
export async function GET() {
|
||||||
@ -60,15 +62,15 @@ export async function GET() {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
// Establish Nextcloud session
|
// Establish Nextcloud session
|
||||||
const sessionCookie = await establishNextcloudSession(nextcloudUrl, session.accessToken);
|
const nextcloudToken = await establishNextcloudSession(nextcloudUrl, session.accessToken);
|
||||||
|
|
||||||
// Get user's folders using WebDAV with session cookie
|
// Get user's folders using WebDAV with Nextcloud token
|
||||||
const webdavUrl = `${nextcloudUrl}/remote.php/dav/files/${encodeURIComponent(session.user.email)}/`;
|
const webdavUrl = `${nextcloudUrl}/remote.php/dav/files/${encodeURIComponent(session.user.email)}/`;
|
||||||
console.log('Requesting WebDAV URL:', webdavUrl);
|
console.log('Requesting WebDAV URL:', webdavUrl);
|
||||||
|
|
||||||
const foldersResponse = await fetch(webdavUrl, {
|
const foldersResponse = await fetch(webdavUrl, {
|
||||||
headers: {
|
headers: {
|
||||||
'Cookie': sessionCookie,
|
'Authorization': `Bearer ${nextcloudToken}`,
|
||||||
'Depth': '1',
|
'Depth': '1',
|
||||||
'Content-Type': 'application/xml',
|
'Content-Type': 'application/xml',
|
||||||
},
|
},
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user