Skip to content

Commit e79824b

Browse files
Move createOIDCUser to oidc-rp-client (from identity-provider)
1 parent 3ed3e11 commit e79824b

File tree

2 files changed

+55
-57
lines changed

2 files changed

+55
-57
lines changed

lib/identity-provider.js

Lines changed: 7 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -531,62 +531,6 @@ IdentityProvider.prototype.getGraph = function (uri, callback) {
531531
})
532532
}
533533

534-
/**
535-
* Sends a request to the OIDC Provider's Users API endpoint, to create a new
536-
* user record with the provider.
537-
* @method createOIDCUser
538-
* @param oidcRpClient {OidcRpClient} Local/trusted OIDC client
539-
* @param webId {String} WebID URL of the new user to be created
540-
* @param options {Object} User options hashmap
541-
* @param options.password {String} User's signin password. NOTE: Must be 8+
542-
* characters, mix of alpha and numeric
543-
* @param [options.email] {String} User's email address (for recovery etc)
544-
* @param [options.name] {String} User's name
545-
* @throws {Error} HTTP 400 on missing required params, and HTTP 500 if no
546-
* oidcRpClient has been configured.
547-
* @return {Promise}
548-
*/
549-
IdentityProvider.prototype.createOIDCUser =
550-
function createOIDCUser (oidcRpClient, webId, options = {}) {
551-
if (!oidcRpClient) {
552-
let error = new Error('No OIDC RP client configured')
553-
error.status = 500
554-
return Promise.reject(error)
555-
}
556-
if (!webId) {
557-
let error = new Error('No WebID Url provided')
558-
error.status = 400
559-
return Promise.reject(error)
560-
}
561-
if (!options.password) {
562-
let error = new Error('No password provided')
563-
error.status = 400
564-
return Promise.reject(error)
565-
}
566-
var userData = {
567-
_id: webId,
568-
email: options.email,
569-
profile: webId,
570-
name: options.name,
571-
password: options.password
572-
}
573-
var client = oidcRpClient.trustedClient.client
574-
return client
575-
.token({
576-
grant_type: 'client_credentials',
577-
scope: 'realm'
578-
})
579-
.then((tokenResponse) => {
580-
let createOptions = { token: tokenResponse.access_token }
581-
return client.users.create(userData, createOptions)
582-
})
583-
.catch((err) => {
584-
err.status = err.status || err.statusCode || 400
585-
err.message = err.error.message || err.error.error
586-
return Promise.reject(err)
587-
})
588-
}
589-
590534
/**
591535
* Handles POST requests to /api/accounts/new, creates a new user account.
592536
* @param req
@@ -600,6 +544,7 @@ IdentityProvider.prototype.createOIDCUser =
600544
* @param [req.body.url] {String} User account URL (`username.databox.com`)
601545
* @param [req.body.username] {String} Username, passed through to `agent()` and
602546
* used in WebID URL creation if the `url` parameter is missing.
547+
* @throws {Error} HTTP 400 on missing required params, HTTP 500 on misc errors
603548
* @method post
604549
*/
605550
IdentityProvider.prototype.post = function post (req, res, next) {
@@ -625,7 +570,12 @@ IdentityProvider.prototype.post = function post (req, res, next) {
625570
return callback()
626571
}
627572
const oidcRpClient = req.app.locals.oidc
628-
return this.createOIDCUser(oidcRpClient, agent, options)
573+
if (!oidcRpClient) {
574+
let error = new Error('No OIDC RP client configured')
575+
error.status = 500
576+
return callback(error)
577+
}
578+
return oidcRpClient.createOIDCUser(agent, options)
629579
.then(() => callback())
630580
.catch((err) => {
631581
callback(err)

lib/oidc-rp-client.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,54 @@ module.exports = class OidcRpClient {
6767
})
6868
}
6969

70+
/**
71+
* Sends a request to the OIDC Provider's Users API endpoint, to create a new
72+
* user record with the provider.
73+
* @method createOIDCUser
74+
* @param webId {String} WebID URL of the new user to be created
75+
* @param options {Object} User options hashmap
76+
* @param options.password {String} User's signin password. NOTE: Must be 8+
77+
* characters, mix of alpha and numeric
78+
* @param [options.email] {String} User's email address (for recovery etc)
79+
* @param [options.name] {String} User's name
80+
* @throws {Error} HTTP 400 on missing required params.
81+
* @return {Promise}
82+
*/
83+
createOIDCUser (webId, options = {}) {
84+
if (!webId) {
85+
let error = new Error('No WebID Url provided')
86+
error.status = 400
87+
return Promise.reject(error)
88+
}
89+
if (!options.password) {
90+
let error = new Error('No password provided')
91+
error.status = 400
92+
return Promise.reject(error)
93+
}
94+
var userData = {
95+
_id: webId,
96+
email: options.email,
97+
profile: webId,
98+
name: options.name,
99+
password: options.password
100+
}
101+
var client = this.trustedClient.client
102+
return client
103+
.token({
104+
grant_type: 'client_credentials',
105+
scope: 'realm'
106+
})
107+
.then((tokenResponse) => {
108+
let createOptions = { token: tokenResponse.access_token }
109+
return client.users.create(userData, createOptions)
110+
})
111+
.catch((err) => {
112+
err.status = err.status || err.statusCode || 400
113+
err.message = err.error.message || err.error.error
114+
return Promise.reject(err)
115+
})
116+
}
117+
70118
clientForIssuer (issuer) {
71119
var trustedClient = this.trustedClient.client
72120
var baseRedirectUri = trustedClient.redirect_uri

0 commit comments

Comments
 (0)