How to use Credentials-based auth sessions
Goal
In this how to guide, we will go over how to use Credentials-based auth sessions. For this example, we will use OrangeHRM (demo site) as the target service.
Follow this guide step by step or you can use the Credentials based auth sessions
project template to get a jump start. You can also watch a walkthrough of this guide below:
Step by step
1. Create a project and enable auth-sessions
In this guide, we will use Credentials-based auth sessions. In this type of auth sessions, the user credentials are used to run a browser automation code that creates a session in the target service. We also persist the credentials to use when the session expires.
- Navigate to https://app.intuned.io/projects
- Click
New
to create a new project - Navigate to
Intuned.json
, which serves as the IDE settings - Select
Auth Session
tab and set the following configurations- Toggle
Enable auth sessions
on - Set
Auth session type
toAPI created
- Toggle
To learn more about credentials-based auth sessions, check out Credentials-based auth sessions.
2. implement the check
, create
and refresh
functions
Credentials-based auth sessions require the following functions to be implemented:
-
check
function: This function is called before executing any authenticated action (API). If the function returns false, this signals that the auth session is expired/invalid and the auth session will be marked as expired for Recorder-based auth sessions. The API that was called will return 401 in this case. -
create
function: this function is used when a new auth session needs to be created/updated. This function takes the user provides credentials and performs browser automation that logs the user into the target service. -
refresh
function: this function is used to auto refresh the auth session when it expires. In most cases, the logic of thecreate
andrefresh
functions are identical.
- Navigate to
auth-sessions
folder and define the scripts needed to sign into the target service.
create.ts
should be defined to log the user into the target service.
check.ts
should be defined to verify that the auth session is valid.
- in this example, the
refresh
function default implementation will call thecreate
function. This is will work fine here - no need to change it.
3. Create the authenticated APIs
Create APIs for all actions you would like to automate.
- Create an API named
add-new-user.ts
and write the script to add a new user to the HR portal.
- Create an API named
get-claims.ts
and write the script to get the employee claims
4. Validate the APIs in the IDE and Deploy
- Create an auth session in the IDE
- Validate
add-new-user
andget-claims
APIs with the auth session you created in the previous step.
5. Deploy and validate the deployed APIs
-
Validate
add-new-user
andget-claims
APIs with an simple async request
(Optional) How to build UX to allow users to “connect to OrangeHRM”
One main use case of auth session is building integration with services that require user authentication and has no APIs. For example, lets assume that you are building a EHR/automation platform and you to automate the process of pulling claims. To do this on behalf of your users, you would need to build a UX that allows users to connect their OrangeHRM accounts to your EHR/automation platform. To see an example of this, please checkout the following project and use it as a guide:
(Optional) Build an authenticated scraper using service accounts
Lets assume that we have the above project and APIs created and that we want to use them to build an authenticated scraper that will retrieve all claims from a cretin account on a daily basis. Lets also assume that this account access will be done via a static set of credentials that we will have.
You can watch a walk through of this guide below:
1. Create an auth session using the Intuned UI
- Navigate to the project that you have created in the previous steps
- Click on the
Auth Sessions
tab - Click on
New Auth Session
- Provide an auth session name. For example,
orange-hrm-service-account
- Paste new credentials for the target service
2. Create a job to retrieve all claims daily
Jobs are a way to schedule recurring or batched/grouped executions. For more info about Jobs, checkout Jobs.
In this example, we know that we need to call the get-claims
API and send the result to a webhook. We will create a job that does this weekly.
Creating jobs can be done via UI or API. For this example, we will use the UI. Checkout Jobs API overview for more info.
-
Get a webhook url, for testing, you can use
https://webhook.site/
to get a temp url. In a real scenario, you will use your own webhook url and persist the data to store. -
Go to the Jobs tab in the UX and create a new job with the following config. Don’t forget to replace
<YOUR_WEBHOOK_URL>
with your webhook url.
- Now, what will happen is that every day, the job will run, get the data from the API and send the result to the webhook url you provided.