diff options
Diffstat (limited to 'backend/api')
| -rw-r--r-- | backend/api/generated.go | 224 | ||||
| -rw-r--r-- | backend/api/handlers.go | 48 |
2 files changed, 237 insertions, 35 deletions
diff --git a/backend/api/generated.go b/backend/api/generated.go index e39e3ba..7e64929 100644 --- a/backend/api/generated.go +++ b/backend/api/generated.go @@ -34,7 +34,12 @@ const ( // Defines values for GamePlayerMessageS2CExecResultPayloadStatus. const ( - Success GamePlayerMessageS2CExecResultPayloadStatus = "success" + GamePlayerMessageS2CExecResultPayloadStatusSuccess GamePlayerMessageS2CExecResultPayloadStatus = "success" +) + +// Defines values for GameWatcherMessageS2CExecResultPayloadStatus. +const ( + GameWatcherMessageS2CExecResultPayloadStatusSuccess GameWatcherMessageS2CExecResultPayloadStatus = "success" ) // Game defines model for Game. @@ -123,6 +128,57 @@ type GamePlayerMessageS2CStartPayload struct { StartAt int `json:"start_at"` } +// GameWatcherMessage defines model for GameWatcherMessage. +type GameWatcherMessage struct { + union json.RawMessage +} + +// GameWatcherMessageS2C defines model for GameWatcherMessageS2C. +type GameWatcherMessageS2C struct { + union json.RawMessage +} + +// GameWatcherMessageS2CCode defines model for GameWatcherMessageS2CCode. +type GameWatcherMessageS2CCode struct { + Data GameWatcherMessageS2CCodePayload `json:"data"` + Type string `json:"type"` +} + +// GameWatcherMessageS2CCodePayload defines model for GameWatcherMessageS2CCodePayload. +type GameWatcherMessageS2CCodePayload struct { + Code string `json:"code"` + PlayerId int `json:"player_id"` +} + +// GameWatcherMessageS2CExecResult defines model for GameWatcherMessageS2CExecResult. +type GameWatcherMessageS2CExecResult struct { + Data GameWatcherMessageS2CExecResultPayload `json:"data"` + Type string `json:"type"` +} + +// GameWatcherMessageS2CExecResultPayload defines model for GameWatcherMessageS2CExecResultPayload. +type GameWatcherMessageS2CExecResultPayload struct { + PlayerId int `json:"player_id"` + Score *int `json:"score"` + Status GameWatcherMessageS2CExecResultPayloadStatus `json:"status"` + Stderr string `json:"stderr"` + Stdout string `json:"stdout"` +} + +// GameWatcherMessageS2CExecResultPayloadStatus defines model for GameWatcherMessageS2CExecResultPayload.Status. +type GameWatcherMessageS2CExecResultPayloadStatus string + +// GameWatcherMessageS2CStart defines model for GameWatcherMessageS2CStart. +type GameWatcherMessageS2CStart struct { + Data GameWatcherMessageS2CStartPayload `json:"data"` + Type string `json:"type"` +} + +// GameWatcherMessageS2CStartPayload defines model for GameWatcherMessageS2CStartPayload. +type GameWatcherMessageS2CStartPayload struct { + StartAt int `json:"start_at"` +} + // JwtPayload defines model for JwtPayload. type JwtPayload struct { DisplayName string `json:"display_name"` @@ -397,6 +453,130 @@ func (t *GamePlayerMessageS2C) UnmarshalJSON(b []byte) error { return err } +// AsGameWatcherMessageS2C returns the union data inside the GameWatcherMessage as a GameWatcherMessageS2C +func (t GameWatcherMessage) AsGameWatcherMessageS2C() (GameWatcherMessageS2C, error) { + var body GameWatcherMessageS2C + err := json.Unmarshal(t.union, &body) + return body, err +} + +// FromGameWatcherMessageS2C overwrites any union data inside the GameWatcherMessage as the provided GameWatcherMessageS2C +func (t *GameWatcherMessage) FromGameWatcherMessageS2C(v GameWatcherMessageS2C) error { + b, err := json.Marshal(v) + t.union = b + return err +} + +// MergeGameWatcherMessageS2C performs a merge with any union data inside the GameWatcherMessage, using the provided GameWatcherMessageS2C +func (t *GameWatcherMessage) MergeGameWatcherMessageS2C(v GameWatcherMessageS2C) error { + b, err := json.Marshal(v) + if err != nil { + return err + } + + merged, err := runtime.JSONMerge(t.union, b) + t.union = merged + return err +} + +func (t GameWatcherMessage) MarshalJSON() ([]byte, error) { + b, err := t.union.MarshalJSON() + return b, err +} + +func (t *GameWatcherMessage) UnmarshalJSON(b []byte) error { + err := t.union.UnmarshalJSON(b) + return err +} + +// AsGameWatcherMessageS2CStart returns the union data inside the GameWatcherMessageS2C as a GameWatcherMessageS2CStart +func (t GameWatcherMessageS2C) AsGameWatcherMessageS2CStart() (GameWatcherMessageS2CStart, error) { + var body GameWatcherMessageS2CStart + err := json.Unmarshal(t.union, &body) + return body, err +} + +// FromGameWatcherMessageS2CStart overwrites any union data inside the GameWatcherMessageS2C as the provided GameWatcherMessageS2CStart +func (t *GameWatcherMessageS2C) FromGameWatcherMessageS2CStart(v GameWatcherMessageS2CStart) error { + b, err := json.Marshal(v) + t.union = b + return err +} + +// MergeGameWatcherMessageS2CStart performs a merge with any union data inside the GameWatcherMessageS2C, using the provided GameWatcherMessageS2CStart +func (t *GameWatcherMessageS2C) MergeGameWatcherMessageS2CStart(v GameWatcherMessageS2CStart) error { + b, err := json.Marshal(v) + if err != nil { + return err + } + + merged, err := runtime.JSONMerge(t.union, b) + t.union = merged + return err +} + +// AsGameWatcherMessageS2CCode returns the union data inside the GameWatcherMessageS2C as a GameWatcherMessageS2CCode +func (t GameWatcherMessageS2C) AsGameWatcherMessageS2CCode() (GameWatcherMessageS2CCode, error) { + var body GameWatcherMessageS2CCode + err := json.Unmarshal(t.union, &body) + return body, err +} + +// FromGameWatcherMessageS2CCode overwrites any union data inside the GameWatcherMessageS2C as the provided GameWatcherMessageS2CCode +func (t *GameWatcherMessageS2C) FromGameWatcherMessageS2CCode(v GameWatcherMessageS2CCode) error { + b, err := json.Marshal(v) + t.union = b + return err +} + +// MergeGameWatcherMessageS2CCode performs a merge with any union data inside the GameWatcherMessageS2C, using the provided GameWatcherMessageS2CCode +func (t *GameWatcherMessageS2C) MergeGameWatcherMessageS2CCode(v GameWatcherMessageS2CCode) error { + b, err := json.Marshal(v) + if err != nil { + return err + } + + merged, err := runtime.JSONMerge(t.union, b) + t.union = merged + return err +} + +// AsGameWatcherMessageS2CExecResult returns the union data inside the GameWatcherMessageS2C as a GameWatcherMessageS2CExecResult +func (t GameWatcherMessageS2C) AsGameWatcherMessageS2CExecResult() (GameWatcherMessageS2CExecResult, error) { + var body GameWatcherMessageS2CExecResult + err := json.Unmarshal(t.union, &body) + return body, err +} + +// FromGameWatcherMessageS2CExecResult overwrites any union data inside the GameWatcherMessageS2C as the provided GameWatcherMessageS2CExecResult +func (t *GameWatcherMessageS2C) FromGameWatcherMessageS2CExecResult(v GameWatcherMessageS2CExecResult) error { + b, err := json.Marshal(v) + t.union = b + return err +} + +// MergeGameWatcherMessageS2CExecResult performs a merge with any union data inside the GameWatcherMessageS2C, using the provided GameWatcherMessageS2CExecResult +func (t *GameWatcherMessageS2C) MergeGameWatcherMessageS2CExecResult(v GameWatcherMessageS2CExecResult) error { + b, err := json.Marshal(v) + if err != nil { + return err + } + + merged, err := runtime.JSONMerge(t.union, b) + t.union = merged + return err +} + +func (t GameWatcherMessageS2C) MarshalJSON() ([]byte, error) { + b, err := t.union.MarshalJSON() + return b, err +} + +func (t *GameWatcherMessageS2C) UnmarshalJSON(b []byte) error { + err := t.union.UnmarshalJSON(b) + return err +} + // ServerInterface represents all server handlers. type ServerInterface interface { // List games @@ -730,26 +910,28 @@ func (sh *strictHandler) PostLogin(ctx echo.Context) error { // Base64 encoded, gzipped, json marshaled Swagger object var swaggerSpec = []string{ - "H4sIAAAAAAAC/9xX3W7jNhN9FX38CmwLCP7LItj6Lk23aRZb1Fi3V4vAoMWxzZQitRxqEzfQuxf8kWRZ", - "8kpJfFE0F44tcYZnzpwhZ55IotJMSZAGyfyJYLKDlLqvNzQF+z/TKgNtOLinjGMm6H4lw1t4pGkmgMzd", - "+mhKYmL2mf2NRnO5JUVMWK6p4UquEBIlGTbsLi4nlQmXBragrc2WprDirLF02rUw02otILULv9OwIXPy", - "/3Ed0zgENF6EZUVM0FBtgK2oaXj/8e3l5bu37yadcNBQ4+OVeUrmn0kiFAIjMXmg3HC5XYE02nJUP3H7", - "EIsQMqqBhJ0tKS4+/2XDJccdMHIXH5BZuT8is4iJhi8518AsipKlEmDczE8H9XeVS7W+h8TY4GzmFoLu", - "Qf8GiHTrAlUSft+Q+edv09oyXc6uSRE/0+h6tiTFXRcS++blYK5ny/fS6P2LEH0Cyl5mea0YnI7HvW3X", - "FTW0T8OnvC3oXijKbCp9bm1VSzRkTjK3fJ7McJ7YffsE5d7GHs0gqR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bpRFRc0jJioaK4ox7IallJyoMJIJ9hQb5OrpsIxonUvDJTWfOan7Bmp3Z4UuJsiPCtXuIPwAgaKxZrJ", + "qsPktw1Dj6FHvXx/a2r17lWnhqOZ5ge+Z6iaLuKad0gXA2fJr2CvO21MMLGS9jPWnU1u+ZJqJRE9A0wJ", + "yr0tLL3b2QPxyRdQaMNARoPxYGQwyxgEjRmZkqvBaDAiPjGcsoEbrmnkQrgGWxQmqvb66iEkU3IP+t4K", + "GBVFI9Cg0K5FhlnkcwL2C97xodqnbKsoP773SyrT3gANQZXqt4neSMX+sseT/ci5Jl4zWUT50QhjLAU6", + "XyajUdZ5NAjrFo1jzgJrefiEjiWlvSqZipAwDRF26YVluyNUKbprvDTEI9mtcJd8YKg9ufKcRuqT69HV", + "K3yJymW5JOxPUi1ZGILwimy3Ujc31MWHwr61gkkUUbXLfcscS/2Me8OX7EY1bWWh+fEQHuGibZMFl8pb", + "2lYW/ZeJ2U68evRvbYj/Z9S5B+3RzDFDHS7XbrbFEhsYM5OoP1gRBwVQ/yDdzeCF0Ygp4laq8OBzIns6", + "nlw1DZZXzspsJBZHNwewysa011ao5Z9wMFGfzb/B3s/2Nc4a6ZL9udu2VwnnO48megNCG6gQOjqP+6bz", + "g/hCOQu9QEFozqIce6Vzbj/PpieVV6SzyvDfEZTnaJ2mafp3AAAA///2zTVTJhwAAA==", } // GetSwagger returns the content of the embedded swagger specification file diff --git a/backend/api/handlers.go b/backend/api/handlers.go index cd8b3b5..f50558d 100644 --- a/backend/api/handlers.go +++ b/backend/api/handlers.go @@ -179,26 +179,46 @@ func _assertJwtPayloadIsCompatibleWithJWTClaims() { _ = p } +func setupJWTFromAuthorizationHeader(c echo.Context) error { + authorization := c.Request().Header.Get("Authorization") + const prefix = "Bearer " + if !strings.HasPrefix(authorization, prefix) { + return echo.NewHTTPError(http.StatusUnauthorized) + } + token := authorization[len(prefix):] + claims, err := auth.ParseJWT(token) + if err != nil { + return echo.NewHTTPError(http.StatusUnauthorized, err.Error()) + } + c.Set("user", claims) + c.SetRequest(c.Request().WithContext(context.WithValue(c.Request().Context(), "user", claims))) + return nil +} + +func NewEchoJWTMiddleware() echo.MiddlewareFunc { + return func(next echo.HandlerFunc) echo.HandlerFunc { + return func(c echo.Context) error { + err := setupJWTFromAuthorizationHeader(c) + if err != nil { + return echo.NewHTTPError(http.StatusUnauthorized, err.Error()) + } + return next(c) + } + } +} + func NewJWTMiddleware() StrictMiddlewareFunc { return func(handler StrictHandlerFunc, operationID string) StrictHandlerFunc { if operationID == "PostLogin" { return handler - } else { - return func(c echo.Context, request interface{}) (response interface{}, err error) { - authorization := c.Request().Header.Get("Authorization") - const prefix = "Bearer " - if !strings.HasPrefix(authorization, prefix) { - return nil, echo.NewHTTPError(http.StatusUnauthorized) - } - token := authorization[len(prefix):] + } - claims, err := auth.ParseJWT(token) - if err != nil { - return nil, echo.NewHTTPError(http.StatusUnauthorized) - } - c.SetRequest(c.Request().WithContext(context.WithValue(c.Request().Context(), "user", claims))) - return handler(c, request) + return func(c echo.Context, request interface{}) (interface{}, error) { + err := setupJWTFromAuthorizationHeader(c) + if err != nil { + return nil, echo.NewHTTPError(http.StatusUnauthorized, err.Error()) } + return handler(c, request) } } } |
