kerberos.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. var kerberos = require('../build/Release/kerberos')
  2. , KerberosNative = kerberos.Kerberos;
  3. var Kerberos = function() {
  4. this._native_kerberos = new KerberosNative();
  5. }
  6. // callback takes two arguments, an error string if defined and a new context
  7. // uri should be given as service@host. Services are not always defined
  8. // in a straightforward way. Use 'HTTP' for SPNEGO / Negotiate authentication.
  9. // If credentialsCache is not specified, the default credentials cache from the
  10. // environment will be used (ie. KRB5CCNAME). In the case where multiple
  11. // credentials caches may be in use at once (such as for a server doing
  12. // delegation), specify the cache name here and it will be used for this
  13. // exchange. The credentialsCache is optional.
  14. Kerberos.prototype.authGSSClientInit = function(uri, flags, credentialsCache, callback) {
  15. if (typeof(credentialsCache) == 'function') {
  16. callback = credentialsCache;
  17. credentialsCache = '';
  18. }
  19. if (credentialsCache === undefined) {
  20. credentialsCache = '';
  21. }
  22. return this._native_kerberos.authGSSClientInit(uri, flags, credentialsCache, callback);
  23. }
  24. // This will obtain credentials using a credentials cache. To override the default
  25. // location (posible /tmp/krb5cc_nnnnnn, where nnnn is your numeric uid) use
  26. // the environment variable KRB5CNAME.
  27. // The credentials (suitable for using in an 'Authenticate: ' header, when prefixed
  28. // with 'Negotiate ') will be available as context.response inside the callback
  29. // if no error is indicated.
  30. // callback takes one argument, an error string if defined
  31. Kerberos.prototype.authGSSClientStep = function(context, challenge, callback) {
  32. if(typeof challenge == 'function') {
  33. callback = challenge;
  34. challenge = '';
  35. }
  36. return this._native_kerberos.authGSSClientStep(context, challenge, callback);
  37. }
  38. Kerberos.prototype.authGSSClientUnwrap = function(context, challenge, callback) {
  39. if(typeof challenge == 'function') {
  40. callback = challenge;
  41. challenge = '';
  42. }
  43. return this._native_kerberos.authGSSClientUnwrap(context, challenge, callback);
  44. }
  45. Kerberos.prototype.authGSSClientWrap = function(context, challenge, user_name, callback) {
  46. if(typeof user_name == 'function') {
  47. callback = user_name;
  48. user_name = '';
  49. }
  50. return this._native_kerberos.authGSSClientWrap(context, challenge, user_name, callback);
  51. }
  52. // free memory used by a context created using authGSSClientInit.
  53. // callback takes one argument, an error string if defined.
  54. Kerberos.prototype.authGSSClientClean = function(context, callback) {
  55. return this._native_kerberos.authGSSClientClean(context, callback);
  56. }
  57. // The server will obtain credentials using a keytab. To override the
  58. // default location (probably /etc/krb5.keytab) set the KRB5_KTNAME
  59. // environment variable.
  60. // The service name should be in the form service, or service@host.name
  61. // e.g. for HTTP, use "HTTP" or "HTTP@my.host.name". See gss_import_name
  62. // for GSS_C_NT_HOSTBASED_SERVICE.
  63. //
  64. // a boolean turns on "constrained_delegation". this enables acquisition of S4U2Proxy
  65. // credentials which will be stored in a credentials cache during the authGSSServerStep
  66. // method. this parameter is optional. The credentials will be stored in
  67. // a new cache, the location of which will be made available as the "delegatedCredentialsCache"
  68. // property on the returned context AFTER the authGSSServerStep stage.
  69. //
  70. // when "constrained_delegation" is enabled, a username can (optionally) be provided and
  71. // S4U2Self protocol transition will be initiated. In this case, we will not
  72. // require any "auth" data during the authGSSServerStep. This parameter is optional
  73. // but constrained_delegation MUST be enabled for this to work. When S4U2Self is
  74. // used, the username will be assumed to have been already authenticated, and no
  75. // actual authentication will be performed. This is basically a way to "bootstrap"
  76. // kerberos credentials (which can then be delegated with S4U2Proxy) for a user
  77. // authenticated externally.
  78. //
  79. // callback takes two arguments, an error string if defined and a new context
  80. //
  81. Kerberos.prototype.authGSSServerInit = function(service, constrained_delegation, username, callback) {
  82. if(typeof(constrained_delegation) === 'function') {
  83. callback = constrained_delegation;
  84. constrained_delegation = false;
  85. username = null;
  86. }
  87. if (typeof(constrained_delegation) === 'string') {
  88. throw new Error("S4U2Self protocol transation is not possible without enabling constrained delegation");
  89. }
  90. if (typeof(username) === 'function') {
  91. callback = username;
  92. username = null;
  93. }
  94. constrained_delegation = !!constrained_delegation;
  95. return this._native_kerberos.authGSSServerInit(service, constrained_delegation, username, callback);
  96. };
  97. //callback takes one argument, an error string if defined.
  98. Kerberos.prototype.authGSSServerClean = function(context, callback) {
  99. return this._native_kerberos.authGSSServerClean(context, callback);
  100. };
  101. // authData should be the base64 encoded authentication data obtained
  102. // from client, e.g., in the Authorization header (without the leading
  103. // "Negotiate " string) during SPNEGO authentication. The authenticated user
  104. // is available in context.username after successful authentication.
  105. // callback takes one argument, an error string if defined.
  106. //
  107. // Note: when S4U2Self protocol transition was requested in the authGSSServerInit
  108. // no actual authentication will be performed and authData will be ignored.
  109. //
  110. Kerberos.prototype.authGSSServerStep = function(context, authData, callback) {
  111. return this._native_kerberos.authGSSServerStep(context, authData, callback);
  112. };
  113. // authenticate the username and password against the KDC, and verify the KDC using a local
  114. // service key stored in the keytab. See above for details on providing the keytab.
  115. // The service should be the service principal name for a key available in the local keytab,
  116. // e.g. HTTP/somehost.example.com. If service is an empty tring, KDC verification will
  117. // be skipped. DON'T DO THIS - it's a possible security vulnerability if an attacker
  118. // can spoof your KDC (see: https://github.com/qesuto/node-krb5/issues/13)
  119. // callback receives error and boolean
  120. Kerberos.prototype.authUserKrb5Password = function(username, password, service, callback) {
  121. return this._native_kerberos.authUserKrb5Password(username, password, service, callback);
  122. };
  123. Kerberos.prototype.acquireAlternateCredentials = function(user_name, password, domain) {
  124. return this._native_kerberos.acquireAlternateCredentials(user_name, password, domain);
  125. }
  126. Kerberos.prototype.prepareOutboundPackage = function(principal, inputdata) {
  127. return this._native_kerberos.prepareOutboundPackage(principal, inputdata);
  128. }
  129. Kerberos.prototype.decryptMessage = function(challenge) {
  130. return this._native_kerberos.decryptMessage(challenge);
  131. }
  132. Kerberos.prototype.encryptMessage = function(challenge) {
  133. return this._native_kerberos.encryptMessage(challenge);
  134. }
  135. Kerberos.prototype.queryContextAttribute = function(attribute) {
  136. if(typeof attribute != 'number' && attribute != 0x00) throw new Error("Attribute not supported");
  137. return this._native_kerberos.queryContextAttribute(attribute);
  138. }
  139. // Some useful result codes
  140. Kerberos.AUTH_GSS_CONTINUE = 0;
  141. Kerberos.AUTH_GSS_COMPLETE = 1;
  142. // Some useful gss flags
  143. Kerberos.GSS_C_DELEG_FLAG = 1;
  144. Kerberos.GSS_C_MUTUAL_FLAG = 2;
  145. Kerberos.GSS_C_REPLAY_FLAG = 4;
  146. Kerberos.GSS_C_SEQUENCE_FLAG = 8;
  147. Kerberos.GSS_C_CONF_FLAG = 16;
  148. Kerberos.GSS_C_INTEG_FLAG = 32;
  149. Kerberos.GSS_C_ANON_FLAG = 64;
  150. Kerberos.GSS_C_PROT_READY_FLAG = 128;
  151. Kerberos.GSS_C_TRANS_FLAG = 256;
  152. // Export Kerberos class
  153. exports.Kerberos = Kerberos;
  154. // If we have SSPI (windows)
  155. if(kerberos.SecurityCredentials) {
  156. // Put all SSPI classes in it's own namespace
  157. exports.SSIP = {
  158. SecurityCredentials: require('./win32/wrappers/security_credentials').SecurityCredentials
  159. , SecurityContext: require('./win32/wrappers/security_context').SecurityContext
  160. , SecurityBuffer: require('./win32/wrappers/security_buffer').SecurityBuffer
  161. , SecurityBufferDescriptor: require('./win32/wrappers/security_buffer_descriptor').SecurityBufferDescriptor
  162. }
  163. }