简体   繁体   中英

How to add authentication to coffescript HTTP-Listener (hubot)

I want to add a simple user-password authentication to my coffescript for a Hubot. This should act like a "bridge" to send messages to rocketchat. I managed to make a HTTP-Post request without authentication to work.

The script i got so far works pretty good but without authentication:

module.exports = (robot) ->
  robot.router.post '/hubot/say', (req, res) ->

    recipient = req.body.recipient
    message = req.body.message
    dm = req.body.directmessage
    response = "OK"
    robot.logger.info "DM: #{dm}; Recipient: #{recipient}; Message: #{message}"

    if dm == '0'
      #robot.logger.info "Message '#{message}' received for room #{recipient}"

      room = recipient
      user = robot.brain.userForId 'broadcast'
      user.room = room
      user.type = 'groupchat'

      if message
        robot.adapter.send({room, user: {} }, message)

    if dm == '1'
      user = recipient

      if message
        robot.adapter.sendDirect({ user: { name: "#{user}"} }, message)

    res.writeHead 200, {'Content-Type': 'text/plain'}
    res.end response

Hubot has a built-in express web framework, robot.router is actually an express router . Hence, you can use basic-auth library or refer to questions like Basic HTTP authentication with Node and Express 4 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM