Authentication: Implements authentication
This commit is contained in:
@@ -9,9 +9,47 @@ import (
|
||||
"code.severnaya.net/kordophone-mock/v2/server"
|
||||
)
|
||||
|
||||
type MockHTTPServerConfiguration struct {
|
||||
AuthEnabled bool
|
||||
}
|
||||
|
||||
type MockHTTPServer struct {
|
||||
Server server.Server
|
||||
mux http.ServeMux
|
||||
Server server.Server
|
||||
mux http.ServeMux
|
||||
authEnabled bool
|
||||
}
|
||||
|
||||
type AuthError struct {
|
||||
message string
|
||||
}
|
||||
|
||||
func (e *AuthError) Error() string {
|
||||
return e.message
|
||||
}
|
||||
|
||||
func (m *MockHTTPServer) checkAuthentication(r *http.Request) error {
|
||||
if !m.authEnabled {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Check for Authorization header
|
||||
authHeader := r.Header.Get("Authorization")
|
||||
if authHeader == "" {
|
||||
return &AuthError{"Missing Authorization header"}
|
||||
}
|
||||
|
||||
// Check for "Bearer" prefix
|
||||
if authHeader[:7] != "Bearer " {
|
||||
return &AuthError{"Invalid Authorization header"}
|
||||
}
|
||||
|
||||
// Check for valid token
|
||||
token := authHeader[7:]
|
||||
if !m.Server.CheckBearerToken(token) {
|
||||
return &AuthError{"Invalid token"}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MockHTTPServer) handleVersion(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -19,6 +57,12 @@ func (m *MockHTTPServer) handleVersion(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (m *MockHTTPServer) handleStatus(w http.ResponseWriter, r *http.Request) {
|
||||
if err := m.checkAuthentication(r); err != nil {
|
||||
log.Printf("Status: Error checking authentication: %s", err)
|
||||
http.Error(w, err.Error(), http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Fprintf(w, "OK")
|
||||
}
|
||||
|
||||
@@ -38,19 +82,54 @@ func (m *MockHTTPServer) handleConversations(w http.ResponseWriter, r *http.Requ
|
||||
w.Write(jsonData)
|
||||
}
|
||||
|
||||
func (m *MockHTTPServer) handleAuthenticate(w http.ResponseWriter, r *http.Request) {
|
||||
// Decode request body as AuthenticationRequest
|
||||
var authReq AuthenticationRequest
|
||||
err := json.NewDecoder(r.Body).Decode(&authReq)
|
||||
if err != nil {
|
||||
log.Printf("Authenticate: Error decoding request body: %s", err)
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Authenticate
|
||||
token, err := m.Server.Authenticate(authReq.Username, authReq.Password)
|
||||
if err != nil {
|
||||
log.Printf("Authenticate: Error authenticating: %s", err)
|
||||
http.Error(w, err.Error(), http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
// Write response
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
// Encode token as JSON
|
||||
jsonData, err := json.Marshal(token)
|
||||
if err != nil {
|
||||
log.Printf("Error marshalling token: %s", err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Write JSON to response
|
||||
w.Write(jsonData)
|
||||
}
|
||||
|
||||
func (m *MockHTTPServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
m.mux.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
func NewMockHTTPServer() *MockHTTPServer {
|
||||
func NewMockHTTPServer(config MockHTTPServerConfiguration) *MockHTTPServer {
|
||||
this := MockHTTPServer{
|
||||
Server: *server.NewServer(),
|
||||
mux: *http.NewServeMux(),
|
||||
Server: *server.NewServer(),
|
||||
mux: *http.NewServeMux(),
|
||||
authEnabled: config.AuthEnabled,
|
||||
}
|
||||
|
||||
this.mux.Handle("/version", http.HandlerFunc(this.handleVersion))
|
||||
this.mux.Handle("/conversations", http.HandlerFunc(this.handleConversations))
|
||||
this.mux.Handle("/status", http.HandlerFunc(this.handleStatus))
|
||||
this.mux.Handle("/authenticate", http.HandlerFunc(this.handleAuthenticate))
|
||||
|
||||
return &this
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user