Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement PKCE #414

Merged
merged 2 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions server/authUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
Code copied from https://stackoverflow.com/questions/63309409/creating-a-code-verifier-and-challenge-for-pkce-auth-on-spotify-api-in-reactjs
*/

// GENERATING CODE VERIFIER
function dec2hex(dec) {
return ("0" + dec.toString(16)).substr(-2);
}

function generateCodeVerifier() {
var array = new Uint32Array(56 / 2);
window.crypto.getRandomValues(array);
return Array.from(array, dec2hex).join("");
}

// Generate code challenge from code verifier

function sha256(plain) {
// returns promise ArrayBuffer
const encoder = new TextEncoder();
const data = encoder.encode(plain);
return window.crypto.subtle.digest("SHA-256", data);
}

function base64urlencode(a) {
var str = "";
var bytes = new Uint8Array(a);
var len = bytes.byteLength;
for (var i = 0; i < len; i++) {
str += String.fromCharCode(bytes[i]);
}
return btoa(str)
.replace(/\+/g, "-")
.replace(/\//g, "_")
.replace(/=+$/, "");
}

async function generateCodeChallengeFromVerifier(v) {
var hashed = await sha256(v);
var base64encoded = base64urlencode(hashed);
return base64encoded;
}
27 changes: 24 additions & 3 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ const OIDC_CLIENT_ID = process.env.OIDC_CLIENT_ID;
const OIDC_SECRET = process.env.OIDC_SECRET;
const OIDC_URL = process.env.OIDC_URL;
const OIDC_SCOPES = process.env.OIDC_SCOPES || 'openid email';
const OIDC_CODE_CHALLENGE_METHOD = process.env.OIDC_CODE_CHALLENGE_METHOD || 'plain';
const OIDC_USE_PKCE = process.env.OIDC_USE_PKCE === "true" || false;
const OIDC_METADATA = JSON.parse(process.env.OIDC_METADATA || '{}');
const clientMetadata = Object.assign({client_id: OIDC_CLIENT_ID, client_secret: OIDC_SECRET}, OIDC_METADATA);

const codeVerifier = generateCodeVerifier()
const codeChallenge = generateCodeChallengeFromVerifier(codeVerifier)


console.log('OIDC_URL: ', OIDC_URL || 'None');

process.on('uncaughtException', err => console.error('Uncaught exception', err));
Expand Down Expand Up @@ -131,12 +135,29 @@ async function getOidcEndpoint() {
if (!OIDC_URL) return;

const provider = await getOidcProvider();
return provider.authorizationUrl({scope: OIDC_SCOPES, code_challenge_method: OIDC_CODE_CHALLENGE_METHOD});
let authParams = {
scope: OIDC_SCOPES,
}
if (OIDC_USE_PKCE) {
authParams = {
...authParams,
code_challenge: codeChallenge,
code_challenge_method: "S256"
}
}
return provider.authorizationUrl(authParams);
}

async function oidcAuthenticate(code, redirectUri) {
const provider = await getOidcProvider();
const tokenSet = await provider.callback(redirectUri, {code}, {});
let authCheckParams = {}
if (OIDC_USE_PKCE) {
authCheckParams = {
...authCheckParams,
code_verifier: codeVerifier
}
}
const tokenSet = await provider.callback(redirectUri, {code}, authCheckParams);
return tokenSet.id_token;
}

Expand Down
Loading