mirror of
https://github.com/nikdoof/pocket-id.git
synced 2025-12-14 15:22:18 +00:00
feat: add PKCE for non public clients
This commit is contained in:
@@ -10,6 +10,7 @@ type OidcClientDto struct {
|
|||||||
PublicOidcClientDto
|
PublicOidcClientDto
|
||||||
CallbackURLs []string `json:"callbackURLs"`
|
CallbackURLs []string `json:"callbackURLs"`
|
||||||
IsPublic bool `json:"isPublic"`
|
IsPublic bool `json:"isPublic"`
|
||||||
|
PkceEnabled bool `json:"pkceEnabled"`
|
||||||
CreatedBy UserDto `json:"createdBy"`
|
CreatedBy UserDto `json:"createdBy"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -17,6 +18,7 @@ type OidcClientCreateDto struct {
|
|||||||
Name string `json:"name" binding:"required,max=50"`
|
Name string `json:"name" binding:"required,max=50"`
|
||||||
CallbackURLs []string `json:"callbackURLs" binding:"required,urlList"`
|
CallbackURLs []string `json:"callbackURLs" binding:"required,urlList"`
|
||||||
IsPublic bool `json:"isPublic"`
|
IsPublic bool `json:"isPublic"`
|
||||||
|
PkceEnabled bool `json:"pkceEnabled"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type AuthorizeOidcClientRequestDto struct {
|
type AuthorizeOidcClientRequestDto struct {
|
||||||
|
|||||||
@@ -42,6 +42,7 @@ type OidcClient struct {
|
|||||||
ImageType *string
|
ImageType *string
|
||||||
HasLogo bool `gorm:"-"`
|
HasLogo bool `gorm:"-"`
|
||||||
IsPublic bool
|
IsPublic bool
|
||||||
|
PkceEnabled bool
|
||||||
|
|
||||||
CreatedByID string
|
CreatedByID string
|
||||||
CreatedBy User
|
CreatedBy User
|
||||||
|
|||||||
@@ -131,8 +131,8 @@ func (s *OidcService) CreateTokens(code, grantType, clientID, clientSecret, code
|
|||||||
return "", "", &common.OidcInvalidAuthorizationCodeError{}
|
return "", "", &common.OidcInvalidAuthorizationCodeError{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the client is public, the code verifier must match the code challenge
|
// If the client is public or PKCE is enabled, the code verifier must match the code challenge
|
||||||
if client.IsPublic {
|
if client.IsPublic || client.PkceEnabled {
|
||||||
if !s.validateCodeVerifier(codeVerifier, *authorizationCodeMetaData.CodeChallenge, *authorizationCodeMetaData.CodeChallengeMethodSha256) {
|
if !s.validateCodeVerifier(codeVerifier, *authorizationCodeMetaData.CodeChallenge, *authorizationCodeMetaData.CodeChallengeMethodSha256) {
|
||||||
return "", "", &common.OidcInvalidCodeVerifierError{}
|
return "", "", &common.OidcInvalidCodeVerifierError{}
|
||||||
}
|
}
|
||||||
@@ -189,6 +189,8 @@ func (s *OidcService) CreateClient(input dto.OidcClientCreateDto, userID string)
|
|||||||
Name: input.Name,
|
Name: input.Name,
|
||||||
CallbackURLs: input.CallbackURLs,
|
CallbackURLs: input.CallbackURLs,
|
||||||
CreatedByID: userID,
|
CreatedByID: userID,
|
||||||
|
IsPublic: input.IsPublic,
|
||||||
|
PkceEnabled: input.IsPublic || input.PkceEnabled,
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := s.db.Create(&client).Error; err != nil {
|
if err := s.db.Create(&client).Error; err != nil {
|
||||||
@@ -207,6 +209,7 @@ func (s *OidcService) UpdateClient(clientID string, input dto.OidcClientCreateDt
|
|||||||
client.Name = input.Name
|
client.Name = input.Name
|
||||||
client.CallbackURLs = input.CallbackURLs
|
client.CallbackURLs = input.CallbackURLs
|
||||||
client.IsPublic = input.IsPublic
|
client.IsPublic = input.IsPublic
|
||||||
|
client.PkceEnabled = input.IsPublic || input.PkceEnabled
|
||||||
|
|
||||||
if err := s.db.Save(&client).Error; err != nil {
|
if err := s.db.Save(&client).Error; err != nil {
|
||||||
return model.OidcClient{}, err
|
return model.OidcClient{}, err
|
||||||
@@ -406,6 +409,10 @@ func (s *OidcService) createAuthorizationCode(clientID string, userID string, sc
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *OidcService) validateCodeVerifier(codeVerifier, codeChallenge string, codeChallengeMethodSha256 bool) bool {
|
func (s *OidcService) validateCodeVerifier(codeVerifier, codeChallenge string, codeChallengeMethodSha256 bool) bool {
|
||||||
|
if codeVerifier == "" || codeChallenge == "" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
if !codeChallengeMethodSha256 {
|
if !codeChallengeMethodSha256 {
|
||||||
return codeVerifier == codeChallenge
|
return codeVerifier == codeChallenge
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
ALTER TABLE oidc_clients DROP COLUMN pkce_enabled;
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
ALTER TABLE oidc_clients ADD COLUMN pkce_enabled BOOLEAN DEFAULT FALSE;
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
ALTER TABLE oidc_clients DROP COLUMN pkce_enabled;
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
ALTER TABLE oidc_clients ADD COLUMN pkce_enabled BOOLEAN DEFAULT FALSE;
|
||||||
@@ -6,12 +6,26 @@
|
|||||||
id,
|
id,
|
||||||
checked = $bindable(),
|
checked = $bindable(),
|
||||||
label,
|
label,
|
||||||
description
|
description,
|
||||||
}: { id: string; checked: boolean; label: string; description?: string } = $props();
|
disabled = false,
|
||||||
|
onCheckedChange
|
||||||
|
}: {
|
||||||
|
id: string;
|
||||||
|
checked: boolean;
|
||||||
|
label: string;
|
||||||
|
description?: string;
|
||||||
|
disabled?: boolean;
|
||||||
|
onCheckedChange?: (checked: boolean) => void;
|
||||||
|
} = $props();
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div class="items-top mt-5 flex space-x-2">
|
<div class="items-top mt-5 flex space-x-2">
|
||||||
<Checkbox {id} bind:checked />
|
<Checkbox
|
||||||
|
{id}
|
||||||
|
{disabled}
|
||||||
|
onCheckedChange={(v) => onCheckedChange && onCheckedChange(v == true)}
|
||||||
|
bind:checked
|
||||||
|
/>
|
||||||
<div class="grid gap-1.5 leading-none">
|
<div class="grid gap-1.5 leading-none">
|
||||||
<Label for={id} class="mb-0 text-sm font-medium leading-none">
|
<Label for={id} class="mb-0 text-sm font-medium leading-none">
|
||||||
{label}
|
{label}
|
||||||
|
|||||||
@@ -19,7 +19,7 @@
|
|||||||
>
|
>
|
||||||
<div class="flex h-16 items-center">
|
<div class="flex h-16 items-center">
|
||||||
{#if !isAuthPage}
|
{#if !isAuthPage}
|
||||||
<Logo class="mr-3 h-10 w-10" />
|
<Logo class="mr-3 h-8 w-8" />
|
||||||
<h1 class="text-lg font-medium" data-testid="application-name">
|
<h1 class="text-lg font-medium" data-testid="application-name">
|
||||||
{$appConfigStore.appName}
|
{$appConfigStore.appName}
|
||||||
</h1>
|
</h1>
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ export type OidcClient = {
|
|||||||
callbackURLs: [string, ...string[]];
|
callbackURLs: [string, ...string[]];
|
||||||
hasLogo: boolean;
|
hasLogo: boolean;
|
||||||
isPublic: boolean;
|
isPublic: boolean;
|
||||||
|
pkceEnabled: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type OidcClientCreate = Omit<OidcClient, 'id' | 'logoURL' | 'hasLogo'>;
|
export type OidcClientCreate = Omit<OidcClient, 'id' | 'logoURL' | 'hasLogo'>;
|
||||||
|
|||||||
@@ -80,11 +80,19 @@ export function createForm<T extends z.ZodType<any, any>>(schema: T, initialValu
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function setValue(key: keyof z.infer<T>, value: z.infer<T>[keyof z.infer<T>]) {
|
||||||
|
inputsStore.update((inputs) => {
|
||||||
|
inputs[key].value = value;
|
||||||
|
return inputs;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
schema,
|
schema,
|
||||||
inputs: inputsStore,
|
inputs: inputsStore,
|
||||||
data,
|
data,
|
||||||
validate,
|
validate,
|
||||||
|
setValue,
|
||||||
reset
|
reset
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,7 @@
|
|||||||
OidcClientCreateWithLogo
|
OidcClientCreateWithLogo
|
||||||
} from '$lib/types/oidc.type';
|
} from '$lib/types/oidc.type';
|
||||||
import { createForm } from '$lib/utils/form-util';
|
import { createForm } from '$lib/utils/form-util';
|
||||||
import { z } from 'zod';
|
import { set, z } from 'zod';
|
||||||
import OidcCallbackUrlInput from './oidc-callback-url-input.svelte';
|
import OidcCallbackUrlInput from './oidc-callback-url-input.svelte';
|
||||||
|
|
||||||
let {
|
let {
|
||||||
@@ -30,13 +30,15 @@
|
|||||||
const client: OidcClientCreate = {
|
const client: OidcClientCreate = {
|
||||||
name: existingClient?.name || '',
|
name: existingClient?.name || '',
|
||||||
callbackURLs: existingClient?.callbackURLs || [''],
|
callbackURLs: existingClient?.callbackURLs || [''],
|
||||||
isPublic: existingClient?.isPublic || false
|
isPublic: existingClient?.isPublic || false,
|
||||||
|
pkceEnabled: existingClient?.isPublic == true || existingClient?.pkceEnabled || false
|
||||||
};
|
};
|
||||||
|
|
||||||
const formSchema = z.object({
|
const formSchema = z.object({
|
||||||
name: z.string().min(2).max(50),
|
name: z.string().min(2).max(50),
|
||||||
callbackURLs: z.array(z.string().url()).nonempty(),
|
callbackURLs: z.array(z.string().url()).nonempty(),
|
||||||
isPublic: z.boolean()
|
isPublic: z.boolean(),
|
||||||
|
pkceEnabled: z.boolean()
|
||||||
});
|
});
|
||||||
|
|
||||||
type FormSchema = typeof formSchema;
|
type FormSchema = typeof formSchema;
|
||||||
@@ -85,8 +87,19 @@
|
|||||||
id="public-client"
|
id="public-client"
|
||||||
label="Public Client"
|
label="Public Client"
|
||||||
description="Public clients do not have a client secret and use PKCE instead. Enable this if your client is a SPA or mobile app."
|
description="Public clients do not have a client secret and use PKCE instead. Enable this if your client is a SPA or mobile app."
|
||||||
|
onCheckedChange={(v) => {
|
||||||
|
console.log(v)
|
||||||
|
if (v == true) form.setValue('pkceEnabled', true);
|
||||||
|
}}
|
||||||
bind:checked={$inputs.isPublic.value}
|
bind:checked={$inputs.isPublic.value}
|
||||||
/>
|
/>
|
||||||
|
<CheckboxWithLabel
|
||||||
|
id="pkce"
|
||||||
|
label="PKCE"
|
||||||
|
description="Public Key Code Exchange is a security feature to prevent CSRF and authorization code interception attacks."
|
||||||
|
disabled={$inputs.isPublic.value}
|
||||||
|
bind:checked={$inputs.pkceEnabled.value}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div class="mt-8">
|
<div class="mt-8">
|
||||||
<Label for="logo">Logo</Label>
|
<Label for="logo">Logo</Label>
|
||||||
|
|||||||
Reference in New Issue
Block a user