2020-12-03 09:54:27 +01:00
|
|
|
# auth-server-helper
|
2020-03-06 12:06:10 +01:00
|
|
|
|
2022-01-03 14:44:27 +01:00
|
|
|
version: 2.1.x
|
2020-05-17 17:37:41 +02:00
|
|
|
|
2021-01-05 16:50:23 +01:00
|
|
|
customizable and simple authentication
|
|
|
|
|
2020-05-17 17:37:41 +02:00
|
|
|
## Installation
|
|
|
|
|
|
|
|
npm:
|
|
|
|
|
2020-12-03 09:54:27 +01:00
|
|
|
> npm i --save auth-server-helper
|
2020-05-17 17:37:41 +02:00
|
|
|
|
|
|
|
yarn:
|
|
|
|
|
2020-12-03 09:54:27 +01:00
|
|
|
> yarn add auth-server-helper
|
2020-03-06 12:06:10 +01:00
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
2021-01-05 16:50:23 +01:00
|
|
|
### 1. put a gateway in front of the routes you want to secure
|
|
|
|
|
|
|
|
```js
|
|
|
|
const {create_gateway} = require('@sapphirecode/auth-server-helper');
|
|
|
|
|
|
|
|
const gateway = create_gateway({
|
2022-01-03 14:44:27 +01:00
|
|
|
redirect_url: '/auth', // if defined, unauthorized requests will be redirected
|
2021-01-05 16:50:23 +01:00
|
|
|
cookie_name: 'auth_cookie', // if defined, access tokens will be read from this cookie
|
|
|
|
});
|
|
|
|
|
|
|
|
// express
|
|
|
|
app.use(gateway);
|
|
|
|
|
|
|
|
// node http
|
|
|
|
http.createServer((main_req, main_res) =>
|
|
|
|
gateway(main_req, main_res, (req, res) => {
|
|
|
|
// your request handler
|
|
|
|
});
|
|
|
|
);
|
|
|
|
```
|
|
|
|
|
|
|
|
the gateway will forward any authorized requests to the next handler and
|
|
|
|
redirect all others to the specified url
|
|
|
|
|
2022-01-03 14:44:27 +01:00
|
|
|
#### 1.1. Creating a gateway for manual processing of requests
|
|
|
|
|
|
|
|
```js
|
|
|
|
const {GatewayClass} = require('@sapphirecode/auth-server-helper');
|
|
|
|
|
|
|
|
const gateway = new GatewayClass({ /* options */ }); // options are the same as for create_gateway above
|
|
|
|
|
|
|
|
// process a request
|
|
|
|
if (gateway.authenticate(http_request)) { // returns true if request is valid and sets req.connection.token_id and .token_data
|
|
|
|
console.log('access granted');
|
|
|
|
} else {
|
|
|
|
gateway.redirect(response); // redirects the client, triggers deny if no redirect_url was set in options
|
|
|
|
// or
|
|
|
|
gateway.deny(response); // sends status 403
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-01-05 16:50:23 +01:00
|
|
|
### 2. creating the auth endpoint
|
|
|
|
|
|
|
|
```js
|
|
|
|
const {create_auth_handler} = require('@sapphirecode/auth-server-helper');
|
|
|
|
|
|
|
|
const handler = create_auth_handler(
|
2021-01-06 16:06:03 +01:00
|
|
|
async (req) => {
|
2021-01-05 16:50:23 +01:00
|
|
|
if (req.user === 'foo' && req.password === 'bar')
|
2021-01-06 16:06:03 +01:00
|
|
|
const {access_token_id, refresh_token_id} = await req.allow_access({
|
2021-01-05 16:50:23 +01:00
|
|
|
access_token_expires_in: 600, // seconds until access tokens expire
|
|
|
|
include_refresh_token: true, // should the answer include a refresh token? default: false
|
|
|
|
refresh_token_expires_in: 3600, // seconds until refresh tokens expire (required if refresh tokens are generated)
|
|
|
|
data: {user: 'foo'}, // additional custom data to include in the token
|
|
|
|
});
|
|
|
|
|
|
|
|
if (req.user === 'part' && req.password === 'baz')
|
2021-01-06 16:06:03 +01:00
|
|
|
const part_id = await req.allow_part(
|
2021-01-05 16:50:23 +01:00
|
|
|
60, // seconds until part_token expires
|
|
|
|
'some_module', // next module handler (defined below)
|
|
|
|
{foo: 'bar'} // custom data to attach to the token
|
|
|
|
);
|
|
|
|
|
|
|
|
// all allow_ functions return a token id, which can later be used to invalidate specific tokens from the server side
|
|
|
|
|
|
|
|
req.deny();
|
|
|
|
},
|
|
|
|
{
|
|
|
|
refresh: {
|
|
|
|
/*...same options as allow_access */
|
|
|
|
}, // define the behaviour of refresh tokens. Refresh tokens will not be accepted if this option is undefined
|
|
|
|
modules: {
|
|
|
|
some_module(req) {
|
|
|
|
// request handlers for part_tokens
|
|
|
|
|
|
|
|
// access custom data:
|
|
|
|
const auth_data = req.request.connection.auth;
|
|
|
|
auth_data.token_id; // token id
|
|
|
|
auth_data.token_data; // custom data
|
|
|
|
// the same works in handlers after the gateway, information is always stored in request.connection.auth
|
|
|
|
},
|
|
|
|
},
|
|
|
|
cookie_name: 'auth_cookie', // if defined, access tokens will be stored in this cookie
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
// express
|
|
|
|
app.use(handler);
|
|
|
|
|
|
|
|
// node http
|
|
|
|
// ... create server, on path /auth run the handler
|
2022-01-03 15:40:13 +01:00
|
|
|
handler(req, res); // the handler will also return true if allow_access or allow_part was called
|
2021-01-05 16:50:23 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
after the auth handler, the request will be completed, no additional content
|
|
|
|
should be served here.
|
|
|
|
|
|
|
|
### Invalidating tokens after they are delivered to the client
|
|
|
|
|
|
|
|
```js
|
|
|
|
const {blacklist} = require('@sapphirecode/auth-server-helper');
|
|
|
|
|
|
|
|
blacklist.add_signature(token_id); // the token id is returned from any function that creates tokens
|
|
|
|
```
|
2020-05-17 17:37:41 +02:00
|
|
|
|
2021-01-08 13:30:53 +01:00
|
|
|
### Exporting and importing public keys to validate tokens across server instances
|
|
|
|
|
|
|
|
```js
|
|
|
|
const {keystore} = require('@sapphirecode/auth-server-helper');
|
|
|
|
|
|
|
|
const export = keystore.export_verification_data();
|
|
|
|
|
|
|
|
// second instance
|
|
|
|
|
|
|
|
keystore.import_verification_data(export);
|
|
|
|
```
|
|
|
|
|
2021-01-09 12:20:14 +01:00
|
|
|
### Exporting and importing blacklist entries across server instances
|
|
|
|
|
|
|
|
```js
|
|
|
|
const {blacklist} = require('@sapphirecode/auth-server-helper');
|
|
|
|
|
|
|
|
const export = blacklist.export_blacklist();
|
|
|
|
|
|
|
|
// second instance
|
|
|
|
|
|
|
|
blacklist.import_blacklist(export);
|
|
|
|
```
|
|
|
|
|
2021-01-15 14:45:05 +01:00
|
|
|
### Clearing Keystore and Blacklist
|
|
|
|
|
|
|
|
Resetting the Keystore instance generates a new instance id and deletes all
|
|
|
|
imported or generated keys.
|
|
|
|
|
|
|
|
```js
|
|
|
|
const {keystore, blacklist} = require('@sapphirecode/auth-server-helper');
|
|
|
|
|
|
|
|
// clear keystore
|
|
|
|
keystore.reset_instance();
|
|
|
|
|
|
|
|
// clear blacklist
|
|
|
|
blacklist.clear();
|
|
|
|
|
|
|
|
// clear blacklist items older than 10 seconds
|
|
|
|
blacklist.clear(Date.now() - 10000);
|
|
|
|
```
|
|
|
|
|
2020-05-17 17:37:41 +02:00
|
|
|
## License
|
|
|
|
|
|
|
|
MIT © Timo Hocker <timo@scode.ovh>
|