1.1.19 • Published 4 years ago

ssomg v1.1.19

Weekly downloads
-
License
ISC
Repository
-
Last release
4 years ago

SSOMG

This is the node client for the SSOMG single sign on project, designed to work with Express.

Setup

Add this package with the cli command

npm install --save ssomg

and require it in your root index.js file with

ssomg = require('ssomg');

and connect it to your express app like this

var app = express();
app.use(cookieParser()); // req.cookies must be set for sessions to work
ssomg.mount( app );

and then using the route protection middleware, limit route access to specific user roles.

var { protect } = require("ssomg")

router.use( protect("Read") );

or

var { protect } = require("ssomg")

router.get("/example", protect("Admin"), routeHandler )
router.get("/example2", protect(["Admin", "Read", "App Role 2"]), routeHandler )

Protect can either take a string or an array, depending on the number of roles you wish to authorise the route for.

Environment

When you register the app and the users in the main SSOMG admin, you'll be issued with an app id the public key used to verify tokens. You'll need to add the following attributes to your environment file to make sure it works correctly:

PUB_KEY_PATH=./keys/sso
APP_ID=xxxxxxxxxxxxxxxxxxxxxxxx
SSO_HOST=https://auth.mysite.com
NODE_ENV=production (this ensures secure cookies are used)

Using a .env file and with the npm package dotenv or docker is recommended. Set this up quickly by running npm install --save dotenv in the terminal, and require('dotenv').config() at the top of your app entrypoint.

Gotchas

Make sure the mount is after cookies are enabled Make sure the URLs in the env file and the provider app contain the correct protocol(http(s)), and ports. Without the correct protocols, the app will behave unexpectedly.

Details and customisation

Protect can accept an optional set of arguments to define what happens if the user is logged in but does not have permission: { failure_redirect: "The url to redirect to if failure occurs", failure_flash: "A flash message to set if failure occurs. Must have a req.flash() function present", failure_callback: function( req, res, next ) { /* This is a middleware function that is called when the user fails to authorize for a route. Supercedes failure_redirect and failure_flash / } }

If a user JWT is set, req.user will be set like this:

"req.user": {
  "_id": "xxxxx",
  "first_name": "First",
  "last_name": "Last",
  "email": "xxxxxxxx@xxxx.xx",
  "roles": [
    "App Role 1",
    "App Role 2"
  ],
  "refresh_token": "xxxxxxxxx",
  "iat": xxxxxxxxxx,
  "exp": xxxxxxxxxx
}

Who do I talk to?

If there's a problem, open an issue or talk to Henry.

1.1.19

4 years ago

1.1.16

4 years ago

1.1.18

4 years ago

1.1.17

4 years ago

1.1.15

4 years ago

1.1.14

4 years ago

1.1.13

4 years ago

1.1.12

4 years ago

1.1.11

5 years ago

1.1.10

5 years ago

1.1.9

5 years ago

1.1.8

5 years ago

1.1.7

5 years ago

1.1.6

5 years ago

1.1.5

5 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago