forked from apportable/meteor-discourse-sso
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sso.coffee
38 lines (26 loc) · 1.01 KB
/
sso.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
crypto = Npm.require 'crypto'
querystring = Npm.require 'querystring'
class @SingleSignOn extends Minimongoid
ACCESSORS = ['nonce', 'name', 'email', 'external_id']
@parse: (params, sso_secret) ->
sso = @init()
sso.sso_secret = sso_secret
throw new Error "Bad signature for payload" unless sso.sign(params.sso) == params.sig
decoded = (new Buffer params.sso, 'base64').toString()
decoded_hash = querystring.parse decoded
_.each ACCESSORS, (k) -> sso[k] = decoded_hash[k]
sso
sign: (payload) ->
crypto.createHmac('sha256', @sso_secret).update(payload).digest('hex')
to_url: (base_url) ->
"#{base_url}#{if base_url.match(/\?/) then '&' else '?'}#{@payload()}"
payload: ->
payload = new Buffer(@unsigned_payload()).toString('base64')
"sso=#{encodeURIComponent payload}&sig=#{@sign payload}"
unsigned_payload: ->
unsigned_payload = {}
_.each ACCESSORS, (k) ->
if val = @[k]
unsigned_payload[k] = val
, @
querystring.stringify unsigned_payload