Handling properly now ?
This commit is contained in:
@@ -74,28 +74,26 @@ func HandleLogin(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Println(signed)
|
||||
}
|
||||
|
||||
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)
|
||||
http.Handle(pattern, middleware(mux))
|
||||
}
|
||||
|
||||
func AuthJWT(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// Check if the Authorization header is provided
|
||||
fmt.Println("Coucou")
|
||||
authHeader := r.Header.Get("Authorization")
|
||||
if authHeader == "" {
|
||||
http.Error(w, "Missing Authorization header", http.StatusUnauthorized)
|
||||
JSONError{
|
||||
Status: Error,
|
||||
Message: "Missing Authorization header",
|
||||
}.Respond(w, http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
// Bearer token is expected, so split the header into "Bearer <token>"
|
||||
tokenString := strings.TrimPrefix(authHeader, "Bearer ")
|
||||
if tokenString == authHeader {
|
||||
http.Error(w, "Invalid Authorization header format", http.StatusUnauthorized)
|
||||
JSONError{
|
||||
Status: Error,
|
||||
Message: "Invalid Authorization header format",
|
||||
}.Respond(w, http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -109,7 +107,10 @@ func AuthJWT(next http.Handler) http.Handler {
|
||||
})
|
||||
|
||||
if err != nil || !token.Valid {
|
||||
http.Error(w, "Invalid token", http.StatusUnauthorized)
|
||||
JSONError{
|
||||
Status: Error,
|
||||
Message: "Invalid Token",
|
||||
}.Respond(w, http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -32,6 +32,14 @@ func (dsn *DSN) ToString() string {
|
||||
return fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable", dsn.User, dsn.Password, dsn.Hostname, dsn.Port, dsn.DBName)
|
||||
}
|
||||
|
||||
func er(w http.ResponseWriter, r *http.Request) {
|
||||
_ = r
|
||||
JSONError{
|
||||
Status: Error,
|
||||
Message: "Nope",
|
||||
}.Respond(w, http.StatusUnauthorized)
|
||||
}
|
||||
|
||||
func handlerCreateUser(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
if r.Method != http.MethodPost {
|
||||
@@ -83,6 +91,7 @@ func main() {
|
||||
"/": {handler, nil},
|
||||
"/users/login": {HandleLogin, nil},
|
||||
"/users/new": {handlerCreateUser, AuthJWT},
|
||||
"/error": {er, nil},
|
||||
})
|
||||
|
||||
fmt.Printf("Serving on port %s\n", port)
|
||||
|
||||
@@ -1,6 +1,68 @@
|
||||
package main
|
||||
|
||||
import "net/http"
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user