Skip to content

Commit 894eaf7

Browse files
authored
Merge pull request #17 from veracity/feature/rc1
Updated samples, docs and readme
2 parents ebef582 + 60ac089 commit 894eaf7

16 files changed

Lines changed: 551 additions & 4 deletions

File tree

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Version `1.0.0` is the first officially released and supported implementation of
1616

1717
- [Quick Start](#quick-start)
1818
* [onVerify / Verifier](#onverify--verifier)
19+
- [Encrypting session](#encrypting-session)
1920
- [Passing state](#passing-state)
2021
- [Error handling](#error-handling)
2122
- [Authentication process](#authentication-process)
@@ -97,6 +98,39 @@ const verifier = async (tokenData, req, done) => {
9798
passport.use("veracity", new VIDPOpenIDCStrategy(strategySettings, verifier))
9899
```
99100

101+
## Encrypting session
102+
When configuring authentication you need to provide a place to store session data. This is done through the `store` configuration for `express-session`. In the samples we use a MemoryStore instance that keeps the data in memory, but this is not suitable to for production as it does not scale. For such systems you would probably go with a database or cache of some kind such as MySQL or Redis.
103+
104+
Once you set up such a session storage mechanism, however there are some considerations you need to take into account. Since the access tokens for individual users are stored as session data it means that anyone with access to the session storage database can extract any token for a currently logged in user and use it themselves. Since the token is the only key needed to perform actions on behalf of the user it is considered sensitive information and must therefore be protected accordingly.
105+
106+
This library comes with a helper function to deal with just this scenario called `createEncryptedSessionStore`. This function uses the **AES-256-CBC** algorithm to encrypt and decrypt a subset of session data on-the-fly preventing someone with access to the store from seeing the plain access tokens. They will only see an encrypted blob of text.
107+
108+
The way `createEncryptedSessionStore` works is that it replaces the read and write functions of an `express-session` compatible store with augmented versions that decrypt and encrypt a set of specified properties (if present on the session object) respectively. This means that you can still use any of the compatible store connectors and simply pass it through the helper function to get a version that provides encryption.
109+
110+
Using the Redis connector you can configure an encrypted session like this:
111+
```javascript
112+
const session = require("express-session")
113+
const { createEncryptedSessionStore } = require("@veracity/node-auth")
114+
const redisStore = require("connect-redis")(session)
115+
116+
// You should NOT hard-code the encryption key. It should be served from a secure store such as Azure KeyVault or similar
117+
const encryptedRedisStore = createEncryptedSessionStore("encryption key")(redisStore)
118+
119+
// We can now use the encryptedRedisStore in place of a regular store to configure authentication
120+
setupWebAppAuth({
121+
app,
122+
strategy: {
123+
clientId: "",
124+
clientSecret: "",
125+
replyUrl: ""
126+
},
127+
session: {
128+
secret: "ce4dd9d9-cac3-4728-a7d7-d3e6157a06d9",
129+
store: encryptedRedisStore // Use encrypted version of redis store
130+
}
131+
})
132+
```
133+
100134
## Passing state
101135
Sometimes it is useful to be able to pass data from before the login begins all the way through the authentication process until control is returned back to your code. This library supports this in two ways for web and native applications (the bearer token validation strategy does not support this):
102136

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@veracity/node-auth",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "A library for authenticating with Veracity and retrieving one or more access tokens for communicating with APIs.",
55
"scripts": {
66
"build:copy-files": "ts-node -T scripts/copy-files.ts",

samples/101-JS-Auth/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# JS Helper example
22
This example implements Veracity authentication using the `setupWebAppAuth` helper function. For details see the `start.js` file.
33

4-
You need to fill in application credentials on line 14-16 in `start.js` before this sample will run. Visit the [Veracity for Developers project portal](https://developer.veracity.com/projects) to create them.
4+
You need to fill in application credentials on line 23-25 in `start.js` before this sample will run. Visit the [Veracity for Developers project portal](https://developer.veracity.com/projects) to create them.
55

66
To run the sample:
77
```javascript

samples/101-JS-Auth/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
},
2424
"homepage": "https://developer.veracity.com",
2525
"dependencies": {
26+
"@veracity/node-auth": "^1.0.0",
2627
"body-parser": "^1.19.0",
2728
"express": "^4.17.1",
2829
"express-session": "^1.16.2",

samples/102-TS-Auth/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# TypeScript Helper example
2+
This example implements Veracity authentication using the `setupWebAppAuth` helper function and TypeScript. For details see the `start.ts` file.
3+
4+
You need to fill in application credentials on line 23-25 in `start.ts` before this sample will run. Visit the [Veracity for Developers project portal](https://developer.veracity.com/projects) to create them.
5+
6+
To run the sample:
7+
```javascript
8+
npm i
9+
npm start
10+
```
11+
12+
## HTTPS
13+
This sample uses `node-forge` along with the `generateCertificate` utility to create a self-signed certificate for local development. This is **not** suitable for production and should be replaced with a more secure certificate signed by a trusted third-party. For example: [https://letsencrypt.org/](https://letsencrypt.org/)
14+
15+
## Dependencies
16+
- `@veracity/node-auth`
17+
- `express`
18+
- `express-session`
19+
- `passport`
20+
- `node-forge`

samples/102-TS-Auth/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "@veracity/node-auth-js-helper-example",
2+
"name": "@veracity/node-auth-ts-helper-example",
33
"private": true,
44
"version": "1.0.0",
55
"description": "",
@@ -29,6 +29,7 @@
2929
"typescript": "^3.6.3"
3030
},
3131
"dependencies": {
32+
"@veracity/node-auth": "^1.0.0",
3233
"body-parser": "^1.19.0",
3334
"express": "^4.17.1",
3435
"express-session": "^1.16.2",

samples/103-JS-Passport/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# JS Passport example
2+
This example implements Veracity authentication using the `VIDPWebAppStrategy` passport strategy directly. This is intended for more advanced scenarios where your code or structure makes it hard or impossible to use the simpler helper function. This sample ends up with the same features as the ones using the helper, but with more code as we have to implement everything ourselves.
3+
4+
You need to fill in application credentials on line 33-35 in `start.js` before this sample will run. Visit the [Veracity for Developers project portal](https://developer.veracity.com/projects) to create them.
5+
6+
To run the sample:
7+
```javascript
8+
npm i
9+
npm start
10+
```
11+
12+
## HTTPS
13+
This sample uses `node-forge` along with the `generateCertificate` utility to create a self-signed certificate for local development. This is **not** suitable for production and should be replaced with a more secure certificate signed by a trusted third-party. For example: [https://letsencrypt.org/](https://letsencrypt.org/)
14+
15+
## Dependencies
16+
- `@veracity/node-auth`
17+
- `express`
18+
- `express-session`
19+
- `passport`
20+
- `node-forge`
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "@veracity/node-strategy-js-helper-example",
3+
"private": true,
4+
"version": "1.0.0",
5+
"description": "",
6+
"scripts": {
7+
"start": "node start.js"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/veracity/node-auth.git"
12+
},
13+
"keywords": [
14+
"veracity",
15+
"authentication",
16+
"openid",
17+
"typescript"
18+
],
19+
"author": "Veracity",
20+
"license": "MIT",
21+
"bugs": {
22+
"url": "https://github.com/veracity/node-auth/issues"
23+
},
24+
"homepage": "https://developer.veracity.com",
25+
"dependencies": {
26+
"@veracity/node-auth": "^1.0.0",
27+
"body-parser": "^1.19.0",
28+
"express": "^4.17.1",
29+
"express-session": "^1.16.2",
30+
"node-forge": "^0.9.1",
31+
"passport": "^0.4.0"
32+
}
33+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
7+
<title>Veracity node-auth - sample app</title>
8+
<style>
9+
nav a{
10+
display: inline-block;
11+
padding: 3px 10px;
12+
}
13+
nav a:first-child{
14+
padding-left: 0;
15+
}
16+
</style>
17+
</head>
18+
<body>
19+
<h1>Veracity Authentication with node</h1>
20+
<p>
21+
This example demonstrates how to set up basic authentication with Veracity and viewing the response.
22+
</p>
23+
<nav>
24+
<a href="/login?returnTo=/user">Login now</a>
25+
<a href="/user">View user data</a>
26+
<a href="/refresh">Refresh token</a>
27+
<a href="/logout">Logout</a>
28+
</nav>
29+
</body>
30+
</html>

0 commit comments

Comments
 (0)