Browse files

Merge branch 'oauth'

Github pull request #85
  • Loading branch information...
2 parents bfa6955 + 010afcc commit 712c5317134ef40c44f7ae689128edfd7f5a268b @astro astro committed Oct 9, 2012
Showing with 82 additions and 9 deletions.
  1. +39 −0 examples/echo_bot_oauth.js
  2. +15 −6 lib/xmpp/client.js
  3. +25 −2 lib/xmpp/sasl.js
  4. +2 −0 lib/xmpp/session.js
  5. +1 −1 package.json
View
39 examples/echo_bot_oauth.js
@@ -0,0 +1,39 @@
+/**
+ * Echo Bot - the XMPP Hello World
+ **/
+var xmpp = require('../lib/node-xmpp');
+var argv = process.argv;
+
+if (argv.length != 4) {
+ console.error('Usage: node echo_bot_oauth.js <my-jid> <oauth-token>');
+ process.exit(1);
+}
+
+var cl = new xmpp.Client({ jid: argv[2],
+ host: 'talk.google.com',
+ oauth2_token: argv[3],
+ oauth2_auth: 'http://www.google.com/talk/protocol/auth' });
+cl.on('online',
+ function() {
+ cl.send(new xmpp.Element('presence', { }).
+ c('show').t('chat').up().
+ c('status').t('Happily echoing your <message/> stanzas')
+ );
+ });
+cl.on('stanza',
+ function(stanza) {
+ if (stanza.is('message') &&
+ // Important: never reply to errors!
+ stanza.attrs.type !== 'error') {
+
+ // Swap addresses...
+ stanza.attrs.to = stanza.attrs.from;
+ delete stanza.attrs.from;
+ // and send back.
+ cl.send(stanza);
+ }
+ });
+cl.on('error',
+ function(e) {
+ console.error(e);
+ });
View
21 lib/xmpp/client.js
@@ -35,7 +35,7 @@ var IQID_SESSION = 'sess',
* legacySSL: Boolean (optional) - connect to the legacy SSL port, requires at least the host to be specified
* credentials: Dictionary (optional) - TLS or SSL key and certificate credentials
*
- * Example:
+ * Examples:
* var cl = new xmpp.Client({
* jid: "me@example.com",
* password: "secret"
@@ -44,7 +44,13 @@ var IQID_SESSION = 'sess',
* jid: '-' + fbUID + '@chat.facebook.com',
* api_key: '54321', // api key of your facebook app
* access_token: 'abcdefg', // user access token
- * host: 'chat.facebook.com',
+ * host: 'chat.facebook.com'
+ * });
+ * var gtalk = new xmpp.Client({
+ * jid: 'me@gmail.com',
+ * oauth2_token: 'xxxx.xxxxxxxxxxx', // from OAuth2
+ * oauth2_auth: 'http://www.google.com/talk/protocol/auth',
+ * host: 'talk.google.com'
* });
*
* Example SASL EXTERNAL:
@@ -174,13 +180,16 @@ Client.prototype.useFeatures = function() {
this.mech.password = this.password;
this.mech.api_key = this.api_key;
this.mech.access_token = this.access_token;
+ this.mech.oauth2_token = this.oauth2_token;
+ this.mech.oauth2_auth = this.oauth2_auth;
this.mech.realm = this.jid.domain; // anything?
this.mech.digest_uri = "xmpp/" + this.jid.domain;
var authMsg = encode64(this.mech.auth());
- this.send(new ltx.Element('auth',
- { xmlns: NS_XMPP_SASL,
- mechanism: this.mech.name
- }).t(authMsg));
+ var attrs = this.mech.authAttrs();
+ attrs.xmlns = NS_XMPP_SASL;
+ attrs.mechanism = this.mech.name;
+ this.send(new ltx.Element('auth', attrs).
+ t(authMsg));
} else {
this.emit('error', 'No usable SASL mechanism');
}
View
27 lib/xmpp/sasl.js
@@ -8,7 +8,7 @@ var EventEmitter = require('events').EventEmitter;
* @param {Array} mechs Server-offered SASL mechanism names
*/
function selectMechanism(offeredMechs, preferredMech, availableMech) {
- var mechClasses = [XFacebookPlatform, DigestMD5,
+ var mechClasses = [XOAuth2, XFacebookPlatform, DigestMD5,
Plain, Anonymous];
var byName = {};
var mech;
@@ -47,7 +47,12 @@ function availableMechanisms(availableMech) {
exports.availableMechanisms = availableMechanisms;
// Mechanisms
-var Mechanism = EventEmitter;
+function Mechanism() {
+}
+util.inherits(Mechanism, EventEmitter);
+Mechanism.authAttrs = function() {
+ return {};
+};
function Plain() {
}
@@ -64,6 +69,24 @@ Plain.prototype.authServer = function(auth, client) {
client.authenticate(this.username, params[2]);
};
+function XOAuth2() {
+}
+util.inherits(XOAuth2, Mechanism);
+XOAuth2.prototype.name = "X-OAUTH2";
+XOAuth2.prototype.auth = function() {
+ return "\0" + this.authzid + "\0" + this.oauth2_token;
+};
+XOAuth2.prototype.authServer = function(auth, client) {
+ var params = auth.split("\x00");
+ this.username = params[1];
+ client.authenticate(this.username, params[2]);
+};
+XOAuth2.prototype.authAttrs = function() {
+ return { "auth:service": "oauth2",
+ "xmlns:auth": this.oauth2_auth
+ };
+};
+
function XFacebookPlatform() {
}
util.inherits(XFacebookPlatform, Mechanism);
View
2 lib/xmpp/session.js
@@ -21,6 +21,8 @@ function Session(opts) {
this.availableSaslMechanisms = [];
this.api_key = opts.api_key;
this.access_token = opts.access_token;
+ this.oauth2_token = opts.oauth2_token;
+ this.oauth2_auth = opts.oauth2_auth;
this.register = opts.register;
delete this.did_bind;
delete this.did_session;
View
2 package.json
@@ -24,7 +24,7 @@
,"email": "astro@spaceboyz.net"
,"web": "http://spaceboyz.net/~astro/"
}]
-,"contributors": ["Stephan Maka", "Derek Hammer", "Daniel Zelisko", "Michael Geers", "Nolan Darilek", "Nathan Rajlich", "Dhruv Matani", "Camilo Aguilar", "Henry Chan", "Justin Campbell", "Trent Mick", "Alexey Shamrin", "Sonny Piers", "Chaitanya Gupta", "Иван", "Julien Genestoux"]
+,"contributors": ["Stephan Maka", "Derek Hammer", "Daniel Zelisko", "Michael Geers", "Nolan Darilek", "Nathan Rajlich", "Dhruv Matani", "Camilo Aguilar", "Henry Chan", "Justin Campbell", "Trent Mick", "Alexey Shamrin", "Sonny Piers", "Chaitanya Gupta", "Иван", "Julien Genestoux", "Ian Langworth"]
,"licenses": [{"type": "MIT"}]
,"engine": "node >=0.4.0"
,"scripts": { "postinstall": "browserify -d -o node-xmpp-browser.js lib/node-xmpp-browserify.js"

0 comments on commit 712c531

Please sign in to comment.