Sample oAuth2.0 server with Client Credential implementation
Purpose of this implementation is to secure service api (web services) and socket api (web socket connection) from any random unauthorized client for the apps that don't have user login. The use case here is we have two different resource servers: 1) A nodejs server that hosts diffferent web services to be consumed in a hybrid (iOS app created using phonegap app) and web app 2) A nodejs server that works as web socket server and its client would be a hybrid (iOS app created using phonegap app) and web app. And we need to restrict their access to authorised clients (Our own hybrid and web apps).
Prerequisite : Understanding of oAuth2.0, nodejs, socket.io, JSON Web Token(JWT)
This package itself is an authoisation server with only Client Credential method implementation.
It issues token to authorised clients.
- Hybrid app: (app/getHybridToken.js)
url : http://localhost:3000/getToken/hybrid
body : client_id, client_secret
Before issuing token it verifies for valid hybrid app, client_id, client_secret
- Web app: (app/getWebToken.js)
url : http://localhost:3000/getToken/web
body : client_id, client_secret
Before issuing token it verifies for valid origin of request, client_id, client_secret
Any client that need to access any of the resource servers first of all must get access_token
- ServiceAPI Middleware : (lib/serviceAPIMW.js)
This is an expressjs middleware. Include this middleware in your service api server, so that only valid request can access the apis. For hybrid apps it validates a valid hybrid app and access_token.
Use middleware as follow:
app.use(isAuthorisedServiceRequest) or app.use('/securedPath', isAuthorisedServiceRequest);
- SocketAPI Middleware : (lib/socketAPIMW.js)
This is a socketio middleware. Include this middleware in your socket api server, so that only valid request can make socket connection. For hybrid apps it validates a valid request origin and access_token.
Use middleware as follow:
io.use(isAuthorisedSocketRequest);
-
Standard boilerplate for a nodejs server
-
Unified logging
-
Cron job to remove log file content everyday
- Only one config file is used. Node config module can be used for separate config file for development and production environment.
- Client_secret and JWT secret should be stored in persistent database such as mongodb and should be accessible to resource (service api and socket api) servers.
- For hybrid apps, token request should come from native code and include device_uuis as additional parameter to make it more secure.
- For hybrid apps, socket connection should be restricted one per device per app.
- Replace values of JWT_SECRET_MOBILE_APP, JWT_SECRET_WEB_APP, VALID_CLIENT_SECRETS in config file