import sources

This commit is contained in:
Олег Бородин
2024-07-30 09:49:53 +02:00
commit e9d4d1ef07
51 changed files with 15484 additions and 0 deletions

30
pkg/auxhttp/genres.go Normal file
View File

@@ -0,0 +1,30 @@
package auxhttp
import (
"net/http"
"github.com/gin-gonic/gin"
)
type GenericResponse[T any] struct {
Result T `json:"result,omitempty"`
Message string `json:"message,omitempty"`
Error bool `json:"error"`
ErrorCode int64 `json:"errorCode"`
}
func SendError(c *gin.Context, err error) {
var response GenericResponse[interface{}]
response.Error = true
if err != nil {
response.Message = err.Error()
response.ErrorCode = 101
}
c.AbortWithStatusJSON(http.StatusOK, response)
}
func SendResult(c *gin.Context, result any) {
var response GenericResponse[any]
response.Result = result
c.JSON(http.StatusOK, response)
}

57
pkg/auxhttp/getbearer.go Normal file
View File

@@ -0,0 +1,57 @@
package auxhttp
import (
"errors"
"fmt"
"strings"
)
func GetBearerToken(authHeader string) (string, error) {
var err error
var res string
const bearerKey = "Bearer"
const numWords = 2
authData := strings.SplitN(authHeader, " ", numWords)
if len(authData) < numWords {
err = errors.New("Authorization key and value not found")
return res, err
}
authKey := strings.TrimSpace(authData[0])
if authKey != bearerKey {
err = fmt.Errorf("Authorization type is different from %s", bearerKey)
return res, err
}
token := authData[1]
token = strings.TrimSpace(token)
if len(token) == 0 {
return res, errors.New("Lenght of authorization token must be greater zero")
}
res = token
return res, err
}
func HaveBearerToken(authHeader string) bool {
const bearerKey = "Bearer"
const numWords = 2
authData := strings.SplitN(authHeader, " ", numWords)
if len(authData) < numWords {
return false
}
authKey := strings.TrimSpace(authData[0])
if authKey != bearerKey {
return false
}
token := authData[1]
token = strings.TrimSpace(token)
if len(token) == 0 {
return false
}
return true
}

47
pkg/auxhttp/parseauth.go Normal file
View File

@@ -0,0 +1,47 @@
package auxhttp
import (
"encoding/base64"
"errors"
"strings"
)
func ParseAuthBasicHeader(header string) (string, string, error) {
var err error
var username string
var password string
authData := strings.SplitN(header, " ", 2)
if len(authData) < 2 {
err = errors.New("Wrong authentification header")
return username, password, err
}
authType := strings.TrimSpace(authData[0])
if authType != "Basic" {
err = errors.New("Authentification type is different from basic")
return username, password, err
}
authPair := strings.TrimSpace(authData[1])
pairEncoded, err := base64.StdEncoding.DecodeString(authPair)
if err != nil {
return username, password, err
}
pair := strings.SplitN(string(pairEncoded), ":", 2)
if len(pair) < 2 {
err = errors.New("Wrong authentification pair")
return username, password, err
}
username = strings.TrimSpace(pair[0])
password = strings.TrimSpace(pair[1])
if username == "" {
err = errors.New("autentification username is null")
return username, password, err
}
if password == "" {
err = errors.New("autentification password is null")
return username, password, err
}
return username, password, err
}