Hubot/Express: disable basic auth in a specific route

痴心易碎 提交于 2019-12-11 03:11:57

问题


I am using Hubot and I've defined the environment variables EXPRESS_USER and EXPRESS_PASSWORD to enable basic authentication. Hubot uses express and basically it's

setupExpress: ->
    user    = process.env.EXPRESS_USER
    pass    = process.env.EXPRESS_PASSWORD
    stat    = process.env.EXPRESS_STATIC

    express = require 'express'

    app = express()

    app.use (req, res, next) =>
      res.setHeader "X-Powered-By", "hubot/#{@name}"
      next()

    app.use express.basicAuth user, pass if user and pass
    app.use express.query()
    app.use express.bodyParser()
    app.use express.static stat if stat`

I want to expose an HTTP command in a script that doesn't need basic auth. However I'm not able to change change the code in Hubot where express it's being initialized

robot.router.get '/some-anonymous-path', (req, res) ->
  console.log 'Should be here without need to authenticate

Does anyone know if it's possible to do it in expressjs.

Thanks in advance

Bruno


回答1:


How about putting nginx in front of your Hubot? Then you can also add SSL, only allow access to specific paths, rewrite urls or even serve static content created by hubot scripts. A simple example nginx.conf block:

upstream hubot {
  server localhost:8080;
}
server {
  listen 80;
  satisfy any;
  allow 127.0.0.1;
  deny all;
  location ~ ^/(public|obscured) {
    allow all;
    proxy_pass http://hubot;
  }
  location ~ ^/(static) {
    auth_basic "Restricted";
    auth_basic_user_file htpasswd;
    root /www;
  }
  location ~ {
    auth_basic "Restricted";
    auth_basic_user_file htpasswd;
    proxy_pass http://hubot;
  }
}

Then toss an htpasswd pair in /etc/nginx/htpasswd and in your hubot init script set BIND_ADDRESS=localhost (default bind is 0.0.0.0) and you'll be off to the races.



来源:https://stackoverflow.com/questions/24392180/hubot-express-disable-basic-auth-in-a-specific-route

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!