Skip to content

Commit 8764240

Browse files
feat(NODE-7164): add TS definition file to kerberos package (#274)
1 parent 3a5a37f commit 8764240

File tree

6 files changed

+625
-9
lines changed

6 files changed

+625
-9
lines changed

.eslintrc.json

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,17 @@
3434
"prettier/prettier": "error",
3535
"no-console": "error",
3636
"valid-typeof": "error",
37-
"eqeqeq": ["error", "always", {"null": "ignore"}],
38-
"strict": ["error", "global"],
37+
"eqeqeq": [
38+
"error",
39+
"always",
40+
{
41+
"null": "ignore"
42+
}
43+
],
44+
"strict": [
45+
"error",
46+
"global"
47+
],
3948
"no-restricted-syntax": [
4049
"error",
4150
{
@@ -66,6 +75,19 @@
6675
"no-console": "off",
6776
"no-restricted-syntax": "off"
6877
}
78+
},
79+
{
80+
"files": [
81+
"**/*.ts"
82+
],
83+
"extends": [
84+
"plugin:@typescript-eslint/eslint-recommended",
85+
"plugin:@typescript-eslint/recommended"
86+
],
87+
"plugins": [
88+
"prettier",
89+
"@typescript-eslint"
90+
]
6991
}
7092
]
71-
}
93+
}

README.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,35 @@ NOTE: The test suite requires an active kerberos deployment.
123123
<dd></dd>
124124
</dl>
125125

126+
## Constants
127+
128+
<dl>
129+
<dt><a href="#GSS_C_DELEG_FLAG">GSS_C_DELEG_FLAG</a></dt>
130+
<dd></dd>
131+
<dt><a href="#GSS_C_MUTUAL_FLAG">GSS_C_MUTUAL_FLAG</a></dt>
132+
<dd></dd>
133+
<dt><a href="#GSS_C_REPLAY_FLAG">GSS_C_REPLAY_FLAG</a></dt>
134+
<dd></dd>
135+
<dt><a href="#GSS_C_SEQUENCE_FLAG">GSS_C_SEQUENCE_FLAG</a></dt>
136+
<dd></dd>
137+
<dt><a href="#GSS_C_CONF_FLAG">GSS_C_CONF_FLAG</a></dt>
138+
<dd></dd>
139+
<dt><a href="#GSS_C_INTEG_FLAG">GSS_C_INTEG_FLAG</a></dt>
140+
<dd></dd>
141+
<dt><a href="#GSS_C_ANON_FLAG">GSS_C_ANON_FLAG</a></dt>
142+
<dd></dd>
143+
<dt><a href="#GSS_C_PROT_READY_FLAG">GSS_C_PROT_READY_FLAG</a></dt>
144+
<dd></dd>
145+
<dt><a href="#GSS_C_TRANS_FLAG">GSS_C_TRANS_FLAG</a></dt>
146+
<dd></dd>
147+
<dt><a href="#GSS_C_NO_OID">GSS_C_NO_OID</a></dt>
148+
<dd></dd>
149+
<dt><a href="#GSS_MECH_OID_KRB5">GSS_MECH_OID_KRB5</a></dt>
150+
<dd></dd>
151+
<dt><a href="#GSS_MECH_OID_SPNEGO">GSS_MECH_OID_SPNEGO</a></dt>
152+
<dd></dd>
153+
</dl>
154+
126155
## Functions
127156

128157
<dl>
@@ -230,6 +259,42 @@ Perform the client side kerberos unwrap step
230259

231260
Processes a single kerberos server-side step using the supplied client data.
232261

262+
<a name="GSS_C_DELEG_FLAG"></a>
263+
264+
## GSS_C_DELEG_FLAG
265+
<a name="GSS_C_MUTUAL_FLAG"></a>
266+
267+
## GSS_C_MUTUAL_FLAG
268+
<a name="GSS_C_REPLAY_FLAG"></a>
269+
270+
## GSS_C_REPLAY_FLAG
271+
<a name="GSS_C_SEQUENCE_FLAG"></a>
272+
273+
## GSS_C_SEQUENCE_FLAG
274+
<a name="GSS_C_CONF_FLAG"></a>
275+
276+
## GSS_C_CONF_FLAG
277+
<a name="GSS_C_INTEG_FLAG"></a>
278+
279+
## GSS_C_INTEG_FLAG
280+
<a name="GSS_C_ANON_FLAG"></a>
281+
282+
## GSS_C_ANON_FLAG
283+
<a name="GSS_C_PROT_READY_FLAG"></a>
284+
285+
## GSS_C_PROT_READY_FLAG
286+
<a name="GSS_C_TRANS_FLAG"></a>
287+
288+
## GSS_C_TRANS_FLAG
289+
<a name="GSS_C_NO_OID"></a>
290+
291+
## GSS_C_NO_OID
292+
<a name="GSS_MECH_OID_KRB5"></a>
293+
294+
## GSS_MECH_OID_KRB5
295+
<a name="GSS_MECH_OID_SPNEGO"></a>
296+
297+
## GSS_MECH_OID_SPNEGO
233298
<a name="checkPassword"></a>
234299

