/
x509.ts
44 lines (40 loc) 路 1.28 KB
/
x509.ts
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
import { Credential } from "../types.ts";
import { AuthContext, AuthPlugin } from "./base.ts";
import { HandshakeDocument } from "../protocol/handshake.ts";
import { driverMetadata } from "../protocol/mod.ts";
import { Document } from "../../deps.ts";
export interface X509Command extends Document {
authenticate: number;
mechanism: string;
user?: string;
}
export class X509AuthPlugin extends AuthPlugin {
constructor() {
super();
}
prepare(authContext: AuthContext): Document {
const handshakeDoc = <HandshakeDocument> {
ismaster: true,
client: driverMetadata,
compression: authContext.options.compression,
speculativeAuthenticate: x509AuthenticateCommand(authContext.credentials),
};
return handshakeDoc;
}
auth(authContext: AuthContext): Promise<Document> {
if (authContext.response!.speculativeAuthenticate) {
return Promise.resolve(authContext.response!);
}
return authContext.protocol.commandSingle(
"$external",
x509AuthenticateCommand(authContext.credentials),
);
}
}
function x509AuthenticateCommand(credentials?: Credential): Document {
const command: X509Command = { authenticate: 1, mechanism: "MONGODB-X509" };
if (credentials) {
command.user = credentials!.username;
}
return command;
}