Skip to content

Commit

Permalink
Fixing public web demo (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
usmanliaqat97 authored Nov 2, 2023
1 parent ca62d6f commit d5197cb
Show file tree
Hide file tree
Showing 23 changed files with 595 additions and 340 deletions.
3 changes: 1 addition & 2 deletions demo/.dockerignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
node_modules/
src/static/yarn.lock
.env*
3 changes: 3 additions & 0 deletions demo/.env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
EXAMPLE_SERVER_PORT=8080
PORT=8080
BASE_URL=https://eu.rp.secure.iproov.me
API_KEY=
API_SECRET=

NPM_ACCESS_TOKEN=
1 change: 1 addition & 0 deletions demo/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
9 changes: 0 additions & 9 deletions demo/.prettierrc

This file was deleted.

47 changes: 38 additions & 9 deletions demo/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,42 @@
FROM node:lts-alpine
# BASE
FROM node:18-alpine as base

ARG NPM_ACCESS_TOKEN="${NPM_ACCESS_TOKEN}"
ENV NPM_ACCESS_TOKEN $NPM_ACCESS_TOKEN


# INSTALLER
FROM base as installer

WORKDIR /app

COPY ./src ./src
COPY ./package.json ./package.json
COPY ./yarn.lock ./yarn.lock

# 1. install server deps
RUN yarn install --ignore-scripts --frozen-lockfile

# XXX:NOTE: There are no npm build for the FE app
# 2. install separately only web-sdk in "static" as static module
RUN npm config set '//registry.npmjs.org/:_authToken' "${NPM_ACCESS_TOKEN}"
RUN cd src/static && yarn install --ignore-scripts --frozen-lockfile


# BUILDER
FROM base as builder

WORKDIR /app

# There are no build for the server - just copy what is required
COPY --from=installer /app .


# RUNNER
FROM base as runner

WORKDIR /app
ADD src/yarn.lock .
ADD src/package.json .
RUN yarn
ADD src/static/package.json ./static/
RUN cd /app/static && yarn && cd /app
ADD src/ .

EXPOSE 80
COPY --from=builder /app .

ENTRYPOINT ["node", "server.js"]
CMD yarn start
2 changes: 1 addition & 1 deletion demo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

This demo is a self-contained NodeJS server and minimal frontend which covers:

* Running a server with a `BASE_URL`, `API_KEY` and `API_SECRET` that you can obtain from https://portal.iproov.com;
* Running a server with a `BASE_URL`, `API_KEY`, `API_SECRET` that you can obtain from https://portal.iproov.com and `NPM_ACCESS_TOKEN` that you can create once your get access to our private NPM registry (please contact [email protected]).
* Creating tokens for Genuine Presence and Liveness transactions;
* Enrolling and verifying;
* Customising iProov Web with web component slots and CustomEvents.
Expand Down
21 changes: 21 additions & 0 deletions demo/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "@iproov/web-demo-sample",
"version": "2.0.0",
"description": "A demo of iProov's Web SDK integration",
"main": "server.js",
"author": "iProov Team",
"license": "GPL",
"private": true,
"type": "module",
"scripts": {
"start": "node src/server.js"
},
"dependencies": {
"@hapi/basic": "^6.0.0",
"@hapi/hapi": "^20.2.2",
"@hapi/inert": "^6.0.4",
"dotenv": "^8.6.0",
"prettier": "^2.7.1",
"superagent": "^6.1.0"
}
}
6 changes: 4 additions & 2 deletions demo/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ source "$ENV_FILE"

EXAMPLE_SERVER_PORT=${EXAMPLE_SERVER_PORT:=8080}

yarn install

if [ ! -r "$ENV_FILE" ]
then
echo "Missing environment file: $ENV_FILE"
echo "Use the template in .env.example and populate with keys from https://portal.iproov.com."
exit 1
fi

docker build . -t iproov-web-example
docker run --env-file "$ENV_FILE" -e EXAMPLE_SERVER_PORT="$EXAMPLE_SERVER_PORT" -e IS_DOCKER=1 -p "0.0.0.0:$EXAMPLE_SERVER_PORT":80 --rm iproov-web-example
docker build -t iproov-web-demo --build-arg NPM_ACCESS_TOKEN=$NPM_ACCESS_TOKEN .
docker run --env-file "$ENV_FILE" -e EXAMPLE_SERVER_PORT="$EXAMPLE_SERVER_PORT" -e PORT="$PORT" -p "$EXAMPLE_SERVER_PORT":"$PORT" --rm iproov-web-demo
42 changes: 25 additions & 17 deletions demo/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,52 @@
* @return {{API_KEY: *, API_SECRET: *, BASE_URL: *, EXAMPLE_SERVER_PORT: *, PORT: *}}
*/
export function configure(env) {
const { BASE_URL, API_KEY, API_SECRET } = env
let { PORT, EXAMPLE_SERVER_PORT } = env
const errors = []
const { BASE_URL, API_KEY, API_SECRET } = env;
let { PORT, EXAMPLE_SERVER_PORT } = env;
const errors = [];
if (!BASE_URL) {
errors.push("BASE_URL is undefined. Example: https://eu.rp.secure.iproov.me.")
errors.push(
"BASE_URL is undefined. Example: https://eu.rp.secure.iproov.me."
);
}
if (!API_KEY) {
errors.push("API_KEY is undefined. You can obtain one from https://portal.iproov.com.")
errors.push(
"API_KEY is undefined. You can obtain one from https://portal.iproov.com."
);
} else if (API_KEY.length < 40) {
errors.push("API_KEY seems incorrect, it should be a 40 character alphanumeric string.")
errors.push(
"API_KEY seems incorrect, it should be a 40 character alphanumeric string."
);
}
if (!API_SECRET) {
errors.push(
"API_SECRET is undefined. It must correspond to your API_KEY. If lost, reset it at https://portal.iproov.com."
)
);
} else if (API_SECRET.length < 40) {
errors.push("API_SECRET seems incorrect, it should be a 40 character alphanumeric string.")
errors.push(
"API_SECRET seems incorrect, it should be a 40 character alphanumeric string."
);
}