235300
## checkPassword(username, password, service, [defaultRealm])
@@ -283,6 +348,8 @@ Details are looked up via the `/etc/keytab` file.
283348
| [options.principal] | <code>string</code> | Optional string containing the client principal in the form 'user@realm' (e.g. 'jdoe@example.com'). |
284349
| [options.flags] | <code>number</code> | Optional integer used to set GSS flags. (e.g. `GSS_C_DELEG_FLAG\|GSS_C_MUTUAL_FLAG\|GSS_C_SEQUENCE_FLAG` will allow for forwarding credentials to the remote host) |
285350
| [options.mechOID] | <code>number</code> | Optional GSS mech OID. Defaults to None (GSS_C_NO_OID). Other possible values are `GSS_MECH_OID_KRB5`, `GSS_MECH_OID_SPNEGO`. |
351+
| [options.user] | <code>string</code> | The username with which to authenticate. Only used on Windows. |
352+
| [options.pass] | <code>string</code> | The password with which to authenticate. Only used on Windows. |
286353

287354
Initializes a context for client-side authentication with the given service principal.
288355

index.d.ts

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
export const GSS_C_DELEG_FLAG: number;
2+
export const GSS_C_MUTUAL_FLAG: number;
3+
export const GSS_C_REPLAY_FLAG: number;
4+
export const GSS_C_SEQUENCE_FLAG: number;
5+
export const GSS_C_CONF_FLAG: number;
6+
export const GSS_C_INTEG_FLAG: number;
7+
export const GSS_C_ANON_FLAG: number;
8+
export const GSS_C_PROT_READY_FLAG: number;
9+
export const GSS_C_TRANS_FLAG: number;
10+
11+
export const GSS_C_NO_OID: number;
12+
export const GSS_MECH_OID_KRB5: number;
13+
export const GSS_MECH_OID_SPNEGO: number;
14+
15+
/**
16+
* Optional settings for *KerberosClient.wrap* method.
17+
*/
18+
export interface WrapOptions {
19+
/**
20+
* The user to authorize.
21+
*/
22+
user?: string;
23+
24+
/**
25+
* Indicates if the wrap should request message confidentiality.
26+
*/
27+
protect?: boolean;
28+
}
29+
30+
/**
31+
* Options for `initializeClient()`.
32+
*/
33+
export interface InitializeClientOptions {
34+
/**
35+
* Optional string containing the client principal in the form 'user@realm' (e.g. 'jdoe@example.com').
36+
*/
37+
principal?: string;
38+
/**
39+
* Optional integer used to set GSS flags. (e.g. `GSS_C_DELEG_FLAG|GSS_C_MUTUAL_FLAG|GSS_C_SEQUENCE_FLAG` will allow for forwarding credentials to the remote host).
40+
*/
41+
gssFlag?: number;
42+
/**
43+
* Optional GSS mech OID. Defaults to None (GSS_C_NO_OID). Other possible values are `GSS_MECH_OID_KRB5`, `GSS_MECH_OID_SPNEGO`.
44+
*/
45+
mechOID?: number;
46+
47+
/**
48+
* The username with which to authenticate. Only used on Windows.
49+
*/
50+
user?: string;
51+
52+
/**
53+
* The password with which to authenticate. Only used on Windows.
54+
*/
55+
pass?: string;
56+
}
57+
58+
export class KerberosClient {
59+
/**
60+
* The username used for authentication.
61+
*/
62+
username: string;
63+
/**
64+
* The last response received during authentication steps.
65+
*/
66+
response: string;
67+
/**
68+
* Indicates whether confidentiality was applied or not (GSSAPI only).
69+
*/
70+
responseConf: string;
71+
/**
72+
* Indicates that authentication has successfully completed or not.
73+
*/
74+
contextComplete: boolean;
75+
76+
/**
77+
* Processes a single kerberos client-side step using the supplied server challenge.
78+
*
79+
* @param challenge A string containing the base64-encoded server data (which may be empty for the first step)
80+
*/
81+
step(challenge: string): Promise<string>;
82+
83+
/**
84+
* Perform the client side kerberos wrap step.
85+
*
86+
* @param challenge The response returned after calling `unwrap`
87+
* @param options Optional settings
88+
*/
89+
wrap(challenge: string, options?: WrapOptions): Promise<string>;
90+
91+
/**
92+
* Perform the client side kerberos unwrap step
93+
*
94+
* @param challenge A string containing the base64-encoded server data
95+
*/
96+
unwrap(challenge: string): Promise<string>;
97+
}
98+
99+
export class KerberosServer {
100+
/**
101+
* The username used for authentication
102+
*/
103+
username: string;
104+
/**
105+
* @description The last response received during authentication steps
106+
*/
107+
response: string;
108+
/**
109+
* @description The target used for authentication
110+
*/
111+
targetName: string;
112+
/**
113+
* @description Indicates that authentication has successfully completed or not
114+
*/
115+
contextComplete: boolean;
116+
117+
/**
118+
* Processes a single kerberos server-side step using the supplied client data.
119+
*
120+
* @param challenge A string containing the base64-encoded client data
121+
*/
122+
step(challenge: string): Promise<string>;
123+
}
124+
125+
/**
126+
* This function provides a simple way to verify that a user name and password
127+
* match those normally used for Kerberos authentication.
128+
* It does this by checking that the supplied user name and password can be
129+
* used to get a ticket for the supplied service.
130+
* If the user name does not contain a realm, then the default realm supplied
131+
* is used.
132+
*
133+
* For this to work properly the Kerberos must be configured properly on this
134+
* machine.
135+
* That will likely mean ensuring that the edu.mit.Kerberos preference file
136+
* has the correct realms and KDCs listed.
137+
*
138+
* IMPORTANT: This method is vulnerable to KDC spoofing attacks and it should
139+
* only be used for testing. Do not use this in any production system - your
140+
* security could be compromised if you do.
141+
*
142+
* @param username The Kerberos user name. If no realm is supplied, then the `defaultRealm` will be used.
143+
* @param password The password for the user.
144+
* @param service The Kerberos service to check access for.
145+
* @param defaultRealm The default realm to use if one is not supplied in the user argument.
146+
*/
147+
export function checkPassword(
148+
name: string,
149+
password: string,
150+
service: string,
151+
defaultRealm?: string
152+
): Promise<void>;
153+
154+
/**
155+
* This function returns the service principal for the server given a service type and hostname.
156+
*
157+
* Details are looked up via the `/etc/keytab` file.
158+
*
159+
* @param service The Kerberos service type for the server.
160+
* @param hostname The hostname of the server.
161+
*/
162+
export function principalDetails(service: string, hostname: string): Promise<string>;
163+
164+
/**
165+
* Initializes a context for client-side authentication with the given service principal.
166+
*
167+
* @param service A string containing the service principal in the form '`type@fqdn`'.
168+
* @param [options] Optional settings
169+
*/
170+
export function initializeClient(
171+
service: string,
172+
options?: InitializeClientOptions
173+
): Promise<KerberosClient>;
174+
175+
/**
176+
* Initializes a context for server-side authentication with the given service principal.
177+
*
178+
* @param service A string containing the service principal in the form 'type@fqdn' (e.g. 'imap@mail.apple.com').
179+
*/
180+
export function initializeServer(service: string): Promise<KerberosServer>;
181+
182+
/**
183+
* The version of the Kerberos package.
184+
*/
185+
export const version: string;

