Skip to content

Commit 154f59e

Browse files
committed
Enforce SSO login and refactor SSO config update
Added enforcement of SSO login for users in workspaces with enforced SSO. Refactored SSO configuration update logic by introducing setSsoConfig method in WorkspaceModel and updating resolver to use it, ensuring only SSO config is modified.
1 parent 65c22e3 commit 154f59e

File tree

3 files changed

+57
-22
lines changed

3 files changed

+57
-22
lines changed

src/models/workspace.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,25 @@ export default class WorkspaceModel extends AbstractModel<WorkspaceDBScheme> imp
420420
);
421421
}
422422

423+
/**
424+
* Update SSO configuration
425+
* @param ssoConfig - SSO configuration to set (or undefined to remove)
426+
*/
427+
public async setSsoConfig(ssoConfig: WorkspaceDBScheme['sso'] | undefined): Promise<void> {
428+
this.sso = ssoConfig;
429+
430+
await this.collection.updateOne(
431+
{
432+
_id: new ObjectId(this._id),
433+
},
434+
{
435+
$set: {
436+
sso: this.sso,
437+
},
438+
}
439+
);
440+
}
441+
423442
/**
424443
* Due date of the current workspace tariff plan
425444
*/

src/resolvers/user.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,21 @@ export default {
9898
throw new AuthenticationError('Wrong email or password');
9999
}
100100

101+
/**
102+
* Check if there is a workspace with enforced SSO
103+
* If user is a member of any workspace with enforced SSO, they must use SSO login
104+
*/
105+
const workspacesIds = await user.getWorkspacesIds([]);
106+
const workspaces = await factories.workspacesFactory.findManyByIds(workspacesIds);
107+
108+
const enforcedWorkspace = workspaces.find(w => w.sso?.enabled && w.sso?.enforced);
109+
110+
if (enforcedWorkspace) {
111+
throw new AuthenticationError(
112+
'This workspace requires SSO login. Please use SSO to sign in.'
113+
);
114+
}
115+
101116
return user.generateTokensPair();
102117
},
103118

src/resolvers/workspace.js

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -382,33 +382,34 @@ module.exports = {
382382
}
383383

384384
/**
385-
* Prepare update data
385+
* Prepare SSO configuration
386386
* If enabled=false, preserve existing SSO config and only update enabled flag
387387
* If enabled=true, update full SSO configuration
388388
*/
389-
const updateData = {
390-
...workspace,
391-
sso: config.enabled ? {
392-
enabled: config.enabled,
393-
enforced: config.enforced || false,
394-
type: 'saml',
395-
saml: {
396-
idpEntityId: config.saml.idpEntityId,
397-
ssoUrl: config.saml.ssoUrl,
398-
x509Cert: config.saml.x509Cert,
399-
nameIdFormat: config.saml.nameIdFormat,
400-
attributeMapping: {
401-
email: config.saml.attributeMapping.email,
402-
name: config.saml.attributeMapping.name,
403-
},
389+
const ssoConfig = config.enabled ? {
390+
enabled: config.enabled,
391+
enforced: config.enforced || false,
392+
type: 'saml',
393+
saml: {
394+
idpEntityId: config.saml.idpEntityId,
395+
ssoUrl: config.saml.ssoUrl,
396+
x509Cert: config.saml.x509Cert,
397+
nameIdFormat: config.saml.nameIdFormat,
398+
attributeMapping: {
399+
email: config.saml.attributeMapping.email,
400+
name: config.saml.attributeMapping.name,
404401
},
405-
} : workspace.sso ? {
406-
...workspace.sso,
407-
enabled: false,
408-
} : undefined,
409-
};
402+
},
403+
} : workspace.sso ? {
404+
...workspace.sso,
405+
enabled: false,
406+
} : undefined;
410407

411-
await workspace.updateWorkspace(updateData);
408+
/**
409+
* Update SSO configuration using model method
410+
* This method handles the update correctly without touching other fields
411+
*/
412+
await workspace.setSsoConfig(ssoConfig);
412413

413414
return true;
414415
},

0 commit comments

Comments
 (0)