-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathme.coffee
208 lines (187 loc) · 7.3 KB
/
me.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# OAuth daemon
# Copyright (C) 2016 Webshell SAS
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
async = require 'async'
Url = require 'url'
restify = require 'restify'
request = require 'request'
zlib = require 'zlib'
fs = require 'fs'
qs = require 'qs'
Stream = require 'stream'
module.exports = (env) ->
oauth = env.utilities.oauth
fixUrl = (ref) -> ref.replace /^([a-zA-Z\-_]+:\/)([^\/])/, '$1/$2'
env.middlewares.slashme = {}
env.middlewares.slashme.all = []
# Chain of middlewares that are applied to each Slashme endpoints
createMiddlewareChain = () ->
(req, res, next) ->
chain = []
i = 0
for k, middleware of env.middlewares.slashme.all
do (middleware) ->
chain.push (callback) ->
middleware req, res, callback
if chain.length == 0
return next()
async.waterfall chain, () ->
next()
middlewares_slashme_chain = createMiddlewareChain()
AbsentFeatureError = (feature) ->
return new env.utilities.check.Error "This provider does not support the " + feature + " feature yet"
cors_middleware = (req, res, next) ->
oauthio = req.headers.oauthio
if ! oauthio
return env.utilities.check.Error "You must provide a valid 'oauthio' http header"
oauthio = qs.parse(oauthio)
if ! oauthio.k
return cb new env.utilities.check.Error "oauthio_key", "You must provide a 'k' (key) in 'oauthio' header"
origin = null
ref = fixUrl(req.headers['referer'] || req.headers['origin'] || "http://localhost");
urlinfos = Url.parse(ref)
if not urlinfos.hostname
ref = origin = "http://localhost"
else
origin = urlinfos.protocol + '//' + urlinfos.host
res.setHeader 'Access-Control-Allow-Origin', origin
res.setHeader 'Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE'
next()
fieldMap = (body, map_array, filter) ->
result = {}
for k of map_array
field = map_array[k]
if !filter || k in filter
if typeof field == 'string'
if field == '='
result[k] = body[k]
else
result[k] = body[field]
else if typeof field == 'function'
result[k] = field(body)
result.raw = if result.raw then result.raw else body
return result
exp = {}
exp.raw = ->
fixUrl = (ref) -> ref.replace /^([a-zA-Z\-_]+:\/)([^\/])/, '$1/$2'
check = env.utilities.check
env.server.opts new RegExp('^/auth/([a-zA-Z0-9_\\.~-]+)/me$'), (req, res, next) =>
origin = null
ref = fixUrl(req.headers['referer'] || req.headers['origin'] || "http://localhost");
urlinfos = Url.parse(ref)
if not urlinfos.hostname
return next new restify.InvalidHeaderError 'Missing origin or referer.'
origin = urlinfos.protocol + '//' + urlinfos.host
res.setHeader 'Access-Control-Allow-Origin', origin
res.setHeader 'Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE'
if req.headers['access-control-request-headers']
res.setHeader 'Access-Control-Allow-Headers', req.headers['access-control-request-headers']
res.cache maxAge: 120
res.send 200
next false
env.plugins.slashme.me = (provider, oauthio, filter, callback) ->
env.data.providers.getMeMapping provider, (err, content) =>
if !err
if content.url
env.plugins.request.apiRequest {apiUrl: content.url, headers: { 'User-Agent': 'Node' } }, provider, oauthio, (err, options) =>
return callback AbsentFeatureError('me()') if err
options.json = true
options.method ?= 'GET'
request options, (err, response, body) =>
return callback AbsentFeatureError('me()') if err
# parsing body and mapping values to common field names, and sending the result
return callback null, fieldMap(body, content.fields, filter)
else if content.fetch
user_fetcher = {}
apiRequest = env.plugins.request.apiRequest
async.eachSeries content.fetch, (item, cb) ->
if typeof item == 'object'
url = item.url
apiRequest {apiUrl: item.url, method: item.method || 'get', headers: { 'User-Agent': 'Node' } }, provider, oauthio, (err, options) =>
return callback AbsentFeatureError('me()') if err
options.json = true
options.method ?= 'GET'
rq = request options
chunks = []
rq.on 'response', (rs) ->
rs.on 'data', (chunk) ->
chunks.push chunk
rs.on 'end', ->
buffer = Buffer.concat chunks
if rs.headers['content-encoding'] == 'gzip'
zlib.gunzip buffer, (err, decoded) ->
return callback err if err
body = JSON.parse decoded.toString()
for k of item.export
value = item.export[k](body)
user_fetcher[k] = value
cb()
else
body = JSON.parse buffer.toString()
for k of item.export
value = item.export[k](body)
user_fetcher[k] = value
cb()
if typeof item == 'function'
url = item(user_fetcher)
if typeof url == 'object'
return callback null, fieldMap(url, content.fields, filter)
apiRequest {apiUrl: url, headers: { 'User-Agent': 'Node' } }, provider, oauthio, (err, options) =>
return callback AbsentFeatureError('me()') if err
options.json = true
options.method ?= 'GET'
delete options.headers['accept-encoding']
rq = request options
chunks = []
rq.on 'response', (rs) ->
rs.on 'data', (chunk) ->
chunks.push chunk
rs.on 'end', ->
buffer = Buffer.concat chunks
if rs.headers['content-encoding'] == 'gzip'
zlib.gunzip buffer, (err, decoded) ->
return callback err if err
try
body = JSON.parse decoded.toString()
catch e
return callback e if e
return callback null, fieldMap(body, content.fields, filter)
else
try
body = JSON.parse buffer.toString()
catch e
return callback e if e
return callback null, fieldMap(body, content.fields, filter)
, ->
else
return callback AbsentFeatureError('me()')
else
return callback AbsentFeatureError('me()')
env.server.get new RegExp('^/auth/([a-zA-Z0-9_\\.~-]+)/me$'), restify.queryParser(), cors_middleware, middlewares_slashme_chain, (req, res, next) =>
cb = env.server.send res, next
provider = req.params[0]
filter = req.query.filter
filter = filter?.split ','
oauthio = req.headers.oauthio
if ! oauthio
return cb new Error "You must provide a valid 'oauthio' http header"
oauthio = qs.parse(oauthio)
if ! oauthio.k
return cb new Error "oauthio_key", "You must provide a 'k' (key) in 'oauthio' header"
env.plugins.slashme.me provider, oauthio, filter, (err, me) ->
return next err if err
res.send me
next()
return exp