Uptime & Heartbeat Monitors
Integrations
Developing a web or native App with OAuth2 and PKCE
Single page web applications or native apps cannot keep their client secrets private and are therefor called public clients. For such cases the client secret can be omitted by relying on PKCE.
For this guide we assume that you have read the general OAuth2 introduction as well as created an application as shown in the getting started guide.
You may find the whole source code project in this Github repository.

Preparing your iLert app

Before we can start, we need to make sure that our iLert application is configured correctly. Navigate to your application and make sure that you have entered a redirect url that fits your local or deployed webapp. Also copy the client id, as we will need it to configure your application.

Redirecting the user

Using the parameters generated by iLert during the creation of our app, we can build a redirect url for our application, that will take unauthorized users to iLert and ask them to login and grant their permission for our app. In the case of our app, we ask for the profile scope (find more information on other available scopes here), as well as the source:w scope which will grant us permission to read the user's profile data, as well as to read and edit alert sources on behalf of the authorizing user.
Make sure that the redirect_uri that is passed to the redirect url is exactly the same as the one the you have entered in your application's settings or the authorization will fail. This validation is necessary, as verifing the redirect_uri is a very important part of implementing a secure OAuth2 flow. In case you need to send optional data e.g. your own id, use the state query parameter. It will be returned to your client.
Also note that per default for PKCE-only clients no refresh token will be issued, unless the additional offline access scope is requested. Please keep refresh tokens secure.

PKCE generation

