One database per user security

感情迁移 提交于 2019-12-05 18:42:27

If you write a Progressive Web App, the Cookie Authentication is great for this, because the browser handles it for you. Use the pouchdb-authentication to log in directly to CouchDB.

On the CouchDB side, configure the Cookies as persistent, and put some longer lifetime on it. You can set it to 2 weeks, for example, so your users will only be asked for the password if they haven't logged in for two weeks.

The cookie TTL is automatically refreshed once a certain threshold is reached (I recall it's at half of the cookie TTL, so it would be refreshed if the cookie is more than a week old).

CouchDB is built for the web, so you can take advantage of it. ;-)

Thanks for your help, i was not able to use couchdb-auth-proxy so I ended up with the following solution that has the advantage to prevent direct access to couchdb :

1) Create a node server to authenticate the user, if auth successful then return couchdb token to the app for cookie authentication

2) Create a node server used only as a couchdb proxy using node-http-proxy

with the following code :

(it is required that this router code come very early in the express middleware otherwise it might change the response and pouchdb sync does not work, so place it before app.use(bodyParser.json()) )

    router.all('/*', (req: Request, res: Response, next: NextFunction) {

            let token = req.get('X-Auth-Cdb-Token');

            let httpProxy = require('http-proxy');

            const proxy = httpProxy.createProxyServer({
                target: target,
            });

            req.headers['Cookie'] = 'AuthSession='+token

            proxy.web(req, res);

    });

3) In your app set the pouchdb remote database with the following header :

    remoteDB = new PouchDB(url, {
         skip_setup: true,
         ajax: {
            headers: {
                'X-Auth-Cdb-Token': couchdbToken
            },
            withCredentials: false
         }
    })
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!