This repository has been archived by the owner on Jul 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stash-auth.coffee
138 lines (124 loc) · 4.02 KB
/
stash-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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
qs = require "querystring"
{join} = require "path"
{OAuth} = require "oauth"
handleOAuthError = (err, next)->
# https://github.com/ciaranj/node-oauth/issues/250
if typeof err is "object" and not (err instanceof Error)
data = {}
if err.data
if err.data.match? "<html>"
data.html = err.data
else
try data[k] = v for k, v of qs.parse err.data
msg_json = {}
msg_json[k] = v for k, v of data when k isnt "oauth_signature" and k isnt "oauth_signature_base_string"
error = new Error "Stash OAuth HTTP status code #{err.statusCode}: #{JSON.stringify msg_json}"
error[k] = v for k, v of data
error[k] = v for k, v of err
next error
else
next err
module.exports = class StashAuth
constructor: (
@API_URL
CONSUMER_KEY
privateKeyData
fullCallbackURL
)->
@requestTokenURL = "#{@API_URL}/plugins/servlet/oauth/request-token"
@accessTokenURL = "#{@API_URL}/plugins/servlet/oauth/access-token"
@userAuthorizationURL = "#{@API_URL}/plugins/servlet/oauth/authorize"
@consumer =
new OAuth(
@requestTokenURL
@accessTokenURL
CONSUMER_KEY
""
"1.0"
fullCallbackURL
"RSA-SHA1"
null
privateKeyData
)
auth: (req, res, next)=>
req.session.stashAuthReturnURL = req.originalUrl
if req.session.oauthAccessToken
method = (url, params, callback)=>
if typeof arguments[1] is "function"
[params, callback] = [{}, arguments[1]]
if m = url.match /^(.*)\?(.+)$/
more_params = params
params = qs.parse m[2]
params[k] = v for k, v of more_params
url = m[1]
if Object.keys(params).length > 0
url = "#{url}?#{qs.stringify params}"
unless url.match /^https?:\/\//
url = "#{@API_URL}/rest/#{url}"
[
url
params
(err, data)=>
return handleOAuthError err, callback if err
unless data is "" or typeof data is "object"
try data = JSON.parse data catch json_err
callback json_err, data
req.session.oauthAccessToken
req.session.oauthAccessTokenSecret
]
req.stash =
get: (url, params, callback)=>
[url, params, callback, access, access_secret] = method url, params, callback
@consumer.get url,
access, access_secret
"application/json"
callback
post: (url, params, callback)=>
[url, params, callback, access, access_secret] = method url, params, callback
@consumer.post url,
access, access_secret
"{}", "application/json"
callback
put: (url, params, callback)=>
[url, params, callback, access, access_secret] = method url, params, callback
@consumer.put url,
access, access_secret
"{}", "application/json"
callback
delete: (url, params, callback)=>
[url, params, callback, access, access_secret] = method url, params, callback
@consumer.delete url,
access, access_secret
"{}", "application/json"
callback
getAll: (url, params, callback)=>
[url, params, callback] = method url, params, callback
values = []
start = 0
do getSome = (start)=>
params.start = start
req.stash.get url, params, (err, data)=>
return callback err if err
values = values.concat data.values
if data.isLastPage
callback null, values
else
getSome data.nextPageStart
next()
else
@consumer.getOAuthRequestToken (err, oauthToken, oauthTokenSecret, results)=>
return handleOAuthError err, next if err
req.session.oauthRequestToken = oauthToken
req.session.oauthRequestTokenSecret = oauthTokenSecret
res.redirect "#{@userAuthorizationURL}?oauth_token=#{oauthToken}"
authCallback: (req, res, next)=>
@consumer.getOAuthAccessToken(
req.session.oauthRequestToken
req.session.oauthRequestTokenSecret
req.query.oauth_verifier,
(err, oauthAccessToken, oauthAccessTokenSecret, results)=>
return handleOAuthError err, next if err
req.session.oauthAccessToken = oauthAccessToken
req.session.oauthAccessTokenSecret = oauthAccessTokenSecret
res.redirect req.session.stashAuthReturnURL
)