forked from sahat/satellizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
satellizer.coffee
392 lines (310 loc) · 10.9 KB
/
satellizer.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#Global Vars
config =
logoutRedirect: '/'
loginRedirect: '/'
loginUrl: '/auth/login'
signupUrl: '/auth/signup'
signupRedirect: '/login'
loginRoute: '/login'
signupRoute: '/signup'
user: 'currentUser'
tokenName: 'satellizerToken'
providers =
google:
url: '/auth/google'
authorizationEndpoint: 'https://accounts.google.com/o/oauth2/auth'
redirectUri: window.location.origin
scope: 'openid profile email'
scopeDelimiter: ' '
requiredUrlParams: ['scope']
optionalUrlParams: ['display']
display: 'popup'
type: '2.0'
popupOptions:
width: 452
height: 633
facebook:
url: '/auth/facebook'
authorizationEndpoint: 'https://www.facebook.com/dialog/oauth'
redirectUri: window.location.origin + '/'
scope: 'email'
scopeDelimiter: ','
requiredUrlParams: ['display', 'scope']
display: 'popup'
type: '2.0'
popupOptions:
width: 481
height: 269
linkedin:
url: '/auth/linkedin'
authorizationEndpoint: 'https://www.linkedin.com/uas/oauth2/authorization'
redirectUri: window.location.origin
requiredUrlParams: ['state']
scope: []
scopeDelimiter: ' '
state: 'STATE'
type: '2.0'
popupOptions:
width: 527
height: 582
github:
name: 'github'
url: '/auth/github'
authorizationEndpoint: 'https://github.com/login/oauth/authorize'
redirectUri: window.location.origin
scope: []
scopeDelimiter: ' '
type: '2.0'
popupOptions:
width: 1020
height: 618
twitter:
url: '/auth/twitter'
type: '1.0'
class Satellizer extends Module
class $auth extends Provider
constructor: ($q) ->
config: config
providers: providers
facebook: (params) ->
angular.extend providers.facebook, params
google: (params) ->
angular.extend providers.facebook, params
linkedin: (params) ->
angular.extend providers.linkedin, params
github: (params) ->
angular.extend providers.github, params
twitter: (params) ->
angular.extend providers.twitter, params
oauthBase: (params) ->
providers[params.name] = providers[params.name] or {}
angular.extend providers[params.name], params
oauth1: (params) =>
@oauthBase(params)
providers[params.name].type = '1.0'
oauth2: (params) =>
@oauthBase(params)
providers[params.name].type = '2.0'
$get: ($q, Oauth1, Oauth2, Local) ->
$auth = {}
$auth.authenticate = (name) ->
deferred = $q.defer()
provider = if providers[name].type is 1.0 then Oauth1 else Oauth2
provider.open(provider[name])
.then (response) ->
Local.parseUser response.data.token, deferred
.catch (response) ->
deferred.reject response
return deferred.promise
$auth.login = (user) ->
Local.login user
$auth.signup = (user) ->
Local.signup user
$auth.logout = (user) ->
Local.logout()
$auth.isAuthenticated = () ->
Local.isAuthenticated()
return $auth
class Local extends Factory
constructor: ($q, $http, $rootScope, $location) ->
local = {}
local.parseUser = (token, deferred) ->
payload = JSON.parse(window.atob(token.split('.')[1]))
localStorage.setItem config.tokenName, token
$rootScope[config.user] = payload.user
$location.path config.loginRedirect
deferred.resolve payload.user
local.login = (user) ->
deferred = $q.defer()
$http.post config.loginUrl, user
.then (response) ->
local.parseUser response.data.token, deferred
.catch (response) ->
deferred.reject response
return deferred.promise
local.signup = (user) ->
deferred = $q.defer()
$http.post config.signupUrl, user
.then () ->
$location.path config.signupRedirect
deferred.resolve()
.catch (response) ->
deferred.reject response
return deferred.promise
local.logout = () ->
deferred = $q.defer()
delete $rootScope[config.user]
localStorage.removeItem config.tokenName
$location.path config.logoutRedirect
deferred.resolve()
return deferred.promise
local.isAuthenticated = () ->
return Boolean $rootScope.currentUser
return local
class Oauth2 extends Factory
constructor: ($q, $http, Utils, Popup) ->
defaults =
url: null
name: null
scope: null
scopeDelimiter: null
clientId: null
redirectUri: null
popupOptions: null
authorizationEndpoint: null
requiredUrlParams: null
optionalUrlParams: null
defaultUrlParams: ['response_type', 'client_id', 'redirect_uri']
responseType: 'code'
oauth2 = {}
oauth2.open = (options) ->
angular.extend defaults, options
deferred = $q.defer()
url = oauth2.buildUrl()
Popup.open url, defaults.popupOptions
.then (oauthData) ->
oauth2.exchangeForToken oauthData
.then (response) ->
deferred.resolve response
.catch (response) ->
deferred.reject response
.catch (error) ->
deferred.reject error
return deferred.promise
oauth2.exchangeForToken = (oauthData) ->
return $http.post defaults,url,
code: oauthData.code
clientId: defaults.clientId
redirectUri: defaults.redirectUri
oauth2.buildUrl = () ->
baseUrl = defaults.authorizationEndpoint
qs = oauth2.buildQueryString()
return [baseUrl, qs].join('?')
oauth2.buildQueryString = () ->
keyValuePairs = []
urlParams = ['defaultUrlParams', 'requiredUrlParams', 'optionalUrlParams']
angular.forEach urlParams, (params) ->
angular.forEach defaults[params], (paramName) ->
camelizedName = Utils.camelCase(paramName)
paramValue = defaults[camelizedName]
keyValuePairs.push [paramName, encodeURIComponent(paramValue)]
return keyValuePairs.map( (pair) ->
return pair.join('=')
).join('&')
return oauth2
class Oauth1 extends Factory
constructor: ($q, $http, Popup) ->
defaults =
url: null
name: null
popupOptions: null
oauth1 = {}
oauth1.open = (options) ->
angular.extend defaults, options
deferred = $q.defer()
Popup.open defaults.url
.then (oauthData) ->
oauth1.exchangeForToken(oauthData)
.then (response) ->
deferred.resolve(response.data)
return deferred.promise
oauth1.exchangeForToken = (oauthData) ->
oauthData = oauth1.buildQueryString oauthData
return $http.get(defaults.url + '?' + oauthData)
oauth1.buildQueryString = (obj) ->
str = []
angular.forEach obj, (value, key) ->
str.push encodeURIComponent(key) + '=' + encodeURIComponent(value)
return str.join('&')
return oauth1
class Popup extends factory
constructor: ($q, $interval, $window) ->
popupWindow = null
polling = null
popup = {}
popup.open = (url, options) ->
deferred = $q.defer()
optionsString = popup.stringifyOptions popup.prepareOptions(options or {})
popupWindow = $window.open url, '_blank', optionsString
popupWindow.focus()
popup.postMessageHandler deferred
popup.pollPopup deferred
return deferred.promise
popup.pollPopup = (deferred) ->
polling = $interval () ->
if popupWindow.closed
$interval.cancel polling
deferred.reject {data: 'Authorization Failed'}
, 35
popup.postMessageHandler = (deferred) ->
$window.addEventListener 'message', (event) ->
if event.origin is $window.location.origin
popupWindow.close()
if event.data.error
deferred.reject {data: event.data.error}
else
deferred.resolve event.data
popup.prepareOptions = (options) ->
width = options.width or 500
height = options.height or 500
return angular.extend
width: width
height: height
left: $window.screenX + (($window.outerWidth - width) / 2)
top: $window.screenY + (($window.outerHeight - height) / 2.5)
, options
popup.stringifyOptions = (options) ->
parts = []
angular.forEach options, (value, key) ->
parts.push key + '=' + value
return parts.join ','
return popup
class RunBlock extends Factory
constructor: ($rootScope, $window, $location, Utils) ->
return {
run: () ->
token = $window.localStorage[config.tokenName]
if token
payload = SON.parse($window.atob(token.split('.')[1]))
$rootScope[config.user] = payload.user
params = $window.location.search.substring(1)
qs = if Object.keys($location.search()).length then $location.search() else Utils.parseQueryString params
if $window.opener and $window.opener.location.origin is $window.location.origin
if qs.oauth_token and qs.oauth_verifier
$window.opener.postMessage({ oauth_token: qs.oauth_token, oauth_verifier: qs.oauth_verifier }, $window.location.origin)
else if qs.code
$window.opener.postMessage({ code: qs.code }, $window.location.origin)
else if qs.error
$window.opener.postMessage({ error: qs.error }, $window.location.origin)
}
class Utils extends Service
constructor: () ->
@camelCase = (name) ->
return name.replace /([\:\-\_]+(.))/g, (_, separator, letter, offset) ->
letter.toUpperCase() if offset
return letter
@parseQueryString = (keyValue) ->
obj = {}
angular.forEach (keyValue or '').split('&'), (keyValue) ->
if keyValue
value = keyValue.split '='
key = decodeURIComponent value[0]
obj[key] = if angular.isDefined value[1] then decodeURIComponent value[1] else true
return obj
class httpInterceptor extends config
constructor: ($httpProvider) ->
$httpProvider.interceptors.push ($q, $window, $location) ->
return {
request: (config) ->
if $window.localStorage[config.tokenName]
config.headers.Authorization = 'Bearer' + $window.lcalStorage[config.tokenName]
return config
responseError: (response) ->
if response.status is 401
delete $window.localStorage[config.tokenName]
$location.path config.loginRoute
return $q.reject response
}
class onRun extends Run
constructor: (RunBlock) ->
RunBlock.run()