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

chore(console,core): launch organization jit #5999

Merged
merged 1 commit into from
Jun 14, 2024
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
20 changes: 20 additions & 0 deletions .changeset/smart-laws-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
"@logto/console": minor
"@logto/phrases": minor
"@logto/schemas": minor
"@logto/core": minor
"@logto/integration-tests": patch
---

feature: just-in-time user provisioning for organizations

This feature allows organizations to provision users when signing up with their email address or being added by Management API. If the user's email domain matches one of the organization's configured domains, the user will be automatically provisioned to the organization.

To enable this feature, you can add email domain via the Management API or the Logto Console:

- We added the following new endpoints to the Management API:
- `GET /organizations/{organizationId}/email-domains`
- `POST /organizations/{organizationId}/email-domains`
- `PUT /organizations/{organizationId}/email-domains`
- `DELETE /organizations/{organizationId}/email-domains/{emailDomain}`
- In the Logto Console, you can manage email domains in the organization details page -> "Just-in-time provisioning" section.
131 changes: 64 additions & 67 deletions packages/console/src/pages/OrganizationDetails/Settings/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import DetailsForm from '@/components/DetailsForm';
import FormCard from '@/components/FormCard';
import MultiOptionInput from '@/components/MultiOptionInput';
import UnsavedChangesAlertModal from '@/components/UnsavedChangesAlertModal';
import { isDevFeaturesEnabled } from '@/consts/env';
import CodeEditor from '@/ds-components/CodeEditor';
import FormField from '@/ds-components/FormField';
import RadioGroup, { Radio } from '@/ds-components/RadioGroup';
Expand Down Expand Up @@ -134,72 +133,70 @@ function Settings() {
/>
</FormField>
</FormCard>
{isDevFeaturesEnabled && (
<FormCard
title="organization_details.jit.title"
description="organization_details.jit.description"
>
<FormField title="organization_details.jit.is_enabled_title">
<Controller
name="isJitEnabled"
control={control}
render={({ field }) => (
<div className={styles.jitContent}>
<RadioGroup
name="isJitEnabled"
value={String(field.value)}
onChange={(value) => {
field.onChange(value === 'true');
}}
>
<Radio
value="false"
title="organization_details.jit.is_enabled_false_description"
/>
<Radio
value="true"
title="organization_details.jit.is_enabled_true_description"
/>
</RadioGroup>
{field.value && (
<Controller
name="jitEmailDomains"
control={control}
render={({ field: { onChange, value } }) => (
<MultiOptionInput
className={styles.emailDomains}
values={value}
renderValue={(value) => value}
validateInput={(input) => {
if (!domainRegExp.test(input)) {
return t('organization_details.jit.invalid_domain');
}

if (value.includes(input)) {
return t('organization_details.jit.domain_already_added');
}

return { value: input };
}}
placeholder={t('organization_details.jit.email_domains_placeholder')}
error={errors.jitEmailDomains?.message}
onChange={onChange}
onError={(error) => {
setError('jitEmailDomains', { type: 'custom', message: error });
}}
onClearError={() => {
clearErrors('jitEmailDomains');
}}
/>
)}
/>
)}
</div>
)}
/>
</FormField>
</FormCard>
)}
<FormCard
title="organization_details.jit.title"
description="organization_details.jit.description"
>
<FormField title="organization_details.jit.is_enabled_title">
<Controller
name="isJitEnabled"
control={control}
render={({ field }) => (
<div className={styles.jitContent}>
<RadioGroup
name="isJitEnabled"
value={String(field.value)}
onChange={(value) => {
field.onChange(value === 'true');
}}
>
<Radio
value="false"
title="organization_details.jit.is_enabled_false_description"
/>
<Radio
value="true"
title="organization_details.jit.is_enabled_true_description"
/>
</RadioGroup>
{field.value && (
<Controller
name="jitEmailDomains"
control={control}
render={({ field: { onChange, value } }) => (
<MultiOptionInput
className={styles.emailDomains}
values={value}
renderValue={(value) => value}
validateInput={(input) => {
if (!domainRegExp.test(input)) {
return t('organization_details.jit.invalid_domain');
}

if (value.includes(input)) {
return t('organization_details.jit.domain_already_added');
}

return { value: input };
}}
placeholder={t('organization_details.jit.email_domains_placeholder')}
error={errors.jitEmailDomains?.message}
onChange={onChange}
onError={(error) => {
setError('jitEmailDomains', { type: 'custom', message: error });
}}
onClearError={() => {
clearErrors('jitEmailDomains');
}}
/>
)}
/>
)}
</div>
)}
/>
</FormField>
</FormCard>
<UnsavedChangesAlertModal hasUnsavedChanges={!isDeleting && isDirty} />
</DetailsForm>
);
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/libraries/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,11 @@
);
}

// TODO: If the user's email is not verified, we should not provision the user into any organization.

Check warning on line 145 in packages/core/src/libraries/user.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/libraries/user.ts#L145

Added line #L145 was not covered by tests
const provisionOrganizations = async (): Promise<readonly string[]> => {
// Just-in-time organization provisioning
const userEmailDomain = data.primaryEmail?.split('@')[1];
// TODO: Remove this check when launching
if (EnvSet.values.isDevFeaturesEnabled && userEmailDomain) {
if (userEmailDomain) {

Check warning on line 149 in packages/core/src/libraries/user.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/libraries/user.ts#L149

Added line #L149 was not covered by tests
const organizationQueries = new OrganizationQueries(connection);
const organizationIds = await organizationQueries.emailDomains.getOrganizationIdsByDomain(
userEmailDomain
Expand Down
7 changes: 1 addition & 6 deletions packages/core/src/routes/organization/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
import { yes } from '@silverhand/essentials';
import { z } from 'zod';

import { EnvSet } from '#src/env-set/index.js';
import koaGuard from '#src/middleware/koa-guard.js';
import koaPagination from '#src/middleware/koa-pagination.js';
import koaQuotaGuard from '#src/middleware/koa-quota-guard.js';
Expand Down Expand Up @@ -139,11 +138,7 @@ export default function organizationRoutes<T extends ManagementApiRouter>(
);

userRoleRelationRoutes(router, organizations);

// TODO: Remove this check when launching
if (EnvSet.values.isDevFeaturesEnabled) {
emailDomainRoutes(router, organizations);
}
emailDomainRoutes(router, organizations);

// MARK: Mount sub-routes
organizationRoleRoutes(...args);
Expand Down
Loading