Perform Recollection
Introduction
There are certain cases where the Adaptive Browser SDK may need to be run on a dedicated page in order to collect fresh data from the browser. This might be needed because the existing data has become stale or it might be because collection was not (or could not be) performed on the login page.
This guide will show how you can set up a dedicated collection page which performs collection and then POSTs the resulting sessionId back to your application.
Pre-requisites
Collection requires that you have an application set up for policy-based authentication and on-boarded for Adaptive Access. This is covered in On-boarding a native application.
The code shown in this guide is an addition to the NodeJS application described in Set up a sample application.
Add the collection page
Create file
Create a new file in your sample application directory called collect.html
. This is the page that will trigger collection.
touch collect.html
Create file and insert snippet
The collect.html
page must include the web snippet created for the application in the <head>
of the page to load and initialize the Browser SDK. The Browser SDK will then perform the client-side data collection that is required by Adaptive Access.
Add the following code to the collect.html
page:
<html>
<head>
<!--
Paste the snippet that was provided when you
on-boarded your application.
-->
<script>
function collect() {
getSessionId().then(sessionID => {
const form = document.createElement('form');
form.method = 'POST';
form.action = '/collect';
const sessionIDField = document.createElement('input');
sessionIDField.type = 'hidden';
sessionIDField.name = 'sessionId';
sessionIDField.value = sessionID;
form.appendChild(sessionIDField);
document.body.appendChild(form);
form.submit();
});
}
</script>
<title>Please wait...</title>
</head>
<body onload='collect()'>
<h1>Please wait...</h1>
</body>
</html>
Paste the web snippet provided during on-boarding into the collect.html
page - replacing the <!-- ... -->
block that shows where to put it.
What's happening here?
The code on this collection page waits for a session Id to be available and then adds that session Id into a form and submits it to your application.
Expose collection page in application
To expose the collection page in your application, add the following code to the end of the index.js
file:
// GET /collect loads a standalone API collection page.
// It auto-posts to /collect with sessionId when complete.
app.get("/collect", (_, res) => {
res.sendFile(__dirname + '/collect.html');
});
Handle POST of sessionId
When the collection page runs in the browser, it will result in a POST back to the \collect
endpoint. The application will need to store the session Id and then call whatever is going to use the sessionId (perhaps a token refresh operation).
Add the following code to the end of the index.js
file:
// Handle collection page POST to /collect
app.post("/collect", (req, res) => {
req.session.sessionId = req.body.sessionId //Save sessionId
console.log("Got sessionId: " + req.body.sessionId);
if (req.session.postcollect == "refresh") {
doRefresh(req, res); // Defined by refresh code
} else {
res.send(`<html>Collection Complete!</html>`);
}
delete req.session.postcollect;
});
The code above uses a session variable called postcollect
to determine what to do after the sessionId has been obtained.
At the moment, this variable is not being set and so calling the /collect endpoint will result in a simple Collection Complete! message being returned to the browser.
The doRefresh function is not defined yet. This is defined when setting up the sample application to support an Adaptive Token Refresh flow.
Updated over 1 year ago