Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add IP change listener to getVerifiedLocationToken() #193

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Radar.initialize('prj_test_pk_...', { /* options */ });

Add the following script in your `html` file
```html
<script src="https://js.radar.com/v4.4.8/radar.min.js"></script>
<script src="https://js.radar.com/v4.4.10-beta.1/radar.min.js"></script>
```

Then initialize the Radar SDK
Expand All @@ -73,8 +73,8 @@ To create a map, first initialize the Radar SDK with your publishable key. Then
```html
<html>
<head>
<link href="https://js.radar.com/v4.4.8/radar.css" rel="stylesheet">
<script src="https://js.radar.com/v4.4.8/radar.min.js"></script>
<link href="https://js.radar.com/v4.4.10-beta.1/radar.css" rel="stylesheet">
<script src="https://js.radar.com/v4.4.10-beta.1/radar.min.js"></script>
</head>

<body>
Expand All @@ -98,8 +98,8 @@ To create an autocomplete input, first initialize the Radar SDK with your publis
```html
<html>
<head>
<link href="https://js.radar.com/v4.4.8/radar.css" rel="stylesheet">
<script src="https://js.radar.com/v4.4.8/radar.min.js"></script>
<link href="https://js.radar.com/v4.4.10-beta.1/radar.css" rel="stylesheet">
<script src="https://js.radar.com/v4.4.10-beta.1/radar.min.js"></script>
</head>

<body>
Expand Down Expand Up @@ -130,8 +130,8 @@ To power [geofencing](https://radar.com/documentation/geofencing/overview) exper
```html
<html>
<head>
<link href="https://js.radar.com/v4.4.8/radar.css" rel="stylesheet">
<script src="https://js.radar.com/v4.4.8/radar.min.js"></script>
<link href="https://js.radar.com/v4.4.10-beta.1/radar.css" rel="stylesheet">
<script src="https://js.radar.com/v4.4.10-beta.1/radar.min.js"></script>
</head>

<body>
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "radar-sdk-js",
"version": "4.4.8",
"version": "4.4.10-beta.1",
"description": "Web Javascript SDK for Radar, location infrastructure for mobile and web apps.",
"homepage": "https://radar.com",
"type": "module",
Expand Down
34 changes: 0 additions & 34 deletions src/api/track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ class TrackAPI {
let response: any;
if (fraud) {
const host = 'https://api-verified.radar.io';
const pingHost = 'ping.radar-verify.com';

const lang = navigator.language;
const langs = navigator.languages;
Expand All @@ -109,31 +108,9 @@ class TrackAPI {
},
});

let sclVal = -1;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove old ping call

let cslVal = -1;
/*
try {
const [sclRes, csl] = await Promise.all([
Http.request({
host: `https://${pingHost}`,
method: 'GET',
path: 'ping',
}),
ping(`wss://${pingHost}`),
]);
const { scl }: any = sclRes;
sclVal = scl;
cslVal = csl;
} catch (err) {
// do nothing, send scl = -1 and csl = -1
}
*/

const payload = {
payload: JSON.stringify({
...body,
scl: sclVal,
csl: cslVal,
lang,
langs,
}),
Expand All @@ -153,17 +130,6 @@ class TrackAPI {
},
});

if (options.debug && response && response.user) {
if (!response.user.metadata) {
response.user.metadata = {};
}

response.user.metadata['radar:debug'] = {
sclVal,
cslVal,
};
}

let { user, events, token, expiresAt, expiresIn, passed, failureReasons, _id } = response;
const location = { latitude, longitude, accuracy };
if (expiresAt) {
Expand Down
36 changes: 34 additions & 2 deletions src/api/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,27 @@ let lastToken: RadarTrackVerifiedResponse | null = null;
let lastTokenNow: number = 0;
let expectedCountryCode: string | null = null;
let expectedStateCode: string | null = null;
let lastIp: string | null = null;

class VerifyAPI {
static async checkIpChanges() {
const { ip }: any = await Http.request({
method: 'GET',
path: 'ping',
});

const ipChanged = ip !== lastIp;
if (ipChanged) {
Logger.info(`IP changed from ${lastIp} to ${ip}`);

lastToken = null;
}

lastIp = ip;

return ipChanged;
}

static async trackVerified(params: RadarTrackVerifiedParams, encrypted: Boolean = false) {
const options = Config.get();

Expand Down Expand Up @@ -104,6 +123,11 @@ class VerifyAPI {
}
}

if (params.ipChanges && trackRes?.user?.ip) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also picks up IP changes in trackVerified() responses

lastIp = trackRes?.user?.ip;
Logger.info(`Setting ip to ${lastIp}`);
}

lastToken = trackRes;
lastTokenNow = performance.now();

Expand All @@ -115,15 +139,19 @@ class VerifyAPI {
}

static async startTrackingVerified(params: RadarStartTrackingVerifiedParams) {
let { interval } = params;
if (!interval) {
interval = 60;
Logger.info(`interval not provided, using 60 seconds`);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adds default interval of 60 seconds, avoids bug if interval undefined

}

const doTrackVerified = async () => {
let trackRes;
try {
trackRes = await this.trackVerified(params);
} catch (err: any) {
Logger.error(`trackVerified error: ${err.message}`);
}

const { interval } = params;

let expiresIn = 0;
let minInterval = interval;
Expand Down Expand Up @@ -164,6 +192,10 @@ class VerifyAPI {
static async getVerifiedLocationToken(params: RadarTrackVerifiedParams) {
const lastTokenElapsed = (performance.now() - lastTokenNow) / 1000;

if (params.ipChanges) {
await this.checkIpChanges();
}

if (lastToken && lastToken.passed) {
if (lastTokenElapsed < (lastToken.expiresIn || 0)) {
return lastToken;
Expand Down
8 changes: 6 additions & 2 deletions src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,16 @@ class Http {
if (host && (host === 'http://localhost:52516' || host === 'https://radar-verify.com:52516')) {
reject(new RadarVerifyAppError());
} else {
reject(new RadarServerError());
reject(new RadarNetworkError());
}
}

xhr.ontimeout = function () {
reject(new RadarVerifyAppError());
if (host && (host === 'http://localhost:52516' || host === 'https://radar-verify.com:52516')) {
reject(new RadarVerifyAppError());
} else {
reject(new RadarNetworkError());
}
}

xhr.send(JSON.stringify(body));
Expand Down
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,13 @@ export interface RadarTrackVerifiedParams {
description?: string;
metadata?: RadarMetadata;
skipVerifyApp?: boolean;
ipChanges?: boolean;
}

export interface RadarStartTrackingVerifiedParams {
interval: number;
skipVerifyApp?: boolean;
ipChanges?: boolean;
}

export enum RadarEventConfidence {
Expand Down Expand Up @@ -217,6 +219,7 @@ export interface RadarUser {
dma?: RadarRegion;
postalCode?: RadarRegion;
fraud?: RadarFraud;
ip?: string;
}

export interface RadarTrackResponse extends RadarResponse {
Expand Down
2 changes: 1 addition & 1 deletion src/ui/RadarMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class RadarMap extends maplibregl.Map {
radarMapOptions,
{ style },
);
Logger.debug('map initailized with options', mapOptions);
Logger.debug('map initialized with options', mapOptions);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix typo


(mapOptions as maplibregl.MapOptions).transformRequest = (url, resourceType) => {
// this handles when a style is switched
Expand Down
2 changes: 1 addition & 1 deletion src/version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default '4.4.8';
export default '4.4.10-beta.1';