-
Notifications
You must be signed in to change notification settings - Fork 17
/
OAuthConnectButton.vue
95 lines (90 loc) · 2.42 KB
/
OAuthConnectButton.vue
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
<template>
<button v-if="!!requestUrl"
class="oauth-connect--button"
@click="onOAuthClick">
<span class="icon icon-external" />
{{ t('integration_openproject', 'Connect to OpenProject') }}
</button>
<div v-else class="oauth-connect--message">
{{ adminConfigNotOkMessage }}
</div>
</template>
<script>
import axios from '@nextcloud/axios'
import { generateUrl } from '@nextcloud/router'
import { showError } from '@nextcloud/dialogs'
import { translate as t } from '@nextcloud/l10n'
export default {
name: 'OAuthConnectButton',
props: {
requestUrl: {
type: [String, Boolean],
required: true,
},
},
computed: {
adminConfigNotOkMessage() {
return t('integration_openproject', 'Some OpenProject integration application settings are not working.'
+ ' Please contact your Nextcloud administrator.')
},
},
methods: {
generateRandomString(length) {
let text = ''
const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~'
for (let i = 0; i < length; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length))
}
return text
},
async digest(text) {
return await crypto.subtle.digest('SHA-256', new TextEncoder().encode(text))
},
async generateCodeChallenge(codeVerifier) {
const digest = await this.digest(codeVerifier)
return btoa(String.fromCharCode(...new Uint8Array(digest)))
.replace(/=/g, '')
.replace(/\+/g, '-')
.replace(/\//g, '_')
},
async onOAuthClick() {
const oauthState = this.generateRandomString(10)
const codeVerifier = this.generateRandomString(128)
const codeChallenge = await this.generateCodeChallenge(codeVerifier)
const requestUrl = this.requestUrl
+ '&state=' + encodeURIComponent(oauthState)
+ '&code_challenge=' + codeChallenge
+ '&code_challenge_method=S256'
const req = {
values: {
oauth_state: oauthState,
code_verifier: codeVerifier,
},
}
const url = generateUrl('/apps/integration_openproject/config')
axios.put(url, req)
.then(() => {
window.location.replace(requestUrl)
})
.catch((error) => {
showError(
t('integration_openproject', 'Failed to save OpenProject OAuth state')
+ ': ' + error.message
)
})
},
},
}
</script>
<style lang="scss" scoped>
.oauth-connect {
&--message {
font-size: 1rem;
text-align: center;
font-weight: 400;
color: #878787;
padding: 0px 18px;
line-height: 1.4rem;
}
}
</style>