forked from jed/authom
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8d193a9
commit 920776b
Showing
4 changed files
with
98 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,75 @@ | |||
var EventEmitter = require("events").EventEmitter | |||
, util = require("util") | |||
, url = require("url") | |||
, OAuth | |||
, secrets = {} | |||
|
|||
try { OAuth = require("oauth").OAuth } | |||
catch (e) { | |||
throw new Error("oauth library could not be loaded.") | |||
} | |||
|
|||
function Trello(options) { | |||
this.id = options.id | |||
this.secret = options.secret | |||
this.app_name = options.app_name | |||
|
|||
this.on("request", this.onRequest.bind(this)) | |||
|
|||
EventEmitter.call(this); | |||
} | |||
|
|||
util.inherits(Trello, EventEmitter); | |||
|
|||
Trello.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) | |||
} | |||
|
|||
Trello.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://trello.com/1/OAuthGetRequestToken", | |||
"https://trello.com/1/OAuthGetAccessToken", | |||
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, error) | |||
|
|||
secrets[oauth_token] = oauth_token_secret | |||
setTimeout(function(){ delete secrets[oauth_token] }, 60000) | |||
|
|||
res.writeHead(302, { | |||
Location: "https://trello.com/1/OAuthAuthorizeToken?oauth_token=" + oauth_token + (self.app_name !== undefined ? ('&name='+self.app_name) : '') | |||
}) | |||
|
|||
res.end() | |||
}) | |||
|
|||
function onToken(error, oauth_access_token, oauth_access_token_secret, results){ | |||
if (error) return self.emit("error", req, res, uri.query, error) | |||
|
|||
self.emit("auth", req, res, { | |||
token: oauth_access_token, | |||
secret: oauth_access_token_secret, | |||
id: results.encoded_user_id, | |||
data: results | |||
}) | |||
} | |||
} | |||
|
|||
module.exports = Trello |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters