1
1
'use strict' ;
2
2
3
3
const kerberos = require ( 'bindings' ) ( 'kerberos' ) ;
4
+ const KerberosClient = kerberos . KerberosClient ;
5
+ const KerberosServer = kerberos . KerberosServer ;
4
6
5
7
// Result Codes
6
8
const AUTH_GSS_CONTINUE = 0 ;
@@ -36,6 +38,93 @@ function validateParameter(parameter, spec) {
36
38
}
37
39
}
38
40
41
+ /// allows api methods to be called with a callback or a promise
42
+ function promisify ( fn , paramDefs ) {
43
+ return function ( ) {
44
+ const args = Array . prototype . slice . call ( arguments ) ;
45
+ const params = [ ] ;
46
+ for ( let i = 0 ; i < paramDefs . length ; ++ i ) {
47
+ const def = paramDefs [ i ] ;
48
+ let arg = args [ i ] ;
49
+
50
+ if ( def . default && arg == null ) arg = def . default ;
51
+ if ( def . type === 'object' && def . default != null ) {
52
+ arg = Object . assign ( { } , def . default , arg ) ;
53
+ }
54
+
55
+ // special case to allow `options` to be optional
56
+ if ( def . name === 'options' && ( typeof arg === 'function' || arg == null ) ) {
57
+ arg = { } ;
58
+ }
59
+
60
+ validateParameter ( arg , paramDefs [ i ] ) ;
61
+ params . push ( arg ) ;
62
+ }
63
+
64
+ const callback = arguments [ arguments . length - 1 ] ;
65
+ if ( typeof callback !== 'function' ) {
66
+ return new Promise ( ( resolve , reject ) => {
67
+ params . push ( ( err , response ) => {
68
+ if ( err ) return reject ( err ) ;
69
+ resolve ( response ) ;
70
+ } ) ;
71
+
72
+ fn . apply ( this , params ) ;
73
+ } ) ;
74
+ }
75
+
76
+ params . push ( callback ) ;
77
+ fn . apply ( this , params ) ;
78
+ } ;
79
+ }
80
+
81
+ /**
82
+ * Processes a single GSSAPI client-side step using the supplied server data.
83
+ *
84
+ * @param {string } challenge A string containing the base64-encoded server data (which may be empty for the first step)
85
+ * @param {function } callback Returns a result code, or an error if one was
86
+ * @returns {string } response
87
+ */
88
+ KerberosClient . prototype . step = promisify ( KerberosClient . prototype . step , [
89
+ { name : 'challenge' , type : 'string' }
90
+ ] ) ;
91
+
92
+ /**
93
+ * Perform the client side GSSAPI wrap step.
94
+ *
95
+ * @memberof KerberosClient
96
+ * @param {string } challenge The result of the `authGSSClientResponse` after the `authGSSClientUnwrap`
97
+ * @param {object } [options] Optional settings
98
+ * @param {string } [options.user] The user to authorize
99
+ * @param {function } callback
100
+ */
101
+ KerberosClient . prototype . wrap = promisify ( KerberosClient . prototype . wrap , [
102
+ { name : 'challenge' , type : 'string' } ,
103
+ { name : 'options' , type : 'object' }
104
+ ] ) ;
105
+
106
+ /**
107
+ * Perform the client side GSSAPI unwrap step
108
+ *
109
+ * @memberof KerberosClient
110
+ * @param {string } challenge A string containing the base64-encoded server data
111
+ * @param {function } [callback]
112
+ */
113
+ KerberosClient . prototype . unwrap = promisify ( KerberosClient . prototype . unwrap , [
114
+ { name : 'challenge' , type : 'string' }
115
+ ] ) ;
116
+
117
+ /**
118
+ * Processes a single GSSAPI server-side step using the supplied client data.
119
+ *
120
+ * @memberof KerberosServer
121
+ * @param {string } challenge A string containing the base64-encoded client data
122
+ * @param {function } callback
123
+ */
124
+ KerberosServer . prototype . step = promisify ( KerberosServer . prototype . step , [
125
+ { name : 'challenge' , type : 'string' }
126
+ ] ) ;
127
+
39
128
/**
40
129
* This function provides a simple way to verify that a user name and password
41
130
* match those normally used for Kerberos authentication.
@@ -59,16 +148,12 @@ function validateParameter(parameter, spec) {
59
148
* @param {string } [defaultRealm] The default realm to use if one is not supplied in the user argument.
60
149
* @param {function } callback
61
150
*/
62
- function checkPassword ( username , password , service , defaultRealm , callback ) {
63
- if ( typeof defaultRealm === 'function' ) ( callback = defaultRealm ) , ( defaultRealm = null ) ;
64
- validateParameter ( username , { name : 'username' , type : 'string' } ) ;
65
- validateParameter ( password , { name : 'password' , type : 'string' } ) ;
66
- validateParameter ( service , { name : 'service' , type : 'string' } ) ;
67
- validateParameter ( defaultRealm , { name : 'defaultRealm' , type : 'string' } ) ;
68
- validateParameter ( callback , { name : 'callback' , type : 'function' } ) ;
69
-
70
- kerberos . checkPassword ( username , password , service , defaultRealm , callback ) ;
71
- }
151
+ const checkPassword = promisify ( kerberos . checkPassword , [
152
+ { name : 'username' , type : 'string' } ,
153
+ { name : 'password' , type : 'string' } ,
154
+ { name : 'service' , type : 'string' } ,
155
+ { name : 'defaultRealm' , type : 'string' }
156
+ ] ) ;
72
157
73
158
/**
74
159
* This function returns the service principal for the server given a service
@@ -80,13 +165,10 @@ function checkPassword(username, password, service, defaultRealm, callback) {
80
165
* @param {string } hostname The hostname of the server.
81
166
* @param {function } callback
82
167
*/
83
- function principalDetails ( service , hostname , callback ) {
84
- validateParameter ( service , { name : 'service' , type : 'string' } ) ;
85
- validateParameter ( hostname , { name : 'hostname' , type : 'string' } ) ;
86
- validateParameter ( callback , { name : 'callback' , type : 'function' } ) ;
87
-
88
- kerberos . principalDetails ( service , hostname , callback ) ;
89
- }
168
+ const principalDetails = promisify ( kerberos . principalDetails , [
169
+ { name : 'service' , type : 'string' } ,
170
+ { name : 'hostname' , type : 'string' }
171
+ ] ) ;
90
172
91
173
/**
92
174
* The callback format for inserts
@@ -108,16 +190,10 @@ function principalDetails(service, hostname, callback) {
108
190
* @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.
109
191
* @param {initializeClientCallback } callback The operation callback
110
192
*/
111
- function initializeClient ( service , options , callback ) {
112
- if ( typeof options === 'function' ) ( callback = options ) , ( options = { } ) ;
113
- options = Object . assign ( { } , { mechOID : GSS_C_NO_OID } , options ) ;
114
-
115
- validateParameter ( service , { name : 'service' , type : 'string' } ) ;
116
- validateParameter ( options , { name : 'options' , type : 'object' } ) ;
117
- validateParameter ( callback , { name : 'callback' , type : 'function' } ) ;
118
-
119
- kerberos . initializeClient ( service , options , callback ) ;
120
- }
193
+ const initializeClient = promisify ( kerberos . initializeClient , [
194
+ { name : 'service' , type : 'string' } ,
195
+ { name : 'options' , type : 'object' , default : { mechOID : GSS_C_NO_OID } }
196
+ ] ) ;
121
197
122
198
/**
123
199
* Initializes a context for GSSAPI server-side authentication with the given
@@ -128,46 +204,9 @@ function initializeClient(service, options, callback) {
128
204
* @param {string } service A string containing the service principal in the form 'type@fqdn' (e.g. '[email protected] ').
129
205
* @param {initializeServerCallback } callback
130
206
*/
131
- function initializeServer ( service , callback ) {
132
- validateParameter ( service , { name : 'service' , type : 'string' } ) ;
133
- validateParameter ( callback , { name : 'callback' , type : 'function' } ) ;
134
-
135
- kerberos . initializeServer ( service , callback ) ;
136
- }
137
-
138
- /**
139
- * Processes a single GSSAPI client-side step using the supplied server data.
140
- *
141
- * @memberof KerberosClient
142
- * @param {string } challenge A string containing the base64-encoded server data (which may be empty for the first step)
143
- * @param {function } callback Returns a result code, or an error if one was encountered
144
- */
145
-
146
- /**
147
- * Perform the client side GSSAPI unwrap step
148
- *
149
- * @memberof KerberosClient
150
- * @param {string } challenge A string containing the base64-encoded server data
151
- * @param {function } callback
152
- */
153
-
154
- /**
155
- * Perform the client side GSSAPI wrap step.
156
- *
157
- * @memberof KerberosClient
158
- * @param {string } challenge The result of the `authGSSClientResponse` after the `authGSSClientUnwrap`
159
- * @param {object } [options] Optional settings
160
- * @param {string } [options.user] The user to authorize
161
- * @param {function } callback
162
- */
163
-
164
- /**
165
- * Processes a single GSSAPI server-side step using the supplied client data.
166
- *
167
- * @memberof KerberosServer
168
- * @param {string } challenge A string containing the base64-encoded client data
169
- * @param {function } callback
170
- */
207
+ const initializeServer = promisify ( kerberos . initializeServer , [
208
+ { name : 'service' , type : 'string' }
209
+ ] ) ;
171
210
172
211
module . exports = {
173
212
initializeClient,
0 commit comments