Compare commits
No commits in common. "c553565f2c8c71df0dc2e7b3a0546991edae2e61" and "a9c14cd74cded3bb526bcbba44e452a723832cf2" have entirely different histories.
c553565f2c
...
a9c14cd74c
12 changed files with 83 additions and 199 deletions
|
@ -1,25 +0,0 @@
|
|||
linters:
|
||||
enable-all: true
|
||||
disable:
|
||||
# Deprecated
|
||||
- varcheck
|
||||
- ifshort
|
||||
- interfacer
|
||||
- maligned
|
||||
- deadcode
|
||||
- scopelint
|
||||
- golint
|
||||
- structcheck
|
||||
- exhaustivestruct
|
||||
- nosnakecase
|
||||
# Too extremist/unusable
|
||||
- depguard
|
||||
- varnamelen
|
||||
- exhaustruct
|
||||
- wsl
|
||||
- contextcheck
|
||||
- wrapcheck
|
||||
linters-settings:
|
||||
lll:
|
||||
# Too short byt default
|
||||
line-length: 160
|
|
@ -1,11 +0,0 @@
|
|||
steps:
|
||||
lint:
|
||||
image: golang:1.22
|
||||
commands:
|
||||
- go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
|
||||
- golangci-lint run
|
||||
when:
|
||||
- event: pull_request
|
||||
repo: gnouseu/plakken
|
||||
- event: push
|
||||
branch: main
|
|
@ -4,21 +4,19 @@ import (
|
|||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"git.gnous.eu/gnouseu/plakken/internal/constant"
|
||||
)
|
||||
|
||||
// InitConfig Structure for program initialisation.
|
||||
// InitConfig Structure for program initialisation
|
||||
type InitConfig struct {
|
||||
ListenAddress string
|
||||
RedisAddress string
|
||||
RedisUser string
|
||||
RedisPassword string
|
||||
RedisDB int
|
||||
URLLength uint8
|
||||
UrlLength uint8
|
||||
}
|
||||
|
||||
// GetConfig Initialise configuration form .env.
|
||||
// GetConfig Initialise configuration form .env
|
||||
func GetConfig() InitConfig {
|
||||
listenAddress := os.Getenv("PLAKKEN_LISTEN")
|
||||
redisAddress := os.Getenv("PLAKKEN_REDIS_ADDRESS")
|
||||
|
@ -39,7 +37,7 @@ func GetConfig() InitConfig {
|
|||
log.Fatal("Invalid PLAKKEN_URL_LENGTH")
|
||||
}
|
||||
|
||||
if urlLength > constant.MaxURLLength {
|
||||
if urlLength > 255 {
|
||||
log.Fatal("PLAKKEN_URL_LENGTH cannot be greater than 255")
|
||||
}
|
||||
|
||||
|
@ -49,6 +47,6 @@ func GetConfig() InitConfig {
|
|||
RedisUser: os.Getenv("PLAKKEN_REDIS_USER"),
|
||||
RedisPassword: os.Getenv("PLAKKEN_REDIS_PASSWORD"),
|
||||
RedisDB: redisDB,
|
||||
URLLength: uint8(urlLength),
|
||||
UrlLength: uint8(urlLength),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,8 +11,4 @@ const (
|
|||
ArgonThreads = 4
|
||||
ArgonKeyLength = 32
|
||||
ArgonIterations = 2
|
||||
MaxURLLength = 255
|
||||
SecondsInDay = 86400
|
||||
SecondsInHour = 3600
|
||||
SecondsInMinute = 60
|
||||
)
|
||||
|
|
|
@ -13,9 +13,9 @@ type DBConfig struct {
|
|||
DB *redis.Client
|
||||
}
|
||||
|
||||
var ctx = context.Background() //nolint:gochecknoglobals
|
||||
var ctx = context.Background()
|
||||
|
||||
// InitDB initialise redis connection settings.
|
||||
// InitDB initialise redis connection settings
|
||||
func InitDB(addr string, user string, password string, db int) *redis.Options {
|
||||
DBConfig := &redis.Options{
|
||||
Addr: addr,
|
||||
|
@ -27,20 +27,18 @@ func InitDB(addr string, user string, password string, db int) *redis.Options {
|
|||
return DBConfig
|
||||
}
|
||||
|
||||
// ConnectDB make new database connection.
|
||||
// ConnectDB make new database connection
|
||||
func ConnectDB(dbConfig *redis.Options) *redis.Client {
|
||||
localDB := redis.NewClient(dbConfig)
|
||||
|
||||
return localDB
|
||||
localDb := redis.NewClient(dbConfig)
|
||||
return localDb
|
||||
}
|
||||
|
||||
// Ping test connection to Redis database.
|
||||
// Ping test connection to Redis database
|
||||
func Ping(db *redis.Client) error {
|
||||
status := db.Ping(ctx)
|
||||
if status.String() != "ping: PONG" {
|
||||
return &pingError{}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -67,7 +65,7 @@ func (config DBConfig) InsertPaste(key string, content string, secret string, tt
|
|||
}
|
||||
}
|
||||
|
||||
func (config DBConfig) URLExist(url string) bool {
|
||||
func (config DBConfig) UrlExist(url string) bool {
|
||||
return config.DB.Exists(ctx, url).Val() == 1
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package httpserver
|
||||
package httpServer
|
||||
|
||||
import (
|
||||
"embed"
|
||||
|
@ -12,7 +12,7 @@ import (
|
|||
|
||||
type ServerConfig struct {
|
||||
HTTPServer *http.Server
|
||||
URLLength uint8
|
||||
UrlLength uint8
|
||||
DB *redis.Client
|
||||
Static embed.FS
|
||||
Templates embed.FS
|
||||
|
@ -29,11 +29,11 @@ func (config ServerConfig) home(w http.ResponseWriter, _ *http.Request) {
|
|||
}
|
||||
}
|
||||
|
||||
// Configure HTTP router.
|
||||
// Configure HTTP router
|
||||
func (config ServerConfig) router() {
|
||||
WebConfig := plak.WebConfig{
|
||||
DB: config.DB,
|
||||
URLLength: config.URLLength,
|
||||
UrlLength: config.UrlLength,
|
||||
Templates: config.Templates,
|
||||
}
|
||||
staticFiles := http.FS(config.Static)
|
||||
|
@ -46,7 +46,7 @@ func (config ServerConfig) router() {
|
|||
http.HandleFunc("DELETE /{key}", WebConfig.DeleteRequest)
|
||||
}
|
||||
|
||||
// Config Configure HTTP server.
|
||||
// Config Configure HTTP server
|
||||
func Config(listenAddress string) *http.Server {
|
||||
server := &http.Server{
|
||||
Addr: listenAddress,
|
||||
|
@ -57,7 +57,7 @@ func Config(listenAddress string) *http.Server {
|
|||
return server
|
||||
}
|
||||
|
||||
// Server Start HTTP server.
|
||||
// Server Start HTTP server
|
||||
func (config ServerConfig) Server() {
|
||||
log.Println("Listening on " + config.HTTPServer.Addr)
|
||||
|
|
@ -5,7 +5,6 @@ import (
|
|||
"bytes"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
@ -19,7 +18,7 @@ type argon2idHash struct {
|
|||
hash []byte
|
||||
}
|
||||
|
||||
// Argon2id config.
|
||||
// Argon2id config
|
||||
type config struct {
|
||||
saltLength uint8
|
||||
memory uint32
|
||||
|
@ -28,7 +27,7 @@ type config struct {
|
|||
iterations uint32
|
||||
}
|
||||
|
||||
// generateSecret for password hashing or token generation.
|
||||
// generateSecret for password hashing or token generation
|
||||
func generateSecret(length uint8) ([]byte, error) {
|
||||
secret := make([]byte, length)
|
||||
|
||||
|
@ -40,26 +39,26 @@ func generateSecret(length uint8) ([]byte, error) {
|
|||
return secret, err
|
||||
}
|
||||
|
||||
// GenerateToken generate hexadecimal token.
|
||||
// GenerateToken generate hexadecimal token
|
||||
func GenerateToken() (string, error) {
|
||||
secret, err := generateSecret(constant.TokenLength)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
token := hex.EncodeToString(secret)
|
||||
token := fmt.Sprintf("%x", secret)
|
||||
|
||||
return token, nil
|
||||
}
|
||||
|
||||
// generateArgon2ID Generate an argon2id hash from source string and specified salt.
|
||||
// generateArgon2ID Generate an argon2id hash from source string and specified salt
|
||||
func (config config) generateArgon2ID(source string, salt []byte) []byte {
|
||||
hash := argon2.IDKey([]byte(source), salt, config.iterations, config.memory, config.threads, config.keyLength)
|
||||
|
||||
return hash
|
||||
}
|
||||
|
||||
// Password hash a source string with argon2id, return properly encoded hash.
|
||||
// Password hash a source string with argon2id, return properly encoded hash
|
||||
func Password(password string) (string, error) {
|
||||
config := config{
|
||||
saltLength: constant.ArgonSaltSize,
|
||||
|
@ -96,10 +95,10 @@ func VerifyPassword(password string, hash string) (bool, error) {
|
|||
return bytes.Equal(result, argon2Hash.hash), nil
|
||||
}
|
||||
|
||||
// parseHash parse existing encoded argon2id string.
|
||||
// parseHash parse existing encoded argon2id string
|
||||
func parseHash(source string) (argon2idHash, config, error) {
|
||||
separateItem := strings.Split(source, "$")
|
||||
if len(separateItem) != 6 { //nolint:gomnd
|
||||
if len(separateItem) != 6 {
|
||||
return argon2idHash{}, config{}, &parseError{message: "Hash format is not valid"}
|
||||
}
|
||||
|
||||
|
@ -108,7 +107,7 @@ func parseHash(source string) (argon2idHash, config, error) {
|
|||
}
|
||||
|
||||
separateParam := strings.Split(separateItem[3], ",")
|
||||
if len(separateParam) != 3 { //nolint:gomnd
|
||||
if len(separateParam) != 3 {
|
||||
return argon2idHash{}, config{}, &parseError{message: "Hash config is not valid"}
|
||||
}
|
||||
|
||||
|
@ -127,35 +126,22 @@ func parseHash(source string) (argon2idHash, config, error) {
|
|||
keyLength := uint32(len(hash))
|
||||
|
||||
var memory int
|
||||
memory, err = strconv.Atoi(strings.ReplaceAll(separateParam[0], "m=", ""))
|
||||
memory, err = strconv.Atoi(strings.Replace(separateParam[0], "m=", "", -1))
|
||||
if err != nil {
|
||||
return argon2idHash{}, config{}, err
|
||||
}
|
||||
|
||||
var iterations int
|
||||
iterations, err = strconv.Atoi(strings.ReplaceAll(separateParam[1], "t=", ""))
|
||||
iterations, err = strconv.Atoi(strings.Replace(separateParam[1], "t=", "", -1))
|
||||
if err != nil {
|
||||
return argon2idHash{}, config{}, err
|
||||
}
|
||||
|
||||
var threads int
|
||||
threads, err = strconv.Atoi(strings.ReplaceAll(separateParam[2], "p=", ""))
|
||||
threads, err = strconv.Atoi(strings.Replace(separateParam[2], "p=", "", -1))
|
||||
if err != nil {
|
||||
return argon2idHash{}, config{}, err
|
||||
}
|
||||
|
||||
argon2idStruct := argon2idHash{
|
||||
salt: salt,
|
||||
hash: hash,
|
||||
}
|
||||
|
||||
hashConfig := config{
|
||||
saltLength: saltLength,
|
||||
memory: uint32(memory),
|
||||
threads: uint8(threads),
|
||||
iterations: uint32(iterations),
|
||||
keyLength: keyLength,
|
||||
}
|
||||
|
||||
return argon2idStruct, hashConfig, nil
|
||||
return argon2idHash{salt: salt, hash: hash}, config{saltLength: saltLength, memory: uint32(memory), threads: uint8(threads), iterations: uint32(iterations), keyLength: keyLength}, nil
|
||||
}
|
||||
|
|
|
@ -6,12 +6,10 @@ import (
|
|||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"git.gnous.eu/gnouseu/plakken/internal/constant"
|
||||
)
|
||||
|
||||
// GenerateURL generate random string for plak url.
|
||||
func GenerateURL(length uint8) string {
|
||||
// GenerateUrl generate random string for plak url
|
||||
func GenerateUrl(length uint8) string {
|
||||
listChars := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
|
||||
b := make([]rune, length)
|
||||
for i := range b {
|
||||
|
@ -21,7 +19,7 @@ func GenerateURL(length uint8) string {
|
|||
return string(b)
|
||||
}
|
||||
|
||||
// CheckCharRedundant verify is a character is redundant in a string.
|
||||
// CheckCharRedundant verify is a character is redundant in a string
|
||||
func CheckCharRedundant(source string, char string) bool { // Verify if a char is redundant
|
||||
return strings.Count(source, char) > 1
|
||||
}
|
||||
|
@ -36,24 +34,22 @@ func parseIntBeforeSeparator(source *string, sep string) (int, error) { // retur
|
|||
value, err = strconv.Atoi(strings.Split(*source, sep)[0])
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
||||
return 0, &parseIntBeforeSeparatorError{message: *source + ": cannot parse value as int"}
|
||||
}
|
||||
if value < 0 { // Only positive value is correct
|
||||
return 0, &parseIntBeforeSeparatorError{message: *source + ": format only take positive value"}
|
||||
}
|
||||
|
||||
if value > 99 { //nolint:gomnd
|
||||
if value > 99 {
|
||||
return 0, &parseIntBeforeSeparatorError{message: *source + ": Format only take two number"}
|
||||
}
|
||||
|
||||
*source = strings.Join(strings.Split(*source, sep)[1:], "")
|
||||
}
|
||||
|
||||
return value, nil
|
||||
}
|
||||
|
||||
// ParseExpiration Parse "1d1h1m1s" duration format. Return 0 & error if error.
|
||||
// ParseExpiration Parse "1d1h1m1s" duration format. Return 0 & error if error
|
||||
func ParseExpiration(source string) (int, error) {
|
||||
var expiration int
|
||||
var tempOutput int
|
||||
|
@ -65,44 +61,39 @@ func ParseExpiration(source string) (int, error) {
|
|||
source = strings.ToLower(source)
|
||||
|
||||
tempOutput, err = parseIntBeforeSeparator(&source, "d")
|
||||
expiration = tempOutput * constant.SecondsInDay
|
||||
expiration = tempOutput * 86400
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
||||
return 0, &ParseExpirationError{message: "Invalid syntax"}
|
||||
}
|
||||
tempOutput, err = parseIntBeforeSeparator(&source, "h")
|
||||
expiration += tempOutput * constant.SecondsInHour
|
||||
expiration += tempOutput * 3600
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
||||
return 0, &ParseExpirationError{message: "Invalid syntax"}
|
||||
}
|
||||
tempOutput, err = parseIntBeforeSeparator(&source, "m")
|
||||
expiration += tempOutput * constant.SecondsInMinute
|
||||
expiration += tempOutput * 60
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
||||
return 0, &ParseExpirationError{message: "Invalid syntax"}
|
||||
}
|
||||
tempOutput, err = parseIntBeforeSeparator(&source, "s")
|
||||
expiration += tempOutput * 1
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
||||
return 0, &ParseExpirationError{message: "Invalid syntax"}
|
||||
}
|
||||
|
||||
return expiration, nil
|
||||
}
|
||||
|
||||
// ValidKey Verify if a key is valid (only letter, number, - and _).
|
||||
// ValidKey Verify if a key is valid (only letter, number, - and _)
|
||||
func ValidKey(key string) bool {
|
||||
result, err := regexp.MatchString("^[a-zA-Z0-9_.-]*$", key)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
log.Println(key, result)
|
||||
|
||||
return result
|
||||
}
|
||||
|
|
|
@ -3,7 +3,6 @@ package plak
|
|||
import (
|
||||
"context"
|
||||
"embed"
|
||||
"html/template"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
|
@ -14,17 +13,19 @@ import (
|
|||
"git.gnous.eu/gnouseu/plakken/internal/secret"
|
||||
"git.gnous.eu/gnouseu/plakken/internal/utils"
|
||||
"github.com/redis/go-redis/v9"
|
||||
|
||||
"html/template"
|
||||
)
|
||||
|
||||
var ctx = context.Background() //nolint:gochecknoglobals
|
||||
var ctx = context.Background()
|
||||
|
||||
type WebConfig struct {
|
||||
DB *redis.Client
|
||||
URLLength uint8
|
||||
UrlLength uint8
|
||||
Templates embed.FS
|
||||
}
|
||||
|
||||
// plak "Object" for plak.
|
||||
// plak "Object" for plak
|
||||
type plak struct {
|
||||
Key string
|
||||
Content string
|
||||
|
@ -32,7 +33,7 @@ type plak struct {
|
|||
DB *redis.Client
|
||||
}
|
||||
|
||||
// create a plak.
|
||||
// create a plak
|
||||
func (plak plak) create() (string, error) {
|
||||
dbConf := database.DBConfig{
|
||||
DB: plak.DB,
|
||||
|
@ -43,7 +44,7 @@ func (plak plak) create() (string, error) {
|
|||
return "", err
|
||||
}
|
||||
|
||||
if dbConf.URLExist(plak.Key) {
|
||||
if dbConf.UrlExist(plak.Key) {
|
||||
return "", &createError{message: "key already exist"}
|
||||
}
|
||||
|
||||
|
@ -58,23 +59,21 @@ func (plak plak) create() (string, error) {
|
|||
return token, nil
|
||||
}
|
||||
|
||||
// PostCreate manage POST request for create plak.
|
||||
// PostCreate manage POST request for create plak
|
||||
func (config WebConfig) PostCreate(w http.ResponseWriter, r *http.Request) {
|
||||
content := r.FormValue("content")
|
||||
if content == "" {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
filename := r.FormValue("filename")
|
||||
var key string
|
||||
if len(filename) == 0 {
|
||||
key = utils.GenerateURL(config.URLLength)
|
||||
key = utils.GenerateUrl(config.UrlLength)
|
||||
} else {
|
||||
if !utils.ValidKey(filename) {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
|
||||
return
|
||||
}
|
||||
key = filename
|
||||
|
@ -91,7 +90,6 @@ func (config WebConfig) PostCreate(w http.ResponseWriter, r *http.Request) {
|
|||
if err != nil {
|
||||
log.Println(err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -105,18 +103,16 @@ func (config WebConfig) PostCreate(w http.ResponseWriter, r *http.Request) {
|
|||
if err != nil {
|
||||
log.Println(err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
http.Redirect(w, r, "/"+key, http.StatusSeeOther)
|
||||
}
|
||||
|
||||
// CurlCreate PostCreate plak with minimum param, ideal for curl. Force 7 day expiration.
|
||||
// CurlCreate PostCreate plak with minimum param, ideal for curl. Force 7 day expiration
|
||||
func (config WebConfig) CurlCreate(w http.ResponseWriter, r *http.Request) {
|
||||
if r.ContentLength == 0 {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -126,7 +122,7 @@ func (config WebConfig) CurlCreate(w http.ResponseWriter, r *http.Request) {
|
|||
log.Println(err)
|
||||
}
|
||||
|
||||
key := utils.GenerateURL(config.URLLength)
|
||||
key := utils.GenerateUrl(config.UrlLength)
|
||||
|
||||
plak := plak{
|
||||
Key: key,
|
||||
|
@ -139,7 +135,6 @@ func (config WebConfig) CurlCreate(w http.ResponseWriter, r *http.Request) {
|
|||
token, err = plak.create()
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -159,7 +154,7 @@ func (config WebConfig) CurlCreate(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
}
|
||||
|
||||
// View for plak.
|
||||
// View for plak
|
||||
func (config WebConfig) View(w http.ResponseWriter, r *http.Request) {
|
||||
dbConf := database.DBConfig{
|
||||
DB: config.DB,
|
||||
|
@ -167,8 +162,7 @@ func (config WebConfig) View(w http.ResponseWriter, r *http.Request) {
|
|||
var currentPlak plak
|
||||
key := r.PathValue("key")
|
||||
|
||||
//nolint:nestif
|
||||
if dbConf.URLExist(key) {
|
||||
if dbConf.UrlExist(key) {
|
||||
currentPlak = plak{
|
||||
Key: key,
|
||||
DB: config.DB,
|
||||
|
@ -185,14 +179,12 @@ func (config WebConfig) View(w http.ResponseWriter, r *http.Request) {
|
|||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
log.Println(err)
|
||||
|
||||
return
|
||||
}
|
||||
err = t.Execute(w, currentPlak)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
log.Println(err)
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
@ -201,7 +193,7 @@ func (config WebConfig) View(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
}
|
||||
|
||||
// DeleteRequest manage plak deletion endpoint.
|
||||
// DeleteRequest manage plak deletion endpoint
|
||||
func (config WebConfig) DeleteRequest(w http.ResponseWriter, r *http.Request) {
|
||||
dbConf := database.DBConfig{
|
||||
DB: config.DB,
|
||||
|
@ -209,8 +201,7 @@ func (config WebConfig) DeleteRequest(w http.ResponseWriter, r *http.Request) {
|
|||
key := r.PathValue("key")
|
||||
var valid bool
|
||||
|
||||
//nolint:nestif
|
||||
if dbConf.URLExist(key) {
|
||||
if dbConf.UrlExist(key) {
|
||||
var err error
|
||||
token := r.URL.Query().Get("secret")
|
||||
|
||||
|
@ -218,6 +209,7 @@ func (config WebConfig) DeleteRequest(w http.ResponseWriter, r *http.Request) {
|
|||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
if valid {
|
||||
|
@ -231,31 +223,29 @@ func (config WebConfig) DeleteRequest(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
return
|
||||
} else {
|
||||
w.WriteHeader(http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}
|
||||
|
||||
// delete DeleteRequest plak from database.
|
||||
// delete DeleteRequest plak from database
|
||||
func (plak plak) delete() error {
|
||||
err := plak.DB.Del(ctx, plak.Key).Err()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
||||
return &deletePlakError{name: plak.Key, err: err}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// getContent get plak content.
|
||||
// getContent get plak content
|
||||
func (plak plak) getContent() plak {
|
||||
plak.Content = plak.DB.HGet(ctx, plak.Key, "content").Val()
|
||||
|
||||
return plak
|
||||
}
|
||||
|
|
8
main.go
8
main.go
|
@ -6,7 +6,7 @@ import (
|
|||
|
||||
"git.gnous.eu/gnouseu/plakken/internal/config"
|
||||
"git.gnous.eu/gnouseu/plakken/internal/database"
|
||||
"git.gnous.eu/gnouseu/plakken/internal/httpserver"
|
||||
"git.gnous.eu/gnouseu/plakken/internal/httpServer"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -25,9 +25,9 @@ func main() {
|
|||
log.Fatal(err)
|
||||
}
|
||||
|
||||
serverConfig := httpserver.ServerConfig{
|
||||
HTTPServer: httpserver.Config(initConfig.ListenAddress),
|
||||
URLLength: initConfig.URLLength,
|
||||
serverConfig := httpServer.ServerConfig{
|
||||
HTTPServer: httpServer.Config(initConfig.ListenAddress),
|
||||
UrlLength: initConfig.UrlLength,
|
||||
DB: db,
|
||||
Static: static,
|
||||
Templates: templates,
|
||||
|
|
|
@ -10,17 +10,8 @@ import (
|
|||
"golang.org/x/crypto/argon2"
|
||||
)
|
||||
|
||||
func TestSecret(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
testPasswordFormat(t)
|
||||
testVerifyPassword(t)
|
||||
testVerifyPasswordInvalid(t)
|
||||
}
|
||||
|
||||
func testPasswordFormat(t *testing.T) {
|
||||
t.Helper()
|
||||
regex := fmt.Sprintf("\\$argon2id\\$v=%d\\$m=%d,t=%d,p=%d\\$[A-Za-z0-9+/]*\\$[A-Za-z0-9+/]*$", argon2.Version, constant.ArgonMemory, constant.ArgonIterations, constant.ArgonThreads) //nolint:lll
|
||||
func TestPasswordFormat(t *testing.T) {
|
||||
regex := fmt.Sprintf("\\$argon2id\\$v=%d\\$m=%d,t=%d,p=%d\\$[A-Za-z0-9+/]*\\$[A-Za-z0-9+/]*$", argon2.Version, constant.ArgonMemory, constant.ArgonIterations, constant.ArgonThreads)
|
||||
|
||||
got, err := secret.Password("Password!")
|
||||
if err != nil {
|
||||
|
@ -33,9 +24,8 @@ func testPasswordFormat(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func testVerifyPassword(t *testing.T) {
|
||||
t.Helper()
|
||||
result, err := secret.VerifyPassword("Password!", "$argon2id$v=19$m=65536,t=2,p=4$A+t5YGpyy1BHCbvk/LP1xQ$eNuUj6B2ZqXlGi6KEqep39a7N4nysUIojuPXye+Ypp0") //nolint:lll
|
||||
func TestVerifyPassword(t *testing.T) {
|
||||
result, err := secret.VerifyPassword("Password!", "$argon2id$v=19$m=65536,t=2,p=4$A+t5YGpyy1BHCbvk/LP1xQ$eNuUj6B2ZqXlGi6KEqep39a7N4nysUIojuPXye+Ypp0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
@ -45,9 +35,8 @@ func testVerifyPassword(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func testVerifyPasswordInvalid(t *testing.T) {
|
||||
t.Helper()
|
||||
result, err := secret.VerifyPassword("notsamepassword", "$argon2id$v=19$m=65536,t=2,p=4$A+t5YGpyy1BHCbvk/LP1xQ$eNuUj6B2ZqXlGi6KEqep39a7N4nysUIojuPXye+Ypp0") //nolint:lll
|
||||
func TestVerifyPasswordInvalid(t *testing.T) {
|
||||
result, err := secret.VerifyPassword("notsamepassword", "$argon2id$v=19$m=65536,t=2,p=4$A+t5YGpyy1BHCbvk/LP1xQ$eNuUj6B2ZqXlGi6KEqep39a7N4nysUIojuPXye+Ypp0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
|
|
@ -7,25 +7,7 @@ import (
|
|||
"git.gnous.eu/gnouseu/plakken/internal/utils"
|
||||
)
|
||||
|
||||
func TestUtils(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
testCheckCharNotRedundantTrue(t)
|
||||
testCheckCharNotRedundantFalse(t)
|
||||
testParseExpirationFull(t)
|
||||
testParseExpirationMissing(t)
|
||||
testParseExpirationWithCaps(t)
|
||||
testParseExpirationNull(t)
|
||||
testParseExpirationNegative(t)
|
||||
testParseExpirationInvalid(t)
|
||||
testParseExpirationInvalidRedundant(t)
|
||||
testParseExpirationInvalidTooHigh(t)
|
||||
testValidKey(t)
|
||||
testInValidKey(t)
|
||||
}
|
||||
|
||||
func testCheckCharNotRedundantTrue(t *testing.T) { // Test CheckCharRedundant with redundant char
|
||||
t.Helper()
|
||||
func TestCheckCharNotRedundantTrue(t *testing.T) { // Test CheckCharRedundant with redundant char
|
||||
want := true
|
||||
got := utils.CheckCharRedundant("2d1h3md4h7s", "h")
|
||||
if got != want {
|
||||
|
@ -33,8 +15,7 @@ func testCheckCharNotRedundantTrue(t *testing.T) { // Test CheckCharRedundant wi
|
|||
}
|
||||
}
|
||||
|
||||
func testCheckCharNotRedundantFalse(t *testing.T) { // Test CheckCharRedundant with not redundant char
|
||||
t.Helper()
|
||||
func TestCheckCharNotRedundantFalse(t *testing.T) { // Test CheckCharRedundant with not redundant char
|
||||
want := false
|
||||
got := utils.CheckCharRedundant("2d1h3m47s", "h")
|
||||
if got != want {
|
||||
|
@ -42,8 +23,7 @@ func testCheckCharNotRedundantFalse(t *testing.T) { // Test CheckCharRedundant w
|
|||
}
|
||||
}
|
||||
|
||||
func testParseExpirationFull(t *testing.T) { // test parseExpirationFull with all valid separator
|
||||
t.Helper()
|
||||
func TestParseExpirationFull(t *testing.T) { // test parseExpirationFull with all valid separator
|
||||
result, _ := utils.ParseExpiration("2d1h3m47s")
|
||||
correctValue := 176627
|
||||
if result != correctValue {
|
||||
|
@ -51,8 +31,7 @@ func testParseExpirationFull(t *testing.T) { // test parseExpirationFull with al
|
|||
}
|
||||
}
|
||||
|
||||
func testParseExpirationMissing(t *testing.T) { // test parseExpirationFull with all valid separator
|
||||
t.Helper()
|
||||
func TestParseExpirationMissing(t *testing.T) { // test parseExpirationFull with all valid separator
|
||||
result, _ := utils.ParseExpiration("1h47s")
|
||||
correctValue := 3647
|
||||
if result != correctValue {
|
||||
|
@ -60,8 +39,7 @@ func testParseExpirationMissing(t *testing.T) { // test parseExpirationFull with
|
|||
}
|
||||
}
|
||||
|
||||
func testParseExpirationWithCaps(t *testing.T) { // test parseExpirationFull with all valid separator
|
||||
t.Helper()
|
||||
func TestParseExpirationWithCaps(t *testing.T) { // test parseExpirationFull with all valid separator
|
||||
result, _ := utils.ParseExpiration("2D1h3M47s")
|
||||
correctValue := 176627
|
||||
if result != correctValue {
|
||||
|
@ -69,8 +47,7 @@ func testParseExpirationWithCaps(t *testing.T) { // test parseExpirationFull wit
|
|||
}
|
||||
}
|
||||
|
||||
func testParseExpirationNull(t *testing.T) { // test ParseExpirationFull with all valid separator
|
||||
t.Helper()
|
||||
func TestParseExpirationNull(t *testing.T) { // test ParseExpirationFull with all valid separator
|
||||
result, _ := utils.ParseExpiration("0")
|
||||
correctValue := 0
|
||||
if result != correctValue {
|
||||
|
@ -78,8 +55,7 @@ func testParseExpirationNull(t *testing.T) { // test ParseExpirationFull with al
|
|||
}
|
||||
}
|
||||
|
||||
func testParseExpirationNegative(t *testing.T) { // test ParseExpirationFull with all valid separator
|
||||
t.Helper()
|
||||
func TestParseExpirationNegative(t *testing.T) { // test ParseExpirationFull with all valid separator
|
||||
_, got := utils.ParseExpiration("-42h1m4s")
|
||||
want := &utils.ParseExpirationError{}
|
||||
if !errors.As(got, &want) {
|
||||
|
@ -87,17 +63,16 @@ func testParseExpirationNegative(t *testing.T) { // test ParseExpirationFull wit
|
|||
}
|
||||
}
|
||||
|
||||
func testParseExpirationInvalid(t *testing.T) { // test ParseExpirationFull with all valid separator
|
||||
t.Helper()
|
||||
func TestParseExpirationInvalid(t *testing.T) { // test ParseExpirationFull with all valid separator
|
||||
_, got := utils.ParseExpiration("8h42h1m1d4s")
|
||||
want := &utils.ParseExpirationError{}
|
||||
if !errors.As(got, &want) {
|
||||
t.Fatal("Error in ParseExpirationFull, want : ", want, "got : ", got)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func testParseExpirationInvalidRedundant(t *testing.T) { // test ParseExpirationFull with all valid separator
|
||||
t.Helper()
|
||||
func TestParseExpirationInvalidRedundant(t *testing.T) { // test ParseExpirationFull with all valid separator
|
||||
_, got := utils.ParseExpiration("8h42h1m1h4s")
|
||||
want := &utils.ParseExpirationError{}
|
||||
if !errors.As(got, &want) {
|
||||
|
@ -105,8 +80,7 @@ func testParseExpirationInvalidRedundant(t *testing.T) { // test ParseExpiration
|
|||
}
|
||||
}
|
||||
|
||||
func testParseExpirationInvalidTooHigh(t *testing.T) { // test ParseExpirationFull with all valid separator
|
||||
t.Helper()
|
||||
func TestParseExpirationInvalidTooHigh(t *testing.T) { // test ParseExpirationFull with all valid separator
|
||||
_, got := utils.ParseExpiration("2d1h3m130s")
|
||||
want := &utils.ParseExpirationError{}
|
||||
if !errors.As(got, &want) {
|
||||
|
@ -114,8 +88,7 @@ func testParseExpirationInvalidTooHigh(t *testing.T) { // test ParseExpirationFu
|
|||
}
|
||||
}
|
||||
|
||||
func testValidKey(t *testing.T) { // test ValidKey with a valid key
|
||||
t.Helper()
|
||||
func TestValidKey(t *testing.T) { // test ValidKey with a valid key
|
||||
got := utils.ValidKey("ab_a-C42")
|
||||
want := true
|
||||
|
||||
|
@ -124,8 +97,7 @@ func testValidKey(t *testing.T) { // test ValidKey with a valid key
|
|||
}
|
||||
}
|
||||
|
||||
func testInValidKey(t *testing.T) { // test ValidKey with invalid key
|
||||
t.Helper()
|
||||
func TestInValidKey(t *testing.T) { // test ValidKey with invalid key
|
||||
got := utils.ValidKey("ab_?a-C42")
|
||||
want := false
|
||||
|
||||
|
|
Loading…
Reference in a new issue