Skip to content

Commit 0ffac13

Browse files
Environment configuration
1 parent 4f5c5ba commit 0ffac13

File tree

12 files changed

+259
-100
lines changed

12 files changed

+259
-100
lines changed

.eslintignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
/dist/*
22
/lib/*
3-
/examples/*
43
!.*

.eslintrc.cjs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,12 @@ module.exports = {
88
rules: {
99
'@typescript-eslint/no-explicit-any': ['warn'],
1010
},
11+
overrides: [
12+
{
13+
files: ['./examples/**'],
14+
rules: {
15+
'@typescript-eslint/no-unused-vars': 'off',
16+
}
17+
},
18+
]
1119
};

.github/workflows/run-smoke-test.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ jobs:
1616
registry-url: https://registry.npmjs.org/
1717
- run: npm install
1818
- run: npm run build
19-
- run: npm install
20-
working-directory: ./examples
2119
- run: npm install && node --unhandled-rejections=strict index.js
2220
working-directory: ./examples/basic/
2321
env:

examples/auth/client/index.js

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,40 @@ import {
99
Light,
1010
SecurityFeatureType,
1111
} from '@regulaforensics/document-reader-webclient';
12-
import axios from "axios";
13-
import qs from "qs";
12+
import axios from 'axios';
13+
import qs from 'qs';
1414

15-
const GATEWAY_BASE_URL = "http://localhost:8080"
15+
const GATEWAY_BASE_URL = 'http://localhost:8080';
1616

1717
const { PORTRAIT, DOCUMENT_FRONT } = GraphicFieldType;
1818
const { DOCUMENT_NUMBER } = TextFieldType;
1919

2020
async function get_authorization_token() {
2121
let data = qs.stringify({
22-
'grant_type': 'password',
23-
'username': 'testuser1',
24-
'password': 't3stP@ss',
25-
'client_id': 'account',
26-
'scope': 'openid'
22+
grant_type: 'password',
23+
username: 'testuser1',
24+
password: 't3stP@ss',
25+
client_id: 'account',
26+
scope: 'openid',
2727
});
2828

2929
let config = {
30-
method: 'post',
31-
maxBodyLength: Infinity,
32-
url: `${GATEWAY_BASE_URL}/realms/regula/protocol/openid-connect/token`,
33-
headers: {
34-
'Content-Type': 'application/x-www-form-urlencoded'
35-
},
36-
data : data
37-
};
38-
return axios.request(config)
39-
.then((response) => {return response.data["access_token"]})
40-
.catch((error) => {console.log(error)});
30+
method: 'post',
31+
maxBodyLength: Infinity,
32+
url: `${GATEWAY_BASE_URL}/realms/regula/protocol/openid-connect/token`,
33+
headers: {
34+
'Content-Type': 'application/x-www-form-urlencoded',
35+
},
36+
data: data,
37+
};
38+
return axios
39+
.request(config)
40+
.then((response) => {
41+
return response.data['access_token'];
42+
})
43+
.catch((error) => {
44+
console.log(error);
45+
});
4146
}
4247

4348
(async () => {
@@ -47,9 +52,12 @@ async function get_authorization_token() {
4752
if (fs.existsSync('regula.license')) {
4853
license = fs.readFileSync('regula.license');
4954
}
50-
const token = await get_authorization_token()
55+
const token = await get_authorization_token();
5156

52-
const api = new DocumentReaderApi({ basePath: apiBasePath, baseOptions: {headers: {"Authorization": `Bearer ${token}`}}});
57+
const api = new DocumentReaderApi({
58+
basePath: apiBasePath,
59+
baseOptions: { headers: { Authorization: `Bearer ${token}` } },
60+
});
5361

5462
api.setLicense(license);
5563

examples/auth/client/package-lock.json

Lines changed: 92 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/auth/client/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"type": "module",
44
"dependencies": {
55
"@regulaforensics/document-reader-webclient": "file:../../..",
6+
"axios": "^1.6.2",
67
"qs": "^6.11.2"
78
}
89
}

examples/auth/server/.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

examples/auth/server/auth.js

Lines changed: 33 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,46 @@
1-
import axios from "axios";
2-
import jwt from "jsonwebtoken";
3-
import jwkToPem from "jwk-to-pem";
4-
import { path, find, compose, flip, curryN } from "ramda";
1+
import axios from 'axios';
2+
import jwt from 'jsonwebtoken';
3+
import jwkToPem from 'jwk-to-pem';
4+
import { path, find, compose, flip, curryN } from 'ramda';
55

66
const verify = curryN(2)(jwt.verify);
7-
const IDP_KEYCLOAK_URL = "http://keycloak:8080";
8-
const IDP_KEYCLOAK_REALM = "regula";
9-
10-
const findPublicKeyFromKid = publicKey => kid =>
11-
find(isTheRightKid(kid))(publicKey);
12-
13-
const getKid = path(["header", "kid"]);
14-
15-
const decode = compose(
16-
curryN(2),
17-
flip
18-
)(jwt.decode);
19-
20-
const getUserFromPublicKey = token =>
21-
compose(
22-
makeUser,
23-
verify(token)
24-
);
25-
26-
const makeUser = ({
27-
sub,
28-
preferred_username,
29-
email_verified,
30-
resource_access,
31-
email,
32-
name,
33-
...others
34-
}) => ({
7+
const IDP_KEYCLOAK_URL = 'http://keycloak:8080';
8+
const IDP_KEYCLOAK_REALM = 'regula';
9+
10+
const findPublicKeyFromKid = (publicKey) => (kid) => find(isTheRightKid(kid))(publicKey);
11+
12+
const getKid = path(['header', 'kid']);
13+
14+
const decode = compose(curryN(2), flip)(jwt.decode);
15+
16+
const getUserFromPublicKey = (token) => compose(makeUser, verify(token));
17+
18+
const makeUser = ({ sub, preferred_username, email_verified, resource_access, email, name, ...others }) => ({
3519
id: sub,
3620
userName: preferred_username,
3721
emailVerified: email_verified,
3822
resourceAccess: resource_access,
3923
email,
4024
name,
41-
...others
42-
});
25+
...others,
26+
});
4327

44-
const isTheRightKid = kid => publicKey => publicKey.kid === kid;
28+
const isTheRightKid = (kid) => (publicKey) => publicKey.kid === kid;
4529

46-
const getUserFromJWK = token => jwk =>
47-
compose(
48-
getUserFromPublicKey(token),
49-
jwkToPem,
50-
findPublicKeyFromKid(jwk),
51-
getKid,
52-
decode({ complete: true })
53-
)(token);
30+
const getUserFromJWK = (token) => (jwk) =>
31+
compose(
32+
getUserFromPublicKey(token),
33+
jwkToPem,
34+
findPublicKeyFromKid(jwk),
35+
getKid,
36+
decode({ complete: true }),
37+
)(token);
5438

5539
export const verifyOffline = (accessToken, options = {}) => {
56-
const url = `${IDP_KEYCLOAK_URL}/realms/${IDP_KEYCLOAK_REALM}/protocol/openid-connect/certs`;
40+
const url = `${IDP_KEYCLOAK_URL}/realms/${IDP_KEYCLOAK_REALM}/protocol/openid-connect/certs`;
5741

58-
return axios.get(url)
59-
.then(path(["data", "keys"]))
60-
.then(getUserFromJWK(accessToken))
61-
};
42+
return axios
43+
.get(url)
44+
.then(path(['data', 'keys']))
45+
.then(getUserFromJWK(accessToken));
46+
};

0 commit comments

Comments
 (0)