diff options
-rw-r--r-- | auth.js | 20 | ||||
-rw-r--r-- | graffiti.js | 7 | ||||
-rw-r--r-- | utils.js | 11 |
3 files changed, 18 insertions, 20 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('') + } + } diff --git a/graffiti.js b/graffiti.js index 0180a87..1b3365e 100644 --- a/graffiti.js +++ b/graffiti.js @@ -1,5 +1,4 @@ import Auth from './auth.js' -import { randomString } from './utils.js' export default class { @@ -53,7 +52,7 @@ export default class { async request(msg) { // Create a random message ID - const messageID = randomString() + const messageID = Auth.randomString() // Create a listener for the reply const dataPromise = new Promise(resolve => { @@ -150,7 +149,7 @@ export default class { queryID=null) { // Create a random query ID - if (!queryID) queryID = randomString() + if (!queryID) queryID = Auth.randomString() // Send the request await this.request({ @@ -210,7 +209,7 @@ export default class { } // Pre-generate the object's ID if it does not already exist - if (!object._id) object._id = randomString() + if (!object._id) object._id = Auth.randomString() } // Utility function to get a universally unique string diff --git a/utils.js b/utils.js deleted file mode 100644 index ed21d46..0000000 --- a/utils.js +++ /dev/null @@ -1,11 +0,0 @@ -export function randomString() { - return Math.random().toString(36).substr(2) -} - -export async function 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('') -} |