Skip to content

Instantly share code, notes, and snippets.

@boronine
Created October 16, 2012 06:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boronine/3897532 to your computer and use it in GitHub Desktop.
Save boronine/3897532 to your computer and use it in GitHub Desktop.
app.post '/api/verify', (req, res) ->
fail = ->
res.json status: 'failure'
if not req.body?
fail()
opts = {
host: "https://verifier.login.persona.org"
path: "/verify"
method: 'POST'
regectUnauthorized: true
}
vreq = https.request opts, (verifier) ->
body = ""
verifier.on 'error', fail
verifier.on 'data', (chunk) -> body += chunk
verifier.on 'end', ->
try
response = JSON.parse body
valid = response? and response.valid
email = response.email
catch error
fail()
if response.status != 'okay' or not req.session?
fail()
req.session.email = response.email
res.json status: 'okay'
# SSL validation can fail
vreq.on 'error', fail
vreq.setHeader "Content-Type", "application/json"
data = JSON.stringify
assertion: req.body.assertion,
audience: 'http://localhost:3000/'
vreq.setHeader "Content-Length", data.length
vreq.end data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment