-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.html
210 lines (199 loc) · 7.71 KB
/
auth.html
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<!DOCTYPE html>
<html lang="en">
<head>
<title>Auth</title>
<link rel="icon" href="/favicon-32x32.png" type="image/gif" sizes="32x32">
<link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/@mdi/[email protected]/css/materialdesignicons.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.min.css" rel="stylesheet">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui">
<style>
[v-cloak] {
display: none;
}
</style>
</head>
<body>
<!-- // App -->
<div id="app">
<v-app v-cloak>
<!-- // App Bar -->
<v-app-bar app>
<v-toolbar-title class="my-2 ml-md-8">
<v-img width="80px" src="/logo.png" />
</v-toolbar-title>
<v-spacer></v-spacer>
<v-btn @click='toggleTheme' class="mr-md-4" icon>
<v-icon>mdi-lightbulb-on</v-icon>
</v-btn>
</v-app-bar>
<!-- // Main -->
<v-main>
<v-breadcrumbs class="ml-md-8" :items="[{text: 'Home', href: '/'}, {text: 'Auth', disabled: true}]">
</v-breadcrumbs>
<v-container>
<!-- // Card -->
<v-card v-if='authCode != null' class="mx-auto" max-width='1000'>
<v-card-text>
<!-- // Auth Code and Tokens -->
<template v-for="key in ['authCode', 'accessToken', 'refreshToken']" :key="'row-'+key">
<v-row v-if='$data[key] != null' class="px-4 py-3" no-gutters>
<v-col cols='12' md='3' class="font-weight-bold">
<label role="button" @click='copyToClipboard($data[key])'>{{ keyText[key] }}</label>
</v-col>
<v-col cols='12' md='9'>
<label role="button" @click='copyToClipboard($data[key])'>{{ $data[key] }}</label>
</v-col>
</v-row>
<v-divider v-if='$data[key] != null' />
</template>
<!-- // Instructions -->
<v-row class="pt-8 ml-4 font-italic info--text" no-gutters>
Click any label to copy its value.
</v-row>
</v-card-text>
</v-card>
<!-- // Card -->
<v-card :class="{'px-4 pb-4 mx-auto': true, 'mt-4': authCode != null}" max-width='1000'>
<v-card-text>
<!-- // Redirect URI -->
<v-row no-gutters>
<v-text-field label='Redirect URI' v-model.trim='redirectUri'></v-text-field>
</v-row>
<!-- // Scopes -->
<v-row no-gutters>
<v-combobox chips deletable-chips small-chips multiple :delimiters="[',', '\n', ' ']"
hint="Press comma / enter / space to enter multiple values" :append-icon='null' v-model='scopes'>
</v-combobox>
</v-row>
<!-- // Client ID -->
<v-row no-gutters>
<v-text-field label='Client ID' v-model.trim='clientId'></v-text-field>
</v-row>
<!-- // Client Secret -->
<v-row v-if='authCode != null' no-gutters>
<v-text-field label='Client Secret' v-model.trim='clientSecret'></v-text-field>
</v-row>
<!-- // Buttons -->
</v-card-text>
<v-card-actions class="pl-4">
<v-btn tile color='primary' @click="authorize" small>Authorize</v-btn>
<v-btn tile color='primary' @click="fetchTokens" :disabled='authCode == null || refreshToken != null'
small>Get Tokens</v-btn>
</v-card-actions>
</v-card>
<!-- // Temp textbox to copy text to clipboard -->
<input type="text" ref='clipBoardInput' v-show='showTempInput' v-model.trim='textToCopy' />
<!-- // Snackbar -->
<v-snackbar v-model="snackbar" :timeout="1500">{{ message }}</v-snackbar>
</v-container>
</v-main>
</v-app>
</div>
<!-- // JS Code -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.min.js"></script>
<script>
// URL Object to extract query params
let urlObj = new URL(document.location.href)
// Scopes
let scopes = ['email']
if (localStorage.getItem('scopes') !== null)
scopes = localStorage.getItem('scopes').split(',')
var app = new Vue({
el: '#app',
vuetify: new Vuetify({
theme: {
dark: localStorage.getItem('lightTheme') != 'Yes'
}
}),
data() {
return {
keyText: {
authCode: 'Auth Code',
accessToken: 'Access Token',
refreshToken: 'Refresh Token',
},
// Auth variables
clientId: localStorage.getItem('clientId') || '',
clientSecret: localStorage.getItem('clientSecret') || '',
redirectUri: localStorage.getItem('redirectUri') || `${window.location.origin}${window.location.pathname}`,
scopes: scopes,
// Auth responses
authCode: urlObj.searchParams.get('code'),
accessToken: null,
refreshToken: null,
// For copy to clipboard
textToCopy: '',
showTempInput: false,
snackbar: false,
message: '',
}
},
methods: {
authorize: function () {
// Save to localstorage
localStorage.setItem('redirectUri', this.redirectUri)
localStorage.setItem('scopes', this.scopes.join(','))
localStorage.setItem('clientId', this.clientId)
// Build URL
let authUrl = 'https://accounts.google.com/o/oauth2/v2/auth' +
`?client_id=${this.clientId}` +
`&redirect_uri=${this.redirectUri}` +
`&scope=${this.scopes.join(' ')}` +
`&response_type=code` +
'&access_type=offline' +
'&prompt=consent'
// Redirect
document.location.href = authUrl
},
// Fetch tokens
fetchTokens() {
// Save to localstorage
localStorage.setItem('clientSecret', this.clientSecret)
let reqBody = `code=${this.authCode}&client_id=${this.clientId}` +
`&client_secret=${this.clientSecret}&redirect_uri=${this.redirectUri}` +
`&grant_type=authorization_code`
// Fetch tokens
fetch('https://oauth2.googleapis.com/token', {
method: 'post',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: reqBody
})
.then(res => res.json())
.then(res => {
this.accessToken = res.access_token
this.refreshToken = res.refresh_token
})
},
// Copy to clipboard
copyToClipboard(text) {
// Shoe temp input
this.showTempInput = true
this.textToCopy = text
// On next tick
this.$nextTick(() => {
// Copy value of textbox to clipboard
this.$refs.clipBoardInput.select()
document.execCommand("copy")
// Hide temp input
this.showTempInput = false
})
// Show message
this.showMessage(`Copied to clipboard "${text}"`)
},
// Show message
showMessage(message) {
this.message = message
this.snackbar = true
},
// Toggle theme
toggleTheme() {
this.$vuetify.theme.dark = !this.$vuetify.theme.dark
localStorage.setItem('lightTheme', this.$vuetify.theme.dark ? 'No' : 'Yes')
}
}
})
</script>
</body>
</html>