This repository has been archived by the owner on Jul 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
/
auth.coffee
214 lines (179 loc) · 7.27 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
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
209
210
211
212
213
214
###
Presentz.org - A website to publish presentations with video and slides synchronized.
Copyright (C) 2012 Federico Fissore
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) 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/>.
###
"use strict"
everyauth = require "everyauth"
merge_facebook_user_data = (user, ext_user) ->
user.name ?= ext_user.name
user.user_name ?= ext_user.username || ext_user.id
user.link ?= ext_user.link
user.email ?= ext_user.email
user.facebook_id ?= ext_user.id
merge_twitter_user_data = (user, ext_user) ->
user.name ?= ext_user.name || ext_user.screen_name
user.user_name ?= ext_user.screen_name
user.link ?= "https://twitter.com/#{ext_user.screen_name}"
user.twitter_id ?= ext_user.id
merge_google_user_data = (user, ext_user) ->
user.name ?= ext_user.name || ext_user.given_name
user.user_name ?= ext_user.id
user.link ?= ext_user.link
user.google_id ?= ext_user.id
merge_linkedin_user_data = (user, ext_user) ->
user.name ?= "#{ext_user.firstName} #{ext_user.lastName}"
user_name_parts = ext_user.publicProfileUrl.split("/")
user.user_name = user_name_parts[user_name_parts.length - 1]
user.link ?= ext_user.publicProfileUrl
user.linkedin_id ?= ext_user.id
merge_github_user_data = (user, ext_user) ->
user.name ?= ext_user.name or ext_user.login
user.user_name ?= ext_user.login
user.link ?= ext_user.html_url
user.email ?= ext_user.email
user.github_id ?= ext_user.login
merge_foursquare_user_data = (user, ext_user) ->
user.name ?= "#{ext_user.firstName} #{ext_user.lastName}"
user.user_name ?= ext_user.id
user.email ?= ext_user.contact.email
user.foursquare_id ?= ext_user.id
create_or_update_user = (db, results, session, user_data, merge_function, promise) ->
save_callback = (err, user) ->
return promise.fail(err) if err?
promise.fulfill
id: user["@rid"]
if session.auth? and session.auth.userId?
rid = session.auth.userId
else if results.length > 0
rid = results[0].rid
if rid?
db.loadRecord rid, (err, user) ->
return promise.fail(err) if err?
merge_function user, user_data
db.save user, save_callback
else
user =
_type: "user"
merge_function user, user_data
db.createVertex user, (err, user) ->
db.createEdge "#6:0", user, { label: "user" }, (err) ->
save_callback(err, user)
find_or_create_user = (db, query_tmpl, merge_function) ->
return (session, accessToken, accessTokenExtra, user_data) ->
promise = @Promise()
db.command "#{query_tmpl}'#{user_data.id}'", (err, results) ->
return promise.fail(err) if err?
create_or_update_user db, results, session, user_data, merge_function, promise
return promise
handleAuthCallbackError = (req, res) ->
res.redirect 302, "/?access_denied"
authCallbackDidErr = (req, res) ->
return req.query? and (req.query.error? and req.query.error is "access_denied") or (req.query.access_denied?)
facebook_init = (config, db) ->
everyauth.facebook.configure
appId: config.auth.facebook.app_id
appSecret: config.auth.facebook.app_secret
scope: "email"
myHostname: config.hostname
findOrCreateUser: find_or_create_user(db, "SELECT @rid FROM V where _type = 'user' and facebook_id = ", merge_facebook_user_data)
redirectPath: "/r/back_to_referer"
handleAuthCallbackError: handleAuthCallbackError
twitter_init = (config, db) ->
everyauth.twitter.configure
consumerKey: config.auth.twitter.consumer_key
consumerSecret: config.auth.twitter.consumer_secret
myHostname: config.hostname
findOrCreateUser: find_or_create_user(db, "SELECT @rid FROM V where _type = 'user' and twitter_id = ", merge_twitter_user_data)
redirectPath: "/r/back_to_referer"
handleAuthCallbackError: handleAuthCallbackError
google_init = (config, db) ->
everyauth.google.configure
appId: config.auth.google.app_id
appSecret: config.auth.google.app_secret
scope: "https://www.googleapis.com/auth/userinfo.profile"
myHostname: config.hostname
findOrCreateUser: find_or_create_user(db, "SELECT @rid FROM V where _type = 'user' and google_id = ", merge_google_user_data)
redirectPath: "/r/back_to_referer"
handleAuthCallbackError: handleAuthCallbackError
linkedin_init = (config, db) ->
everyauth.linkedin.configure
consumerKey: config.auth.linkedin.consumer_key
consumerSecret: config.auth.linkedin.consumer_secret
myHostname: config.hostname
findOrCreateUser: find_or_create_user(db, "SELECT @rid FROM V where _type = 'user' and linkedin_id = ", merge_linkedin_user_data)
redirectPath: "/r/back_to_referer"
handleAuthCallbackError: handleAuthCallbackError
github_init = (config, db) ->
everyauth.github.configure
appId: config.auth.github.app_id
appSecret: config.auth.github.app_secret
myHostname: config.hostname
findOrCreateUser: find_or_create_user(db, "SELECT @rid FROM V where _type = 'user' and github_id = ", merge_github_user_data)
callbackPath: "/auth/github/callback"
redirectPath: "/r/back_to_referer"
handleAuthCallbackError: handleAuthCallbackError
authCallbackDidErr: authCallbackDidErr
foursquare_init = (config, db) ->
everyauth.foursquare.configure
appId: config.auth.foursquare.app_id
appSecret: config.auth.foursquare.app_secret
myHostname: config.hostname
findOrCreateUser: find_or_create_user(db, "SELECT @rid FROM V where _type = 'user' and foursquare_id = ", merge_foursquare_user_data)
redirectPath: "/r/back_to_referer"
handleAuthCallbackError: handleAuthCallbackError
authCallbackDidErr: authCallbackDidErr
init = (config, db) ->
everyauth.everymodule.findUserById (userId, callback) ->
db.loadRecord userId, callback
everyauth.everymodule.logoutPath "/byebye"
facebook_init config, db
twitter_init config, db
google_init config, db
linkedin_init config, db
github_init config, db
foursquare_init config, db
return everyauth
socials_prefixes =
facebook:
col: "facebook_id"
prefix: "fb"
twitter:
col: "twitter_id"
prefix: "tw"
google:
col: "google_id"
prefix: "go"
linkedin:
col: "linkedin_id"
prefix: "in"
github:
col: "github_id"
prefix: "gh"
foursquare:
col: "foursquare_id"
prefix: "fs"
social_column_from_prefix = (prefix, callback) ->
for key, value of socials_prefixes
return callback(undefined, value.col) if value.prefix is prefix
callback(new Error("Unknon social prefix: #{prefix}"))
put_user_in_locals = (req, res, next) ->
if req.user?
user = req.user
for key, value of socials_prefixes
user.catalog = "/u/#{value.prefix}/#{user.user_name}" if user[value.col]?
res.locals.user = user
next()
exports.init = init
exports.put_user_in_locals = put_user_in_locals
exports.socials_prefixes = socials_prefixes
exports.social_column_from_prefix = social_column_from_prefix