forked from RocketChat/Rocket.Chat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathldap.ts
More file actions
142 lines (128 loc) · 3.25 KB
/
ldap.ts
File metadata and controls
142 lines (128 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { LDAP } from '@rocket.chat/core-services';
import {
ajv,
validateUnauthorizedErrorResponse,
validateBadRequestErrorResponse,
validateForbiddenErrorResponse,
} from '@rocket.chat/rest-typings';
import { Match, check } from 'meteor/check';
import { SystemLogger } from '../../../../server/lib/logger/system';
import { settings } from '../../../settings/server';
import type { ExtractRoutesFromAPI } from '../ApiClass';
import { API } from '../api';
type ldapTestSearchProps = {
username: string;
};
const ldapTestSearchPropsSchema = {
type: 'object',
properties: {
username: {
type: 'string',
},
},
required: ['username'],
additionalProperties: false,
};
const isLdapTestSearch = ajv.compile<ldapTestSearchProps>(ldapTestSearchPropsSchema);
const ldapEndpoints = API.v1
.get(
'ldap.testConnection',
{
authRequired: true,
permissionsRequired: ['test-admin-options'],
response: {
200: ajv.compile<{
message: string;
}>({
type: 'object',
properties: {
message: {
type: 'string',
enum: ['LDAP_Connection_successful'],
},
success: {
type: 'boolean',
enum: [true],
description: 'Whether the connection was successful or not',
},
},
required: ['success', 'message'],
additionalProperties: false,
}),
400: validateBadRequestErrorResponse,
401: validateUnauthorizedErrorResponse,
403: validateForbiddenErrorResponse,
},
},
async function action() {
if (!this.userId) {
throw new Meteor.Error('error-invalid-user');
}
if (settings.get<boolean>('LDAP_Enable') !== true) {
throw new Meteor.Error('LDAP_disabled');
}
try {
await LDAP.testConnection();
} catch (error) {
SystemLogger.error(error);
throw new Meteor.Error('Connection_failed');
}
return API.v1.success({
message: 'LDAP_Connection_successful' as const,
});
},
)
.post(
'ldap.testSearch',
{
authRequired: true,
permissionsRequired: ['test-admin-options'],
body: isLdapTestSearch,
response: {
200: ajv.compile<{
message: string;
}>({
type: 'object',
properties: {
message: {
type: 'string',
enum: ['LDAP_User_Found'],
},
success: {
type: 'boolean',
enum: [true],
description: 'Whether the connection was successful or not',
},
},
required: ['success', 'message'],
additionalProperties: false,
}),
400: validateBadRequestErrorResponse,
401: validateUnauthorizedErrorResponse,
403: validateForbiddenErrorResponse,
},
},
async function action() {
check(
this.bodyParams,
Match.ObjectIncluding({
username: String,
}),
);
if (!this.userId) {
throw new Meteor.Error('error-invalid-user');
}
if (settings.get('LDAP_Enable') !== true) {
throw new Meteor.Error('LDAP_disabled');
}
await LDAP.testSearch(this.bodyParams.username);
return API.v1.success({
message: 'LDAP_User_Found' as const,
});
},
);
export type LdapEndpoints = ExtractRoutesFromAPI<typeof ldapEndpoints>;
declare module '@rocket.chat/rest-typings' {
// eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-empty-interface
interface Endpoints extends LdapEndpoints {}
}