-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement PKCE Flow for OAuth 2.0 in Mock API (#11)
* Refactor OAuth API to support PKCE * Fix checkCodeVerifier & Update code challenge storage key * Add test for oauth PKCE * Add documentation for pkce * Update oauth test * make sure it works with openid-client * review fixes * conditionally enable HTTPS --------- Co-authored-by: Thai Pangsakulyanont <dtinth@spacet.me>
- Loading branch information
Showing
3 changed files
with
140 additions
and
4 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
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,16 @@ | ||
import crypto from "crypto"; | ||
|
||
export function randomCodeVerifier(length: number) { | ||
const array = crypto.getRandomValues(new Uint8Array(length)); | ||
return Array.from(array, (byte) => byte.toString(36)) | ||
.join("") | ||
.slice(0, length); | ||
} | ||
|
||
export function generateCodeChallenge(codeVerifier: string, method = "plain") { | ||
if (!method || method == "plain") { | ||
return codeVerifier; | ||
} | ||
const hash = crypto.createHash("sha256").update(codeVerifier).digest(); | ||
return hash.toString("base64url"); | ||
} |