Skip to content

Commit

Permalink
handleAuth
Browse files Browse the repository at this point in the history
=
  • Loading branch information
peterphanouvong committed May 23, 2022
1 parent 92dc280 commit c97acd4
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 11 deletions.
2 changes: 1 addition & 1 deletion bundle.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions nextjs-workspace.code-workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"folders": [
{
"path": "."
},
{
"path": "../nextjs-test-app"
}
]
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@kinde-oss/kinde-auth-nextjs",
"version": "1.0.0-beta.13",
"description": "Kinde Auth SDK for NextJS",
"main": "index.js",
"main": "bundle.js",
"scripts": {
"test": "jest",
"build": "rollup -c"
Expand Down
9 changes: 9 additions & 0 deletions src/handlers/handleAuth.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { login } from "./login";
import { logout } from "./logout";
import { me } from "./me";
import { register } from "./register";

export default () =>
function handler(req, res) {
Expand All @@ -11,6 +14,12 @@ export default () =>
switch (route) {
case "login":
login(req, res);
case "register":
register(req, res);
case "me":
me(req, res);
case "logout":
logout(req, res);
default:
res.status(404).end();
}
Expand Down
8 changes: 2 additions & 6 deletions src/handlers/login.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { setupChallenge } from "../utils/setupChallenge";

export const login = (req, res) => {
// setupChallenge();
console.log("log in");
console.log("req", req);
console.log("res", res);

res.status(200).json({ name: "John Doe" });
setupChallenge();
res.status(200).json({ function: "Login" });
};
6 changes: 6 additions & 0 deletions src/handlers/logout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { setupChallenge } from "../utils/setupChallenge";

export const logout = (req, res) => {
setupChallenge();
res.status(200).json({ function: "logout" });
};
6 changes: 6 additions & 0 deletions src/handlers/me.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { setupChallenge } from "../utils/setupChallenge";

export const me = (req, res) => {
setupChallenge();
res.status(200).json({ function: "me" });
};
6 changes: 6 additions & 0 deletions src/handlers/register.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { setupChallenge } from "../utils/setupChallenge";

export const register = (req, res) => {
setupChallenge();
res.status(200).json({ function: "Register" });
};
11 changes: 11 additions & 0 deletions src/utils/base64Encode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Base64-urlencodes the input string
export function base64UrlEncode(str) {
// Convert the ArrayBuffer to string using Uint8 array to conver to what btoa accepts.
// btoa accepts chars only within ascii 0-255 and base64 encodes them.
// Then convert the base64 encoded to base64url encoded
// (replace + with -, replace / with _, trim trailing =)
return btoa(String.fromCharCode.apply(null, new Uint8Array(str)))
.replace(/\+/g, "-")
.replace(/\//g, "_")
.replace(/=+$/, "");
}
8 changes: 8 additions & 0 deletions src/utils/pkceChallengeFromVerifier.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { base64UrlEncode } from "./base64Encode";
import { sha256 } from "./sha256";

// Return the base64-urlencoded sha256 hash for the PKCE challenge
export async function pkceChallengeFromVerifier(v) {
const hashed = await sha256(v);
return base64UrlEncode(hashed);
}
9 changes: 6 additions & 3 deletions src/utils/setupChallenge.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { randomString } from "./randomString";
import { SESSION_PREFIX } from "../config/sessionPrefix";
import { pkceChallengeFromVerifier } from "./pkceChallengeFromVerifier";

var cookie = require("cookie");

export const setupChallenge = () => {
export const setupChallenge = async () => {
const state = randomString();
const code_verifier = randomString(); // the secret
// Hash and base64-urlencode the secret to use as the challenge
const code_challenge = "await pkceChallengeFromVerifier(code_verifier)";
const code_challenge = await pkceChallengeFromVerifier(code_verifier);

cookie.parse(`${SESSION_PREFIX}-${state}`, code_verifier);

// Build and encode the authorisation request url
const url = new URL(config.authorization_endpoint);
const url = new URL("https://developer.mozilla.org/oauth2/auth");
console.log(url);
return { state, code_challenge, url };
};
8 changes: 8 additions & 0 deletions src/utils/sha256.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const { subtle } = require("crypto");
// Calculate the SHA256 hash of the input text.
// Returns a promise that resolves to an ArrayBuffer
export function sha256(plain) {
const encoder = new TextEncoder();
const data = encoder.encode(plain);
return subtle.digest("SHA-256", data);
}

0 comments on commit c97acd4

Please sign in to comment.