Skip to content

Commit edb52be

Browse files
committed
fix(files_sharing): fallback self.crypto.randomUUID
Signed-off-by: skjnldsv <skjnldsv@protonmail.com>
1 parent f773b27 commit edb52be

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

lib/guest.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ class GuestUser implements NextcloudUser {
1616

1717
constructor() {
1818
if (!browserStorage.getItem('guestUid')) {
19-
browserStorage.setItem('guestUid', self.crypto.randomUUID())
19+
browserStorage.setItem('guestUid', randomUUID())
2020
}
2121

2222
this._displayName = browserStorage.getItem('guestNickname') || ''
23-
this.uid = browserStorage.getItem('guestUid') || self.crypto.randomUUID()
23+
this.uid = browserStorage.getItem('guestUid') || randomUUID()
2424
this.isAdmin = false
2525

2626
subscribe('user:info:changed', (guest) => {
@@ -73,3 +73,24 @@ export function setGuestNickname(nickname: string): void {
7373

7474
getGuestUser().displayName = nickname
7575
}
76+
77+
/**
78+
* Generate a random UUID (version 4) if the crypto API is not available.
79+
* If the crypto API is available, it uses the less secure `randomUUID` method.
80+
* Crypto API is available in modern browsers on secure contexts (HTTPS).
81+
*
82+
* @return {string} A random UUID.
83+
*/
84+
function randomUUID(): string {
85+
// Use the crypto API if available
86+
if (self?.crypto?.randomUUID) {
87+
return self.crypto.randomUUID()
88+
}
89+
90+
// Generate a random UUID (version 4)
91+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
92+
const r = Math.random() * 16 | 0
93+
const v = c === 'x' ? r : (r & 0x3 | 0x8)
94+
return v.toString(16)
95+
})
96+
}

0 commit comments

Comments
 (0)