Updated status codes, added swagger.yml

This commit is contained in:
darmiel 2021-05-13 19:01:22 +02:00
parent 109b265884
commit 20f853b02d
4 changed files with 80 additions and 12 deletions

View file

@ -5,6 +5,7 @@ import (
"github.com/darmiel/yaxc/internal/common" "github.com/darmiel/yaxc/internal/common"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"github.com/muesli/termenv" "github.com/muesli/termenv"
"net/http"
"strings" "strings"
) )
@ -13,7 +14,7 @@ func (s *yAxCServer) handleGetAnywhere(ctx *fiber.Ctx) (err error) {
// validate path // validate path
if !common.ValidateAnywherePath(path) { if !common.ValidateAnywherePath(path) {
return ctx.Status(400).SendString("ERROR: Invalid path") return fiber.NewError(http.StatusNotAcceptable, "invalid anywhere-path")
} }
var res string var res string
@ -24,7 +25,7 @@ func (s *yAxCServer) handleGetAnywhere(ctx *fiber.Ctx) (err error) {
// Encryption // Encryption
if q := ctx.Query("secret"); q != "" { if q := ctx.Query("secret"); q != "" {
if !s.EnableEncryption { if !s.EnableEncryption {
return errEncryptionNotEnabled return fiber.NewError(http.StatusLocked, "encryption is currently not enabled on this server")
} }
// do not fail on error // do not fail on error
if encrypt, err := common.Decrypt(res, q); err == nil { if encrypt, err := common.Decrypt(res, q); err == nil {

View file

@ -7,6 +7,7 @@ import (
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/utils" "github.com/gofiber/fiber/v2/utils"
"github.com/muesli/termenv" "github.com/muesli/termenv"
"net/http"
"strings" "strings"
"time" "time"
) )
@ -31,13 +32,13 @@ func (s *yAxCServer) handlePostAnywhereWithHash(ctx *fiber.Ctx) (err error) {
func (s *yAxCServer) setAnywhereWithHash(ctx *fiber.Ctx, path, hash string) (err error) { func (s *yAxCServer) setAnywhereWithHash(ctx *fiber.Ctx, path, hash string) (err error) {
// validate path // validate path
if !common.ValidateAnywherePath(path) { if !common.ValidateAnywherePath(path) {
return ctx.Status(400).SendString("ERROR: Invalid path") return fiber.NewError(http.StatusNotAcceptable, "invalid anywhere-path")
} }
// Read content // Read content
bytes := ctx.Body() bytes := ctx.Body()
if s.MaxBodyLength > 0 && len(bytes) > s.MaxBodyLength { if s.MaxBodyLength > 0 && len(bytes) > s.MaxBodyLength {
return s.errBodyLen return fiber.NewError(http.StatusRequestEntityTooLarge, "exceeded max body length")
} }
content := string(bytes) content := string(bytes)
@ -45,26 +46,26 @@ func (s *yAxCServer) setAnywhereWithHash(ctx *fiber.Ctx, path, hash string) (err
ttl := s.DefaultTTL ttl := s.DefaultTTL
if q := ctx.Query("ttl"); q != "" { if q := ctx.Query("ttl"); q != "" {
if ttl, err = time.ParseDuration(q); err != nil { if ttl, err = time.ParseDuration(q); err != nil {
return return fiber.NewError(http.StatusUnprocessableEntity, "invalid ttl. (examples: 10s, 5m, 1h)")
} }
} }
// Encryption // Encryption
if q := ctx.Query("secret"); q != "" { if q := ctx.Query("secret"); q != "" {
if !s.EnableEncryption { if !s.EnableEncryption {
return errEncryptionNotEnabled return fiber.NewError(http.StatusLocked, "encryption is currently not enabled on this server")
} }
// fail on error // fail on error
encrypt, err := common.Encrypt(content, q) encrypt, err := common.Encrypt(content, q)
if err != nil { if err != nil {
return err return fiber.NewError(http.StatusInternalServerError, "error encrypting content: "+err.Error())
} }
content = string(encrypt) content = string(encrypt)
} }
// Check if ttl is valid // Check if ttl is valid
if (s.MinTTL != 0 && s.MinTTL > ttl) || (s.MaxTTL != 0 && s.MaxTTL < ttl) { if (s.MinTTL != 0 && s.MinTTL > ttl) || (s.MaxTTL != 0 && s.MaxTTL < ttl) {
return ctx.Status(400).SendString("ERROR: TTL out of range") return fiber.NewError(http.StatusRequestedRangeNotSatisfiable, "ttl out of range")
} }
// generate hash // generate hash

View file

@ -45,15 +45,13 @@ type YAxCConfig struct {
type yAxCServer struct { type yAxCServer struct {
*YAxCConfig *YAxCConfig
App *fiber.App App *fiber.App
Backend Backend Backend Backend
errBodyLen error
} }
func NewServer(cfg *YAxCConfig) (s *yAxCServer) { func NewServer(cfg *YAxCConfig) (s *yAxCServer) {
s = &yAxCServer{ s = &yAxCServer{
YAxCConfig: cfg, YAxCConfig: cfg,
errBodyLen: errors.New("exceeded max body length"),
} }
// backend // backend

68
swagger.yml Normal file
View file

@ -0,0 +1,68 @@
swagger: "2.0"
info:
description: "YAxC - Yet Another Cross Clipboard"
version: "1.1.0"
title: "YAxC"
host: "yaxc.d2a.io"
basePath: "/"
tags:
- name: "{anywhere}"
description: "Public Clipboard Name"
schemes:
- "https"
- "http"
paths:
/{anywhere}:
post:
tags:
- "{anywhere}"
summary: Post new clipboard data
consumes:
- 'text/plain'
produces:
- 'text/plain'
parameters:
- in: "secret"
name: "Encryption Password"
description: "Encrypts the content with the password (server-side!)"
required: false
- in: "ttl"
name: "TTL"
description: "Waiting time after which the content is deleted"
responses:
"200":
description: "OK"
"406":
description: "Invalid Path"
"413":
description: "Body too large"
"416":
description: "TTL out of range"
"422":
description: "Invalid TTL. (Examples: 10s, 5m, 1h)"
"423":
description: "Encryption password was specified, but encryption is not enabled on the server"
"500":
description: "Intenral server error; e.g. Encryption failed (server-side)"
get:
tags:
- "{anywhere}"
summary: Get clipboard data
produces:
- 'text/plain'
parameters:
- in: "secret"
name: "Encryption Password"
description: "Decrypts the contents with the password (server-side!)"
required: false
responses:
"200":
description: "OK"
"406":
description: "Invalid Path"
"423":
description: "Encryption password was specified, but encryption is not enabled on the server"