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

[INTER-708] Response transparency and webhook validation #104

Merged
merged 45 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
24af300
build: update lockfile
TheUnderScorer May 28, 2024
45a8866
chore: update schema and examples
TheUnderScorer May 28, 2024
a664c2d
feat: add revision string field to confidence object
TheUnderScorer May 28, 2024
c3ca8d7
feat: add os Mismatch
TheUnderScorer May 28, 2024
9f581ab
feat: add delete API
TheUnderScorer May 28, 2024
3b098cb
docs: remove yarn mentions
TheUnderScorer May 28, 2024
33aab60
chore: add deleteVisitor example
TheUnderScorer May 28, 2024
b4d7652
feat: add checkWebhookHeader function for validating webhook signatur…
TheUnderScorer May 29, 2024
79b2fc7
feat: expose raw response in errors
TheUnderScorer May 29, 2024
5bd279a
ci: remove --frozen-lockfile flag
TheUnderScorer May 29, 2024
5b5240e
ci: use pnpm 9
TheUnderScorer May 31, 2024
f65f484
ci: add debug ls -a
TheUnderScorer May 31, 2024
b2cb329
ci: change trigger
TheUnderScorer May 31, 2024
4400973
ci: revert debug changes
TheUnderScorer May 31, 2024
b93d1d3
refactor: use ES in examples
TheUnderScorer May 31, 2024
aee3242
feat: provide error as-is without serialization in `getEvent` method
TheUnderScorer Jun 3, 2024
fb594a3
chore: apply suggestions
TheUnderScorer Jun 3, 2024
ef69009
docs(README): remove ipLocation field
TheUnderScorer Jun 3, 2024
ef80a2a
refactor: remove dual cast
TheUnderScorer Jun 3, 2024
0e1dbd8
chore: fix invalid type check
TheUnderScorer Jun 3, 2024
4cad313
docs: use err.status
TheUnderScorer Jun 3, 2024
931d3b7
docs: fix getEvent example
TheUnderScorer Jun 3, 2024
561055a
test: update snapshot
TheUnderScorer Jun 3, 2024
c952325
chore: fix Readme snippets, update prettier config and format
JuroUhlar Jun 3, 2024
f8abd7f
refactor: rename to isValidHmacSignature
TheUnderScorer Jun 10, 2024
84c3001
Merge remote-tracking branch 'origin/feature/INTER-708-response-trans…
TheUnderScorer Jun 10, 2024
d280d47
build: replace API reference with typedoc
TheUnderScorer Jun 10, 2024
993f3c2
chore: mark part of exported functions as private
TheUnderScorer Jun 10, 2024
1a2a21a
refactor: rename webhook signature functions
TheUnderScorer Jun 12, 2024
fa3fa9b
refactor: merge ifs
TheUnderScorer Jun 12, 2024
673f7ad
feat: make `Options.region` optional
TheUnderScorer Jun 12, 2024
5e695af
chore: improve examples
TheUnderScorer Jun 12, 2024
eb8d494
refactor: fix typo in JSDoc
TheUnderScorer Jun 12, 2024
b741200
chore: import DecryptionAlgorithm in sealed results example
TheUnderScorer Jun 12, 2024
c9a306c
chore: dx and documentation improvements
JuroUhlar Jun 12, 2024
8daf4d4
chore: improve examples
JuroUhlar Jun 12, 2024
a7a176e
chore: readme typos
JuroUhlar Jun 12, 2024
222b23b
chore: fix functional_tests
JuroUhlar Jun 12, 2024
3c130c0
chore: fix examples in contributing.md
JuroUhlar Jun 12, 2024
de3ddcd
chore: only run functional tests on main and test?
JuroUhlar Jun 12, 2024
0ddf2e6
chore: run functional tests on the PR branch
JuroUhlar Jun 12, 2024
e779ff1
chore: remove unnecessary fallbacks
JuroUhlar Jun 13, 2024
6d142b8
Merge pull request #105 from fingerprintjs/uhlar/dx-improvements-and-…
JuroUhlar Jun 13, 2024
b58a153
ci: trigger
TheUnderScorer Jun 13, 2024
76b4e15
Merge remote-tracking branch 'origin/feature/INTER-708-response-trans…
TheUnderScorer Jun 13, 2024
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
6 changes: 3 additions & 3 deletions .github/workflows/coverage-report.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Coverage report
name: Generate docs and coverage report

