Auth library
Contents
Manage principals and authentication
| If, while using this API, you are getting authentication errors like "Access denied to user [unknown]" it means that user role in the current context lacks sufficient permissions (for example when executed from inside a service). In this case you must explicitly execute the function in the context of System Administrator role. |
Usage
Add the following to your build.gradle file:
dependencies {
include xplibs.auth
}
Add the import statement to your code:
import authLib from '/lib/xp/auth';
You are now ready to use the API.
Functions
addMembers
Adds members to a principal (group or role).
Parameters
Takes the principalKey (a GroupKey or RoleKey) of the group or role to add members to, and an array of members — the UserKey or GroupKey values to add.
Returns
void
Example
import {addMembers} from '/lib/xp/auth';
addMembers('role:roleId', ['user:system:user1', 'group:system:group-a']);
changePassword
Changes or clears the password for the specified user.
Parameters
changePassword() takes a single ChangePasswordParams object with these properties:
| Name | Type | Description |
|---|---|---|
|
userKey |
string |
Key of the user to change password for. |
|
password |
string |
Optional. The new password to set. If |
Returns
void
Example
import {getUser, changePassword} from '/lib/xp/auth';
const user = getUser();
if (user) {
changePassword({
userKey: user.key,
password: 'new-secret-password'
});
}
createGroup
Creates a group.
Parameters
createGroup() takes a single CreateGroupParams object with these properties:
| Name | Type | Description |
|---|---|---|
|
idProvider |
string |
Key of the id provider where the group will be created. |
|
name |
string |
Group name. |
|
displayName |
string |
Optional. Group display name. |
|
description |
string |
Optional. Group description. |
Returns
object : (Group) The created group.
Example
import {createGroup} from '/lib/xp/auth';
const group = createGroup({
idProvider: 'myIdProvider',
name: 'group-a',
displayName: 'Group A',
description: "description"
});
({
type: "group",
key: "group:system:group-a",
displayName: "Group A",
description: "description"
})
createIdProvider
Creates an ID provider.
Parameters
createIdProvider() takes a single CreateIdProviderParams object with these properties:
| Name | Type | Description |
|---|---|---|
|
key |
string |
ID provider key. |
|
displayName |
string |
ID provider display name. |
|
description |
string |
Optional. ID provider description. |
|
idProviderConfig |
Optional. Binds this provider to one application. Without it the provider is incomplete and login plumbing won’t resolve back to an app. |
|
|
permissions |
Optional. ID provider permissions. |
Returns
object : (IdProvider) The created ID provider.
Example
import {createIdProvider} from '/lib/xp/auth';
const idProvider = createIdProvider({
key: 'myIdProvider',
displayName: 'My ID Provider',
description: 'My ID provider description',
idProviderConfig: {
applicationKey: 'com.example.myidprovider',
config: {
// app-specific configuration
}
},
permissions: [
{
principal: 'role:system.admin',
access: 'ADMINISTRATOR'
}
]
});
({
key: "myIdProvider",
displayName: "My ID Provider",
description: "My ID provider description",
idProviderConfig: {
applicationKey: "com.example.myidprovider",
config: {
// app-specific configuration
}
}
})
createRole
Creates a role.
Parameters
createRole() takes a single CreateRoleParams object with these properties:
| Name | Type | Description |
|---|---|---|
|
name |
string |
The name of the role. The key of the role will be |
|
displayName |
string |
Optional. Role display name. |
|
description |
string |
Optional. Role description. |
Returns
object : (Role) The created role.
Example
import {createRole} from '/lib/xp/auth';
const role = createRole({
name: 'aRole',
displayName: 'Role Display Name',
description: 'description'
});
({
type: "role",
key: "role:aRole",
displayName: "Role Display Name",
description: "description"
})
createUser
Creates a user.
Parameters
createUser() takes a single CreateUserParams object with these properties:
| Name | Type | Description |
|---|---|---|
|
idProvider |
string |
Key of the id provider where the user will be created. |
|
name |
string |
User login name. |
|
displayName |
string |
Optional. User display name. |
|
|
string |
Optional. User e-mail. |
Returns
object : (User) The created user.
Example
import {createUser} from '/lib/xp/auth';
const user = createUser({
idProvider: 'myIdProvider',
name: 'user1',
displayName: 'The One And Only',
email: 'user1@enonic.com'
});
({
type: "user",
key: "user:system:user1",
displayName: "The One And Only",
disabled: false,
email: "user1@enonic.com",
login: "user1",
idProvider: "myIdProvider",
hasPassword: false
})
deletePrincipal
Deletes the principal with the specified key.
Parameters
Takes the principalKey (a PrincipalKey) of the principal to delete.
Returns
boolean: true if the principal was deleted, false otherwise.
Example
import {deletePrincipal} from '/lib/xp/auth';
const deleted = deletePrincipal('user:myIdProvider:userId');
findPrincipals
Search for principals matching the specified criteria.
Parameters
findPrincipals() takes a single FindPrincipalsParams object with these properties:
| Name | Type | Description |
|---|---|---|
|
type |
Optional. Principal type to look for, one of |
|
|
idProvider |
string |
Optional. Key of the id provider to look for. If not specified, all id providers are included. |
|
start |
number |
Optional. First principal to return from the search results. Used for pagination. Defaults to |
|
count |
number |
Optional. A limit on the number of principals to return. Defaults to |
|
name |
string |
Optional. Name of the principal to look for. |
|
searchText |
string |
Optional. Text to look for in any principal field. |
Returns
object : (FindPrincipalsResult) The total number of principals matching the search, the count of principals included, and an array of hits containing the principals.
Example
import {findPrincipals} from '/lib/xp/auth';
const result = findPrincipals({
idProvider: "user-store",
start: 0,
count: 10,
searchText: "user1"
});
({
total: 1,
count: 1,
hits: [
{
type: "user",
key: "user:system:user1",
displayName: "The One And Only",
disabled: false,
email: "user1@enonic.com",
login: "user1",
idProvider: "myIdProvider",
hasPassword: true
}
]
})
findUsers
Search for users matching the specified query.
Parameters
findUsers() takes a single FindUsersParams object with these properties:
| Name | Type | Description |
|---|---|---|
|
query |
string |
Query expression. |
|
start |
number |
Optional. Start index for paging. Defaults to |
|
count |
number |
Optional. Number of users to fetch. Defaults to |
|
sort |
string |
Optional. Sorting expression. |
|
includeProfile |
boolean |
Optional. If set to |
Returns
object : (FindPrincipalsResult) The total number of hits, the number returned, and an array of the matching User objects.
Example
Juve family.
import {findUsers} from '/lib/xp/auth';
const findUsersResult = findUsers({
count: 1,
query: "displayName LIKE '*Juve'"
});
({
total: 2,
count: 1,
hits: [
{
type: "user",
key: "user:system:jorgen-juve",
displayName: "Jørgen Juve",
disabled: false,
email: "jju@enonic.com",
login: "jorgen-juve",
idProvider: "system",
hasPassword: true
}
]
})
If you want to find a user by key, you have to use the _id field in the query parameter. |
import {findUsers} from '/lib/xp/auth';
const findUsersResult = findUsers({
count: 1,
query: "_id = 'user:system:jorgen-juve'"
});
generatePassword
Generates a random secure password that may be suggested to a user.
Parameters
Takes no arguments.
Returns
string: A suggestion for a secure password.
Example
import {generatePassword} from '/lib/xp/auth';
const pwd = generatePassword();
getIdProviderConfig
Returns the ID provider configuration. Meant to be called from an ID provider implementation.
Parameters
Takes no arguments. The expected config shape can be supplied as a TypeScript type parameter, e.g. getIdProviderConfig<{ field?: string }>().
Returns
object: An object with all the values in the configuration, or null if no configuration is set.
Example
import {getIdProviderConfig} from '/lib/xp/auth';
const config = getIdProviderConfig<{ field?: string }>();
if (config) {
log.info('Field value for the current ID provider config = %s', config.field);
}
getIdProviders
Returns all id providers configured on the system. Each entry’s idProviderConfig is present only when the provider is bound to an application; incomplete providers come back without it.
Parameters
Takes no arguments.
Returns
object[] : (IdProvider[]) A list of ID providers.
Example
import {getIdProviders} from '/lib/xp/auth';
const idProviders = getIdProviders();
([
{
key: "system",
displayName: "System Id Provider"
},
{
key: "myIdProvider",
displayName: "My ID Provider",
description: "My ID provider description",
idProviderConfig: {
applicationKey: "com.example.myidprovider",
config: {}
}
}
])
getMembers
Returns a list of principals that are members of the specified principal.
Parameters
Returns
object[] : (User | Group)[] A list of users and groups that are members of the principal, or an empty array if none.
Example
import {getMembers} from '/lib/xp/auth';
const result = getMembers('group:system:content-managers');
[{
type: "user",
key: "user:system:user1",
displayName: "User 1",
disabled: false,
email: "user1@enonic.com",
login: "user1",
idProvider: "system",
hasPassword: true
},
{
type: "user",
key: "user:system:user2",
displayName: "User 2",
disabled: false,
email: "user2@enonic.com",
login: "user2",
idProvider: "system",
hasPassword: false
}]
getMemberships
Returns the list of principals which the specified principal is a member of.
Parameters
Takes the principalKey (a UserKey or GroupKey) of the user or group to retrieve memberships for, and an optional transitive boolean — retrieve transitive memberships. Considered false if not specified.
Returns
Example
import {getMemberships} from '/lib/xp/auth';
const result = getMemberships('user:system:user1');
[{
type: "role",
key: "role:system.admin",
displayName: "Administrator"
}]
import {getMemberships} from '/lib/xp/auth';
const result = getMemberships('user:system:user1', true);
[
{
type: "role",
key: "role:system.admin.login",
displayName: "Login to administration console"
},
{
type: "group",
key: "group:system:content-managers",
displayName: "Content Managers"
},
{
type: "role",
key: "role:cms.expert",
displayName: "CMS Expert"
},
{
type: "role",
key: "role:cms.project.default.editor",
displayName: "Default project editor"
}
]
getPrincipal
Returns the principal with the specified key. The result is narrowed to User, Group or Role when the key type makes the principal type unambiguous.
Parameters
Takes the principalKey (a PrincipalKey) of the principal to look for.
Returns
object : (Principal) The principal as an object, or null if it does not exist.
Example
import {getPrincipal} from '/lib/xp/auth';
const result = getPrincipal('user:system:user1');
({
type: "user",
key: "user:system:user1",
displayName: "User 1",
disabled: false,
email: "user1@enonic.com",
login: "user1",
idProvider: "system",
hasPassword: true
})
getProfile
Returns the profile of a user.
Parameters
getProfile() takes a single GetProfileParams object with these properties:
| Name | Type | Description |
|---|---|---|
|
key |
Principal key of the user. |
|
|
scope |
string |
Optional. Scope of the data to retrieve. |
Returns
object: Profile data, or null if the user was not found.
Example
import {getProfile} from '/lib/xp/auth';
const result = getProfile({
key: 'user:system:user1'
});
({
nickName: "User Nick"
})
getUser
Returns the logged-in user. If not logged-in, this will return null.
Parameters
getUser() takes an optional GetUserParams object with these properties:
| Name | Type | Description |
|---|---|---|
|
includeProfile |
boolean |
Optional. If set to |
Returns
object : (User) The logged-in user, or null if not logged in. When includeProfile is true, the user also carries a profile property.
Example
import {getUser} from '/lib/xp/auth';
const result = getUser();
({
type: "user",
key: "user:system:user1",
displayName: "User 1",
disabled: false,
email: "user1@enonic.com",
login: "user1",
idProvider: "system",
hasPassword: true
})
hasRole
Checks if the logged-in user has the specified role.
Parameters
Takes the role name (a string) to check for.
Returns
boolean: true if the current user has the role, false otherwise.
Example
import {hasRole} from '/lib/xp/auth';
const isAdmin = hasRole('system.admin');
login
Logs in a user through the specified idProvider, with username and password.
Parameters
login() takes a single LoginParams object with these properties:
| Name | Type | Description |
|---|---|---|
|
user |
string |
Name of the user to log in. |
|
password |
string |
Optional. Password for the user. Ignored if |
|
idProvider |
string |
Optional. Name of the id provider where the user is stored. If not specified the system id provider is used. |
|
skipAuth |
boolean |
Optional. Skip authentication. Defaults to |
|
sessionTimeout |
number |
Optional. Session timeout, in seconds. Defaults to the value of |
|
scope |
Optional. Scope of the login. When set to |
Returns
object : (LoginResult) The login result.
Example
import {login} from '/lib/xp/auth';
const loginResult = login({
user: 'user1',
password: 'myPwd1',
idProvider: 'myIdProvider'
});
({
authenticated: true,
user: {
type: "user",
key: "user:system:user1",
displayName: "The One And Only",
disabled: false,
email: "user1@enonic.com",
login: "user1",
idProvider: "myIdProvider",
hasPassword: true
}
})
modifyGroup
Updates an existing group.
Parameters
modifyGroup() takes a single ModifyGroupParams object with these properties:
| Name | Type | Description |
|---|---|---|
|
key |
Principal key of the group to modify. |
|
|
editor |
function |
Editor callback |
Returns
object : (Group) The updated group, or null if the group was not found.
Example
import {modifyGroup} from '/lib/xp/auth';
// Callback to edit the group.
function groupEditor(c) {
c.displayName = 'Modified display name';
c.description = 'descriptionX';
return c;
}
// Modify group with specified key.
const group = modifyGroup({
key: 'group:system:group-a',
editor: groupEditor
});
({
type: "group",
key: "group:system:group-a",
displayName: "Modified display name",
description: "descriptionX"
})
modifyProfile
Updates an existing user profile. Will create a new profile, if an existing one with provided scope was not found.
Parameters
modifyProfile() takes a single ModifyProfileParams object with these properties:
| Name | Type | Description |
|---|---|---|
|
key |
Principal key of the user. |
|
|
scope |
string |
Optional. Scope of the data to retrieve and update. |
|
editor |
function |
Editor callback |
Returns
object: The updated profile, or null if the user was not found.
Example
import {modifyProfile} from '/lib/xp/auth';
// Callback to edit the profile.
function profileEditor(c) {
if (!c) {
c = {};
}
c.nickName = "User Nick";
return c;
}
// Modify profile with specified key.
const profileModified = modifyProfile({
key: 'user:system:user1',
editor: profileEditor
});
({
nickName: "User Nick"
})
modifyRole
Updates an existing role.
Parameters
modifyRole() takes a single ModifyRoleParams object with these properties:
| Name | Type | Description |
|---|---|---|
|
key |
Principal key of the role to modify. |
|
|
editor |
function |
Editor callback |
Returns
object : (Role) The updated role, or null if the role was not found.
Example
import {modifyRole} from '/lib/xp/auth';
// Callback to edit the role.
function roleEditor(c) {
c.displayName = 'Nothing Role';
c.description = 'This role does not give access to anything!';
return c;
}
// Modify role with a specified key.
const theRole = modifyRole({
key: 'role:aRole',
editor: roleEditor
});
({
type: "role",
key: "role:aRole",
displayName: "Nothing Role",
description: "This role does not give access to anything!"
})
modifyUser
Updates an existing user.
Parameters
modifyUser() takes a single ModifyUserParams object with these properties:
| Name | Type | Description |
|---|---|---|
|
key |
Principal key of the user to modify. |
|
|
editor |
function |
Editor callback |
Returns
object : (User) The updated user, or null if the user was not found.
Example
import {modifyUser} from '/lib/xp/auth';
// Callback to edit the user.
function userEditor(c) {
c.displayName = 'User 1-A';
c.email = 'user1a@enonic.com';
return c;
}
// Modify user with a specified key.
const theUser = modifyUser({
key: 'user:system:user1',
editor: userEditor
});
({
type: "user",
key: "user:system:user1",
displayName: "User 1-A",
disabled: false,
email: "user1a@enonic.com",
login: "user1",
idProvider: "myIdProvider",
hasPassword: true
})
removeMembers
Removes members from a principal (group or role).
Parameters
Takes the principalKey (a GroupKey or RoleKey) of the group or role to remove members from, and an array of members — the UserKey or GroupKey values to remove.
Returns
void
Example
import {removeMembers} from '/lib/xp/auth';
removeMembers('role:roleId', ['user:system:user1', 'group:system:group-a']);
Type Definitions
The core principal types — User, Group, Role, Principal, and the key types PrincipalKey, UserKey, GroupKey, RoleKey and PrincipalType — are shared across the libraries and defined on the Types page.
LoginScopeType
A string union: 'SESSION' | 'REQUEST' | 'NONE'. Controls how long a successful login stays valid — see the scope property of login().
LoginResult
Properties
| Name | Type | Description |
|---|---|---|
|
authenticated |
boolean |
Whether the user was successfully authenticated. |
|
user |
Optional. The authenticated user. Present when |
|
|
message |
string |
Optional. A message describing the result, typically present on failure. |
FindPrincipalsResult
The result of findPrincipals() and findUsers().
Properties
| Name | Type | Description |
|---|---|---|
|
total |
number |
Total number of principals matching the search. |
|
count |
number |
Number of principals included in |
|
hits |
The matching principals. For |
IdProvider
Properties
| Name | Type | Description |
|---|---|---|
|
key |
string |
ID provider key. |
|
displayName |
string |
ID provider display name. |
|
description |
string |
Optional. ID provider description. |
|
idProviderConfig |
Optional. Present only when the provider is bound to an application. |
IdProviderConfig
Properties
| Name | Type | Description |
|---|---|---|
|
applicationKey |
string |
Key of the application whose functions serve requests for this provider. |
|
config |
object |
Optional. Per-instance configuration tree passed to the bound application. |
IdProviderAccessControlEntry
Properties
| Name | Type | Description |
|---|---|---|
|
principal |
Principal the entry grants access to. |
|
|
access |
Permission level. |
IdProviderAccess
A string union of permission levels: 'READ' | 'CREATE_USERS' | 'WRITE_USERS' | 'ID_PROVIDER_MANAGER' | 'ADMINISTRATOR'.