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

Regression: Fix LDAP sync route #23775

Merged
merged 4 commits into from
Nov 23, 2021
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
1 change: 1 addition & 0 deletions .mocharc.api.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = {
file: 'tests/end-to-end/teardown.js',
spec: [
'tests/end-to-end/api/*.js',
'tests/end-to-end/api/*.ts',
'tests/end-to-end/apps/*.js',
],
};
12 changes: 7 additions & 5 deletions app/api/server/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,21 @@ type UnauthorizedResult<T> = {

export type NonEnterpriseTwoFactorOptions = {
authRequired: true;
twoFactorRequiredNonEnterprise: true;
forceTwoFactorAuthenticationForNonEnterprise: true;
twoFactorRequired: true;
permissionsRequired?: string[];
twoFactorOptions: ITwoFactorOptions;
}

type Options = {
permissionsRequired?: string[];
twoFactorOptions?: ITwoFactorOptions;
twoFactorRequired?: boolean;
authRequired?: boolean;
twoFactorRequiredNonEnterprise?: true;
};
forceTwoFactorAuthenticationForNonEnterprise?: boolean;
} | {
authRequired: true;
twoFactorRequired: true;
twoFactorOptions?: ITwoFactorOptions;
}

type Request = {
method: 'GET' | 'POST' | 'PUT' | 'DELETE';
Expand Down
7 changes: 4 additions & 3 deletions app/api/server/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,9 @@ export class APIClass extends Restivus {
}

processTwoFactor({ userId, request, invocation, options, connection }) {
if (!options.twoFactorRequired) {
return;
}
const code = request.headers['x-2fa-code'];
const method = request.headers['x-2fa-method'];

Expand Down Expand Up @@ -399,9 +402,7 @@ export class APIClass extends Restivus {
};
Accounts._setAccountData(connection.id, 'loginToken', this.token);

if (_options.twoFactorRequired) {
api.processTwoFactor({ userId: this.userId, request: this.request, invocation, options: _options, connection });
}
api.processTwoFactor({ userId: this.userId, request: this.request, invocation, options: _options, connection });

result = DDP._CurrentInvocation.withValue(invocation, () => Promise.await(originalAction.apply(this))) || API.v1.success();

Expand Down
4 changes: 2 additions & 2 deletions ee/server/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { isEnterprise } from '../../app/license/server/license';

export const isNonEnterpriseTwoFactorOptions = (options?: Options):
options is NonEnterpriseTwoFactorOptions => !!options
&& 'twoFactorRequiredNonEnterprise' in options
&& Boolean(options.twoFactorRequiredNonEnterprise);
&& 'forceTwoFactorAuthenticationForNonEnterprise' in options
&& Boolean(options.forceTwoFactorAuthenticationForNonEnterprise);

API.v1.processTwoFactor = use(API.v1.processTwoFactor, function([params, ...context], next) {
if (isNonEnterpriseTwoFactorOptions(params.options) && !isEnterprise()) {
Expand Down
6 changes: 5 additions & 1 deletion ee/server/api/ldap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { settings } from '../../../app/settings/server';
import { API } from '../../../app/api/server/api';
import { LDAPEE } from '../sdk';

API.v1.addRoute('ldap.syncNow', { authRequired: true, twoFactorRequiredNonEnterprise: true }, {
API.v1.addRoute('ldap.syncNow', {
authRequired: true,
forceTwoFactorAuthenticationForNonEnterprise: true,
twoFactorRequired: true,
}, {
async post() {
if (!this.userId) {
throw new Error('error-invalid-user');
Expand Down
24 changes: 24 additions & 0 deletions tests/end-to-end/api/26-LDAP.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { expect } from 'chai';
import type { Response } from 'supertest';

import { getCredentials, api, request, credentials } from '../../data/api-data.js';

describe('LDAP', function() {
this.retries(0);

before((done) => getCredentials(done));

describe('[/ldap.syncNow]', () => {
it('should throw an error containing totp-required error ', (done) => {
request.post(api('ldap.syncNow'))
.set(credentials)
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res: Response) => {
expect(res.body).to.have.property('success', false);
expect(res.body).to.have.property('errorType', 'totp-required');
})
.end(done);
});
});
});