on:
push:
Expand All @@ -10,6 +10,6 @@ jobs:
name: Coverage report
uses: fingerprintjs/dx-team-toolkit/.github/workflows/docs-and-coverage.yml@v1
with:
skip-docs-step: true
prepare-gh-pages-commands: |
mv coverage/lcov-report/* ./gh-pages
mv docs/* ./gh-pages
mv coverage/lcov-report ./gh-pages/coverage
13 changes: 7 additions & 6 deletions .github/workflows/functional_tests.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Functional tests
on:
workflow_dispatch:
pull_request_target:
pull_request:
schedule:
- cron: '0 5 * * *'
jobs:
Expand All @@ -12,7 +12,6 @@ jobs:
artifactName: node-sdk-artifact
artifactPath: ./dist


functional_tests:
name: 'Smoke test on node ${{ matrix.node-version }}'
needs: build
Expand All @@ -22,7 +21,7 @@ jobs:
max-parallel: 3
fail-fast: false
matrix:
node-version: [ 18, 19, 20, 21 ]
node-version: [18, 19, 20, 21]

steps:
- if: ${{ github.event_name == 'pull_request_target' }}
Expand All @@ -36,7 +35,7 @@ jobs:
- name: 'Install pnpm'
uses: pnpm/action-setup@129abb77bf5884e578fcaf1f37628e41622cc371
with:
version: 8
version: 9

- uses: actions/setup-node@v4
with:
Expand All @@ -47,7 +46,7 @@ jobs:
name: node-sdk-artifact
path: ./dist
- name: Install Dependencies for SDK
run: pnpm install --frozen-lockfile
run: pnpm install
env:
CI: true
- name: Install Dependencies for example
Expand All @@ -56,7 +55,9 @@ jobs:
env:
CI: true
- name: Run test
run: node index.js
run: |
node getEvent.mjs
node getVisitorHistory.mjs
working-directory: ./example
env:
API_KEY: '${{ secrets.PRIVATE_KEY }}'
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,6 @@ dist
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
.pnp.*

docs
33 changes: 21 additions & 12 deletions contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Working with code

We prefer using [yarn](https://yarnpkg.com/) for installing dependencies and running scripts.
We prefer using [pnpm](https://pnpmpkg.com/) for installing dependencies and running scripts.

The main branch is locked for the push action. For proposing changes, use the standard [pull request approach](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request). It's recommended to discuss fixes or new functionality in the Issues, first.

Expand All @@ -11,7 +11,7 @@ The main branch is locked for the push action. For proposing changes, use the st
Run the following command that will regenerate types:

```shell
yarn generateTypes
pnpm generateTypes
```

It uses schema stored in [res/schema.json](resources/fingerprint-server-api.yaml). To fetch the latest schema run:
Expand All @@ -25,21 +25,29 @@ It uses schema stored in [res/schema.json](resources/fingerprint-server-api.yaml
Just run:

```shell
yarn build
pnpm build
```

### How to build API reference documentation

Run:

```shell
pnpm run docs
```

### Code style

The code style is controlled by [ESLint](https://eslint.org/) and [Prettier](https://prettier.io/). Run to check that the code style is ok:

```shell
yarn lint
pnpm lint
```

You aren't required to run the check manually, the CI will do it. Run the following command to fix style issues (not all issues can be fixed automatically):

```shell
yarn lint:fix
pnpm lint:fix
```

### Running tests
Expand All @@ -49,7 +57,7 @@ Tests are located in `tests` folder and run by [jest](https://jestjs.io/) in nod
To run tests you can use IDE instruments or just run:

```shell
yarn test
pnpm test
```

### Testing the local source code of the SDK
Expand All @@ -60,19 +68,20 @@ Use the `example` folder to make API requests using the local version of the SDK
2. Install dependencies and build the SDK (inside the root folder):

```shell
yarn install
yarn build
pnpm install
pnpm build
```

3. Install dependencies and run the example (inside the `example` folder)):
3. Install dependencies and run the examples (inside the `example` folder)):

```shell
cd example
yarn install
node index.js
pnpm install
node getEvent.mjs
node getVisitorHistory.mjs
```

Every time you change the SDK code, you need to rebuild it in the root folder using `yarn build` and then run the example again.
Every time you change the SDK code, you need to rebuild it in the root folder using `pnpm build` and then run the example again.

### How to publish

Expand Down
5 changes: 4 additions & 1 deletion example/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ API_KEY=<API_KEY>
VISITOR_ID=<VISITOR_ID>
REQUEST_ID=<REQUEST_ID>
# "eu" or "ap", "us" is the default
REGION=<REGION>
REGION=<REGION>
WEBHOOK_SIGNATURE_SECRET=<WEBHOOK_SIGNATURE_SECRET>
BASE64_KEY=<BASE64_KEY>
BASE64_SEALED_RESULT=<BASE64_SEALED_RESULT>
38 changes: 38 additions & 0 deletions example/deleteVisitor.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { FingerprintJsServerApiClient, Region, isDeleteVisitorError } from '@fingerprintjs/fingerprintjs-pro-server-api'
import { config } from 'dotenv'
config()

const apiKey = process.env.API_KEY
const visitorId = process.env.VISITOR_ID
const envRegion = process.env.REGION

if (!visitorId) {
console.error('Visitor ID not defined')
process.exit(1)
}

if (!apiKey) {
console.error('API key not defined')
process.exit(1)
}

let region = Region.Global
if (envRegion === 'eu') {
region = Region.EU
} else if (envRegion === 'ap') {
region = Region.AP
}

const client = new FingerprintJsServerApiClient({ region, apiKey })

try {
await client.deleteVisitorData(visitorId)
console.log(`All data associated with visitor ${visitorId} is scheduled to be deleted.`)
} catch (error) {
if (isDeleteVisitorError(error)) {
console.log(error.status, error.error)
} else {
console.error('unknown error: ', error)
}
process.exit(1)
}
40 changes: 40 additions & 0 deletions example/getEvent.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { FingerprintJsServerApiClient, Region, isEventError } from '@fingerprintjs/fingerprintjs-pro-server-api'
import { config } from 'dotenv'
config()

const apiKey = process.env.API_KEY
const requestId = process.env.REQUEST_ID
const envRegion = process.env.REGION

if (!requestId) {
console.error('Request ID not defined')
process.exit(1)
}

if (!apiKey) {
console.error('API key not defined')
process.exit(1)
}

let region = Region.Global
if (envRegion === 'eu') {
region = Region.EU
} else if (envRegion === 'ap') {
region = Region.AP
}

const client = new FingerprintJsServerApiClient({ region, apiKey })

try {
const event = await client.getEvent(requestId)
console.log(JSON.stringify(event, null, 2))
} catch (error) {
if (isEventError(error)) {
console.log(`error ${error.status}: `, error.error?.message)
// You can also access the raw response
console.log(error.response.statusText)
} else {
console.log('unknown error: ', error)
}
process.exit(1)
}
48 changes: 48 additions & 0 deletions example/getVisitorHistory.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { FingerprintJsServerApiClient, Region, isVisitorsError } from '@fingerprintjs/fingerprintjs-pro-server-api'
import { config } from 'dotenv'
config()

const apiKey = process.env.API_KEY
const visitorId = process.env.VISITOR_ID
const envRegion = process.env.REGION

if (!visitorId) {
console.error('Visitor ID not defined')
process.exit(1)
}

if (!apiKey) {
console.error('API key not defined')
process.exit(1)
}

let region = Region.Global
if (envRegion === 'eu') {
region = Region.EU
} else if (envRegion === 'ap') {
region = Region.AP
}

const client = new FingerprintJsServerApiClient({ region, apiKey })

try {
const visitorHistory = await client.getVisitorHistory(visitorId, { limit: 10 })
console.log(JSON.stringify(visitorHistory, null, 2))
} catch (error) {
if (isVisitorsError(error)) {
console.log(error.status, error.error)
if (error.status === 429) {
retryLater(error.retryAfter) // Needs to be implemented on your side
}
} else {
console.error('unknown error: ', error)
}
process.exit(1)
}

/**
* @param {number} delay - How many seconds to wait before retrying
*/
function retryLater(delay) {
console.log(`Implement your own retry logic here and retry after ${delay} seconds`)
}
36 changes: 0 additions & 36 deletions example/index.js

This file was deleted.

2 changes: 1 addition & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "fingerprintjs-pro-server-api-node-sdk-example",
"version": "1.0.0",
"description": "",
"main": "index.js",
"main": "index.mjs",
"private": true,
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
Expand Down
26 changes: 0 additions & 26 deletions example/sealedResults.js

This file was deleted.

Loading
Loading