Skip to content

Commit 87b046c

Browse files
WIP (create account using webid as OIDC _id)
1 parent 3eedd67 commit 87b046c

4 files changed

Lines changed: 61 additions & 27 deletions

File tree

lib/capability-discovery.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ const serviceConfigDefaults = {
1111
'accounts': {
1212
// 'changePassword': '/api/account/changePassword',
1313
// 'delete': '/api/accounts/delete',
14+
15+
// Create new user (see IdentityProvider.post() in identity-provider.js)
1416
'new': '/api/accounts/new',
1517
'recover': '/api/accounts/recover',
1618
'signin': '/api/accounts/signin',

lib/handlers/oidc.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,28 @@ module.exports.oidcIssuerHeader = oidcIssuerHeader
2020
* Usage:
2121
*
2222
* ```
23-
* app.use('/api/oidc', oidcHandler.api(corsSettings))
23+
* app.use('/api/oidc', oidcHandler.api(corsSettings, oidcRpClient))
2424
* ```
2525
* @param corsSettings
26-
* @returns {Router} Express router
26+
* @param oidcRpClient
27+
* @return {Router} Express router
2728
*/
2829
function api (corsSettings, oidcRpClient) {
2930
const router = express.Router('/')
3031

3132
if (corsSettings) {
3233
router.use(corsSettings)
3334
}
34-
35-
// router.post('/signin', bodyParser.urlencoded({extended: false}),
36-
// (req, res, next) => {
37-
// // const userServer = req.body.oidcServer
38-
// })
39-
router.get('/rp', authCallback(oidcRpClient), authSessionInit, rpCallback(oidcRpClient))
40-
// router.get('/signout', (req, res, next) => {
41-
// req.session.userId = null
42-
// req.session.identified = false
43-
// res.send('signed out...')
44-
// })
35+
// The /rp (relying party) callback is called at the end of the OIDC signin
36+
// process
37+
router.get('/rp',
38+
// Authenticate the RP callback (exchange code for id token)
39+
authCallback(oidcRpClient),
40+
// Start up the session
41+
authSessionInit,
42+
// Redirect the user back to returnToUrl (that they were requesting before
43+
// being forced to sign in)
44+
rpCallback(oidcRpClient))
4545

4646
return router
4747
}
@@ -95,7 +95,6 @@ function authenticate (oidcRpClient) {
9595
* @param req
9696
* @param res
9797
* @param next
98-
* @returns {*}
9998
*/
10099
function authSessionInit (req, res, next) {
101100
if (!req.userInfo) {

lib/identity-provider.js

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -524,14 +524,41 @@ IdentityProvider.prototype.getGraph = function (uri, callback) {
524524
})
525525
}
526526

527-
// Handle POST requests on account creation
527+
/**
528+
* Handles new account creation (in multi-user mode).
529+
* Currently mounted on /api/accounts/new endpoint.
530+
* @param req
531+
* @param res
532+
* @param next
533+
*/
528534
IdentityProvider.prototype.post = function (req, res, next) {
529-
if (!req.body) {
535+
const idpMode = req.app.locals.ldp.idp
536+
if (!req.body) { // This is unlikely to ever be true, express sets body={}
530537
debug('Options missing')
531-
var err = new Error('Settings to create the account have not been passed!')
532-
err.status = 406
538+
let err = new Error('Settings to create the account have not been passed!')
539+
err.status = 400
533540
return next(err)
534541
}
542+
// Username is required only in MultiUser / idp mode (for both TLS and OIDC)
543+
if (idpMode && !req.body.username) {
544+
debug('Username missing (IDP mode)')
545+
let err = new Error('Username required')
546+
err.status = 400
547+
return next(err)
548+
}
549+
if (this.auth === 'oidc') {
550+
// OIDC auth required username & pass in both single-user and IDP modes
551+
if (!req.body.username) {
552+
let err = new Error('Username required')
553+
err.status = 400
554+
return next(err)
555+
}
556+
if (!req.body.password) {
557+
let err = new Error('Password required')
558+
err.status = 400
559+
return next(err)
560+
}
561+
}
535562

536563
var self = this
537564
var email = req.app.locals.email
@@ -554,24 +581,30 @@ IdentityProvider.prototype.post = function (req, res, next) {
554581
return callback()
555582
}
556583
let trustedClient = oidcRpClient.trustedClient
557-
return trustedClient.client.token({
558-
grant_type: 'client_credentials',
559-
scope: 'realm'
560-
})
584+
return trustedClient.client
585+
.token({
586+
grant_type: 'client_credentials',
587+
scope: 'realm'
588+
})
561589
.then((tokenResponse) => {
562-
let token = { token: tokenResponse.access_token }
590+
let createOptions = { token: tokenResponse.access_token }
563591
// NOTE: Password must be 8+ chars, mix alpha and numbers
564592
let userData = {
593+
_id: agent,
565594
email: options.email,
566595
profile: agent, // WebID URL
567596
name: options.name,
568597
password: options.password // || 'swordfish123'
569598
}
570-
return trustedClient.client.users
571-
.create(userData, token)
599+
return trustedClient.client.users.create(userData, createOptions)
572600
})
573601
.then(() => callback())
574-
.catch(callback)
602+
.catch((err) => {
603+
debug('Error creating user: ' + err)
604+
let error = new Error('Error creating user on OIDC provider: ' + err)
605+
error.status = 400
606+
return callback(error)
607+
})
575608
},
576609
(callback) => {
577610
// Auth == TLS section only

lib/oidc-rp-client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ module.exports = class OidcRpClient {
119119
/**
120120
* Returns the Signin page URL for the trusted OIDC provider
121121
* @param oidcExpress {OIDCExpressClient}
122-
* @returns {String}
122+
* @return {String}
123123
*/
124124
urlForSignin (oidcExpress) {
125125
// return 'https://anvil.local/authorize?stuff'

0 commit comments

Comments
 (0)