diff options
Diffstat (limited to 'auth.js')
-rw-r--r-- | auth.js | 20 |
1 files changed, 15 insertions, 5 deletions
@@ -1,14 +1,12 @@ -import { randomString, sha256 } from './utils.js' - export default { async logIn(graffitiURL) { // Generate a random client secret and state - const clientSecret = randomString() - const state = randomString() + const clientSecret = this.randomString() + const state = this.randomString() // The client ID is the secret's hex hash - const clientID = await sha256(clientSecret) + const clientID = await this.sha256(clientSecret) // Store the client secret as a local variable window.localStorage.setItem('graffitiClientSecret', clientSecret) @@ -116,4 +114,16 @@ export default { return url }, + randomString() { + return Math.random().toString(36).substr(2) + }, + + async sha256(input) { + const encoder = new TextEncoder() + const inputBytes = encoder.encode(input) + const outputBuffer = await crypto.subtle.digest('SHA-256', inputBytes) + const outputArray = Array.from(new Uint8Array(outputBuffer)) + return outputArray.map(b => b.toString(16).padStart(2, '0')).join('') + } + } |