The accounts module manages user accounts on the Weble IoT Universal Gateway: creation, listing, modification, deletion, password management, and authentication.
Load it with:
peer('supervision').require('accounts', function(err, accounts){
// accounts.getAccounts(...), accounts.insertAccount(...), accounts.authenticate(...)
})
Results never contain the password hash (password). They do contain the account salt.
getAccounts(callback)Return all configured user accounts as an array.
accounts.getAccounts(function(err, list){
list.forEach(function(a){
console.log(a.id, a.username, a.administrator ? 'admin' : 'user')
})
})
getAccountById(id, callback)Return a single account by id.
insertAccount(account, callback)Create a new account. Pass an object or an array of objects to insert several at once.
accounts.insertAccount({
id : 'jdoe',
username : 'jdoe',
password : 'initialPassword!',
name : 'Jane Doe',
administrator : false,
defaultVisu : 'myInstallation'
}, function(err, created){
if(err) return console.error(err)
console.log('created', created.id)
})
Required fields: username, password. Common optional fields:
| Field | Description |
|---|---|
id |
Account identifier (defaults to a generated value if omitted) |
name |
Display name |
administrator |
true for admin accounts (full access) |
defaultVisu |
Name of the visualization installation opened after login (Default Visu in the users table); several names are separated by commas. Required to sign in to the visualization with a local account: see Visualization › Sign in. |
redirect |
Page opened after login instead of the administration (Redirect in the users table), for example a dashboard at /dashboards/<id>: see Dashboards. Takes precedence over defaultVisu. |
allowDeviceLogin |
Allow login via device UUID instead of password |
salt |
Only with a pre-hashed password: password must then be RIPEMD160(clear password + salt) in lowercase hex (40 characters). With a clear password, omit it: the gateway generates the salt. |
role |
Id of the role that sets the rights of the account. Give a role to every account that is not an administrator, see Roles. |
updateAccount(account, callback)Modify an existing account. Pass an object containing the id and the fields to update — only those fields are touched.
accounts.updateAccount({
id : 'jdoe',
name : 'Jane M. Doe'
}, function(err, updated){
if(err) console.error(err)
})
Pass an array to update several accounts at once.
updateAccountPassword(obj, callback)Change an account's password. The password is hashed and a fresh salt is generated.
accounts.updateAccountPassword({
id : 'jdoe',
password : 'newSecretPassword!'
}, function(err){
if(err) console.error(err)
})
deleteAccount(arr, callback)Permanently remove one or several accounts. Pass an id, an account object, or an array of ids/objects.
accounts.deleteAccount('jdoe', function(err, removed){
if(err) console.error(err)
})
If you delete the default admin account, the gateway immediately recreates it with its factory password (printed on the label, or admin on older gateways and on Windows).
authenticate(username, password, dynamicSalt, callback)Verify credentials against the account store. The callback returns the account object on success or 'UNKNOWN_ACCOUNT' on failure.
accounts.authenticate('jdoe', 'secret', null, function(err, account){
if(err) return console.error('login failed:', err)
console.log('logged in as', account.username, '(admin:', account.administrator + ')')
})
Pass the password as it is. MD5 hashes only work on old accounts without a salt. This is the same primitive used internally by POST /auth.
getAccountSalt(username, callback)Return the static salt of an account by username — used by clients that want to pre-hash a password before sending it.
mustChangePassword(id, callback)Returns true when the account is the admin account and its password is still admin. In that case the interface forces a password change at login. The callback is (err, mustChange, info): info.factory is true when the admin account still uses the factory password printed on the device label.
apiKey, authenticate(apiKey, anyPassword, null, cb) logs in as that account. Keep API keys secret.allowDeviceLogin: true, or when that device entry has allowDeviceLogin: true (see section 4).The user names weble, support and maintenance are reserved (Account name not allowed).
| Error | Cause |
|---|---|
USERNAME_MISSING / USERNAME_EXISTS |
no user name, or name already used |
MISSING_PASSWORD |
insertAccount without password |
PASSWORD_MISSING |
updateAccountPassword without password |
PASSWORD_DISMATCH |
password and rpassword differ |
UNKNOWN_ACCOUNT |
wrong credentials, or no account given |
ACCOUNT_SELF_ROLE_CHANGE, ROLE_ESCALATION, LAST_ADMIN |
from a web session: you cannot change your own role, give a role with more rights than yours, or remove the administrator flag from your own account |
If an account has allowDeviceLogin: true, mobile devices can authenticate by registering their UUID with the account. The system then accepts the UUID as a substitute for the password — useful for paired mobile apps that should not have to store the user's password.
setAccountDevice(id, device, callback)Register a device for an account. The device object must contain at least a uuid field.
accounts.setAccountDevice('jdoe', {
uuid : 'a1b2c3d4-...',
name : "Jane's iPhone"
}, function(err){
if(err) console.error(err)
})
Calling setAccountDevice for the same UUID on a different account moves the device — a UUID can only be paired with one account at a time.
| Event | Arguments | Description |
|---|---|---|
account inserted |
(account) |
New account created |
account updated |
(account) |
Account modified |
account <id> updated |
(account) |
Specific account modified |
account removed |
(account) |
Account deleted |
account <id> removed |
(account) |
Specific account deleted |
roleInserted / roleUpdated / roleDeleted |
(role) |
Role created, modified or deleted |
initialized |
none | Account store finished loading at startup |
To restrict what an account can do, give it a restricted role:
administrator: falseonly hides the administration pages. Always assign a role to non-administrator accounts.
Create the role first in Administration › Access (the role editor also grants the internal functions some methods need), then get its id with accounts.getRoles and assign it:
var ROLE_ID = '<id of the restricted role>' // from accounts.getRoles()
var users = ['alice', 'bob', 'carol'].map(function(name){
return { username: name, password: 'TempPass!42', administrator: false, role: ROLE_ID }
})
accounts.insertAccount(users, function(err){
if(err) console.error(err)
else console.log('created', users.length, 'restricted accounts')
})
A role with no inherit refuses everything it does not grant. Ask each user to change the temporary password at first login.
A role defines what an account may do. A role is { id, name, inherit, permissions }:
permissions: object whose keys are permission paths such as /peers/supervision/gateways/methods/writeValue and whose values are true (granted) or false (refused). Some methods run partly in the caller and call an internal twin (for example query calls queryPrivate): grant both keys, or use the role editor, which adds them automatically.inherit: '*' starts from all rights (useful to remove a few); omitted, everything not listed is refused. Only a super-administrator can create an all-rights role.insertRole always generates the id. updateRole merges permissions key by key: set a key to '__delete__' to remove it, or send { __replace__: { … } } to replace the whole set.Roles are edited in Administration › Access.
| Method | Description |
|---|---|
getRoles(callback) |
All roles, keyed by id |
getRoleById(id, callback) |
One role |
insertRole(role, callback) |
Create a role |
updateRole(role, callback) |
Modify a role |
removeRole(id, callback) |
Delete a role. Fails with 409 while an account uses it |