-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.coffee
52 lines (40 loc) · 1.4 KB
/
auth.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
http = require('http')
exec = require('child_process').exec
URL = require('url')
OAuth = require('oauth')
listenForAuthResponse = (cb) ->
server = http.createServer (req, res) ->
return unless /\/authorized/.test(req.url)
res.end('Successfully Authenticated! Return to the terminal to continue.')
url = URL.parse(req.url, true)
cb url.query.oauth_verifier
server.listen(8088, '127.0.0.1')
auth = (opts, cb) ->
oauth = new OAuth.OAuth(
'http://www.flickr.com/services/oauth/request_token',
'http://www.flickr.com/services/oauth/access_token',
opts.API_KEY,
opts.API_SECRET,
'1.0A',
null,
'HMAC-SHA1'
)
requestParams =
oauth_callback: 'http://localhost:8088/authorized'
oauth.getOAuthRequestToken requestParams, (err, token, secret, res) ->
if err
console.error "Error getting request token", err
return cb(err)
url = "http://www.flickr.com/services/oauth/authorize"
url += "?oauth_token=#{ token }"
exec "open #{ url }", (err) ->
if err
console.error "Error opening browser", err
return cb(err)
listenForAuthResponse (verifier) ->
oauth.getOAuthAccessToken token, secret, verifier, (err, accessToken, accessSecret, res) ->
if err
console.error "Error getting access token", err
return cb(err)
cb(null, accessToken, accessSecret)
module.exports = auth