-
Notifications
You must be signed in to change notification settings - Fork 168
/
Copy pathpublicLinkManager.ts
135 lines (117 loc) · 3.95 KB
/
publicLinkManager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { ClientService } from 'web-pkg/src/services'
import { ConfigurationManager } from 'web-pkg/src/configuration'
import { Store } from 'vuex'
import isEmpty from 'lodash-es/isEmpty'
import omit from 'lodash-es/omit'
export interface PublicLinkManagerOptions {
clientService: ClientService
configurationManager: ConfigurationManager
store: Store<any>
}
export class PublicLinkManager {
private clientService: ClientService
private configurationManager: ConfigurationManager
private store: Store<any>
constructor(options: PublicLinkManagerOptions) {
this.clientService = options.clientService
this.configurationManager = options.configurationManager
this.store = options.store
}
private static buildStorageKey(token: string, suffix: string): string {
return `oc.publicLink.${token}.${suffix}`
}
async clear(token: string): Promise<void> {
;['resolved', 'passwordRequired', 'password'].forEach((key) => {
sessionStorage.removeItem(PublicLinkManager.buildStorageKey(token, key))
})
await this.store.dispatch('runtime/auth/clearPublicLinkContext')
}
isResolved(token: string): boolean {
const resolved = sessionStorage.getItem(PublicLinkManager.buildStorageKey(token, 'resolved'))
return resolved === 'true'
}
setResolved(token: string, resolved: boolean): void {
sessionStorage.setItem(PublicLinkManager.buildStorageKey(token, 'resolved'), resolved + '')
}
isPasswordRequired(token: string): boolean {
const passwordRequired = sessionStorage.getItem(
PublicLinkManager.buildStorageKey(token, 'passwordRequired')
)
return passwordRequired === 'true'
}
setPasswordRequired(token: string, required: boolean): void {
sessionStorage.setItem(
PublicLinkManager.buildStorageKey(token, 'passwordRequired'),
required + ''
)
}
getPassword(token: string): string {
const password = sessionStorage.getItem(PublicLinkManager.buildStorageKey(token, 'password'))
if (password) {
try {
return Buffer.from(password, 'base64').toString()
} catch (e) {
this.clear(token)
}
}
return ''
}
setPassword(token: string, password: string): void {
if (password.length) {
const encodedPassword = Buffer.from(password).toString('base64')
sessionStorage.setItem(PublicLinkManager.buildStorageKey(token, 'password'), encodedPassword)
} else {
sessionStorage.removeItem(PublicLinkManager.buildStorageKey(token, 'password'))
}
}
async updateContext(token: string) {
if (!this.isResolved(token)) {
return
}
if (
this.store.getters['runtime/auth/isPublicLinkContextReady'] &&
this.store.getters['runtime/auth/publicLinkToken'] === token
) {
return
}
let password
if (this.isPasswordRequired(token)) {
password = this.getPassword(token)
}
try {
await this.fetchCapabilities({
token,
password
})
} catch (e) {
console.error(e)
}
this.store.commit('runtime/auth/SET_PUBLIC_LINK_CONTEXT', {
publicLinkToken: token,
publicLinkPassword: password,
publicLinkContextReady: true
})
}
private async fetchCapabilities({ token = '', password = '' }): Promise<void> {
if (!isEmpty(this.store.getters.capabilities)) {
return
}
const client = this.clientService.ocsPublicLinkContext(
this.configurationManager.serverUrl,
token,
password
)
const response = await client.getCapabilities()
// FIXME: ocis at the moment is not able to create archives for public links that are password protected
// until this is supported by the backend remove it hard as a workaround
// https://github.com/owncloud/web/issues/6423
if (password) {
this.store.commit('SET_CAPABILITIES', {
capabilities: omit(response.capabilities, ['files.archivers']),
version: response.version
})
return
}
this.store.commit('SET_CAPABILITIES', response)
}
}