Use a skipper for the auth, so tokens aren't needed for swagger docs.

This commit is contained in:
Patrick Connolly
2018-11-08 08:09:05 +08:00
parent 41f49495ba
commit b45a5578d2
4 changed files with 108 additions and 11 deletions

View File

@@ -39,7 +39,7 @@ type Message struct {
Gateway string `json:"gateway"`
}
// @securityDefinitions.apikey
// @securityDefinitions.apiKey ApiKeyAuth
// @in header
// @name Authorization
func New(cfg *bridge.Config) bridge.Bridger {
@@ -52,11 +52,20 @@ func New(cfg *bridge.Config) bridge.Bridger {
b.Messages.SetCapacity(b.GetInt("Buffer"))
}
if b.GetString("Token") != "" {
e.Use(middleware.KeyAuth(func(key string, c echo.Context) (bool, error) {
return key == b.GetString("Token"), nil
e.Pre(middleware.RemoveTrailingSlash())
e.Use(middleware.KeyAuthWithConfig(middleware.KeyAuthConfig{
Validator: func(key string, c echo.Context) (bool, error) {
return key == b.GetString("Token"), nil
},
Skipper: func(c echo.Context) bool {
for _, path := range strings.Fields("/ /api /api/health /swagger /swagger/*") {
if c.Path() == path { return true }
}
return false
},
}))
}
for _, path := range strings.Fields("/api /swagger /") {
for _, path := range strings.Fields("/ /api /swagger") {
e.GET(path, b.handleDocsRedirect)
}
e.GET("/swagger/*", echoSwagger.WrapHandler)
@@ -116,6 +125,7 @@ func (b *Api) handleDocsRedirect(c echo.Context) error {
// @Produce json
// @Param message body config.Message true "Message object to create"
// @Success 200 {object} config.Message
// @Security ApiKeyAuth
// @Router /message [post]
func (b *API) handlePostMessage(c echo.Context) error {
message := config.Message{}
@@ -137,6 +147,7 @@ func (b *API) handlePostMessage(c echo.Context) error {
// @Summary List new messages
// @Produce json
// @Success 200 {array} config.Message
// @Security ApiKeyAuth
// @Router /messages [get]
func (b *API) handleMessages(c echo.Context) error {
b.Lock()
@@ -150,6 +161,7 @@ func (b *API) handleMessages(c echo.Context) error {
// @Summary Stream realtime messages
// @Produce json-stream
// @Success 200 {object} config.Message
// @Security ApiKeyAuth
// @Router /stream [get]
func (b *API) handleStream(c echo.Context) error {
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSON)

View File

@@ -1,6 +1,6 @@
// GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
// This file was generated by swaggo/swag at
// 2018-11-05 15:12:37.21158 +0800 CST m=+0.084936216
// 2018-11-08 08:07:23.413198 +0800 CST m=+0.090593234
package docs
@@ -11,7 +11,7 @@ import (
var doc = `{
"swagger": "2.0",
"info": {
"description": "A read/write API for the Matterbridge chat bridge.",
"description": "OK",
"title": "Matterbridge API",
"contact": {},
"license": {
@@ -21,8 +21,27 @@ var doc = `{
},
"basePath": "/api",
"paths": {
"/api/health": {
"get": {
"description": "OK",
"summary": "Checks if the server is alive.",
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "string"
}
}
}
}
},
"/message": {
"post": {
"security": [
{
"ApiKeyAuth": []
}
],
"consumes": [
"application/json"
],
@@ -55,6 +74,11 @@ var doc = `{
},
"/messages": {
"get": {
"security": [
{
"ApiKeyAuth": []
}
],
"produces": [
"application/json"
],
@@ -74,6 +98,11 @@ var doc = `{
},
"/stream": {
"get": {
"security": [
{
"ApiKeyAuth": []
}
],
"produces": [
"application/x-json-stream"
],
@@ -127,6 +156,10 @@ var doc = `{
"type": "string",
"example": "slack 1541361213.030700"
},
"parent_id": {
"description": "Unique ID of a parent message, if threaded\n",
"type": "string"
},
"protocol": {
"description": "Chat protocol of incoming message\n",
"type": "string",
@@ -155,7 +188,7 @@ var doc = `{
}
},
"securityDefinitions": {
"": {
"ApiKeyAuth": {
"type": "apiKey",
"name": "Authorization",
"in": "header"

View File

@@ -1,7 +1,7 @@
{
"swagger": "2.0",
"info": {
"description": "A read/write API for the Matterbridge chat bridge.",
"description": "OK",
"title": "Matterbridge API",
"contact": {},
"license": {
@@ -11,8 +11,27 @@
},
"basePath": "/api",
"paths": {
"/api/health": {
"get": {
"description": "OK",
"summary": "Checks if the server is alive.",
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "string"
}
}
}
}
},
"/message": {
"post": {
"security": [
{
"ApiKeyAuth": []
}
],
"consumes": [
"application/json"
],
@@ -45,6 +64,11 @@
},
"/messages": {
"get": {
"security": [
{
"ApiKeyAuth": []
}
],
"produces": [
"application/json"
],
@@ -64,6 +88,11 @@
},
"/stream": {
"get": {
"security": [
{
"ApiKeyAuth": []
}
],
"produces": [
"application/x-json-stream"
],
@@ -117,6 +146,10 @@
"type": "string",
"example": "slack 1541361213.030700"
},
"parent_id": {
"description": "Unique ID of a parent message, if threaded\n",
"type": "string"
},
"protocol": {
"description": "Chat protocol of incoming message\n",
"type": "string",
@@ -145,7 +178,7 @@
}
},
"securityDefinitions": {
"": {
"ApiKeyAuth": {
"type": "apiKey",
"name": "Authorization",
"in": "header"

View File

@@ -35,6 +35,10 @@ definitions:
Unique ID of message on the gateway
example: slack 1541361213.030700
type: string
parent_id:
description: |
Unique ID of a parent message, if threaded
type: string
protocol:
description: |
Chat protocol of incoming message
@@ -61,12 +65,21 @@ definitions:
type: object
info:
contact: {}
description: A read/write API for the Matterbridge chat bridge.
description: OK
license:
name: Apache 2.0
url: https://github.com/42wim/matterbridge/blob/master/LICENSE
title: Matterbridge API
paths:
/api/health:
get:
description: OK
responses:
"200":
description: OK
schema:
type: string
summary: Checks if the server is alive.
/message:
post:
consumes:
@@ -87,6 +100,8 @@ paths:
schema:
$ref: '#/definitions/config.Message'
type: object
security:
- ApiKeyAuth: []
summary: Create/Update a message
/messages:
get:
@@ -99,6 +114,8 @@ paths:
items:
$ref: '#/definitions/config.Message'
type: array
security:
- ApiKeyAuth: []
summary: List new messages
/stream:
get:
@@ -110,9 +127,11 @@ paths:
schema:
$ref: '#/definitions/config.Message'
type: object
security:
- ApiKeyAuth: []
summary: Stream realtime messages
securityDefinitions:
"":
ApiKeyAuth:
in: header
name: Authorization
type: apiKey