Skip to content

Commit 1304f20

Browse files
committed
perf(NextcloudBookmarks): Support ticket authentication
Signed-off-by: Marcel Klehr <[email protected]>
1 parent 7317a8e commit 1304f20

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

src/lib/adapters/NextcloudBookmarks.ts

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ export default class NextcloudBookmarksAdapter implements Adapter, BulkImportRes
7878
private hasFeatureJavascriptLinks: boolean = null
7979
private hashSettings: IHashSettings
8080
private capabilities: any
81+
private ticket: string
82+
private ticketTimestamp: number
8183

8284
constructor(server: NextcloudBookmarksConfig) {
8385
this.server = server
@@ -102,6 +104,8 @@ export default class NextcloudBookmarksAdapter implements Adapter, BulkImportRes
102104

103105
setData(data:NextcloudBookmarksConfig):void {
104106
this.server = { ...data }
107+
this.ticket = null
108+
this.ticketTimestamp = 0
105109
}
106110

107111
getData():NextcloudBookmarksConfig {
@@ -842,9 +846,9 @@ export default class NextcloudBookmarksAdapter implements Adapter, BulkImportRes
842846
return this.sendRequestNative(verb, url, type, body, returnRawResponse, headers)
843847
}
844848

845-
const authString = Base64.encode(
846-
this.server.username + ':' + this.server.password
847-
)
849+
const authString = !this.ticket || this.ticketTimestamp + 60 * 60 * 1000 < Date.now()
850+
? 'Basic ' + Base64.encode(this.server.username + ':' + this.server.password)
851+
: 'Bearer ' + this.ticket
848852

849853
try {
850854
res = await this.fetchQueue.add(() => {
@@ -855,7 +859,7 @@ export default class NextcloudBookmarksAdapter implements Adapter, BulkImportRes
855859
credentials: this.server.includeCredentials ? 'include' : 'omit',
856860
headers: {
857861
...(type && type !== 'multipart/form-data' && { 'Content-type': type }),
858-
Authorization: 'Basic ' + authString,
862+
Authorization: authString,
859863
...headers
860864
},
861865
signal: this.abortSignal,
@@ -887,6 +891,11 @@ export default class NextcloudBookmarksAdapter implements Adapter, BulkImportRes
887891
}
888892

889893
if (res.status === 401 || res.status === 403) {
894+
if (authString.startsWith('Bearer')) {
895+
this.ticket = null
896+
this.ticketTimestamp = 0
897+
return this.sendRequest(verb, url, type, body, returnRawResponse, headers)
898+
}
890899
throw new AuthenticationError()
891900
}
892901
if (res.status === 503 || res.status >= 400) {
@@ -903,6 +912,11 @@ export default class NextcloudBookmarksAdapter implements Adapter, BulkImportRes
903912
throw new Error('Nextcloud API error for request ' + verb + ' ' + relUrl + ' : \n' + JSON.stringify(json))
904913
}
905914

915+
if (json.ticket) {
916+
this.ticket = json.ticket
917+
this.ticketTimestamp = Date.now()
918+
}
919+
906920
return json
907921
}
908922

@@ -949,9 +963,9 @@ export default class NextcloudBookmarksAdapter implements Adapter, BulkImportRes
949963
private async sendRequestNative(verb: string, url: string, type: string, body: any, returnRawResponse: boolean, headers = {}) {
950964
let res
951965
let timedOut = false
952-
const authString = Base64.encode(
953-
this.server.username + ':' + this.server.password
954-
)
966+
const authString = !this.ticket || this.ticketTimestamp + 60 * 60 * 1000 < Date.now()
967+
? 'Basic ' + Base64.encode(this.server.username + ':' + this.server.password)
968+
: 'Bearer ' + this.ticket
955969
try {
956970
res = await this.fetchQueue.add(() => {
957971
Logger.log(`FETCHING ${verb} ${url}`)
@@ -962,7 +976,7 @@ export default class NextcloudBookmarksAdapter implements Adapter, BulkImportRes
962976
disableRedirects: !this.server.allowRedirects,
963977
headers: {
964978
...(type && type !== 'multipart/form-data' && { 'Content-type': type }),
965-
Authorization: 'Basic ' + authString,
979+
Authorization: authString,
966980
...headers,
967981
},
968982
responseType: 'json',
@@ -993,6 +1007,11 @@ export default class NextcloudBookmarksAdapter implements Adapter, BulkImportRes
9931007
}
9941008

9951009
if (res.status === 401 || res.status === 403) {
1010+
if (authString.startsWith('Bearer')) {
1011+
this.ticket = null
1012+
this.ticketTimestamp = 0
1013+
return this.sendRequestNative(verb, url, type, body, returnRawResponse, headers)
1014+
}
9961015
throw new AuthenticationError()
9971016
}
9981017
if (res.status === 503 || res.status >= 400) {
@@ -1003,6 +1022,11 @@ export default class NextcloudBookmarksAdapter implements Adapter, BulkImportRes
10031022
throw new Error('Nextcloud API error for request ' + verb + ' ' + url + ' : \n' + JSON.stringify(json))
10041023
}
10051024

1025+
if (json.ticket) {
1026+
this.ticket = json.ticket
1027+
this.ticketTimestamp = Date.now()
1028+
}
1029+
10061030
return json
10071031
}
10081032

0 commit comments

Comments
 (0)