Last active
March 30, 2023 19:21
-
-
Save adregan/cd037994c4bf4725ac0935cb110a33bd to your computer and use it in GitHub Desktop.
Authentication issues
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const AUTH_URL = 'https://smartweather.weatherflow.com/authorize.html' | |
export const toAuthUrl = ({ | |
clientId: client_id, | |
codeChallenge: code_challenge, | |
}) => { | |
const params = { | |
response_type: 'code', | |
redirect_uri: '<REDIRECT_URL>', | |
code_challenge_method: 'S256', | |
client_id, | |
code_challenge, | |
} | |
const queryString = new URLSearchParams(params).toString() | |
return `${AUTH_URL}?${queryString}` | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const genVerifier = () => | |
self.crypto.getRandomValues(new Uint16Array(10)).join('') | |
export const toCodeChallenge = async (message) => { | |
const data = new TextEncoder().encode(message) | |
const hash = await crypto.subtle.digest('SHA-256', data) | |
return base64UrlEncode(new Uint8Array(hash)) | |
} | |
const base64UrlEncode = (data) => | |
btoa(String.fromCharCode.apply(null, data)) | |
.replace(/\+/g, '-') | |
.replace(/\//g, '_') | |
.replace(/=/g, '') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { genVerifier, toCodeChallenge } from './code.js' | |
import { toAuthUrl } from './auth.js' | |
import { toToken } from './token.js' | |
const CLIENT_ID = <CLIENT_ID> | |
;(async () => { | |
const codeVerifier = genVerifier() | |
const codeChallenge = await toCodeChallenge(codeVerifier) | |
const authUrl = toAuthUrl({ clientId: CLIENT_ID, codeChallenge }) | |
document.getElementById('authorize').setAttribute('href', authUrl) | |
const code = new URLSearchParams(window.location.search).get('code') ?? '' | |
if (code) { | |
const token = await toToken({ clientId: CLIENT_ID, codeVerifier, code }) | |
console.log(token) | |
} | |
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const TOKEN_URL = 'https://swd.weatherflow.com/id/oauth2/token' | |
export const toToken = async ({ | |
clientId: client_id, | |
codeVerifier: code_verifier, | |
code, | |
}) => | |
( | |
await fetch(TOKEN_URL, { | |
method: 'POST', | |
body: new URLSearchParams({ | |
grant_type: 'authorization_code', | |
client_id, | |
code_verifier, | |
code, | |
}), | |
}) | |
).json() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment