From b45a5578d2094a995a1163b4f83c84b44015ced5 Mon Sep 17 00:00:00 2001 From: Patrick Connolly Date: Thu, 8 Nov 2018 08:09:05 +0800 Subject: [PATCH] Use a skipper for the auth, so tokens aren't needed for swagger docs. --- bridge/api/api.go | 20 ++++++++++++++++---- docs/docs.go | 39 ++++++++++++++++++++++++++++++++++++--- docs/swagger/swagger.json | 37 +++++++++++++++++++++++++++++++++++-- docs/swagger/swagger.yaml | 23 +++++++++++++++++++++-- 4 files changed, 108 insertions(+), 11 deletions(-) diff --git a/bridge/api/api.go b/bridge/api/api.go index cdfccedb..682b4b98 100644 --- a/bridge/api/api.go +++ b/bridge/api/api.go @@ -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) diff --git a/docs/docs.go b/docs/docs.go index d1021be6..ceb48379 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -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" diff --git a/docs/swagger/swagger.json b/docs/swagger/swagger.json index 4d516f66..2bf12b08 100644 --- a/docs/swagger/swagger.json +++ b/docs/swagger/swagger.json @@ -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" diff --git a/docs/swagger/swagger.yaml b/docs/swagger/swagger.yaml index 63c67e64..8c79159f 100644 --- a/docs/swagger/swagger.yaml +++ b/docs/swagger/swagger.yaml @@ -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