feat(auth): implement yggdrasil authenticate endpoint
This commit is contained in:
51
internal/api/auth_handler.go
Normal file
51
internal/api/auth_handler.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"gitea.mrixs.me/minecraft-platform/backend/internal/core"
|
||||
"gitea.mrixs.me/minecraft-platform/backend/internal/models"
|
||||
)
|
||||
|
||||
type AuthHandler struct {
|
||||
Service *core.AuthService
|
||||
}
|
||||
|
||||
// YggdrasilError - стандартный формат ошибки для authserver
|
||||
type YggdrasilError struct {
|
||||
Error string `json:"error"`
|
||||
ErrorMessage string `json:"errorMessage"`
|
||||
}
|
||||
|
||||
func (h *AuthHandler) Authenticate(w http.ResponseWriter, r *http.Request) {
|
||||
var req models.AuthenticateRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
http.Error(w, "Invalid request body", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
response, err := h.Service.Authenticate(r.Context(), req)
|
||||
if err != nil {
|
||||
if errors.Is(err, core.ErrInvalidCredentials) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusForbidden) // 403
|
||||
json.NewEncoder(w).Encode(YggdrasilError{
|
||||
Error: "ForbiddenOperationException",
|
||||
ErrorMessage: "Invalid credentials. Invalid username or password.",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// Другие ошибки - внутренние
|
||||
log.Printf("internal server error during authentication: %v", err)
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
Reference in New Issue
Block a user