carnet api nc

This commit is contained in:
alma 2025-04-20 14:37:11 +02:00
parent dbd5157dde
commit fb59a0e160

View File

@ -57,57 +57,44 @@ async function getWebDAVCredentials(nextcloudUrl: string, username: string, admi
}
const userInfo = await parseXMLResponse(userInfoResponse);
console.log('User Info:', userInfo); // Debug log
console.log('User Info:', userInfo);
const webdavPassword = userInfo.webdav_password;
// Generate a new password
const newPassword = Math.random().toString(36).slice(-12);
console.log('Setting new password for user');
if (!webdavPassword) {
// If no WebDAV password exists, create one
const randomPassword = Math.random().toString(36).slice(-8);
console.log('Creating new WebDAV password:', randomPassword); // Debug log
// Set the user's password directly
const setPasswordResponse = await fetch(`${nextcloudUrl}/ocs/v1.php/cloud/users/${encodeURIComponent(username)}`, {
method: 'PUT',
headers: {
'Authorization': `Basic ${Buffer.from(`${adminUsername}:${adminPassword}`).toString('base64')}`,
'OCS-APIRequest': 'true',
'Content-Type': 'application/x-www-form-urlencoded',
},
body: new URLSearchParams({
key: 'password',
value: newPassword,
}).toString(),
});
const createPasswordResponse = await fetch(`${nextcloudUrl}/ocs/v1.php/cloud/users/${encodeURIComponent(username)}`, {
method: 'PUT',
headers: {
'Authorization': `Basic ${Buffer.from(`${adminUsername}:${adminPassword}`).toString('base64')}`,
'OCS-APIRequest': 'true',
'Content-Type': 'application/x-www-form-urlencoded',
},
body: new URLSearchParams({
key: 'webdav_password',
value: randomPassword,
}).toString(),
});
if (!createPasswordResponse.ok) {
console.error('Failed to create WebDAV password:', await createPasswordResponse.text());
throw new Error(`Failed to create WebDAV password: ${createPasswordResponse.status} ${createPasswordResponse.statusText}`);
}
// Get the new WebDAV password
const newUserInfoResponse = await fetch(`${nextcloudUrl}/ocs/v1.php/cloud/users/${encodeURIComponent(username)}`, {
headers: {
'Authorization': `Basic ${Buffer.from(`${adminUsername}:${adminPassword}`).toString('base64')}`,
'OCS-APIRequest': 'true',
},
});
if (!newUserInfoResponse.ok) {
console.error('Failed to get new user info:', await newUserInfoResponse.text());
throw new Error(`Failed to get new user info: ${newUserInfoResponse.status} ${newUserInfoResponse.statusText}`);
}
const newUserInfo = await parseXMLResponse(newUserInfoResponse);
console.log('New User Info:', newUserInfo); // Debug log
if (!newUserInfo.webdav_password) {
throw new Error('WebDAV password was not set after creation');
}
return newUserInfo.webdav_password;
if (!setPasswordResponse.ok) {
console.error('Failed to set password:', await setPasswordResponse.text());
throw new Error(`Failed to set password: ${setPasswordResponse.status} ${setPasswordResponse.statusText}`);
}
return webdavPassword;
// Verify the password was set by trying to authenticate
const verifyResponse = await fetch(`${nextcloudUrl}/remote.php/dav/files/${encodeURIComponent(username)}/`, {
headers: {
'Authorization': `Basic ${Buffer.from(`${username}:${newPassword}`).toString('base64')}`,
},
});
if (!verifyResponse.ok) {
console.error('Failed to verify password:', await verifyResponse.text());
throw new Error('Password verification failed');
}
return newPassword;
} catch (error) {
console.error('Error in getWebDAVCredentials:', error);
throw error;