Library for work with middleware auth service
This library export some components for work with middleware-auth-serice.
How to get components?
const lib = require('middleware_auth_lib');
This middleware for express.js with work together middleware-auth-service. Token get from request.headers.authorization as string => 'Bearer token';
That permit only requests with right tokens, that auth middleware get success. And send this data in request.locals.data
##How work with?
const auth = lib.authMiddleware({
serviceId: 'signing-service',
provider: 'http://localhost:8082'
});
const app = require('express')();
app.use(auth);
Where parameters: serviceId - identificator of microservice, that permit access provider - address of middleware-auth-service
##In success:
in object request in express middleware saves this data.
request.locals.data = all data from token
(required parameters in: clientId or userId)
##In error:
return response with 400 or 401 error
we connect additional auth middleware for this middleware, that use only if given token from user not jwt (json web token) valid token.
This middleware for express.js with work together laborx profile service. Token get from request.headers.authorization as string => 'Bearer token';
That permit only requests with right tokens, that auth middleware get success. And send this data in request.locals.data
##How work with?
const profile = lib.profileMiddleware({
provider: 'http://localhost:8082/api/v1/security'
});
const app = require('express')();
app.use(profile);
Where parameters: provider - address of laborx profile service
Or may use together with middleware-auth-service.
const profile = lib.profileMiddleware({
provider: 'http://localhost:8082/api/v1/security'
});
const auth = lib.authMiddleware({
serviceId: 'signing-service',
provider: 'http://localhost:8082/api/v1/security'
}, profile);
const app = require('express')();
app.use(auth);
##In success:
in object request in express middleware saves this data.
request.locals.data = {addresses}
##In error:
return response with 400 or 401 error
This client work on client, that connected to service with authMiddleware and work with middleware-auth-service.
##How get token for client?
const tokenLib = new lib.Token({
id: 'chronomint'
provider: 'http://localhost:8083',
secret: 'super',
});
const scopes = ['signing-service];
const token = await tokenLib.getToken(scopes);
await require('request-promise')({
method: 'POST',
uri: 'http://localhost:8087',
headers: {authorization: 'Bearer ' + token}
});
Where parameters in constructor: id - identificator of this service provider - address of middleware-auth-service secret - password of this service scopes - array of scopes[services], that client want access
##How get token for user on client?
const tokenLib = new lib.Token({
id: 'chronomint'
provider: 'http://localhost:8083',
secret: 'super',
});
const scopes = ['signing-service];
const userId = 'userId';
const token = await tokenLib.getUserToken(userId, scopes);
await require('request-promise')({
method: 'POST',
uri: 'http://localhost:8087',
headers: {authorization: 'Bearer ' + token}
});
Where parameters in constructor: id - identificator of this service provider - address of middleware-auth-service secret - password of this service userId - id of client scopes - array of scopes[services], that user want access
LaborX PTY