Skip to content

Commit 3939705

Browse files
authored
improve ldap types (#199)
1 parent ea5a034 commit 3939705

File tree

5 files changed

+79
-36
lines changed

5 files changed

+79
-36
lines changed

.changes/ldap-types.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
"@simulacrum/ldap-simulator": minor
3+
---
4+
improve ldap types

package-lock.json

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/ldap/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"@simulacrum/server": "^0.4.0"
3232
},
3333
"devDependencies": {
34-
"@types/ldapjs": "^2.2.1",
34+
"@types/ldapjs": "^2.2.2",
3535
"@types/seedrandom": "^3.0.1",
3636
"typescript": "^4.4.3"
3737
}

packages/ldap/src/index.ts

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import type { Server } from 'ldapjs';
1+
import type { SearchRequest, Server, SearchResponse, CompareRequest, CompareResponse, BindResponse, BindRequest } from 'ldapjs';
22
import type { Operation } from 'effection';
33
import { createServer, InvalidCredentialsError, NoSuchObjectError, OperationsError } from 'ldapjs';
4-
import type { LDAPOptions } from './types';
4+
import type { LDAPOptions, LDAPStoreOptions, Port, UserData } from './types';
55
import type { SimulationState, Simulator } from '@simulacrum/server';
66
import type { ResourceServiceCreator } from '@simulacrum/server';
77
import dedent from 'dedent';
@@ -14,18 +14,8 @@ const DefaultOptions: Partial<LDAPOptions> = {
1414
port: 389
1515
};
1616

17-
interface UserData {
18-
uuid: string;
19-
cn: string;
20-
password: string;
21-
}
22-
23-
export interface LDAPStoreOptions<T extends UserData> {
24-
users: Iterable<T>;
25-
}
26-
27-
export interface Port {
28-
port: number;
17+
export interface NextFunction {
18+
<E>(err?: E): void;
2919
}
3020

3121
export function createLDAPServer<T extends UserData>(options: LDAPOptions & LDAPStoreOptions<T>): Operation<Server & Port> {
@@ -54,23 +44,21 @@ export function createLDAPServer<T extends UserData>(options: LDAPOptions & LDAP
5444

5545
let server = createServer({ log: silence });
5646

57-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
58-
server.search("", function (req: any, res: any, next: any) {
47+
server.search("", function (req: SearchRequest, res: SearchResponse, next: NextFunction) {
5948
logger.log(dedent`--- Root DSE ---
60-
scope: ${req.scope}
61-
`);
49+
scope: ${req.scope}
50+
`);
6251
res.send({
63-
"dn":"",
64-
"attributes":{
52+
dn: "",
53+
attributes: {
6554
"vendorName": "Frontside, Inc."
6655
},
6756
});
6857
res.end();
6958
return next();
7059
});
7160

72-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
73-
server.search(baseDN, function (req: any, res: any, next: any) {
61+
server.search(baseDN, function (req: SearchRequest, res: SearchResponse, next: NextFunction) {
7462
logger.log(dedent`--- User Search ---
7563
dn: ${req.dn.toString()}
7664
scope: ${req.scope}
@@ -106,18 +94,16 @@ filter: ${req.filter.toString()}
10694
return next();
10795
});
10896

109-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
110-
server.compare(groupDN, (req: any, res: any) => {
97+
server.compare(groupDN, (req: CompareRequest, res: CompareResponse) => {
11198
logger.log('--- Compare ---');
11299
logger.log(`DN: ${req.dn.toString()}`);
113100
logger.log(`attribute name: ${req.attribute}`);
114101
logger.log(`attribute value: ${req.value}`);
115102

116-
res.end(true);
103+
res.end(0);
117104
});
118105

119-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
120-
server.bind(baseDN, function (req: any, res: any, next: any) {
106+
server.bind(baseDN, function (req: BindRequest, res: BindResponse, next: NextFunction) {
121107
logger.log('--- Bind ---');
122108
logger.log(`bind DN: ${req.dn.toString()}`);
123109
logger.log(`bind PW: ${req.credentials}`);

packages/ldap/src/types.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,42 @@
1+
declare module 'ldapjs' {
2+
interface RDNS {
3+
attrs: {
4+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
5+
[key: string]: any;
6+
}
7+
}
8+
interface DN {
9+
rdns: RDNS[];
10+
}
11+
export interface SearchRequest {
12+
dn: DN;
13+
}
14+
15+
export interface LDAPResult {
16+
// 0 is success
17+
end(status?: number): void;
18+
}
19+
20+
export interface SearchResponse extends LDAPResult {
21+
send(entry: SearchEntry | SearchReference, noFiltering?: boolean): void;
22+
}
23+
24+
export interface CompareRequest {
25+
entry: SearchEntry;
26+
attribute: Attribute;
27+
value: string | Buffer;
28+
dn: DN;
29+
}
30+
31+
export type CompareResponse = LDAPResult;
32+
export type BindResponse = LDAPResult;
33+
34+
export interface BindRequest {
35+
credentials: string;
36+
dn: DN
37+
}
38+
}
39+
140
export interface LDAPOptions {
241
log?: boolean;
342
port?: number;
@@ -6,3 +45,17 @@ export interface LDAPOptions {
645
groupDN: string
746
bindPassword: string;
847
}
48+
49+
export interface UserData {
50+
uuid: string;
51+
cn: string;
52+
password: string;
53+
}
54+
55+
export interface LDAPStoreOptions<T extends UserData> {
56+
users: Iterable<T>;
57+
}
58+
59+
export interface Port {
60+
port: number;
61+
}

0 commit comments

Comments
 (0)