@@ -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+ */
528534IdentityProvider . 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
0 commit comments