-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #107 from spotify/update-get-profile-to-use-pkce
update get profile demo to use more secure auth flow
- Loading branch information
Showing
3 changed files
with
66 additions
and
17 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
export async function redirectToAuthCodeFlow(clientId: string) { | ||
const verifier = generateCodeVerifier(128); | ||
const challenge = await generateCodeChallenge(verifier); | ||
|
||
localStorage.setItem("verifier", verifier); | ||
|
||
const params = new URLSearchParams(); | ||
params.append("client_id", clientId); | ||
params.append("response_type", "code"); | ||
params.append("redirect_uri", "http://localhost:5173/callback"); | ||
params.append("scope", "user-read-private user-read-email"); | ||
params.append("code_challenge_method", "S256"); | ||
params.append("code_challenge", challenge); | ||
|
||
document.location = `https://accounts.spotify.com/authorize?${params.toString()}`; | ||
} | ||
|
||
export async function getAccessToken(clientId: string, code: string) { | ||
const verifier = localStorage.getItem("verifier"); | ||
|
||
const params = new URLSearchParams(); | ||
params.append("client_id", clientId); | ||
params.append("grant_type", "authorization_code"); | ||
params.append("code", code); | ||
params.append("redirect_uri", "http://localhost:5173/callback"); | ||
params.append("code_verifier", verifier!); | ||
|
||
const result = await fetch("https://accounts.spotify.com/api/token", { | ||
method: "POST", | ||
headers: { "Content-Type": "application/x-www-form-urlencoded" }, | ||
body: params | ||
}); | ||
|
||
const { access_token } = await result.json(); | ||
return access_token; | ||
} | ||
|
||
function generateCodeVerifier(length: number) { | ||
let text = ''; | ||
let possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; | ||
|
||
for (let i = 0; i < length; i++) { | ||
text += possible.charAt(Math.floor(Math.random() * possible.length)); | ||
} | ||
return text; | ||
} | ||
|
||
async function generateCodeChallenge(codeVerifier: string) { | ||
const data = new TextEncoder().encode(codeVerifier); | ||
const digest = await window.crypto.subtle.digest('SHA-256', data); | ||
return btoa(String.fromCharCode.apply(null, [...new Uint8Array(digest)])) | ||
.replace(/\+/g, '-') | ||
.replace(/\//g, '_') | ||
.replace(/=+$/, ''); | ||
} |
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