Skip to content
Browse files

add support for twitter (closes #9)

  • Loading branch information...
1 parent 6a18c65 commit 19e6b6d378fb610355c4acb4ac059807a087113a @jed committed Mar 10, 2012
Showing with 118 additions and 8 deletions.
  1. +27 −5 README.md
  2. +8 −1 example/express.js
  3. +8 −1 example/server.js
  4. BIN lib/assets/twitter.ico
  5. +71 −0 lib/services/twitter.js
  6. +4 −1 package.json
View
32 README.md
@@ -3,7 +3,7 @@ authom
authom is an authentication library for node.js. It unifies authentication APIs for multiple services into a single EventEmitter, and works with both the built-in node.js HTTP module and as an Express/Connect app.
-authom was designed to solve one problem and solve it well. It has an intuitive node.js-like API, no external dependencies, and doesn't force any particular persistence, session, or middleware approaches on you.
+authom was designed to solve one problem and solve it well. It has an intuitive node.js-like API, no required dependencies, and doesn't force any particular persistence, session, or middleware approaches on you.
Example
-------
@@ -50,7 +50,7 @@ var app = require("express").createServer()
authom.createServer({ /* facebook credentials */ })
authom.createServer({ /* github credentials */ })
authom.createServer({ /* google credentials */ })
-authom.createServer({ /* foursquare credentials */ })
+authom.createServer({ /* twitter credentials */ })
// ... et cetera
authom.on("auth", function(req, res, data) {
@@ -91,6 +91,8 @@ Supported services
<img src="https://github.com/jed/authom/raw/master/lib/assets/soundcloud.ico" style="vertical-align:middle"> SoundCloud (by [jed](https://github.com/jed))
+<img src="https://github.com/jed/authom/raw/master/lib/assets/twitter.ico" style="vertical-align:middle"> Twitter (by [jed](https://github.com/jed))
+
<img src="https://github.com/jed/authom/raw/master/lib/assets/windowslive.ico" style="vertical-align:middle"> Windows Live (by [jed](https://github.com/jed))
Installation and Setup
@@ -426,6 +428,26 @@ var soundcloud = authom.createServer({
})
```
+### Twitter ([create an app](dev.twitter.com/apps/))
+
+Options:
+
+- `service`: "twitter"
+- `id`: the application's `Consumer key`
+- `secret`: the application's `Consumer secret`
+
+Example:
+
+```javascript
+authom.createServer({
+ service: "twitter",
+ id: "LwjCfHAugMghuYtHLS9Ugw",
+ secret: "etam3XHqDSDPceyHti6tRQGoywiISY0vZWfzhQUxGL4"
+})
+```
+
+Notes: Since Twitter is still (!) using the old OAuth1.0a protocol, it requires [@ciaranj](https://github.com/ciaranj)'s [node-oauth](https://github.com/ciaranj/node-oauth) library to be installed.
+
### Windows Live ([create an app](https://manage.dev.live.com/Applications/Index))
Options:
@@ -483,18 +505,18 @@ License
-------
Copyright (c) 2012 Jed Schmidt, http://jed.is/
-
+
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
View
9 example/express.js
@@ -65,6 +65,12 @@ authom.createServer({
scope:"AccountInfoFull"
})
+authom.createServer({
+ service: "twitter",
+ id: "LwjCfHAugMghuYtHLS9Ugw",
+ secret: "etam3XHqDSDPceyHti6tRQGoywiISY0vZWfzhQUxGL4"
+})
+
app.get("/auth/:service", authom.app)
app.get("/", function(req, res) {
@@ -80,6 +86,7 @@ app.get("/", function(req, res) {
"<div><a href='/auth/gowalla'>Who am I on Gowalla?</a></div>" +
"<div><a href='/auth/instagram'>Who am I on Instagram?</a></div>" +
"<div><a href='/auth/soundcloud'>Who am I on SoundCloud?</a></div>" +
+ "<div><a href='/auth/twitter'>Who am I on Twitter?</a></div>" +
"<div><a href='/auth/windowslive'>Who am I on Windows Live?</a></div>" +
"</body>" +
"</html>"
@@ -103,4 +110,4 @@ authom.on("error", function(req, res, data){
app.listen(port, function() {
console.log("listening at http://authom.jedschmidt.com/")
-})
+})
View
9 example/server.js
@@ -16,6 +16,7 @@ var http = require("http")
"<div><a href='/auth/instagram'>Who am I on Instagram?</a></div>" +
"<div><a href='/auth/meetup'>Who am I on Meetup?</a></div>" +
"<div><a href='/auth/soundcloud'>Who am I on SoundCloud?</a></div>" +
+ "<div><a href='/auth/twitter'>Who am I on Twitter?</a></div>" +
"<div><a href='/auth/windowslive'>Who am I on Windows Live?</a></div>" +
"</body>" +
"</html>"
@@ -98,6 +99,12 @@ authom.createServer({
scope:"AccountInfoFull"
})
+authom.createServer({
+ service: "twitter",
+ id: "LwjCfHAugMghuYtHLS9Ugw",
+ secret: "etam3XHqDSDPceyHti6tRQGoywiISY0vZWfzhQUxGL4"
+})
+
authom.on("auth", function(req, res, data) {
var answer = Buffer(
"<html>" +
@@ -130,4 +137,4 @@ authom.on("error", function(req, res, data){
authom.listen(server)
server.listen(port, function() {
console.log("listening at http://authom.jedschmidt.com/")
-})
+})
View
BIN lib/assets/twitter.ico
Binary file not shown.
View
71 lib/services/twitter.js
@@ -0,0 +1,71 @@
+var EventEmitter = require("events").EventEmitter
+ , url = require("url")
+ , OAuth
+ , secrets = {}
+
+try { OAuth = require("oauth").OAuth }
+catch (e) {
+ throw new Error("oauth library could not be loaded.")
+}
+
+function Twitter(options) {
+ this.id = options.id
+ this.secret = options.secret
+
+ this.on("request", this.onRequest.bind(this))
+}
+
+Twitter.prototype = new EventEmitter
+
+Twitter.prototype.parseURI = function(request) {
+ var protocol = request.socket.encrypted ? "https" : "http"
+ , host = request.headers.host || request.connection.remoteAddress
+
+ return url.parse(protocol + "://" + host + request.url, true)
+}
+
+Twitter.prototype.onRequest = function(req, res) {
+ var self = this
+ , uri = this.parseURI(req)
+ , verifier = uri.query.oauth_verifier
+ , token = uri.query.oauth_token
+ , oa = new OAuth(
+ "https://api.twitter.com/oauth/request_token",
+ "https://api.twitter.com/oauth/access_token",
+ this.id,
+ this.secret,
+ "1.0",
+ url.format(uri),
+ "HMAC-SHA1"
+ )
+
+ if (verifier && token) {
+ oa.getOAuthAccessToken(token, secrets[token], verifier, onToken)
+ }
+
+ else oa.getOAuthRequestToken(function(error, oauth_token, oauth_token_secret, results){
+ if (error) return self.emit("error", req, res, uri.query)
+
+ secrets[oauth_token] = oauth_token_secret
+ setTimeout(function(){ delete secrets[oauth_token] }, 60000)
+
+ res.writeHead(302, {
+ Location: "https://twitter.com/oauth/authenticate?oauth_token=" + oauth_token
+ })
+
+ res.end()
+ })
+
+ function onToken(error, oauth_access_token, oauth_access_token_secret, results){
+ if (error) return self.emit("error", req, res, uri.query)
+
+ self.emit("auth", req, res, {
+ token: oauth_access_token,
+ secret: oauth_access_token_secret,
+ id: results.user_id,
+ data: results
+ })
+ }
+}
+
+module.exports = Twitter
View
5 package.json
@@ -25,7 +25,10 @@
"devDependencies": {
"express": "~2.5.4"
},
+ "optionalDependencies": {
+ "oauth": ">=0.9.6"
+ },
"scripts": {
"start": "sudo node ./example/server.js"
}
-}
+}

0 comments on commit 19e6b6d

Please sign in to comment.
Something went wrong with that request. Please try again.