Skip to content

Instantly share code, notes, and snippets.

@turingmachine
Created November 26, 2019 16:42
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 turingmachine/9f1eabbb83fdc0f35c26bc7d9953343d to your computer and use it in GitHub Desktop.
Save turingmachine/9f1eabbb83fdc0f35c26bc7d9953343d to your computer and use it in GitHub Desktop.
const openidRelyingParty = new openid.RelyingParty(
`${config.baseurl}/verify`,
config.baseurl,
true, // Use stateless verification
false, // Strict mode
[
new openid.AttributeExchange({
'https://login.migros.ch/ax/legal/TNB_MYMIGROS': 'required',
'https://login.migros.ch/ax/contact/email': 'optional',
'https://login.migros.ch/ax/person/guid': 'required',
'https://login.migros.ch/ax/person/gender': 'required',
'https://login.migros.ch/ax/namePerson/first': 'required',
'https://login.migros.ch/ax/namePerson/last': 'required',
'https://login.migros.ch/ax/contact/postalAddress/home': 'optional',
'https://login.migros.ch/ax/contact/postalCode/home': 'optional',
'https://login.migros.ch/ax/contact/mconnect/postalAddressAddOn/home':
'optional',
'https://login.migros.ch/ax/contact/city/home': 'optional',
'https://login.migros.ch/ax/contact/phone/cell': 'required',
}),
]
)
app.get('/login', async (req, res) => {
await openidRelyingParty.authenticate(
config.openidIdentifier,
false,
async (error, authUrl) => {
if (error) {
Raven.captureException(error)
res.send('Authentication failed: ' + error.message)
} else if (!authUrl) {
res.send('Authentication failed')
} else {
await new Promise(resolve => req.session.save(() => resolve()))
res.redirect(302, authUrl)
}
}
)
})
app.post('/verify', async (req, res) => {
await openidRelyingParty.verifyAssertion(req, async (error, result) => {
if (error || !result.authenticated) {
console.log(error)
Raven.captureException(error)
req.session.authenticated = false
await new Promise(resolve => req.session.save(() => resolve()))
return res.send(
`Failure : <pre>${JSON.stringify(error, null, 2)}</pre>`
)
}
const [user, created] = await models.Users.findOrCreate({
where: {
MConnectGUID: result['https://login.migros.ch/ax/person/guid'],
},
})
await user.update({
gender: result['https://login.migros.ch/ax/person/gender'],
firstName: result['https://login.migros.ch/ax/namePerson/first'],
lastName: result['https://login.migros.ch/ax/namePerson/last'],
email: result['https://login.migros.ch/ax/contact/email'],
mobile: result['https://login.migros.ch/ax/contact/phone/cell'],
testUser,
})
await user.reload()
req.session.authenticated = true
await new Promise(resolve => req.session.save(() => resolve()))
res.redirect('/shop')
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment