Add token authentication (api)

This commit is contained in:
Wim 2017-06-06 00:05:32 +02:00
parent 3a183cb218
commit ad3cb0386b
3 changed files with 14 additions and 1 deletions

View File

@ -4,6 +4,7 @@ import (
"github.com/42wim/matterbridge/bridge/config" "github.com/42wim/matterbridge/bridge/config"
log "github.com/Sirupsen/logrus" log "github.com/Sirupsen/logrus"
"github.com/labstack/echo" "github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"github.com/zfjagann/golang-ring" "github.com/zfjagann/golang-ring"
"net/http" "net/http"
"sync" "sync"
@ -38,6 +39,11 @@ func New(cfg config.Protocol, account string, c chan config.Message) *Api {
b.Config = &cfg b.Config = &cfg
b.Account = account b.Account = account
b.Remote = c b.Remote = c
if b.Config.Token != "" {
e.Use(middleware.KeyAuth(func(key string, c echo.Context) (bool, error) {
return key == b.Config.Token, nil
}))
}
e.GET("/api/messages", b.handleMessages) e.GET("/api/messages", b.handleMessages)
e.POST("/api/message", b.handlePostMessage) e.POST("/api/message", b.handlePostMessage)
go func() { go func() {

View File

@ -67,7 +67,7 @@ type Protocol struct {
ShowJoinPart bool // all protocols ShowJoinPart bool // all protocols
SkipTLSVerify bool // IRC, mattermost SkipTLSVerify bool // IRC, mattermost
Team string // mattermost Team string // mattermost
Token string // gitter, slack, discord Token string // gitter, slack, discord, api
URL string // mattermost, slack, matrix URL string // mattermost, slack, matrix
UseAPI bool // mattermost, slack UseAPI bool // mattermost, slack
UseSASL bool // IRC UseSASL bool // IRC

View File

@ -612,6 +612,11 @@ BindAddress="127.0.0.1:4242"
#Amount of messages to keep in memory #Amount of messages to keep in memory
Buffer=1000 Buffer=1000
#Bearer token used for authentication
#curl -H "Authorization: Bearer token" http://localhost:4242/api/messages
#OPTIONAL (no authorization if token is empty)
Token="mytoken"
#RemoteNickFormat defines how remote users appear on this bridge #RemoteNickFormat defines how remote users appear on this bridge
#The string "{NICK}" (case sensitive) will be replaced by the actual nick / username. #The string "{NICK}" (case sensitive) will be replaced by the actual nick / username.
#The string "{BRIDGE}" (case sensitive) will be replaced by the sending bridge #The string "{BRIDGE}" (case sensitive) will be replaced by the sending bridge
@ -619,6 +624,8 @@ Buffer=1000
#OPTIONAL (default empty) #OPTIONAL (default empty)
RemoteNickFormat="{NICK}" RemoteNickFormat="{NICK}"
################################################################### ###################################################################
#General configuration #General configuration
################################################################### ###################################################################