2023-06-16 23:35:41 -07:00
|
|
|
package web
|
|
|
|
|
|
|
|
|
|
import (
|
2023-06-22 23:55:46 -07:00
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
|
|
|
|
"strings"
|
2023-06-16 23:35:41 -07:00
|
|
|
|
2023-06-22 23:55:46 -07:00
|
|
|
"code.severnaya.net/kordophone-mock/v2/server"
|
|
|
|
|
"github.com/rs/zerolog/log"
|
2023-06-16 23:35:41 -07:00
|
|
|
)
|
|
|
|
|
|
2023-06-18 13:11:51 -07:00
|
|
|
type MockHTTPServerConfiguration struct {
|
2023-06-22 23:55:46 -07:00
|
|
|
AuthEnabled bool
|
2023-06-18 13:11:51 -07:00
|
|
|
}
|
|
|
|
|
|
2023-06-16 23:35:41 -07:00
|
|
|
type MockHTTPServer struct {
|
2023-06-22 23:55:46 -07:00
|
|
|
Server server.Server
|
|
|
|
|
mux http.ServeMux
|
|
|
|
|
authEnabled bool
|
2023-06-18 13:11:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type AuthError struct {
|
2023-06-22 23:55:46 -07:00
|
|
|
message string
|
2023-06-18 13:11:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *AuthError) Error() string {
|
2023-06-22 23:55:46 -07:00
|
|
|
return e.message
|
2023-06-18 13:11:51 -07:00
|
|
|
}
|
|
|
|
|
|
2023-06-19 20:34:06 -07:00
|
|
|
func (m *MockHTTPServer) logRequest(r *http.Request, extras ...string) {
|
2023-06-22 23:55:46 -07:00
|
|
|
log.Debug().Msgf("%s %s %s", r.Method, r.URL.Path, strings.Join(extras, " "))
|
2023-06-19 20:34:06 -07:00
|
|
|
}
|
|
|
|
|
|
2023-06-18 13:11:51 -07:00
|
|
|
func (m *MockHTTPServer) checkAuthentication(r *http.Request) error {
|
2023-06-22 23:55:46 -07:00
|
|
|
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
|
2023-06-16 23:35:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MockHTTPServer) handleVersion(w http.ResponseWriter, r *http.Request) {
|
2023-06-22 23:55:46 -07:00
|
|
|
fmt.Fprintf(w, "%s", m.Server.Version())
|
2023-06-16 23:35:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MockHTTPServer) handleStatus(w http.ResponseWriter, r *http.Request) {
|
2023-06-22 23:55:46 -07:00
|
|
|
if err := m.checkAuthentication(r); err != nil {
|
|
|
|
|
log.Error().Err(err).Msg("Status: Error checking authentication")
|
|
|
|
|
http.Error(w, err.Error(), http.StatusUnauthorized)
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-06-18 13:11:51 -07:00
|
|
|
|
2023-06-22 23:55:46 -07:00
|
|
|
fmt.Fprintf(w, "OK")
|
2023-06-16 23:35:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MockHTTPServer) handleConversations(w http.ResponseWriter, r *http.Request) {
|
2023-06-22 23:55:46 -07:00
|
|
|
convos := m.Server.Conversations()
|
|
|
|
|
|
|
|
|
|
// Encode convos as JSON
|
|
|
|
|
jsonData, err := json.Marshal(convos)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Error().Err(err).Msg("Error marshalling conversations")
|
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Write JSON to response
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
w.Write(jsonData)
|
2023-06-16 23:35:41 -07:00
|
|
|
}
|
|
|
|
|
|
2023-06-19 18:31:05 -07:00
|
|
|
func (m *MockHTTPServer) handleMessages(w http.ResponseWriter, r *http.Request) {
|
2023-06-22 23:55:46 -07:00
|
|
|
// TODO handle optional "limit", "beforeDate", "beforeMessageGUID", and "afterMessageGUID" parameters
|
|
|
|
|
|
|
|
|
|
guid := r.URL.Query().Get("guid")
|
|
|
|
|
if len(guid) == 0 {
|
|
|
|
|
log.Error().Msg("handleMessage: Got empty guid parameter")
|
|
|
|
|
http.Error(w, "no guid parameter specified", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
conversation, err := m.Server.ConversationForGUID(guid)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Error().Err(err).Msgf("handleMessage: Error getting conversation (%s)", guid)
|
|
|
|
|
http.Error(w, "conversation not found", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-23 00:32:17 -07:00
|
|
|
messages := m.Server.MessagesForConversation(conversation)
|
2023-06-22 23:55:46 -07:00
|
|
|
|
|
|
|
|
jsonData, err := json.Marshal(messages)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Error().Err(err).Msg("Error marshalling messages")
|
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Write JSON to response
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
w.Write(jsonData)
|
2023-06-19 18:31:05 -07:00
|
|
|
}
|
|
|
|
|
|
2023-06-18 13:11:51 -07:00
|
|
|
func (m *MockHTTPServer) handleAuthenticate(w http.ResponseWriter, r *http.Request) {
|
2023-06-22 23:55:46 -07:00
|
|
|
// Decode request body as AuthenticationRequest
|
|
|
|
|
var authReq AuthenticationRequest
|
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&authReq)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Error().Err(err).Msg("Authenticate: Error decoding request body")
|
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Authenticate
|
|
|
|
|
token, err := m.Server.Authenticate(authReq.Username, authReq.Password)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Error().Err(err).Msg("Authenticate: Error authenticating")
|
|
|
|
|
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.Error().Err(err).Msg("Error marshalling token")
|
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Write JSON to response
|
|
|
|
|
w.Write(jsonData)
|
2023-06-18 13:11:51 -07:00
|
|
|
}
|
|
|
|
|
|
2023-06-19 20:34:06 -07:00
|
|
|
func (m *MockHTTPServer) handleNotFound(w http.ResponseWriter, r *http.Request) {
|
2023-06-22 23:55:46 -07:00
|
|
|
log.Error().Msgf("Unimplemented API endpoint: %s %s", r.Method, r.URL.Path)
|
|
|
|
|
http.NotFound(w, r)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MockHTTPServer) handlePollUpdates(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
// Stub: return 205 (Nothing to report)
|
|
|
|
|
w.WriteHeader(http.StatusResetContent)
|
2023-06-19 20:34:06 -07:00
|
|
|
}
|
|
|
|
|
|
2023-06-16 23:35:41 -07:00
|
|
|
func (m *MockHTTPServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2023-06-22 23:55:46 -07:00
|
|
|
m.logRequest(r, r.URL.Query().Encode())
|
|
|
|
|
m.mux.ServeHTTP(w, r)
|
2023-06-16 23:35:41 -07:00
|
|
|
}
|
|
|
|
|
|
2023-06-18 13:11:51 -07:00
|
|
|
func NewMockHTTPServer(config MockHTTPServerConfiguration) *MockHTTPServer {
|
2023-06-22 23:55:46 -07:00
|
|
|
this := MockHTTPServer{
|
|
|
|
|
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))
|
|
|
|
|
this.mux.Handle("/messages", http.HandlerFunc(this.handleMessages))
|
|
|
|
|
this.mux.Handle("/pollUpdates", http.HandlerFunc(this.handlePollUpdates))
|
|
|
|
|
|
|
|
|
|
this.mux.Handle("/", http.HandlerFunc(this.handleNotFound))
|
|
|
|
|
|
|
|
|
|
return &this
|
2023-06-16 23:35:41 -07:00
|
|
|
}
|