Before redirecting the user to the iLert authorization server, we need to generate a verifier and code challenge that can be used by the authorization server to ensure the origin of the (later) token request matches the origin of the authorization request.
PKCE might seem confusing at first, but in the end it is just a random identifier that verifies that the one who initially started the authorization request, is the same who comes asking for the token. And to ensure noone else knows the plain version of the identifier it is hashed before sharing it with the authorization server.
Below are helper methods to deal with PKCE generation:
1
function dec2hex(dec) {
2
return ("0" + dec.toString(16)).substr(-2);
3
}
4
​
5
function generateCodeVerifier() {
6
const array = new Uint32Array(56 / 2);
7
window.crypto.getRandomValues(array);
8
return Array.from(array, dec2hex).join("");
9
}
10
​
11
function sha256(plain) {
12
const encoder = new TextEncoder();
13
const data = encoder.encode(plain);
14
return window.crypto.subtle.digest("SHA-256", data);
15
}
16
​
17
function base64urlencode(a) {
18
let str = "";
19
const bytes = new Uint8Array(a);
20
const len = bytes.byteLength;
21
for (var i = 0; i < len; i++) {
22
str += String.fromCharCode(bytes[i]);
23
}
24
return btoa(str)
25
.replace(/\+/g, "-")
26
.replace(/\//g, "_")
27
.replace(/=+$/, "");
28
}
29
​
30
async function generateCodeChallengeFromVerifier(v) {
31
const hashed = await sha256(v);
32
const base64encoded = base64urlencode(hashed);
33
return base64encoded;
34
}
Copied!
Which may be used like so:
1
const verifier = generateCodeVerifier();
2
const challenge = await generateCodeChallengeFromVerifier(verifier);
3
sessionStorage.setItem("verifier", verifier);
Copied!
Notice how we are storing the verifier in the session storage, as we will need it later to verify to the authorization server that we are the same ones who initiated the authorize request.
With our verifier and code challenge in place, we can create our redirect url:
Javascript
1
const url = "https://app.ilert.com/api/developers/oauth2/authorize";
2
const queryParams = new URLSearchParams({
3
client_id: '0501439fd7dee9af0c25',
4
response_type: 'code',
5
redirect_uri: 'http://localhost:4597/webapp',
6
scope: 'profile source:w offline_access',
7
state: 'optional-state',
8
code_challenge: '6K9LHF25tUPodHISsO_UoK6cKnbclW2ZY1TaSUAWaIo',
9
code_challenge_method: 'S256'
10
});
11
​
12
window.location = url + "?" + queryParams.toString();
Copied!
In case the user is known to your application or especially if the application is limited to a single account. We suggest to redirect the user directly to the tenant (account url) of the user, this way he might be able to skip the login step if logged in already. e.g. https://someTenant.ilert.com/api/developers/oauth2/authorize instead of https://app.ilert.com/api/developers/...

User grant view

The user will be able to see the scopes that you have requested and grant them.

In case of an error

In case of any error, iLert's authorization server will send the user back to the provided redirect_uri while adding an error and error_description query parameter to it.

In case of successful authorization

If the user has accepted your scopes, iLert's authorization server will redirect the user back to your provided redirect_uri while adding a code query parameter to it. This code parameter can be used in the next step to fetch the access token.
1
http://localhost:4597/webapp?code=c82ec4eca4c0ac90061fdd4225d1&state=optional-state
Copied!

Turning a code into a token

After a successful redirect, we can detect the code query parameter and run an AJAX call to fetch our tokens.
You may use plain XMLHttpRequest or the fetch API, for the sake of simplicity in reading we chose the library axios to make the HTTP calls.
Javascript
1
const verifier = sessionStorage.getItem("verifier"); // stored earlier
2
sessionStorage.removeItem("verifier");
3
const urlSearchParams = new URLSearchParams(window.location.search);
4
const code = Object.fromEntries(urlSearchParams.entries()).code;
5
​
6
const tokenParams = new URLSearchParams();
7
tokenParams.append("client_id", clientId);
8
tokenParams.append("grant_type", "authorization_code");
9
tokenParams.append("code", code);
10
tokenParams.append("code_verifier", verifier);
11
​
12
const tokenUrl = "https://app.ilert.com/api/developers/oauth2/token";
13
const tokenResult = await axios.post(tokenUrl, tokenParams, {
14
headers: {
15
"Content-Type": "application/x-www-form-urlencoded",
16
"Accept": "application/json"
17
}
18
});
19
​
20
const {
21
access_token,
22
refresh_token
23
} = tokenResult.data;
24
​
25
console.log(access_token, refresh_token);
Copied!

In case of an error

You will receive an error response.
1
{
2
"code": "invalid_grant",
3
"error": "Provided code is unknown"
4
}
Copied!

In case of successful token generation

If code and secret are valid, the authorization server will respond with an access as well as a refresh token.
Never expose your user's tokens, especially not the refresh tokens, treat them like passwords.
1
{
2
"token_type": "Bearer",
3
"access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJpbF9uIjoiNWNmIiwiaWxfYSI6MiwiaWxfcyI6InByb2ZpbGUgc291cmNlOnciLCJpc3MiOiJpbGVydCIsImlsX3UiOiJrcnlzdGlhbml0eSIsImlsX3QiOiJBQ0NFU1MiLCJpbF92IjoxLCJleHAiOjE2NDM3MjY3MjN9.Ao0dTxQpcez0IsaUOdsPzWqWhj31ob9Tzf-5KSoqJEmjC4lkeOiRJ255HI8w2IQyj7aunW_SM50rRQBcDHVkt8JmfuT4TgYJ3dvZiJzeZ-YGU1QDT6pCN30wJ9a3rpmv6Y6T2cJD1JvNuEh1qyjdjmJGuBVU3-_0qZSu-Y6CBaRxaBvM27JOf2JkQ4pAN7dK4c2Yw7-MyEkBhtwI_JEMhNCp6GF5SJGhCFBq-xPF1iFqpllIrpqaTWAvRz0ZCNWZCumXCaJAfvnS_OJJoPwEvr2DBV8x0bAuFpECgycE8_--rjU9gZafqiRd2cfFas7AmzQFuvvDGQx_1Zq7leW3EA",
4
"expires_in": 3600,
5
"scope": "profile source:w",
6
"refresh_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJpbGVydCIsImlsX3QiOiJPQVVUSCIsImlsX3YiOjEsImV4cCI6MTY3NTI1OTEyMywiaWxfayI6IjM0ZTMzYzdmZGUzZjQzZTdhMTEwOWJmYTgzMmRkNDdiIn0.ol0HwWO_zAAhKLjLPLDPwvxv7PNPnUnZq8IXd5fC1FY",
7
"refresh_token_expires_in": 31536000
8
}
Copied!

Using the access token

Your application should store the access and especially the refresh token as safely as possible. You can now make requests to iLert's API on behalf of the authorized user.
Do not forget the Bearer prefix in the authorization header before your access token.
Javascript
1
var axios = require("axios").default;
2
​
3
var options = {
4
method: 'GET',
5
url: 'https://api.ilert.com/api/users/current',
6
headers: {
7
Authorization: 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJpbF9uIjoiNWNmIiwiaWxfYSI6MiwiaWxfcyI6InByb2ZpbGUgc291cmNlOnciLCJpc3MiOiJpbGVydCIsImlsX3UiOiJrcnlzdGlhbml0eSIsImlsX3QiOiJBQ0NFU1MiLCJpbF92IjoxLCJleHAiOjE2NDM3MjczNTR9.ZSxXJiR7yPF9BMfsfa9dYVpdv5E2o88iBTG1VFp7-vdF3wciNte7_fKQU2z2aMVudGIZ6dhpBuA8FnSJa3sjaXgsHy2r8Fjvj2XCTuPigaZgURTjAM0zqTC6bxHVkn-G9bKUoJ8mx40U8hFZFPsnD5b9qiZZ6CjqU0CCuiWjoEC8hapHe1TOYnNvqeFPRUYfOhJTXAuOoW0wmcKdrAyk6YbVN74nDVlEbG1TEGIXk893jVyLaoiShqHxkcnyliTWLYg9P_DG0JXFflxugyGttP3GaxAq28KAxI-5VAa_QptXlwybEVN9Q8072a7Xk4R0_jCKbp9uMKE3E4qH72f3PA'
8
}
9
};
10
​
11
axios.request(options).then(function (response) {
12
console.log(response.data);
13
}).catch(function (error) {
14
console.error(error);
15
});
Copied!

Refreshing an access token

As access tokens are short lived (read more about token lifetime here), you will have to refresh them regularly, at least in case they expire and the iLert API returns a 401 HTTP status code error.
Javascript
1
var axios = require("axios").default;
2
​
3
var options = {
4
method: 'POST',
5
url: 'https://api.ilert.com/api/developers/oauth2/token',
6
headers: {
7
'Content-Type': 'application/x-www-form-urlencoded'
8
},
9
data: {
10
client_id: '0501439fd7dee9af0c25',
11
refresh_token: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJpbGVydCIsImlsX3QiOiJPQVVUSCIsImlsX3YiOjEsImV4cCI6MTY3NTI1OTEyMywiaWxfayI6IjM0ZTMzYzdmZGUzZjQzZTdhMTEwOWJmYTgzMmRkNDdiIn0.ol0HwWO_zAAhKLjLPLDPwvxv7PNPnUnZq8IXd5fC1FY',
12
grant_type: 'refresh_token'
13
}
14
};
15
​
16
axios.request(options).then(function (response) {
17
console.log(response.data);
18
}).catch(function (error) {
19
console.error(error);
20
});
Copied!
The responses will be equal to the ones of Turning a code into a token above.

Accessing token information

You could parse the JWT token yourself, however we offer a token info endpoint for convience. Note that this endpoint only accepts access tokens.
Javascript
1
var axios = require("axios").default;
2
​
3
var options = {
4
method: 'POST',
5
url: 'https://api.ilert.com/api/developers/oauth2/token_info',
6
headers: {
7
'Content-Type': 'application/x-www-form-urlencoded'
8
},
9
data: {
10
token: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJpbF9uIjoiNWNmIiwiaWxfYSI6MiwiaWxfcyI6InByb2ZpbGUgc291cmNlOnciLCJpc3MiOiJpbGVydCIsImlsX3UiOiJrcnlzdGlhbml0eSIsImlsX3QiOiJBQ0NFU1MiLCJpbF92IjoxLCJleHAiOjE2NDM3MjczNTR9.ZSxXJiR7yPF9BMfsfa9dYVpdv5E2o88iBTG1VFp7-vdF3wciNte7_fKQU2z2aMVudGIZ6dhpBuA8FnSJa3sjaXgsHy2r8Fjvj2XCTuPigaZgURTjAM0zqTC6bxHVkn-G9bKUoJ8mx40U8hFZFPsnD5b9qiZZ6CjqU0CCuiWjoEC8hapHe1TOYnNvqeFPRUYfOhJTXAuOoW0wmcKdrAyk6YbVN74nDVlEbG1TEGIXk893jVyLaoiShqHxkcnyliTWLYg9P_DG0JXFflxugyGttP3GaxAq28KAxI-5VAa_QptXlwybEVN9Q8072a7Xk4R0_jCKbp9uMKE3E4qH72f3PA'
11
}
12
};
13
​
14
axios.request(options).then(function (response) {
15
console.log(response.data);
16
}).catch(function (error) {
17
console.error(error);
18
});
Copied!
You will receive an info response like this:
1
{
2
"active": true,
3
"tenant_id": "someTenant",
4
"username": "someUser",
5
"scope": "profile source:w",
6
"app_id": 250
7
}
Copied!

Revoking a refresh token

Just like the user may at any time revoke the refresh token that was issued to your application, your application may also proactively revoke the refresh token.
Node.js
1
var axios = require("axios").default;
2
​
3
var options = {
4
method: 'POST',
5
url: 'https://api.ilert.com/api/developers/oauth2/revoke',
6
headers: {
7
'Content-Type': 'application/x-www-form-urlencoded'
8
},
9
data: {
10
token: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJpbGVydCIsImlsX3QiOiJPQVVUSCIsImlsX3YiOjEsImV4cCI6MTY3NTI1OTEyMywiaWxfayI6IjM0ZTMzYzdmZGUzZjQzZTdhMTEwOWJmYTgzMmRkNDdiIn0.ol0HwWO_zAAhKLjLPLDPwvxv7PNPnUnZq8IXd5fC1FY'
11
}
12
};
13
​
14
axios.request(options).then(function (response) {
15
console.log(response.data);
16
}).catch(function (error) {
17
console.error(error);
18
});
Copied!
​
Make sure to checkout further information on OAuth2 apps.