lib/kerberos.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,31 @@ const KerberosClient = kerberos.KerberosClient;
88
const KerberosServer = kerberos.KerberosServer;
99

1010
// GSS Flags
11+
/** @kind constant */
1112
const GSS_C_DELEG_FLAG = 1;
13+
/** @kind constant */
1214
const GSS_C_MUTUAL_FLAG = 2;
15+
/** @kind constant */
1316
const GSS_C_REPLAY_FLAG = 4;
17+
/** @kind constant */
1418
const GSS_C_SEQUENCE_FLAG = 8;
19+
/** @kind constant */
1520
const GSS_C_CONF_FLAG = 16;
21+
/** @kind constant */
1622
const GSS_C_INTEG_FLAG = 32;
23+
/** @kind constant */
1724
const GSS_C_ANON_FLAG = 64;
25+
/** @kind constant */
1826
const GSS_C_PROT_READY_FLAG = 128;
27+
/** @kind constant */
1928
const GSS_C_TRANS_FLAG = 256;
2029

2130
// GSS_OID
31+
/** @kind constant */
2232
const GSS_C_NO_OID = 0;
33+
/** @kind constant */
2334
const GSS_MECH_OID_KRB5 = 9;
35+
/** @kind constant */
2436
const GSS_MECH_OID_SPNEGO = 6;
2537

2638
/**
@@ -182,6 +194,8 @@ const promisifiedInitializeClient = promisify(kerberos.initializeClient);
182194
* @param {string} [options.principal] Optional string containing the client principal in the form 'user@realm' (e.g. 'jdoe@example.com').
183195
* @param {number} [options.flags] Optional integer used to set GSS flags. (e.g. `GSS_C_DELEG_FLAG\|GSS_C_MUTUAL_FLAG\|GSS_C_SEQUENCE_FLAG` will allow for forwarding credentials to the remote host)
184196
* @param {number} [options.mechOID] Optional GSS mech OID. Defaults to None (GSS_C_NO_OID). Other possible values are `GSS_MECH_OID_KRB5`, `GSS_MECH_OID_SPNEGO`.
197+
* @param {string} [options.user] The username with which to authenticate. Only used on Windows.
198+
* @param {string} [options.pass] The password with which to authenticate. Only used on Windows.
185199
* @return {Promise<KerberosClient>} returns Promise
186200
*/
187201
async function initializeClient(service, options = { mechOID: GSS_C_NO_OID }) {

0 commit comments

Comments
 (0)