use query params throughout the whole rest api (#11)

This commit is contained in:
Christoph Haas 2021-05-03 11:40:06 +02:00
parent d794f807ad
commit edfecd536a
3 changed files with 106 additions and 108 deletions

View File

@ -67,22 +67,22 @@ func (s *ApiServer) GetUsers(c *gin.Context) {
// @Tags Users // @Tags Users
// @Summary Retrieves user based on given Email // @Summary Retrieves user based on given Email
// @Produce json // @Produce json
// @Param email path string true "User Email" // @Param email query string true "User Email"
// @Success 200 {object} users.User // @Success 200 {object} users.User
// @Failure 400 {object} ApiError // @Failure 400 {object} ApiError
// @Failure 401 {object} ApiError // @Failure 401 {object} ApiError
// @Failure 403 {object} ApiError // @Failure 403 {object} ApiError
// @Failure 404 {object} ApiError // @Failure 404 {object} ApiError
// @Router /backend/user/{email} [get] // @Router /backend/user [get]
// @Security ApiBasicAuth // @Security ApiBasicAuth
func (s *ApiServer) GetUser(c *gin.Context) { func (s *ApiServer) GetUser(c *gin.Context) {
email := strings.ToLower(strings.TrimSpace(c.Param("email"))) email := strings.ToLower(strings.TrimSpace(c.Query("email")))
if email == "" { if email == "" {
c.JSON(http.StatusBadRequest, ApiError{Message: "email parameter must be specified"}) c.JSON(http.StatusBadRequest, ApiError{Message: "email parameter must be specified"})
return return
} }
user := s.s.users.GetUserUnscoped(c.Param("email"))
user := s.s.users.GetUserUnscoped(email)
if user == nil { if user == nil {
c.JSON(http.StatusNotFound, ApiError{Message: "user not found"}) c.JSON(http.StatusNotFound, ApiError{Message: "user not found"})
return return
@ -134,7 +134,7 @@ func (s *ApiServer) PostUser(c *gin.Context) {
// @Summary Updates a user based on the given user model // @Summary Updates a user based on the given user model
// @Accept json // @Accept json
// @Produce json // @Produce json
// @Param email path string true "User Email" // @Param email query string true "User Email"
// @Param user body users.User true "User Model" // @Param user body users.User true "User Model"
// @Success 200 {object} users.User // @Success 200 {object} users.User
// @Failure 400 {object} ApiError // @Failure 400 {object} ApiError
@ -142,10 +142,10 @@ func (s *ApiServer) PostUser(c *gin.Context) {
// @Failure 403 {object} ApiError // @Failure 403 {object} ApiError
// @Failure 404 {object} ApiError // @Failure 404 {object} ApiError
// @Failure 500 {object} ApiError // @Failure 500 {object} ApiError
// @Router /backend/user/{email} [put] // @Router /backend/user [put]
// @Security ApiBasicAuth // @Security ApiBasicAuth
func (s *ApiServer) PutUser(c *gin.Context) { func (s *ApiServer) PutUser(c *gin.Context) {
email := strings.ToLower(strings.TrimSpace(c.Param("email"))) email := strings.ToLower(strings.TrimSpace(c.Query("email")))
if email == "" { if email == "" {
c.JSON(http.StatusBadRequest, ApiError{Message: "email parameter must be specified"}) c.JSON(http.StatusBadRequest, ApiError{Message: "email parameter must be specified"})
return return
@ -186,7 +186,7 @@ func (s *ApiServer) PutUser(c *gin.Context) {
// @Summary Updates a user based on the given partial user model // @Summary Updates a user based on the given partial user model
// @Accept json // @Accept json
// @Produce json // @Produce json
// @Param email path string true "User Email" // @Param email query string true "User Email"
// @Param user body users.User true "User Model" // @Param user body users.User true "User Model"
// @Success 200 {object} users.User // @Success 200 {object} users.User
// @Failure 400 {object} ApiError // @Failure 400 {object} ApiError
@ -194,10 +194,10 @@ func (s *ApiServer) PutUser(c *gin.Context) {
// @Failure 403 {object} ApiError // @Failure 403 {object} ApiError
// @Failure 404 {object} ApiError // @Failure 404 {object} ApiError
// @Failure 500 {object} ApiError // @Failure 500 {object} ApiError
// @Router /backend/user/{email} [patch] // @Router /backend/user [patch]
// @Security ApiBasicAuth // @Security ApiBasicAuth
func (s *ApiServer) PatchUser(c *gin.Context) { func (s *ApiServer) PatchUser(c *gin.Context) {
email := strings.ToLower(strings.TrimSpace(c.Param("email"))) email := strings.ToLower(strings.TrimSpace(c.Query("email")))
if email == "" { if email == "" {
c.JSON(http.StatusBadRequest, ApiError{Message: "email parameter must be specified"}) c.JSON(http.StatusBadRequest, ApiError{Message: "email parameter must be specified"})
return return
@ -251,17 +251,17 @@ func (s *ApiServer) PatchUser(c *gin.Context) {
// @Tags Users // @Tags Users
// @Summary Deletes the specified user // @Summary Deletes the specified user
// @Produce json // @Produce json
// @Param email path string true "User Email" // @Param email query string true "User Email"
// @Success 204 "No content" // @Success 204 "No content"
// @Failure 400 {object} ApiError // @Failure 400 {object} ApiError
// @Failure 401 {object} ApiError // @Failure 401 {object} ApiError
// @Failure 403 {object} ApiError // @Failure 403 {object} ApiError
// @Failure 404 {object} ApiError // @Failure 404 {object} ApiError
// @Failure 500 {object} ApiError // @Failure 500 {object} ApiError
// @Router /backend/user/{email} [delete] // @Router /backend/user [delete]
// @Security ApiBasicAuth // @Security ApiBasicAuth
func (s *ApiServer) DeleteUser(c *gin.Context) { func (s *ApiServer) DeleteUser(c *gin.Context) {
email := strings.ToLower(strings.TrimSpace(c.Param("email"))) email := strings.ToLower(strings.TrimSpace(c.Query("email")))
if email == "" { if email == "" {
c.JSON(http.StatusBadRequest, ApiError{Message: "email parameter must be specified"}) c.JSON(http.StatusBadRequest, ApiError{Message: "email parameter must be specified"})
return return
@ -285,15 +285,15 @@ func (s *ApiServer) DeleteUser(c *gin.Context) {
// @Tags Peers // @Tags Peers
// @Summary Retrieves all peers for the given interface // @Summary Retrieves all peers for the given interface
// @Produce json // @Produce json
// @Param device path string true "Device Name" // @Param device query string true "Device Name"
// @Success 200 {object} []wireguard.Peer // @Success 200 {object} []wireguard.Peer
// @Failure 401 {object} ApiError // @Failure 401 {object} ApiError
// @Failure 403 {object} ApiError // @Failure 403 {object} ApiError
// @Failure 404 {object} ApiError // @Failure 404 {object} ApiError
// @Router /backend/peers/{device} [get] // @Router /backend/peers [get]
// @Security ApiBasicAuth // @Security ApiBasicAuth
func (s *ApiServer) GetPeers(c *gin.Context) { func (s *ApiServer) GetPeers(c *gin.Context) {
deviceName := strings.ToLower(strings.TrimSpace(c.Param("device"))) deviceName := strings.ToLower(strings.TrimSpace(c.Query("device")))
if deviceName == "" { if deviceName == "" {
c.JSON(http.StatusBadRequest, ApiError{Message: "device parameter must be specified"}) c.JSON(http.StatusBadRequest, ApiError{Message: "device parameter must be specified"})
return return
@ -340,7 +340,7 @@ func (s *ApiServer) GetPeer(c *gin.Context) {
// @Summary Creates a new peer based on the given peer model // @Summary Creates a new peer based on the given peer model
// @Accept json // @Accept json
// @Produce json // @Produce json
// @Param device path string true "Device Name" // @Param device query string true "Device Name"
// @Param peer body wireguard.Peer true "Peer Model" // @Param peer body wireguard.Peer true "Peer Model"
// @Success 200 {object} wireguard.Peer // @Success 200 {object} wireguard.Peer
// @Failure 400 {object} ApiError // @Failure 400 {object} ApiError
@ -348,10 +348,10 @@ func (s *ApiServer) GetPeer(c *gin.Context) {
// @Failure 403 {object} ApiError // @Failure 403 {object} ApiError
// @Failure 404 {object} ApiError // @Failure 404 {object} ApiError
// @Failure 500 {object} ApiError // @Failure 500 {object} ApiError
// @Router /backend/peers/{device} [post] // @Router /backend/peers [post]
// @Security ApiBasicAuth // @Security ApiBasicAuth
func (s *ApiServer) PostPeer(c *gin.Context) { func (s *ApiServer) PostPeer(c *gin.Context) {
deviceName := strings.ToLower(strings.TrimSpace(c.Param("device"))) deviceName := strings.ToLower(strings.TrimSpace(c.Query("device")))
if deviceName == "" { if deviceName == "" {
c.JSON(http.StatusBadRequest, ApiError{Message: "device parameter must be specified"}) c.JSON(http.StatusBadRequest, ApiError{Message: "device parameter must be specified"})
return return
@ -581,16 +581,16 @@ func (s *ApiServer) GetDevices(c *gin.Context) {
// @Tags Interface // @Tags Interface
// @Summary Get the given device // @Summary Get the given device
// @Produce json // @Produce json
// @Param device path string true "Device Name" // @Param device query string true "Device Name"
// @Success 200 {object} wireguard.Device // @Success 200 {object} wireguard.Device
// @Failure 400 {object} ApiError // @Failure 400 {object} ApiError
// @Failure 401 {object} ApiError // @Failure 401 {object} ApiError
// @Failure 403 {object} ApiError // @Failure 403 {object} ApiError
// @Failure 404 {object} ApiError // @Failure 404 {object} ApiError
// @Router /backend/device/{device} [get] // @Router /backend/device [get]
// @Security ApiBasicAuth // @Security ApiBasicAuth
func (s *ApiServer) GetDevice(c *gin.Context) { func (s *ApiServer) GetDevice(c *gin.Context) {
deviceName := strings.ToLower(strings.TrimSpace(c.Param("device"))) deviceName := strings.ToLower(strings.TrimSpace(c.Query("device")))
if deviceName == "" { if deviceName == "" {
c.JSON(http.StatusBadRequest, ApiError{Message: "device parameter must be specified"}) c.JSON(http.StatusBadRequest, ApiError{Message: "device parameter must be specified"})
return return
@ -616,7 +616,7 @@ func (s *ApiServer) GetDevice(c *gin.Context) {
// @Summary Updates the given device based on the given device model (UNIMPLEMENTED) // @Summary Updates the given device based on the given device model (UNIMPLEMENTED)
// @Accept json // @Accept json
// @Produce json // @Produce json
// @Param device path string true "Device Name" // @Param device query string true "Device Name"
// @Param body body wireguard.Device true "Device Model" // @Param body body wireguard.Device true "Device Model"
// @Success 200 {object} wireguard.Device // @Success 200 {object} wireguard.Device
// @Failure 400 {object} ApiError // @Failure 400 {object} ApiError
@ -624,7 +624,7 @@ func (s *ApiServer) GetDevice(c *gin.Context) {
// @Failure 403 {object} ApiError // @Failure 403 {object} ApiError
// @Failure 404 {object} ApiError // @Failure 404 {object} ApiError
// @Failure 500 {object} ApiError // @Failure 500 {object} ApiError
// @Router /backend/device/{device} [put] // @Router /backend/device [put]
// @Security ApiBasicAuth // @Security ApiBasicAuth
func (s *ApiServer) PutDevice(c *gin.Context) { func (s *ApiServer) PutDevice(c *gin.Context) {
updateDevice := wireguard.Device{} updateDevice := wireguard.Device{}
@ -633,7 +633,7 @@ func (s *ApiServer) PutDevice(c *gin.Context) {
return return
} }
deviceName := strings.ToLower(strings.TrimSpace(c.Param("device"))) deviceName := strings.ToLower(strings.TrimSpace(c.Query("device")))
if deviceName == "" { if deviceName == "" {
c.JSON(http.StatusBadRequest, ApiError{Message: "device parameter must be specified"}) c.JSON(http.StatusBadRequest, ApiError{Message: "device parameter must be specified"})
return return
@ -667,7 +667,7 @@ func (s *ApiServer) PutDevice(c *gin.Context) {
// @Summary Updates the given device based on the given partial device model (UNIMPLEMENTED) // @Summary Updates the given device based on the given partial device model (UNIMPLEMENTED)
// @Accept json // @Accept json
// @Produce json // @Produce json
// @Param device path string true "Device Name" // @Param device query string true "Device Name"
// @Param body body wireguard.Device true "Device Model" // @Param body body wireguard.Device true "Device Model"
// @Success 200 {object} wireguard.Device // @Success 200 {object} wireguard.Device
// @Failure 400 {object} ApiError // @Failure 400 {object} ApiError
@ -675,7 +675,7 @@ func (s *ApiServer) PutDevice(c *gin.Context) {
// @Failure 403 {object} ApiError // @Failure 403 {object} ApiError
// @Failure 404 {object} ApiError // @Failure 404 {object} ApiError
// @Failure 500 {object} ApiError // @Failure 500 {object} ApiError
// @Router /backend/device/{device} [patch] // @Router /backend/device [patch]
// @Security ApiBasicAuth // @Security ApiBasicAuth
func (s *ApiServer) PatchDevice(c *gin.Context) { func (s *ApiServer) PatchDevice(c *gin.Context) {
patch, err := c.GetRawData() patch, err := c.GetRawData()
@ -684,7 +684,7 @@ func (s *ApiServer) PatchDevice(c *gin.Context) {
return return
} }
deviceName := strings.ToLower(strings.TrimSpace(c.Param("device"))) deviceName := strings.ToLower(strings.TrimSpace(c.Query("device")))
if deviceName == "" { if deviceName == "" {
c.JSON(http.StatusBadRequest, ApiError{Message: "device parameter must be specified"}) c.JSON(http.StatusBadRequest, ApiError{Message: "device parameter must be specified"})
return return
@ -743,15 +743,15 @@ type PeerDeploymentInformation struct {
// @Tags Provisioning // @Tags Provisioning
// @Summary Retrieves all active peers for the given email address // @Summary Retrieves all active peers for the given email address
// @Produce json // @Produce json
// @Param email path string true "Email Address" // @Param email query string true "Email Address"
// @Success 200 {object} []PeerDeploymentInformation "All active WireGuard peers" // @Success 200 {object} []PeerDeploymentInformation "All active WireGuard peers"
// @Failure 401 {object} ApiError // @Failure 401 {object} ApiError
// @Failure 403 {object} ApiError // @Failure 403 {object} ApiError
// @Failure 404 {object} ApiError // @Failure 404 {object} ApiError
// @Router /provisioning/peers/{email} [get] // @Router /provisioning/peers [get]
// @Security GeneralBasicAuth // @Security GeneralBasicAuth
func (s *ApiServer) GetPeerDeploymentInformation(c *gin.Context) { func (s *ApiServer) GetPeerDeploymentInformation(c *gin.Context) {
email := c.Param("email") email := c.Query("email")
if email == "" { if email == "" {
c.JSON(http.StatusBadRequest, ApiError{Message: "email parameter must be specified"}) c.JSON(http.StatusBadRequest, ApiError{Message: "email parameter must be specified"})
return return

View File

@ -31,7 +31,7 @@ var doc = `{
"host": "{{.Host}}", "host": "{{.Host}}",
"basePath": "{{.BasePath}}", "basePath": "{{.BasePath}}",
"paths": { "paths": {
"/backend/device/{device}": { "/backend/device": {
"get": { "get": {
"security": [ "security": [
{ {
@ -50,7 +50,7 @@ var doc = `{
"type": "string", "type": "string",
"description": "Device Name", "description": "Device Name",
"name": "device", "name": "device",
"in": "path", "in": "query",
"required": true "required": true
} }
], ],
@ -108,7 +108,7 @@ var doc = `{
"type": "string", "type": "string",
"description": "Device Name", "description": "Device Name",
"name": "device", "name": "device",
"in": "path", "in": "query",
"required": true "required": true
}, },
{ {
@ -181,7 +181,7 @@ var doc = `{
"type": "string", "type": "string",
"description": "Device Name", "description": "Device Name",
"name": "device", "name": "device",
"in": "path", "in": "query",
"required": true "required": true
}, },
{ {
@ -540,7 +540,7 @@ var doc = `{
} }
} }
}, },
"/backend/peers/{device}": { "/backend/peers": {
"get": { "get": {
"security": [ "security": [
{ {
@ -559,7 +559,7 @@ var doc = `{
"type": "string", "type": "string",
"description": "Device Name", "description": "Device Name",
"name": "device", "name": "device",
"in": "path", "in": "query",
"required": true "required": true
} }
], ],
@ -614,7 +614,7 @@ var doc = `{
"type": "string", "type": "string",
"description": "Device Name", "description": "Device Name",
"name": "device", "name": "device",
"in": "path", "in": "query",
"required": true "required": true
}, },
{ {
@ -667,7 +667,7 @@ var doc = `{
} }
} }
}, },
"/backend/user/{email}": { "/backend/user": {
"get": { "get": {
"security": [ "security": [
{ {
@ -686,7 +686,7 @@ var doc = `{
"type": "string", "type": "string",
"description": "User Email", "description": "User Email",
"name": "email", "name": "email",
"in": "path", "in": "query",
"required": true "required": true
} }
], ],
@ -744,7 +744,7 @@ var doc = `{
"type": "string", "type": "string",
"description": "User Email", "description": "User Email",
"name": "email", "name": "email",
"in": "path", "in": "query",
"required": true "required": true
}, },
{ {
@ -814,7 +814,7 @@ var doc = `{
"type": "string", "type": "string",
"description": "User Email", "description": "User Email",
"name": "email", "name": "email",
"in": "path", "in": "query",
"required": true "required": true
} }
], ],
@ -875,7 +875,7 @@ var doc = `{
"type": "string", "type": "string",
"description": "User Email", "description": "User Email",
"name": "email", "name": "email",
"in": "path", "in": "query",
"required": true "required": true
}, },
{ {
@ -1091,6 +1091,58 @@ var doc = `{
} }
}, },
"/provisioning/peers": { "/provisioning/peers": {
"get": {
"security": [
{
"GeneralBasicAuth": []
}
],
"produces": [
"application/json"
],
"tags": [
"Provisioning"
],
"summary": "Retrieves all active peers for the given email address",
"parameters": [
{
"type": "string",
"description": "Email Address",
"name": "email",
"in": "query",
"required": true
}
],
"responses": {
"200": {
"description": "All active WireGuard peers",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/server.PeerDeploymentInformation"
}
}
},
"401": {
"description": "Unauthorized",
"schema": {
"$ref": "#/definitions/server.ApiError"
}
},
"403": {
"description": "Forbidden",
"schema": {
"$ref": "#/definitions/server.ApiError"
}
},
"404": {
"description": "Not Found",
"schema": {
"$ref": "#/definitions/server.ApiError"
}
}
}
},
"post": { "post": {
"security": [ "security": [
{ {
@ -1145,60 +1197,6 @@ var doc = `{
} }
} }
} }
},
"/provisioning/peers/{email}": {
"get": {
"security": [
{
"GeneralBasicAuth": []
}
],
"produces": [
"application/json"
],
"tags": [
"Provisioning"
],
"summary": "Retrieves all active peers for the given email address",
"parameters": [
{
"type": "string",
"description": "Email Address",
"name": "email",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "All active WireGuard peers",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/server.PeerDeploymentInformation"
}
}
},
"401": {
"description": "Unauthorized",
"schema": {
"$ref": "#/definitions/server.ApiError"
}
},
"403": {
"description": "Forbidden",
"schema": {
"$ref": "#/definitions/server.ApiError"
}
},
"404": {
"description": "Not Found",
"schema": {
"$ref": "#/definitions/server.ApiError"
}
}
}
}
} }
}, },
"definitions": { "definitions": {

View File

@ -86,28 +86,28 @@ func SetupApiRoutes(s *Server) {
apiV1Backend.GET("/users", api.GetUsers) apiV1Backend.GET("/users", api.GetUsers)
apiV1Backend.POST("/users", api.PostUser) apiV1Backend.POST("/users", api.PostUser)
apiV1Backend.GET("/user/:email", api.GetUser) apiV1Backend.GET("/user", api.GetUser)
apiV1Backend.PUT("/user/:email", api.PutUser) apiV1Backend.PUT("/user", api.PutUser)
apiV1Backend.PATCH("/user/:email", api.PatchUser) apiV1Backend.PATCH("/user", api.PatchUser)
apiV1Backend.DELETE("/user/:email", api.DeleteUser) apiV1Backend.DELETE("/user", api.DeleteUser)
apiV1Backend.GET("/peers/:device", api.GetPeers) apiV1Backend.GET("/peers", api.GetPeers)
apiV1Backend.POST("/peers/:device", api.PostPeer) apiV1Backend.POST("/peers", api.PostPeer)
apiV1Backend.GET("/peer", api.GetPeer) apiV1Backend.GET("/peer", api.GetPeer)
apiV1Backend.PUT("/peer", api.PutPeer) apiV1Backend.PUT("/peer", api.PutPeer)
apiV1Backend.PATCH("/peer", api.PatchPeer) apiV1Backend.PATCH("/peer", api.PatchPeer)
apiV1Backend.DELETE("/peer", api.DeletePeer) apiV1Backend.DELETE("/peer", api.DeletePeer)
apiV1Backend.GET("/devices", api.GetDevices) apiV1Backend.GET("/devices", api.GetDevices)
apiV1Backend.GET("/device/:device", api.GetDevice) apiV1Backend.GET("/device", api.GetDevice)
apiV1Backend.PUT("/device/:device", api.PutDevice) apiV1Backend.PUT("/device", api.PutDevice)
apiV1Backend.PATCH("/device/:device", api.PatchDevice) apiV1Backend.PATCH("/device", api.PatchDevice)
// Simple authenticated routes // Simple authenticated routes
apiV1Deployment := s.server.Group("/api/v1/provisioning") apiV1Deployment := s.server.Group("/api/v1/provisioning")
apiV1Deployment.Use(s.RequireApiAuthentication("")) apiV1Deployment.Use(s.RequireApiAuthentication(""))
apiV1Deployment.GET("/peers/:email", api.GetPeerDeploymentInformation) apiV1Deployment.GET("/peers", api.GetPeerDeploymentInformation)
apiV1Deployment.GET("/peer", api.GetPeerDeploymentConfig) apiV1Deployment.GET("/peer", api.GetPeerDeploymentConfig)
apiV1Deployment.POST("/peers", api.PostPeerDeploymentConfig) apiV1Deployment.POST("/peers", api.PostPeerDeploymentConfig)