Skip to content

Commit eddef4f

Browse files
Do not create a Solid account folder if OIDC user creation fails
Fixes issue #387. Also adds some doc strings.
1 parent 352d75b commit eddef4f

1 file changed

Lines changed: 74 additions & 0 deletions

File tree

‎lib/identity-provider.js‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,71 @@ IdentityProvider.prototype.getGraph = function (uri, callback) {
546546
*/
547547
IdentityProvider.prototype.post = function post (req, res, next) {
548548
if (!req.body) { // This is unlikely to ever be true, express sets body={}
549+
/**
550+
* Sends a request to the OIDC Provider's Users API endpoint, to create a new
551+
* user record with the provider.
552+
* @method createOIDCUser
553+
* @param trustedClient {OidcRpClient} Local/trusted OIDC client
554+
* @param webId {String} WebID URL of the new user to be created
555+
* @param options {Object} User options hashmap
556+
* @param options.password {String} User's signin password. NOTE: Must be 8+
557+
* characters, mix of alpha and numeric
558+
* @param [options.email] {String} User's email address (for recovery etc)
559+
* @param [options.name] {String} User's name
560+
* @return {Promise}
561+
*/
562+
IdentityProvider.prototype.createOIDCUser =
563+
function createOIDCUser (trustedClient, webId, options = {}) {
564+
if (!trustedClient) {
565+
let error = new Error('No OIDC Trusted client configured')
566+
error.status = 500
567+
return Promise.reject(error)
568+
}
569+
if (!webId) {
570+
let error = new Error('No WebID Url provided')
571+
error.status = 400
572+
return Promise.reject(error)
573+
}
574+
if (!options.password) {
575+
let error = new Error('No password provided')
576+
error.status = 400
577+
return Promise.reject(error)
578+
}
579+
var userData = {
580+
_id: webId,
581+
email: options.email,
582+
profile: webId,
583+
name: options.name,
584+
password: options.password
585+
}
586+
return trustedClient.client
587+
.token({
588+
grant_type: 'client_credentials',
589+
scope: 'realm'
590+
})
591+
.then((tokenResponse) => {
592+
let createOptions = { token: tokenResponse.access_token }
593+
return trustedClient.client.users.create(userData, createOptions)
594+
})
595+
}
596+
597+
/**
598+
* Handles POST requests to /api/accounts/new, creates a new user account.
599+
* @param req
600+
* @param res
601+
* @param next
602+
* @param [req.body] {Object} User options submitted by signup app or API
603+
* @param [req.body.name] {String} User's name
604+
* @param [req.body.email] {String} User's email address (for recovery etc)
605+
* @param [req.body.password] {String} User's signin password (used only for
606+
* authentication methods that need it, like OIDC)
607+
* @param [req.body.url] {String} User account URL (`username.databox.com`)
608+
* @param [req.body.username] {String} Username, passed through to `agent()` and
609+
* used in WebID URL creation if the `url` parameter is missing.
610+
* @method post
611+
*/
612+
IdentityProvider.prototype.post = function post (req, res, next) {
613+
if (!req.body) {
549614
debug('Options missing')
550615
var err = new Error('Settings to create the account have not been passed!')
551616
err.status = 400
@@ -562,6 +627,15 @@ IdentityProvider.prototype.post = function post (req, res, next) {
562627
debug('Create account with settings ', options)
563628

564629
waterfall([
630+
(callback) => {
631+
if (this.auth !== 'oidc') {
632+
return callback() // Skip this step, auth not using oidc
633+
}
634+
const trustedClient = req.app.locals.oidc
635+
return this.createOIDCUser(trustedClient, agent, options)
636+
.then(() => callback())
637+
.catch((err) => { callback(err) })
638+
},
565639
(callback) => {
566640
// Generate a new WebID-TLS certificate, if appropriate
567641
if (!(this.auth === 'tls' && options.spkac && options.spkac.length > 0)) {

0 commit comments

Comments
 (0)