How to implements asynchrounous authentication with ticket method (using node.js)

#1

I need to check credentials on mongodb on authenticate method but it should return the rules else throw error if fail…
The question is: how I can to implement an asynchronous authenticator?

function authenticate (args) {

var realm = args[0];

var authid = args[1];

var details = args[2];

if (USERDB[authid] !== undefined) {

	if (null !== details.ticket && details.ticket == "pass_from_client") {

		// how to insert here an asynchronous solution? Can I return a promise? Or send back a response after?

		return {

			role: "frontend"

		}

	} else {

		throw "invalid ticket"

	}

} else {

	throw "no such user";

}

}

``

Something like this:

User.find( { login: authid, password: details.ticket }, function( err, result ) {
// ok user must be authenticated…
return { role: “frontend” }
} )

``

0 Likes

#2

Hi Rafael,

you can simply return a Promise from your authenticator that resolves to the actual return value only when your mongodb query returns.

You will need a proper, async DB client library. Or at least one that works with callbacks (and then use the idiom to transform a callback into a promise that is resolved/rejected when the callback fires).

Cheers,
/Tobias

···

Am 04.04.2016 um 18:30 schrieb Rafael Freitas:

I need to check credentials on mongodb on authenticate method but it
should return the rules else throw error if fail...
The question is: how I can to implement an asynchronous authenticator?

>
function authenticate (args) {
var realm = args[0];
var authid = args[1];
var details = args[2];

if (USERDB[authid] !== undefined) {
if (null !== details.ticket && details.ticket == "pass_from_client") {
// how to insert here an asynchronous solution? Can I return a promise?
Or send back a response after?
return {
role: "frontend"
}
} else {
throw "invalid ticket"
}
} else {
throw "no such user";
}

>

Something like this:

>
User.find({login:authid,password:details.ticket },function(err,result ){
      // ok user must be authenticated...
      return { role: "frontend" }
} )
>

--
You received this message because you are subscribed to the Google
Groups "Crossbar" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to crossbario+...@googlegroups.com
<mailto:crossbario+...@googlegroups.com>.
To post to this group, send email to cross...@googlegroups.com
<mailto:cross...@googlegroups.com>.
To view this discussion on the web visit
https://groups.google.com/d/msgid/crossbario/07a5ad8d-3201-4e7f-bdc8-11eb6627fb83%40googlegroups.com
<https://groups.google.com/d/msgid/crossbario/07a5ad8d-3201-4e7f-bdc8-11eb6627fb83%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.

0 Likes

#3

Thank you so much for your short answer.

···

Em segunda-feira, 4 de abril de 2016 13:30:09 UTC-3, Rafael Freitas escreveu:

I need to check credentials on mongodb on authenticate method but it should return the rules else throw error if fail…
The question is: how I can to implement an asynchronous authenticator?

function authenticate (args) {

var realm = args[0];

var authid = args[1];

var details = args[2];

if (USERDB[authid] !== undefined) {

  if (null !== details.ticket && details.ticket == "pass_from_client") {
  	// how to insert here an asynchronous solution? Can I return a promise? Or send back a response after?
  	return {
  		role: "frontend"
  	}
  } else {
  	throw "invalid ticket"
  }

} else {

  throw "no such user";

}

}

``

Something like this:

User.find( { login: authid, password: details.ticket }, function( err, result ) {
// ok user must be authenticated…
return { role: “frontend” }
} )

``

0 Likes