resolving conflicts

This commit is contained in:
gom-by
2025-01-15 15:52:10 +01:00
4 changed files with 55 additions and 32 deletions

View File

@@ -1,17 +1,16 @@
package api
package api
import (
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"strings"
"time"
"github.com/golang-jwt/jwt/v5"
core "fr.latosa-escrima/api/core"
"github.com/golang-jwt/jwt/v5"
)
var MySigningKey = []byte("COUCOU")
@@ -28,21 +27,37 @@ type Claims struct {
func HandleLogin(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
log.Fatal("Not post method")
core.JSONError{
Status: core.Error,
Message: "Method is not allowed",
}.Respond(w, http.StatusMethodNotAllowed)
return
}
if r.Body == nil {
log.Fatal("No body")
core.JSONError{
Status: core.Error,
Message: "No body has been provided.",
}.Respond(w, http.StatusNoContent)
return
}
body, err := io.ReadAll(r.Body)
if err != nil {
log.Fatal(err)
core.JSONError{
Status: core.Error,
Message: err.Error(),
}.Respond(w, http.StatusNoContent)
return
}
var login LoginInformation
err = json.Unmarshal(body, &login)
if err != nil {
log.Fatal(err)
core.JSONError{
Status: core.Error,
Message: err.Error(),
}.Respond(w, http.StatusNoContent)
return
}
var user core.User
@@ -53,7 +68,11 @@ func HandleLogin(w http.ResponseWriter, r *http.Request) {
Scan(context.Background())
if err != nil {
log.Fatal(err)
core.JSONError{
Status: core.Error,
Message: err.Error(),
}.Respond(w, http.StatusNoContent)
return
}
claims := Claims{
@@ -69,10 +88,18 @@ func HandleLogin(w http.ResponseWriter, r *http.Request) {
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
signed, err := token.SignedString([]byte("hello"))
if err != nil {
log.Fatal(err)
core.JSONError{
Status: core.Error,
Message: err.Error(),
}.Respond(w, http.StatusNoContent)
return
}
fmt.Println(signed)
core.JSONSuccess{
Status: core.Success,
Message: "JWT Created",
Data: map[string]string{"jwt": signed},
}.Respond(w, http.StatusCreated)
}
func AuthJWT(next http.Handler) http.Handler {
@@ -81,8 +108,8 @@ func AuthJWT(next http.Handler) http.Handler {
fmt.Println("Coucou")
authHeader := r.Header.Get("Authorization")
if authHeader == "" {
JSONError{
Status: Error,
core.JSONError{
Status: core.Error,
Message: "Missing Authorization header",
}.Respond(w, http.StatusUnauthorized)
return
@@ -91,8 +118,8 @@ func AuthJWT(next http.Handler) http.Handler {
// Bearer token is expected, so split the header into "Bearer <token>"
tokenString := strings.TrimPrefix(authHeader, "Bearer ")
if tokenString == authHeader {
JSONError{
Status: Error,
core.JSONError{
Status: core.Error,
Message: "Invalid Authorization header format",
}.Respond(w, http.StatusUnauthorized)
return
@@ -108,8 +135,8 @@ func AuthJWT(next http.Handler) http.Handler {
})
if err != nil || !token.Valid {
JSONError{
Status: Error,
core.JSONError{
Status: core.Error,
Message: "Invalid Token",
}.Respond(w, http.StatusUnauthorized)
return

View File

@@ -0,0 +1,87 @@
package core
import (
"net/http"
"encoding/json"
)
type JSONStatus string
const (
Error JSONStatus = "Error"
Success JSONStatus = "Success"
)
type JSONResponse interface {
ToJSON() ([]byte, error)
Respond(w http.ResponseWriter, code int)
}
type JSONError struct {
Status JSONStatus `json:"status"`
Message string `json:"message"`
}
type JSONSuccess struct {
Status JSONStatus `json:"status"`
Message string `json:"message"`
Data any `json:"data,omitempty"`
}
func (r *JSONError) ToJSON() ([]byte, error) {
return json.Marshal(r)
}
func (r *JSONSuccess) ToJSON() ([]byte, error) {
return json.Marshal(r)
}
func defaultResponse(r JSONResponse, w http.ResponseWriter, code int) {
jsonData, err := r.ToJSON()
if err != nil {
http.Error(w, err.Error(), http.StatusNotAcceptable)
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
w.Write(jsonData)
}
func (r JSONError) Respond(w http.ResponseWriter, code int) {
defaultResponse(&r, w, code)
}
func (r JSONSuccess) Respond(w http.ResponseWriter, code int) {
defaultResponse(&r, w, code)
}
func HandleMiddlewareRoute(pattern string,
handler func(w http.ResponseWriter, r *http.Request),
middleware func(http.Handler) http.Handler,
mux *http.ServeMux,
) {
// mux.HandleFunc(pattern, handler)
mux.Handle(pattern, middleware(http.HandlerFunc(handler)))
}
type HandlerFunc func(w http.ResponseWriter, r *http.Request)
type Handler struct {
Handler HandlerFunc
Middleware func(http.Handler) http.Handler
}
func HandleRoutes(mux *http.ServeMux, routes map[string]Handler) {
for pattern, handler := range routes {
if handler.Middleware == nil {
mux.HandleFunc(pattern, handler.Handler)
} else {
HandleMiddlewareRoute(
pattern,
handler.Handler,
handler.Middleware,
mux,
)
}
}
}

View File

@@ -3,8 +3,8 @@ package core
import (
"context"
"database/sql"
"time"
"fmt"
"time"
"github.com/google/uuid"
"github.com/uptrace/bun"