Skip to content

Commit 2291cfe

Browse files
authored
Generating a direct login URL for a user using Digest token authentication. (#1027)
1 parent 675871b commit 2291cfe

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
var UserHelper = Class.create();
2+
UserHelper.prototype = {
3+
SECRET_KEY: '<YOUR_SECRET_KEY>', //secret key defined by you for encoding
4+
MAC_ALG: 'HmacSHA256',
5+
GLIDE_SSO_ID: '<SSO_PROVIDER_SYS_ID>', //system id of digest token sso provider
6+
7+
initialize: function(userGR) {
8+
this.userGR = userGR;
9+
},
10+
11+
getUserById: function(sys_id) {
12+
return this.getUser('sys_id', sys_id);
13+
},
14+
15+
getUserByEmail: function(email) {
16+
return this.getUser('email', email);
17+
},
18+
19+
getUserByName: function(user_name) {
20+
return this.getUser('user_name', user_name);
21+
},
22+
23+
getUser: function(key, value) {
24+
if (key && value) {
25+
this.userGR = new GlideRecord('sys_user');
26+
this.userGR.get(key, value);
27+
}
28+
return this.userGR;
29+
},
30+
31+
//generate the direct login url using user_name or user glide record
32+
login: function(user_name) {
33+
34+
if (user_name) {
35+
this.getUserByName(user_name);
36+
}
37+
38+
if (!this.userGR) {
39+
return null;
40+
}
41+
42+
//generating token
43+
var token = SncAuthentication.encode(this.userGR.getValue('user_name'), this.SECRET_KEY, this.MAC_ALG);
44+
45+
//formating url
46+
var url = gs.getProperty('glide.servlet.uri') + '?glide_sso_id=' + this.GLIDE_SSO_ID + '&SM_USER=' + this.userGR.getValue('user_name') + '&DE_USER=' + token;
47+
48+
return url;
49+
},
50+
51+
type: 'UserHelper'
52+
};
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Script Include for Single Sign-On (SSO) Direct Login URL Generator using UserHelper
2+
3+
1. [Introduction](#introduction)
4+
2. [Installation](#installation)
5+
3. [Example & Usage](#usage)
6+
4. [Security](#security)
7+
8+
## Introduction<a name="introduction"></a>
9+
10+
The UserHelper script include provides functions for generating direct login URLs for users in ServiceNow. This functionality is similar to the impersonate user feature in ServiceNow, but it allows you to directly log in the user without requiring them to enter any credentials.
11+
12+
## Installation<a name="installation"></a>
13+
14+
This script include required Digest Token Authentication Integration below:
15+
16+
#### Prerequisites
17+
18+
* Multi-Provider SSO plugin is activated [documentation](https://docs.servicenow.com/bundle/vancouver-platform-security/page/integrate/single-sign-on/task/t_ActivateMultipleProviderSSO.html)
19+
* Multi-Provider SSO properties are configured [documentation](https://docs.servicenow.com/bundle/vancouver-platform-security/page/integrate/single-sign-on/task/t_ConfigureMultiProviderSSOProps.html)
20+
21+
#### Steps
22+
23+
1. Go to **Multi-Provider SSO** > **Identity Providers**.
24+
2. Select the **Digested Token** record.
25+
3. Add a **Secret Passphrase**. This will be the `<YOUR_SECRET_KEY>` in the script include.
26+
4. Note the **sys_id** of the **Digested Token** record. This will be the `<SSO_PROVIDER_SYS_ID>` for the script include.
27+
28+
## Usage<a name="usage"></a>
29+
30+
To generate a direct login URL for a user, you can use the `login()` function provided by the UserHelper script include. This function takes the user's name or GlideRecord as input and returns a URL that the user can use to log in directly.
31+
32+
The following example shows how to use the UserHelper script include to generate a direct login URL for a user:
33+
```javascript
34+
// Generate a direct login URL for the user "admin".
35+
var userHelper = new UserHelper();
36+
37+
// Generate a direct login URL for the user "admin".
38+
var loginUrl = userHelper.login('admin');
39+
```
40+
```javascript
41+
// Generate a direct login URL for the user with the sys_id "1234567890", email.
42+
var userHelper = new UserHelper();
43+
userHelper.getUserById('1234567890'); //userHelper.getUserByEmail('<EMAIL>');
44+
45+
// Generate a direct login URL for the user "admin".
46+
var loginUrl = userHelper.login();
47+
48+
//loginUrl: https://<instance>.service-now.com/?glide_sso_id=<SSO_PROVIDER_SYS_ID>&SM_USER=admin&DE_USER=htrULTFZTOLl9PHEvNBejz65ghxp6dJgDazXXv9v/wY=
49+
```
50+
51+
## Security<a name="security"></a>
52+
It is crucial to emphasize that the UserHelper script include provides direct access to users without requiring any credentials. Therefore, it is important to set proper security policies to secure this script include. Make sure to follow best practices for securing access to this functionality.

0 commit comments

Comments
 (0)