2024-05-31 00:40:03 -07:00
|
|
|
import { Container } from 'typedi';
|
2023-01-24 17:18:39 -08:00
|
|
|
import type { Entry as LdapUser } from 'ldapts';
|
2024-02-08 06:13:29 -08:00
|
|
|
import { Not } from '@n8n/typeorm';
|
2024-01-15 06:01:48 -08:00
|
|
|
import { Cipher } from 'n8n-core';
|
2023-10-19 04:58:06 -07:00
|
|
|
|
2023-01-24 17:18:39 -08:00
|
|
|
import config from '@/config';
|
|
|
|
import type { User } from '@db/entities/User';
|
2024-05-31 00:40:03 -07:00
|
|
|
import { UserRepository } from '@db/repositories/user.repository';
|
|
|
|
import { AuthProviderSyncHistoryRepository } from '@db/repositories/authProviderSyncHistory.repository';
|
2024-05-17 01:53:15 -07:00
|
|
|
import { LDAP_DEFAULT_CONFIGURATION } from '@/Ldap/constants';
|
2024-07-05 00:51:16 -07:00
|
|
|
import { LdapService } from '@/Ldap/ldap.service.ee';
|
|
|
|
import { saveLdapSynchronization } from '@/Ldap/helpers.ee';
|
2023-03-31 04:51:38 -07:00
|
|
|
import { getCurrentAuthenticationMethod, setCurrentAuthenticationMethod } from '@/sso/ssoHelpers';
|
2023-07-13 01:14:48 -07:00
|
|
|
|
2023-01-24 17:18:39 -08:00
|
|
|
import { randomEmail, randomName, uniqueId } from './../shared/random';
|
|
|
|
import * as testDb from './../shared/testDb';
|
2023-07-13 01:14:48 -07:00
|
|
|
import * as utils from '../shared/utils/';
|
2023-11-08 07:29:39 -08:00
|
|
|
import { createLdapUser, createUser, getAllUsers, getLdapIdentities } from '../shared/db/users';
|
2024-05-17 01:53:15 -07:00
|
|
|
import { getPersonalProject } from '../shared/db/projects';
|
|
|
|
import { createLdapConfig, defaultLdapConfig } from '../shared/ldap';
|
2024-05-31 00:40:03 -07:00
|
|
|
import type { SuperAgentTest } from '../shared/types';
|
2023-01-24 17:18:39 -08:00
|
|
|
|
|
|
|
jest.mock('@/telemetry');
|
|
|
|
|
|
|
|
let owner: User;
|
2023-07-13 01:14:48 -07:00
|
|
|
let authOwnerAgent: SuperAgentTest;
|
2023-01-24 17:18:39 -08:00
|
|
|
|
2023-07-13 01:14:48 -07:00
|
|
|
const testServer = utils.setupTestServer({
|
|
|
|
endpointGroups: ['auth', 'ldap'],
|
|
|
|
enabledFeatures: ['feat:ldap'],
|
|
|
|
});
|
2023-01-24 17:18:39 -08:00
|
|
|
|
2023-07-13 01:14:48 -07:00
|
|
|
beforeAll(async () => {
|
2024-02-07 08:56:02 -08:00
|
|
|
owner = await createUser({ role: 'global:owner' });
|
2023-07-13 01:14:48 -07:00
|
|
|
authOwnerAgent = testServer.authAgentFor(owner);
|
2023-01-24 17:18:39 -08:00
|
|
|
|
2023-10-23 04:39:35 -07:00
|
|
|
defaultLdapConfig.bindingAdminPassword = Container.get(Cipher).encrypt(
|
2023-01-24 17:18:39 -08:00
|
|
|
defaultLdapConfig.bindingAdminPassword,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
await testDb.truncate([
|
|
|
|
'AuthIdentity',
|
|
|
|
'AuthProviderSyncHistory',
|
|
|
|
'SharedCredentials',
|
|
|
|
'Credentials',
|
|
|
|
'SharedWorkflow',
|
|
|
|
'Workflow',
|
|
|
|
]);
|
|
|
|
|
2023-11-10 06:04:26 -08:00
|
|
|
await Container.get(UserRepository).delete({ id: Not(owner.id) });
|
2023-01-24 17:18:39 -08:00
|
|
|
|
|
|
|
jest.mock('@/telemetry');
|
|
|
|
|
|
|
|
config.set('userManagement.isInstanceOwnerSetUp', true);
|
2023-11-01 20:02:49 -07:00
|
|
|
|
|
|
|
await setCurrentAuthenticationMethod('email');
|
2023-01-24 17:18:39 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Member role should not be able to access ldap routes', async () => {
|
2024-01-24 04:38:57 -08:00
|
|
|
const member = await createUser({ role: 'global:member' });
|
2023-07-13 01:14:48 -07:00
|
|
|
const authAgent = testServer.authAgentFor(member);
|
|
|
|
await authAgent.get('/ldap/config').expect(403);
|
|
|
|
await authAgent.put('/ldap/config').expect(403);
|
|
|
|
await authAgent.post('/ldap/test-connection').expect(403);
|
|
|
|
await authAgent.post('/ldap/sync').expect(403);
|
|
|
|
await authAgent.get('/ldap/sync').expect(403);
|
2023-01-24 17:18:39 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('PUT /ldap/config', () => {
|
|
|
|
test('route should validate payload', async () => {
|
|
|
|
const invalidValuePayload = {
|
|
|
|
...LDAP_DEFAULT_CONFIGURATION,
|
|
|
|
loginEnabled: '', // enabled property only allows boolean
|
|
|
|
loginLabel: '',
|
|
|
|
};
|
|
|
|
|
|
|
|
const invalidExtraPropertyPayload = {
|
|
|
|
...LDAP_DEFAULT_CONFIGURATION,
|
|
|
|
example: true, // property not defined in the validation schema
|
|
|
|
};
|
|
|
|
|
|
|
|
const missingPropertyPayload = {
|
|
|
|
loginEnabled: true,
|
|
|
|
loginLabel: '',
|
|
|
|
// missing all other properties defined in the schema
|
|
|
|
};
|
|
|
|
|
|
|
|
const invalidPayloads = [
|
|
|
|
invalidValuePayload,
|
|
|
|
invalidExtraPropertyPayload,
|
|
|
|
missingPropertyPayload,
|
|
|
|
];
|
|
|
|
|
|
|
|
for (const invalidPayload of invalidPayloads) {
|
2023-07-13 01:14:48 -07:00
|
|
|
const response = await authOwnerAgent.put('/ldap/config').send(invalidPayload);
|
2023-01-24 17:18:39 -08:00
|
|
|
expect(response.statusCode).toBe(400);
|
|
|
|
expect(response.body).toHaveProperty('message');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
test('route should update model', async () => {
|
|
|
|
const validPayload = {
|
|
|
|
...LDAP_DEFAULT_CONFIGURATION,
|
|
|
|
loginEnabled: true,
|
|
|
|
loginLabel: '',
|
|
|
|
};
|
|
|
|
|
2023-07-13 01:14:48 -07:00
|
|
|
const response = await authOwnerAgent.put('/ldap/config').send(validPayload);
|
2023-01-24 17:18:39 -08:00
|
|
|
|
|
|
|
expect(response.statusCode).toBe(200);
|
|
|
|
expect(response.body.data.loginEnabled).toBe(true);
|
|
|
|
expect(response.body.data.loginLabel).toBe('');
|
|
|
|
});
|
|
|
|
|
2023-11-01 20:02:49 -07:00
|
|
|
test('route should fail due to trying to enable LDAP login with SSO as current authentication method', async () => {
|
|
|
|
const validPayload = {
|
|
|
|
...LDAP_DEFAULT_CONFIGURATION,
|
|
|
|
loginEnabled: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
config.set('userManagement.authenticationMethod', 'saml');
|
|
|
|
|
|
|
|
const response = await authOwnerAgent.put('/ldap/config').send(validPayload);
|
|
|
|
|
|
|
|
expect(response.statusCode).toBe(400);
|
|
|
|
});
|
|
|
|
|
2023-01-24 17:18:39 -08:00
|
|
|
test('should apply "Convert all LDAP users to email users" strategy when LDAP login disabled', async () => {
|
|
|
|
const ldapConfig = await createLdapConfig();
|
2024-01-15 06:01:48 -08:00
|
|
|
Container.get(LdapService).setConfig(ldapConfig);
|
2023-01-24 17:18:39 -08:00
|
|
|
|
2024-01-24 04:38:57 -08:00
|
|
|
const member = await createLdapUser({ role: 'global:member' }, uniqueId());
|
2023-01-24 17:18:39 -08:00
|
|
|
|
|
|
|
const configuration = ldapConfig;
|
|
|
|
|
|
|
|
// disable the login, so the strategy is applied
|
2023-07-13 01:14:48 -07:00
|
|
|
await authOwnerAgent.put('/ldap/config').send({ ...configuration, loginEnabled: false });
|
2023-01-24 17:18:39 -08:00
|
|
|
|
2023-11-10 06:04:26 -08:00
|
|
|
const emailUser = await Container.get(UserRepository).findOneByOrFail({ id: member.id });
|
2023-11-08 07:29:39 -08:00
|
|
|
const localLdapIdentities = await getLdapIdentities();
|
2023-01-24 17:18:39 -08:00
|
|
|
|
2023-03-24 09:46:06 -07:00
|
|
|
expect(getCurrentAuthenticationMethod()).toBe('email');
|
2023-01-24 17:18:39 -08:00
|
|
|
expect(emailUser.email).toBe(member.email);
|
|
|
|
expect(emailUser.lastName).toBe(member.lastName);
|
|
|
|
expect(emailUser.firstName).toBe(member.firstName);
|
|
|
|
expect(localLdapIdentities.length).toEqual(0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('GET /ldap/config route should retrieve current configuration', async () => {
|
|
|
|
const validPayload = {
|
|
|
|
...LDAP_DEFAULT_CONFIGURATION,
|
|
|
|
loginEnabled: true,
|
|
|
|
loginLabel: '',
|
|
|
|
};
|
|
|
|
|
2023-07-13 01:14:48 -07:00
|
|
|
let response = await authOwnerAgent.put('/ldap/config').send(validPayload);
|
2023-01-24 17:18:39 -08:00
|
|
|
expect(response.statusCode).toBe(200);
|
2023-03-24 09:46:06 -07:00
|
|
|
expect(getCurrentAuthenticationMethod()).toBe('ldap');
|
2023-01-24 17:18:39 -08:00
|
|
|
|
2023-07-13 01:14:48 -07:00
|
|
|
response = await authOwnerAgent.get('/ldap/config');
|
2023-01-24 17:18:39 -08:00
|
|
|
|
|
|
|
expect(response.body.data).toMatchObject(validPayload);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('POST /ldap/test-connection', () => {
|
|
|
|
test('route should success', async () => {
|
2023-04-24 04:17:08 -07:00
|
|
|
jest.spyOn(LdapService.prototype, 'testConnection').mockResolvedValue();
|
2023-01-24 17:18:39 -08:00
|
|
|
|
2023-07-13 01:14:48 -07:00
|
|
|
await authOwnerAgent.post('/ldap/test-connection').expect(200);
|
2023-01-24 17:18:39 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('route should fail', async () => {
|
|
|
|
const errorMessage = 'Invalid connection';
|
|
|
|
|
2023-04-24 04:17:08 -07:00
|
|
|
jest.spyOn(LdapService.prototype, 'testConnection').mockRejectedValue(new Error(errorMessage));
|
2023-01-24 17:18:39 -08:00
|
|
|
|
2023-07-13 01:14:48 -07:00
|
|
|
const response = await authOwnerAgent.post('/ldap/test-connection');
|
2023-01-24 17:18:39 -08:00
|
|
|
expect(response.statusCode).toBe(400);
|
|
|
|
expect(response.body).toHaveProperty('message');
|
|
|
|
expect(response.body.message).toStrictEqual(errorMessage);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('POST /ldap/sync', () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
const ldapConfig = await createLdapConfig({
|
|
|
|
ldapIdAttribute: 'uid',
|
|
|
|
firstNameAttribute: 'givenName',
|
|
|
|
lastNameAttribute: 'sn',
|
|
|
|
emailAttribute: 'mail',
|
|
|
|
});
|
2024-01-15 06:01:48 -08:00
|
|
|
Container.get(LdapService).setConfig(ldapConfig);
|
2023-01-24 17:18:39 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('dry mode', () => {
|
|
|
|
const runTest = async (ldapUsers: LdapUser[]) => {
|
2023-04-24 04:17:08 -07:00
|
|
|
jest.spyOn(LdapService.prototype, 'searchWithAdminBinding').mockResolvedValue(ldapUsers);
|
2023-01-24 17:18:39 -08:00
|
|
|
|
2023-07-13 01:14:48 -07:00
|
|
|
await authOwnerAgent.post('/ldap/sync').send({ type: 'dry' }).expect(200);
|
2023-01-24 17:18:39 -08:00
|
|
|
|
2023-11-10 06:04:26 -08:00
|
|
|
const synchronization = await Container.get(
|
|
|
|
AuthProviderSyncHistoryRepository,
|
|
|
|
).findOneByOrFail({});
|
2023-01-24 17:18:39 -08:00
|
|
|
|
|
|
|
expect(synchronization.id).toBeDefined();
|
|
|
|
expect(synchronization.startedAt).toBeDefined();
|
|
|
|
expect(synchronization.endedAt).toBeDefined();
|
|
|
|
expect(synchronization.created).toBeDefined();
|
|
|
|
expect(synchronization.updated).toBeDefined();
|
|
|
|
expect(synchronization.disabled).toBeDefined();
|
|
|
|
expect(synchronization.status).toBeDefined();
|
|
|
|
expect(synchronization.scanned).toBeDefined();
|
|
|
|
expect(synchronization.error).toBeDefined();
|
|
|
|
expect(synchronization.runMode).toBeDefined();
|
|
|
|
expect(synchronization.runMode).toBe('dry');
|
|
|
|
expect(synchronization.scanned).toBe(ldapUsers.length);
|
|
|
|
return synchronization;
|
|
|
|
};
|
|
|
|
|
|
|
|
test('should detect new user but not persist change in model', async () => {
|
|
|
|
const synchronization = await runTest([
|
|
|
|
{
|
|
|
|
dn: '',
|
|
|
|
mail: randomEmail(),
|
|
|
|
sn: randomName(),
|
|
|
|
givenName: randomName(),
|
|
|
|
uid: uniqueId(),
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
|
|
|
|
expect(synchronization.created).toBe(1);
|
|
|
|
|
|
|
|
// Make sure only the instance owner is on the DB
|
2023-11-10 06:04:26 -08:00
|
|
|
const localDbUsers = await Container.get(UserRepository).find();
|
2023-01-24 17:18:39 -08:00
|
|
|
expect(localDbUsers.length).toBe(1);
|
|
|
|
expect(localDbUsers[0].id).toBe(owner.id);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should detect updated user but not persist change in model', async () => {
|
|
|
|
const ldapUserEmail = randomEmail();
|
|
|
|
const ldapUserId = uniqueId();
|
|
|
|
|
2023-11-08 07:29:39 -08:00
|
|
|
const member = await createLdapUser(
|
2024-01-24 04:38:57 -08:00
|
|
|
{ role: 'global:member', email: ldapUserEmail },
|
2023-01-24 17:18:39 -08:00
|
|
|
ldapUserId,
|
|
|
|
);
|
|
|
|
|
|
|
|
const synchronization = await runTest([
|
|
|
|
{
|
|
|
|
dn: '',
|
|
|
|
mail: ldapUserEmail,
|
|
|
|
sn: randomName(),
|
|
|
|
givenName: 'updated',
|
|
|
|
uid: ldapUserId,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
|
|
|
|
expect(synchronization.updated).toBe(1);
|
|
|
|
|
|
|
|
// Make sure the changes in the "LDAP server" were not persisted in the database
|
2023-11-08 07:29:39 -08:00
|
|
|
const localLdapIdentities = await getLdapIdentities();
|
2023-01-24 17:18:39 -08:00
|
|
|
const localLdapUsers = localLdapIdentities.map(({ user }) => user);
|
|
|
|
expect(localLdapUsers.length).toBe(1);
|
|
|
|
expect(localLdapUsers[0].id).toBe(member.id);
|
|
|
|
expect(localLdapUsers[0].lastName).toBe(member.lastName);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should detect disabled user but not persist change in model', async () => {
|
|
|
|
const ldapUserEmail = randomEmail();
|
|
|
|
const ldapUserId = uniqueId();
|
|
|
|
|
2023-11-08 07:29:39 -08:00
|
|
|
const member = await createLdapUser(
|
2024-01-24 04:38:57 -08:00
|
|
|
{ role: 'global:member', email: ldapUserEmail },
|
2023-01-24 17:18:39 -08:00
|
|
|
ldapUserId,
|
|
|
|
);
|
|
|
|
|
|
|
|
const synchronization = await runTest([]);
|
|
|
|
|
|
|
|
expect(synchronization.disabled).toBe(1);
|
|
|
|
|
|
|
|
// Make sure the changes in the "LDAP server" were not persisted in the database
|
2023-11-08 07:29:39 -08:00
|
|
|
const localLdapIdentities = await getLdapIdentities();
|
2023-01-24 17:18:39 -08:00
|
|
|
const localLdapUsers = localLdapIdentities.map(({ user }) => user);
|
|
|
|
expect(localLdapUsers.length).toBe(1);
|
|
|
|
expect(localLdapUsers[0].id).toBe(member.id);
|
|
|
|
expect(localLdapUsers[0].disabled).toBe(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('live mode', () => {
|
|
|
|
const runTest = async (ldapUsers: LdapUser[]) => {
|
2023-04-24 04:17:08 -07:00
|
|
|
jest.spyOn(LdapService.prototype, 'searchWithAdminBinding').mockResolvedValue(ldapUsers);
|
2023-01-24 17:18:39 -08:00
|
|
|
|
2023-07-13 01:14:48 -07:00
|
|
|
await authOwnerAgent.post('/ldap/sync').send({ type: 'live' }).expect(200);
|
2023-01-24 17:18:39 -08:00
|
|
|
|
2023-11-10 06:04:26 -08:00
|
|
|
const synchronization = await Container.get(
|
|
|
|
AuthProviderSyncHistoryRepository,
|
|
|
|
).findOneByOrFail({});
|
2023-01-24 17:18:39 -08:00
|
|
|
|
|
|
|
expect(synchronization.id).toBeDefined();
|
|
|
|
expect(synchronization.startedAt).toBeDefined();
|
|
|
|
expect(synchronization.endedAt).toBeDefined();
|
|
|
|
expect(synchronization.created).toBeDefined();
|
|
|
|
expect(synchronization.updated).toBeDefined();
|
|
|
|
expect(synchronization.disabled).toBeDefined();
|
|
|
|
expect(synchronization.status).toBeDefined();
|
|
|
|
expect(synchronization.scanned).toBeDefined();
|
|
|
|
expect(synchronization.error).toBeDefined();
|
|
|
|
expect(synchronization.runMode).toBeDefined();
|
|
|
|
expect(synchronization.runMode).toBe('live');
|
|
|
|
expect(synchronization.scanned).toBe(ldapUsers.length);
|
|
|
|
return synchronization;
|
|
|
|
};
|
|
|
|
|
|
|
|
test('should detect new user and persist change in model', async () => {
|
|
|
|
const ldapUser = {
|
|
|
|
mail: randomEmail(),
|
|
|
|
dn: '',
|
|
|
|
sn: randomName(),
|
|
|
|
givenName: randomName(),
|
|
|
|
uid: uniqueId(),
|
|
|
|
};
|
|
|
|
|
|
|
|
const synchronization = await runTest([ldapUser]);
|
|
|
|
expect(synchronization.created).toBe(1);
|
|
|
|
|
|
|
|
// Make sure the changes in the "LDAP server" were persisted in the database
|
2023-11-08 07:29:39 -08:00
|
|
|
const allUsers = await getAllUsers();
|
2023-01-24 17:18:39 -08:00
|
|
|
expect(allUsers.length).toBe(2);
|
|
|
|
|
|
|
|
const ownerUser = allUsers.find((u) => u.email === owner.email)!;
|
|
|
|
expect(ownerUser.email).toBe(owner.email);
|
|
|
|
|
|
|
|
const memberUser = allUsers.find((u) => u.email !== owner.email)!;
|
|
|
|
expect(memberUser.email).toBe(ldapUser.mail);
|
|
|
|
expect(memberUser.lastName).toBe(ldapUser.sn);
|
|
|
|
expect(memberUser.firstName).toBe(ldapUser.givenName);
|
2024-05-17 01:53:15 -07:00
|
|
|
const memberProject = getPersonalProject(memberUser);
|
|
|
|
expect(memberProject).toBeDefined();
|
2023-01-24 17:18:39 -08:00
|
|
|
|
2023-11-08 07:29:39 -08:00
|
|
|
const authIdentities = await getLdapIdentities();
|
2023-01-24 17:18:39 -08:00
|
|
|
expect(authIdentities.length).toBe(1);
|
|
|
|
expect(authIdentities[0].providerId).toBe(ldapUser.uid);
|
|
|
|
expect(authIdentities[0].providerType).toBe('ldap');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should detect updated user and persist change in model', async () => {
|
|
|
|
const ldapUser = {
|
|
|
|
mail: randomEmail(),
|
|
|
|
dn: '',
|
|
|
|
sn: 'updated',
|
|
|
|
givenName: randomName(),
|
|
|
|
uid: uniqueId(),
|
|
|
|
};
|
|
|
|
|
2023-11-08 07:29:39 -08:00
|
|
|
await createLdapUser(
|
2023-01-24 17:18:39 -08:00
|
|
|
{
|
2024-01-24 04:38:57 -08:00
|
|
|
role: 'global:member',
|
2023-01-24 17:18:39 -08:00
|
|
|
email: ldapUser.mail,
|
|
|
|
firstName: ldapUser.givenName,
|
|
|
|
lastName: randomName(),
|
|
|
|
},
|
|
|
|
ldapUser.uid,
|
|
|
|
);
|
|
|
|
|
|
|
|
const synchronization = await runTest([ldapUser]);
|
|
|
|
expect(synchronization.updated).toBe(1);
|
|
|
|
|
|
|
|
// Make sure the changes in the "LDAP server" were persisted in the database
|
2023-11-08 07:29:39 -08:00
|
|
|
const localLdapIdentities = await getLdapIdentities();
|
2023-01-24 17:18:39 -08:00
|
|
|
const localLdapUsers = localLdapIdentities.map(({ user }) => user);
|
|
|
|
|
|
|
|
expect(localLdapUsers.length).toBe(1);
|
|
|
|
expect(localLdapUsers[0].email).toBe(ldapUser.mail);
|
|
|
|
expect(localLdapUsers[0].lastName).toBe(ldapUser.sn);
|
|
|
|
expect(localLdapUsers[0].firstName).toBe(ldapUser.givenName);
|
|
|
|
expect(localLdapIdentities[0].providerId).toBe(ldapUser.uid);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should detect disabled user and persist change in model', async () => {
|
|
|
|
const ldapUser = {
|
|
|
|
mail: randomEmail(),
|
|
|
|
dn: '',
|
|
|
|
sn: 'updated',
|
|
|
|
givenName: randomName(),
|
|
|
|
uid: uniqueId(),
|
|
|
|
};
|
|
|
|
|
2023-11-08 07:29:39 -08:00
|
|
|
await createLdapUser(
|
2023-01-24 17:18:39 -08:00
|
|
|
{
|
2024-01-24 04:38:57 -08:00
|
|
|
role: 'global:member',
|
2023-01-24 17:18:39 -08:00
|
|
|
email: ldapUser.mail,
|
|
|
|
firstName: ldapUser.givenName,
|
|
|
|
lastName: ldapUser.sn,
|
|
|
|
},
|
|
|
|
ldapUser.uid,
|
|
|
|
);
|
|
|
|
|
|
|
|
const synchronization = await runTest([]);
|
|
|
|
expect(synchronization.disabled).toBe(1);
|
|
|
|
|
|
|
|
// Make sure the changes in the "LDAP server" were persisted in the database
|
2023-11-08 07:29:39 -08:00
|
|
|
const allUsers = await getAllUsers();
|
2023-01-24 17:18:39 -08:00
|
|
|
expect(allUsers.length).toBe(2);
|
|
|
|
|
|
|
|
const ownerUser = allUsers.find((u) => u.email === owner.email)!;
|
|
|
|
expect(ownerUser.email).toBe(owner.email);
|
|
|
|
|
|
|
|
const memberUser = allUsers.find((u) => u.email !== owner.email)!;
|
|
|
|
expect(memberUser.email).toBe(ldapUser.mail);
|
|
|
|
expect(memberUser.lastName).toBe(ldapUser.sn);
|
|
|
|
expect(memberUser.firstName).toBe(ldapUser.givenName);
|
|
|
|
expect(memberUser.disabled).toBe(true);
|
|
|
|
|
2023-11-08 07:29:39 -08:00
|
|
|
const authIdentities = await getLdapIdentities();
|
2023-01-24 17:18:39 -08:00
|
|
|
expect(authIdentities.length).toBe(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should remove user instance access once the user is disabled during synchronization', async () => {
|
2024-01-24 04:38:57 -08:00
|
|
|
const member = await createLdapUser({ role: 'global:member' }, uniqueId());
|
2023-01-24 17:18:39 -08:00
|
|
|
|
2023-04-24 04:17:08 -07:00
|
|
|
jest.spyOn(LdapService.prototype, 'searchWithAdminBinding').mockResolvedValue([]);
|
2023-01-24 17:18:39 -08:00
|
|
|
|
2023-07-13 01:14:48 -07:00
|
|
|
await authOwnerAgent.post('/ldap/sync').send({ type: 'live' });
|
2023-01-24 17:18:39 -08:00
|
|
|
|
2023-07-13 01:14:48 -07:00
|
|
|
const response = await testServer.authAgentFor(member).get('/login');
|
2024-02-28 04:12:28 -08:00
|
|
|
expect(response.status).toBe(401);
|
2023-01-24 17:18:39 -08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('GET /ldap/sync should return paginated synchronizations', async () => {
|
|
|
|
for (let i = 0; i < 2; i++) {
|
|
|
|
await saveLdapSynchronization({
|
|
|
|
created: 0,
|
|
|
|
scanned: 0,
|
|
|
|
updated: 0,
|
|
|
|
disabled: 0,
|
|
|
|
startedAt: new Date(),
|
|
|
|
endedAt: new Date(),
|
|
|
|
status: 'success',
|
|
|
|
error: '',
|
|
|
|
runMode: 'dry',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-13 01:14:48 -07:00
|
|
|
let response = await authOwnerAgent.get('/ldap/sync?perPage=1&page=0');
|
2023-01-24 17:18:39 -08:00
|
|
|
expect(response.body.data.length).toBe(1);
|
|
|
|
|
2023-07-13 01:14:48 -07:00
|
|
|
response = await authOwnerAgent.get('/ldap/sync?perPage=1&page=1');
|
2023-01-24 17:18:39 -08:00
|
|
|
expect(response.body.data.length).toBe(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('POST /login', () => {
|
|
|
|
const runTest = async (ldapUser: LdapUser) => {
|
|
|
|
const ldapConfig = await createLdapConfig();
|
2024-01-15 06:01:48 -08:00
|
|
|
Container.get(LdapService).setConfig(ldapConfig);
|
2023-01-24 17:18:39 -08:00
|
|
|
|
2023-11-01 20:02:49 -07:00
|
|
|
await setCurrentAuthenticationMethod('ldap');
|
|
|
|
|
2023-04-24 04:17:08 -07:00
|
|
|
jest.spyOn(LdapService.prototype, 'searchWithAdminBinding').mockResolvedValue([ldapUser]);
|
2023-01-24 17:18:39 -08:00
|
|
|
|
2023-04-24 04:17:08 -07:00
|
|
|
jest.spyOn(LdapService.prototype, 'validUser').mockResolvedValue();
|
2023-01-24 17:18:39 -08:00
|
|
|
|
2023-07-13 01:14:48 -07:00
|
|
|
const response = await testServer.authlessAgent
|
2023-01-24 17:18:39 -08:00
|
|
|
.post('/login')
|
|
|
|
.send({ email: ldapUser.mail, password: 'password' });
|
|
|
|
|
|
|
|
expect(response.statusCode).toBe(200);
|
|
|
|
expect(response.headers['set-cookie']).toBeDefined();
|
2024-03-13 04:47:36 -07:00
|
|
|
expect(response.headers['set-cookie'][0]).toContain('n8n-auth=');
|
2023-01-24 17:18:39 -08:00
|
|
|
|
|
|
|
// Make sure the changes in the "LDAP server" were persisted in the database
|
2023-11-08 07:29:39 -08:00
|
|
|
const localLdapIdentities = await getLdapIdentities();
|
2023-01-24 17:18:39 -08:00
|
|
|
const localLdapUsers = localLdapIdentities.map(({ user }) => user);
|
|
|
|
|
|
|
|
expect(localLdapUsers.length).toBe(1);
|
|
|
|
expect(localLdapUsers[0].email).toBe(ldapUser.mail);
|
|
|
|
expect(localLdapUsers[0].lastName).toBe(ldapUser.sn);
|
|
|
|
expect(localLdapUsers[0].firstName).toBe(ldapUser.givenName);
|
|
|
|
expect(localLdapIdentities[0].providerId).toBe(ldapUser.uid);
|
|
|
|
expect(localLdapUsers[0].disabled).toBe(false);
|
2024-05-17 01:53:15 -07:00
|
|
|
|
|
|
|
await expect(getPersonalProject(localLdapUsers[0])).resolves.toBeDefined();
|
2023-01-24 17:18:39 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
test('should allow new LDAP user to login and synchronize data', async () => {
|
|
|
|
const ldapUser = {
|
|
|
|
mail: randomEmail(),
|
|
|
|
dn: '',
|
|
|
|
sn: '',
|
|
|
|
givenName: randomName(),
|
|
|
|
uid: uniqueId(),
|
|
|
|
};
|
|
|
|
await runTest(ldapUser);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should allow existing LDAP user to login and synchronize data', async () => {
|
|
|
|
const ldapUser = {
|
|
|
|
mail: randomEmail(),
|
|
|
|
dn: '',
|
|
|
|
sn: 'updated',
|
|
|
|
givenName: 'updated',
|
|
|
|
uid: uniqueId(),
|
|
|
|
};
|
|
|
|
|
2023-11-08 07:29:39 -08:00
|
|
|
await createLdapUser(
|
2023-01-24 17:18:39 -08:00
|
|
|
{
|
2024-01-24 04:38:57 -08:00
|
|
|
role: 'global:member',
|
2023-01-24 17:18:39 -08:00
|
|
|
email: ldapUser.mail,
|
|
|
|
firstName: 'firstname',
|
|
|
|
lastName: 'lastname',
|
|
|
|
},
|
|
|
|
ldapUser.uid,
|
|
|
|
);
|
|
|
|
|
|
|
|
await runTest(ldapUser);
|
|
|
|
});
|
|
|
|
|
2023-11-01 20:02:49 -07:00
|
|
|
test('should allow instance owner to sign in with email/password when LDAP is enabled', async () => {
|
|
|
|
const ldapConfig = await createLdapConfig();
|
2024-01-15 06:01:48 -08:00
|
|
|
Container.get(LdapService).setConfig(ldapConfig);
|
2023-11-01 20:02:49 -07:00
|
|
|
|
|
|
|
const response = await testServer.authlessAgent
|
|
|
|
.post('/login')
|
|
|
|
.send({ email: owner.email, password: 'password' });
|
|
|
|
|
|
|
|
expect(response.status).toBe(200);
|
|
|
|
expect(response.body.data?.signInType).toBeDefined();
|
|
|
|
expect(response.body.data?.signInType).toBe('email');
|
|
|
|
});
|
|
|
|
|
2023-01-24 17:18:39 -08:00
|
|
|
test('should transform email user into LDAP user when match found', async () => {
|
|
|
|
const ldapUser = {
|
|
|
|
mail: randomEmail(),
|
|
|
|
dn: '',
|
|
|
|
sn: randomName(),
|
|
|
|
givenName: randomName(),
|
|
|
|
uid: uniqueId(),
|
|
|
|
};
|
|
|
|
|
2023-11-08 07:29:39 -08:00
|
|
|
await createUser({
|
2024-01-24 04:38:57 -08:00
|
|
|
role: 'global:member',
|
2023-01-24 17:18:39 -08:00
|
|
|
email: ldapUser.mail,
|
|
|
|
firstName: ldapUser.givenName,
|
|
|
|
lastName: 'lastname',
|
|
|
|
});
|
|
|
|
|
|
|
|
await runTest(ldapUser);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Instance owner should able to delete LDAP users', () => {
|
|
|
|
test("don't transfer workflows", async () => {
|
|
|
|
const ldapConfig = await createLdapConfig();
|
2024-01-15 06:01:48 -08:00
|
|
|
Container.get(LdapService).setConfig(ldapConfig);
|
2023-01-24 17:18:39 -08:00
|
|
|
|
2024-01-24 04:38:57 -08:00
|
|
|
const member = await createLdapUser({ role: 'global:member' }, uniqueId());
|
2023-01-24 17:18:39 -08:00
|
|
|
|
2023-07-13 01:14:48 -07:00
|
|
|
await authOwnerAgent.post(`/users/${member.id}`);
|
2023-01-24 17:18:39 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('transfer workflows and credentials', async () => {
|
|
|
|
const ldapConfig = await createLdapConfig();
|
2024-01-15 06:01:48 -08:00
|
|
|
Container.get(LdapService).setConfig(ldapConfig);
|
2023-01-24 17:18:39 -08:00
|
|
|
|
2024-01-24 04:38:57 -08:00
|
|
|
const member = await createLdapUser({ role: 'global:member' }, uniqueId());
|
2023-01-24 17:18:39 -08:00
|
|
|
|
|
|
|
// delete the LDAP member and transfer its workflows/credentials to instance owner
|
2023-07-13 01:14:48 -07:00
|
|
|
await authOwnerAgent.post(`/users/${member.id}?transferId=${owner.id}`);
|
2023-01-24 17:18:39 -08:00
|
|
|
});
|
|
|
|
});
|