byu-group-mem
Validates the group membership of a person
Parameters:
configuration - this is an object with the follow properties:
- wso2_request_instance - an instance of the byu-wso2-request module
- group_name - the name of the gro-group that you want to validate against
- [ handler ] - an optional function that can be used to format error responses. The parameters that are passed into the handler function are:
- statusCode - the HTTP status code that corresponds to the error.
- error - the error object that corresponds to the error. If the person ID provided in the JWT does not match a person ID in the group specified, it is considered a 401 error. The error responses that may occur are:
- 504 - 'MembersOf Service Time Out'
- 200 - 'Successful Response'
- 4XX - '[Error Message]'
The middleware will return an JSON formatted error response to the client if one of these errors occurs.
It will continue to the next middleware if the person ID found in the JWT verification step is found in the specified BYU group membership.
Example:
First you must utilize the byu-jwt package that facilitates the JWT verification for BYU. The request headers that are passed in to this function must supply the JWT authentication token in the established manner. You must also supply the well-known-url as the second parameter.
Next you must supply the byu-group-mem middleware function with the proper object and properties.
const express = require('express');
const byuJWT = require('byu-jwt');
const AuthenticationError = byuJWT.AuthenticationError;
const wso2 = require('byu-wso2-request');
const meta = require('meta-ngin');
const verify_group_mem = require('byu-group-mem');
const WELLKNOWN_URL = 'https://api.byu.edu/well-known-stuff/';
const api = express();
const clientKey = '';
const clinetSecret = '';
wso2.setOauthSettings({clientKey: clientKey, clientSecret: clientSecret, wellKnownUrl: WELLKNOWN_URL});
app.use((req, res, next) => {
byuJWT.authenticate(req.headers, WELLKNOWN_URL)
.then(verifiedJWTs => {
req.verifiedJWTs = verifiedJWTs;
next();
})
.catch(err => {
if (err instanceof AuthenticationError) {
return res.status(401).send(meta(401, err.message));
}
console.error(new Date().toISOString(), 'Unexpected error when determining authentication:\n', err);
return res.status(500).send(meta(500, 'Error determining authentication'));
});
});
app.use(verify_group_mem({
wso2_request_instance: wso2,
group_name: 'identity-codes-admin'
handler: (code, error) => {
return {my-metadata: meta(code, error))}
}
}))
app.use('/', function () {
//Handle all other api calls
});
app.listen(3000, function() {
console.log('Starting server');
})