if (!PORT) {
// This is the internal container port that the EXAMPLE_SERVER_PORT should map to. Best left at 80.
PORT = 80
PORT = 80;
}
if (!EXAMPLE_SERVER_PORT) {
console.warn("EXAMPLE_SERVER_PORT not specified, using port 80.")
EXAMPLE_SERVER_PORT = 80
console.warn("EXAMPLE_SERVER_PORT not specified, using port 80.");
EXAMPLE_SERVER_PORT = 80;
}
if (errors.length) {
console.error("Configuration problems detected:")
errors.forEach((e) => console.error(e))
console.error("Contact [email protected] for assistance.")
process.exit(1)
return
console.error("Configuration problems detected:");
errors.forEach((e) => console.error(e));
console.error("Contact [email protected] for assistance.");
process.exit(1);
}
return {
BASE_URL,
API_KEY,
API_SECRET,
PORT,
EXAMPLE_SERVER_PORT,
}
};
}
2 changes: 2 additions & 0 deletions demo/src/etc/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.key
*.crt
18 changes: 0 additions & 18 deletions demo/src/package.json

This file was deleted.

52 changes: 26 additions & 26 deletions demo/src/platform.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
import superagent from "superagent"
import superagent from "superagent";

/**
* Lightweight Platform v2 wrapper
* @see https://eu.rp.secure.iproov.me/docs.html
*/
export class PlatformAPI {
constructor(logger, baseUrl, apiKey, apiSecret) {
this.logger = logger
this.baseUrl = baseUrl
this.apiKey = apiKey
this.apiSecret = apiSecret
this.logger = logger;
this.baseUrl = baseUrl;
this.apiKey = apiKey;
this.apiSecret = apiSecret;
}

apiUrl(endpoint) {
return `${this.baseUrl}/api/v2/${endpoint}`
return `${this.baseUrl}/api/v2/${endpoint}`;
}

withJsonAuth(payload) {
return {
api_key: this.apiKey,
secret: this.apiSecret,
...payload,
}
};
}

withHeaders() {
return {
"accept": "json",
accept: "json",
"user-agent": "superagent",
}
};
}

/**
Expand All @@ -38,28 +38,28 @@ export class PlatformAPI {
*/
async unpack(request) {
try {
const response = await request
return { ...response.body, base_url: this.baseUrl }
const response = await request;
return { ...response.body, base_url: this.baseUrl };
} catch (e) {
// @todo: indicate this is an error - currently we just pass through
if (e.status === 400) {
const { error, error_description } = e.response.body
this.logger.error("Error %s: %s", error, error_description)
const { error, error_description } = e.response.body;
this.logger.error("Error %s: %s", error, error_description);
}
return e.response.body
return e.response.body;
}
}

async token(mode, options = {}) {
const { userId, assuranceType } = options
const { userId, assuranceType } = options;
if (!userId) {
this.logger.warn(`Couldn't create a token because user_id is empty`)
this.logger.warn(`Couldn't create a token because user_id is empty`);
return {
error: "Missing User ID",
error_description: "You must provide a user_id to create a token.",
}
};
}
this.logger.info(`Creating ${assuranceType} ${mode} token for ${userId}`)
this.logger.info(`Creating ${assuranceType} ${mode} token for ${userId}`);
return this.unpack(
superagent
.post(this.apiUrl(`claim/${mode}/token`))
Expand All @@ -68,15 +68,15 @@ export class PlatformAPI {
this.withJsonAuth({
user_id: userId,
assurance_type: assuranceType,
resource: "Web SDK Example",
resource: "Web SDK Demo",
})
)
)
);
}

async validate(mode, options = { userId, token }) {
const { userId, token } = options
this.logger.info(`Validating ${mode} for ${userId} - token: ${token}`)
const { userId, token } = options;
this.logger.info(`Validating ${mode} for ${userId} - token: ${token}`);
return this.unpack(
superagent
.post(this.apiUrl(`claim/${mode}/validate`))
Expand All @@ -89,7 +89,7 @@ export class PlatformAPI {
client: "superagent",
})
)
)
);
}
}

Expand All @@ -101,12 +101,12 @@ export const requestToAPIMapper = Object.freeze({
return {
userId: request.payload.user_id,
assuranceType: request.payload.assurance_type || "genuine_presence",
}
};
},
validate(request) {
return {
token: request.payload.token,
userId: request.payload.user_id,
}
};
},
})
});
Loading

0 comments on commit d5197cb

Please sign in to comment.