@@ -11,6 +11,7 @@ const AccountTemplate = require('./account-template')
1111const debug = require ( './../debug' ) . accounts
1212
1313const DEFAULT_PROFILE_CONTENT_TYPE = 'text/turtle'
14+ const DEFAULT_ADMIN_USERNAME = 'admin'
1415
1516/**
1617 * Manages account creation (determining whether accounts exist, creating
@@ -192,7 +193,8 @@ class AccountManager {
192193 * @param [accountName] {string}
193194 *
194195 * @throws {Error } via accountUriFor()
195- * @return {string }
196+ *
197+ * @return {string|null }
196198 */
197199 accountWebIdFor ( accountName ) {
198200 let accountUri = this . accountUriFor ( accountName )
@@ -343,12 +345,32 @@ class AccountManager {
343345 username : userData . username ,
344346 email : userData . email ,
345347 name : userData . name ,
346- webId : userData . webid || this . accountWebIdFor ( userData . username )
348+ webId : userData . webid || userData . webId ||
349+ this . accountWebIdFor ( userData . username )
350+ }
351+
352+ if ( userConfig . webId && ! userConfig . username ) {
353+ userConfig . username = this . usernameFromWebId ( userConfig . webId )
354+ }
355+
356+ if ( ! userConfig . webId && ! userConfig . username ) {
357+ throw new Error ( 'Username or web id is required' )
347358 }
348359
349360 return UserAccount . from ( userConfig )
350361 }
351362
363+ usernameFromWebId ( webId ) {
364+ if ( ! this . multiUser ) {
365+ return DEFAULT_ADMIN_USERNAME
366+ }
367+
368+ let profileUrl = url . parse ( webId )
369+ let hostname = profileUrl . hostname
370+
371+ return hostname . split ( '.' ) [ 0 ]
372+ }
373+
352374 /**
353375 * Creates a user account storage folder (from a default account template).
354376 *
@@ -382,6 +404,27 @@ class AccountManager {
382404 return this . tokenService . generate ( { webId : userAccount . webId } )
383405 }
384406
407+ /**
408+ * Validates that a token exists and is not expired, and returns the saved
409+ * token contents, or throws an error if invalid.
410+ * Does not consume / clear the token.
411+ *
412+ * @param token {string}
413+ *
414+ * @throws {Error } If missing or invalid token
415+ *
416+ * @return {Object } Saved token data object
417+ */
418+ validateResetToken ( token ) {
419+ let tokenValue = this . tokenService . verify ( token )
420+
421+ if ( ! tokenValue ) {
422+ throw new Error ( 'Invalid or expired reset token' )
423+ }
424+
425+ return tokenValue
426+ }
427+
385428 /**
386429 * Returns a password reset URL (to be emailed to the user upon request)
387430 *
@@ -392,8 +435,11 @@ class AccountManager {
392435 */
393436 passwordResetUrl ( token , returnToUrl ) {
394437 let resetUrl = url . resolve ( this . host . serverUri ,
395- `/account/password/change?token=${ token } ` +
396- `&returnToUrl=${ returnToUrl } ` )
438+ `/account/password/change?token=${ token } ` )
439+
440+ if ( returnToUrl ) {
441+ resetUrl += `&returnToUrl=${ returnToUrl } `
442+ }
397443
398444 return resetUrl
399445 }
0 commit comments