Question - Provider Authentication Best Practices
I understand how user authentication can be handled by configuring deepstream.io for http authentication and passing user credentials to a client’s login() method.
But, is the same approach recommended for non-user authentication?
For example, a provider that exposes RPCs for an admin portal, or a provider which wraps an external web service, would need to authenticate with the deep stream server. Code samples always show providers connecting with a simple username, which implies that in production the http authentication server and the deepstream providers would need something like a shared token/key/etc.
It feels a little weird to use the same http authentication server for both users and providers (because the two use cases have such different traffic and security characteristics), but I don’t see any way to use multiple authentication strategies for deepstream either.
Is there a recommended best practice for (1) authenticating client providers, and (2) handling multiple authentication strategies?
Hi @TertiumQuid ,
deepstream is designed to be as flexible as possible - hence the focus on schema-less auth data and programmable http endpoints. Using one http endpoint for all incoming connections is absolutely fine. I'd recommend in the http endpoint to first distinguish based on connection type and use the serverData return value to provide permissioning information that can later be used in Valve (see sample below). Likewise this provider would be the point to implement multiple auth strategies.
const express = require('express')
const bodyParser = require('body-parser')
const app = express()
app.use(bodyParser.json())
app.post('/auth-user', (req, res) => {
if ( req.body.authData.role === 'provider' ) {
authenticateProvider( req.body.authData, res );
}
else if( req.body.authData.role === 'user' ) {
authenticateUser( req.body.authData, res );
}
else {
reject( res );
}
});
function authenticateProvider( authData, res ) {
if( req.body.authData.token === 'cxk45n3!j/34' ) {
res.json({
username: 'some-provider',
serverData: { canProvideX: true }
});
} else {
reject( res );
}
}
function authenticateUser( authData, res ) {
//...
}
function reject( res ) {
res.status(403).send('Invalid Credentials');
}
app.listen(3000)
Thanks for the detailed response - that clears it up and confirms my approach, which generally looks like your example. FWIW, Deepstream feels like it sits at the right level abstraction for a lot of things, and I agree that any more complexity around auth would probably break that abstraction.