Skip to content

Instantly share code, notes, and snippets.

@objectfoo
Last active February 18, 2019 06:01
Show Gist options
  • Select an option

  • Save objectfoo/e9a7bccddc177350cf1f783e924d2c6b to your computer and use it in GitHub Desktop.

Select an option

Save objectfoo/e9a7bccddc177350cf1f783e924d2c6b to your computer and use it in GitHub Desktop.

The auth server is distinct from the key-cloak api gateway.

An express server's middleware will need access to some key-cloak services. These services would include the current bearer token and an async function fetchApiToken.

This could be implemented as middleware, including a middleware at the top of your stack could init this manager, including fetching the initial token. It would need to have some way to set the credentials that will be used to get the token from the auth server. It would provide the getToken and fetchApiToken functions on the req.app.locals[some-key] property. The key should be configurable, or maybe just over-rideable.

IDEA: instead of a getToken function we could be more like aws4.sign(options, credentials), as shown in got docs, where options is an object with headers and presumably headers.Authentication. The function mutates the headers object directly :(

Getting the intial key should be required for a successful ecv check.

Inside your data fetching middleware you would access the functions on req.app;

Earlier in the middleware we'd also need to have req.id in correlation id;

// add correlation id to req.id
app.use(correlationId({ headerName: 'x-webmd-correlation-id' }));

// init the api token middleware, get first token
app.use(apiAuthorization({ credentials: {} propName: 'apiAuth' }));

// handle user request
app.get('/', (req, res, next) => {
	const { apiAuth } = req.app.locals;
	const reqHeaders = reqToPhmHeaders({ appName: 'ui-app' })(req);
	const whsClient = got.extend({
		baseUrl: 'http://rsrc'
		headers: reqHeaders
	});
	// apiAuth.bearerToken();
	// apiAuth.sign(reqHeaders);  // mutate options.header
	// apiAuth.fetchApiToken();   // fetch new token with creds update cached value
	// apiAuth.refreshApiToken(); // same as fetchApiToken

	return Promise.all([
		withReAuth(whsClient('/api/common'), apiAuth.refreshApiToken(req)),
		withReAuth(whsClient('/regonboarding/di'), apiAuth.refreshApiToken(req))
	])
		.then(parseViewModel(res))
		.then(viewmodel => {
			res.locals = {
				...res.locals,
				...viewmodel
			};
			next();
		})
		.catch(next);
}, (req, res, next) => {
	res.send(index);
});

app.use('*', (req, res) => {
	logger.info('404 page not found', + req.path);
	res.status(404).send('not found');
	// don't call next
});

// logging mw
app.use((err, req, res, next) => {
	logger.error({ err, req, res });
	next(err);
});

// render error page for user, handle ajax respons elsewhere
app.use((err, req, res, next) => {
	if (!res.headersSent) {
		res.status(500).send('error');
	}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment