Files
outline/server/queues/tasks/ValidateSSOAccessTask.ts
T
Tom MoorandClaude Fable 5.1 abc47b442a feat: Sync group descriptions and re-sync groups on SSO validation (#13889)
* feat: Sync group descriptions and re-sync groups on SSO validation

- Sync group descriptions from group sync providers alongside names
- Lock name and description edits for externally synced groups at the
  API layer and in the edit dialog
- Move group sync into a SyncUserGroupsTask, scheduled from login and
  from ValidateSSOAccessTask after a fresh provider validation
- Add authenticationProviders.startGroupSync for providers that require
  an admin consent flow before group sync can be enabled

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* fix: Make group sync setup generic and deduplicate sync jobs

- Expose groupSyncRequiresSetup from the provider list and use it in the
  settings scene instead of checking for the azure provider name
- Rename the setup redirect query param to groupSync
- Give SyncUserGroupsTask a job id per user authentication so concurrent
  syncs for the same authentication are not queued twice

* Use existing hook

---------

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-25 19:49:26 -04:00

93 lines
2.7 KiB
TypeScript

import Logger from "@server/logging/Logger";
import { User, UserAuthentication } from "@server/models";
import { MutexLock } from "@server/utils/MutexLock";
import { Minute } from "@shared/utils/time";
import SyncUserGroupsTask from "./SyncUserGroupsTask";
import { BaseTask, TaskPriority } from "./base/BaseTask";
type Props = {
userId: string;
};
export default class ValidateSSOAccessTask extends BaseTask<Props> {
protected jobId({ userId }: Props) {
return `validate-sso:${userId}`;
}
public async perform({ userId }: Props) {
await MutexLock.using(
`validateSSO:${userId}`,
Minute.ms,
async (signal) => {
const userAuthentications = await UserAuthentication.findAll({
where: { userId },
});
if (userAuthentications.length === 0) {
return;
}
// Check the validity of the user's authentications. Validation is
// throttled internally, so we track whether a fresh validation with
// the provider actually occurred to avoid syncing groups too often.
let error;
const freshlyValidated: UserAuthentication[] = [];
const validity = await Promise.all(
userAuthentications.map(async (authentication) => {
const previouslyValidatedAt = authentication.lastValidatedAt;
try {
const isValid = await authentication.validateAccess();
if (
isValid &&
authentication.lastValidatedAt > previouslyValidatedAt
) {
freshlyValidated.push(authentication);
}
return isValid;
} catch (err) {
error = err;
return false;
}
})
);
if (signal.aborted) {
throw signal.error;
}
if (validity.some((isValid) => isValid)) {
// Re-sync group memberships from each provider that was just
// validated with the third party.
for (const authentication of freshlyValidated) {
await new SyncUserGroupsTask().schedule({
userAuthenticationId: authentication.id,
});
}
return;
}
// If an unexpected error occurred, throw it to trigger a retry.
if (error) {
throw error;
}
// If all are invalid then we need to revoke the users Outline sessions.
const user = await User.findByPk(userId);
Logger.info(
"task",
`Authentication token no longer valid for ${user?.id}`
);
await user?.rotateJwtSecret({});
}
);
}
public get options() {
return {
attempts: 2,
priority: TaskPriority.Background,
};
}
}