Created
October 16, 2012 06:29
-
-
Save boronine/3897532 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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