Skip to content

Commit 41d225d

Browse files
battkajsArvid Håkansson
andauthored
fix: added a new pkce string generator and use that in oauth handshake (#5323)
Co-authored-by: Arvid Håkansson <[email protected]>
1 parent f5ac1b7 commit 41d225d

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

keep-ui/app/(keep)/providers/provider-form.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import { KeepApiError, KeepApiReadOnlyError } from "@/shared/api";
5353
import { showErrorToast } from "@/shared/ui";
5454
import {
5555
base64urlencode,
56+
generatePkceVerifier,
5657
generateRandomString,
5758
sha256,
5859
} from "@/shared/lib/encodings";
@@ -103,7 +104,7 @@ function getInitialFormValues(provider: Provider, isHealthCheck?: boolean) {
103104
const initialValues: ProviderFormData = {
104105
provider_id: provider.id,
105106
install_webhook: !isHealthCheck
106-
? provider.can_setup_webhook ?? false
107+
? (provider.can_setup_webhook ?? false)
107108
: false,
108109
pulling_enabled: provider.pulling_enabled,
109110
};
@@ -205,7 +206,7 @@ const ProviderForm = ({
205206
const callInstallWebhook = async () => await installWebhook(provider);
206207

207208
async function handleOauth() {
208-
const verifier = generateRandomString();
209+
const verifier = generatePkceVerifier();
209210
cookieCutter.set("verifier", verifier);
210211
cookieCutter.set(
211212
"oauth2_install_webhook",

keep-ui/shared/lib/encodings.ts

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* Converts a decimal number to a hexadecimal string with proper padding
3-
*
3+
*
44
* @param dec - The decimal number to convert
55
* @returns A hexadecimal string representation
66
* @internal This is a utility function used by generateRandomString
@@ -11,9 +11,9 @@ function dec2hex(dec: number) {
1111

1212
/**
1313
* Generates a cryptographically secure random string
14-
*
14+
*
1515
* @returns A random hexadecimal string of 56 characters
16-
*
16+
*
1717
* @example
1818
* const randomStr = generateRandomString();
1919
* // e.g. "7b8d4f2e9a1c6b3d5e8f2a7c9b4d1e6f3a8c5b2d7e9f1a3c8b6d4e7f2a9c5"
@@ -24,12 +24,30 @@ export function generateRandomString() {
2424
return Array.from(array, dec2hex).join("");
2525
}
2626

27+
/**
28+
* Generates a PKCE verifier string with length 128 characters
29+
*
30+
* @returns a random string of 128 characters
31+
*
32+
* @example
33+
* const verifier = generatePkceVerifier();
34+
* // e.g. "7b8d4f2e9a1c6b3d5e8f2a7c9b4d1e6f3a8c5b2d7e9f1a3c8b6d4e7f2a9c5"
35+
*/
36+
export function generatePkceVerifier(): string {
37+
const arr = new Uint8Array(96);
38+
window.crypto.getRandomValues(arr);
39+
return btoa(String.fromCharCode(...arr))
40+
.replace(/\+/g, "-")
41+
.replace(/\//g, "_")
42+
.replace(/=+$/, "");
43+
}
44+
2745
/**
2846
* Computes the SHA-256 hash of a string
29-
*
47+
*
3048
* @param plain - The input string to hash
3149
* @returns A Promise that resolves to an ArrayBuffer containing the hash
32-
*
50+
*
3351
* @example
3452
* const hashBuffer = await sha256("hello world");
3553
*/
@@ -41,15 +59,15 @@ export function sha256(plain: string) {
4159

4260
/**
4361
* Encodes an ArrayBuffer to base64url format (URL-safe base64)
44-
*
62+
*
4563
* Base64url encoding is a variant of base64 that is URL and filename safe:
4664
* - Replaces '+' with '-'
4765
* - Replaces '/' with '_'
4866
* - Removes padding '=' characters
49-
*
67+
*
5068
* @param a - The ArrayBuffer to encode
5169
* @returns The base64url-encoded string
52-
*
70+
*
5371
* @example
5472
* const hashBuffer = await sha256("hello world");
5573
* const base64urlStr = base64urlencode(hashBuffer);

0 commit comments

Comments
 (0)