Permalink
Browse files

Initial commit

  • Loading branch information...
1 parent ae9ec00 commit 38fd98bd4d0dde89a9ab91ed8fe22c1a6af055ca luca committed Nov 1, 2012
Showing with 240 additions and 1 deletion.
  1. +24 −0 LICENSE.txt
  2. +43 −1 README.md
  3. +11 −0 binding.gyp
  4. +32 −0 package.json
  5. +130 −0 src/krb5.cc
View
@@ -0,0 +1,24 @@
+(The MIT License)
+
+Copyright (c) 2012 Luca Fornili <qesuto@gmail.com>
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
View
@@ -1,4 +1,46 @@
node-krb5
=========
+### Node.js native addon for simple krb5 user authentication
-Node.js native addon for simple krb5 user authentication
+The module currently builds on linux with krb5 headers and libs (tested on centos) and osx (tested on lion); windows coming soon.
+
+How to Use
+----------
+
+``` js
+var krb5 = require('krb5');
+
+krb5.authenticate('username@REALM', 'password', function(err) {
+ if (err) {
+ console.log("Error: " + err);
+ } else {
+ console.log("OK");
+ }
+});
+```
+
+License
+-------
+
+(The MIT License)
+
+Copyright (c) 2012 Luca Fornili &lt;qesuto@gmail.com&gt;
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
@@ -0,0 +1,11 @@
+{
+ 'targets': [
+ {
+ 'target_name': 'krb5',
+ 'sources': [ 'src/krb5.cc' ],
+ 'link_settings' : {
+ 'libraries': [ '-lkrb5' ]
+ }
+ },
+ ]
+}
View
@@ -0,0 +1,32 @@
+{
+ "name": "node-krb5",
+ "version": "0.0.1",
+ "author": "Qesuto <qesuto@gmail.com>",
+ "description": "Node.js native addon for simple krb5 user authentication",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/qesuto/node-krb5.git"
+ },
+ "homepage": "http://github.com/qesuto/node-krb5",
+ "main": "./build/Release/krb5",
+ "keywords": [
+ "kerberos",
+ "krb5",
+ "auth",
+ "authentication",
+ "active directory",
+ "ad",
+ "windows"
+ ],
+ "engines": {
+ "node": ">=0.5.6"
+ },
+ "dependencies": {
+ "node-gyp": "0.7.2"
+ },
+ "scripts": {
+ "install": "node-gyp rebuild"
+ },
+ "devDependencies": {},
+ "optionalDependencies": {}
+}
View
@@ -0,0 +1,130 @@
+#include <string>
+#include <node.h>
+#include <v8.h>
+#include <krb5.h>
+
+using namespace v8;
+using namespace node;
+
+struct AuthStruct
+{
+ std::string* principal;
+ std::string* password;
+ Persistent<Function> callback;
+ std::string* error_message;
+};
+
+
+void doing_work (uv_work_t *req) {
+ AuthStruct* authStruct = (AuthStruct*) req->data;
+ krb5_context context;
+ krb5_principal principal;
+ krb5_creds creds;
+
+ int err;
+ err = krb5_init_context(&context);
+ if(err == 0)
+ {
+ err = krb5_parse_name(context, authStruct->principal->c_str(), &principal);
+ if(err == 0)
+ {
+ err = krb5_get_init_creds_password(
+ context,
+ &creds,
+ principal,
+ (char*) authStruct->password->c_str(),
+ NULL,
+ NULL,
+ 0,
+ NULL,
+ NULL
+ );
+
+ if(err == 0)
+ {
+ krb5_free_cred_contents(context, &creds);
+ }
+
+ krb5_free_principal(context, principal);
+ }
+
+ if(err != 0){
+ const char* msg = krb5_get_error_message(context, err ); // ha bisogno di un context
+ authStruct->error_message = new std::string(msg);
+ krb5_free_error_message(context, msg);
+ }
+
+ krb5_free_context(context);
+ }
+ else {
+ authStruct->error_message = new std::string(strerror(err));
+ }
+}
+
+void after_doing_work (uv_work_t *req) {
+
+ AuthStruct* authStruct = (AuthStruct*) req->data;
+
+ Handle<Value> argv[1];
+ if(authStruct->error_message)
+ {
+ argv[0] = String::New(authStruct->error_message->c_str());
+ }
+ else
+ {
+ argv[0] = Undefined();
+ }
+
+ TryCatch try_catch;
+ authStruct->callback->Call(Context::GetCurrent()->Global(), 1, argv);
+
+ // cleanup
+ delete authStruct->principal;
+ delete authStruct->password;
+ authStruct->callback.Dispose();
+ if(authStruct->error_message)
+ {
+ delete authStruct->error_message;
+ }
+ delete authStruct;
+ delete req;
+
+ if (try_catch.HasCaught())
+ FatalException(try_catch);
+}
+
+Handle<Value> Method(const Arguments& args) {
+ HandleScope scope;
+
+ if(args.Length() < 3)
+ {
+ printf("too few arguments.\n");
+ return scope.Close(String::New("too few arguments"));
+ }
+
+ if(!args[0]->IsString() || !args[1]->IsString() || !args[2]->IsFunction())
+ {
+ printf("wrong arguments.\n");
+ return scope.Close(String::New("wrong arguments"));
+ }
+
+ AuthStruct* authStruct = new AuthStruct;
+
+ authStruct->principal = new std::string(*String::AsciiValue(Local<String>::Cast(args[0])));
+ authStruct->password = new std::string(*String::AsciiValue(Local<String>::Cast(args[1])));
+ authStruct->callback = Persistent<Function>::New(Local<Function>::Cast(args[2]));
+ authStruct->error_message = NULL;
+
+ uv_work_t *req = new uv_work_t;
+ req->data = authStruct;
+
+ uv_queue_work(uv_default_loop(), req, doing_work, after_doing_work);
+
+ return scope.Close(Undefined());
+}
+
+void init(Handle<Object> target) {
+ NODE_SET_METHOD(target, "authenticate", Method);
+}
+
+NODE_MODULE(krb5, init);

0 comments on commit 38fd98b

Please sign in to comment.