1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
package api
import (
"context"
"net/http"
"strings"
"github.com/labstack/echo/v4"
"github.com/nsfisis/iosdc-2024-albatross-backend/auth"
"github.com/nsfisis/iosdc-2024-albatross-backend/db"
)
var _ StrictServerInterface = (*ApiHandler)(nil)
type ApiHandler struct {
q *db.Queries
}
func NewHandler(queries *db.Queries) *ApiHandler {
return &ApiHandler{
q: queries,
}
}
func (h *ApiHandler) PostLogin(ctx context.Context, request PostLoginRequestObject) (PostLoginResponseObject, error) {
username := request.Body.Username
password := request.Body.Password
userId, err := auth.Login(ctx, h.q, username, password)
if err != nil {
return PostLogin401JSONResponse{
Message: "Invalid username or password",
}, nil
}
user, err := h.q.GetUserById(ctx, int32(userId))
if err != nil {
return PostLogin401JSONResponse{
Message: "Invalid username or password",
}, nil
}
jwt, err := auth.NewJWT(&user)
if err != nil {
return nil, echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
return PostLogin200JSONResponse{
Token: jwt,
}, nil
}
func (h *ApiHandler) GetGames(ctx context.Context, request GetGamesRequestObject) (GetGamesResponseObject, error) {
user := ctx.Value("user").(*auth.JWTClaims)
playerId := request.Params.PlayerId
if !user.IsAdmin {
if playerId == nil || *playerId != user.UserID {
return GetGames403JSONResponse{
Message: "Forbidden",
}, nil
}
}
if playerId == nil {
gameRows, err := h.q.ListGames(ctx)
if err != nil {
return nil, echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
games := make([]Game, len(gameRows))
for i, row := range gameRows {
var startedAt *int
if row.StartedAt.Valid {
startedAtTimestamp := int(row.StartedAt.Time.Unix())
startedAt = &startedAtTimestamp
}
var problem *Problem
if row.ProblemID.Valid {
if !row.Title.Valid || !row.Description.Valid {
panic("inconsistent data")
}
problem = &Problem{
ProblemId: int(row.ProblemID.Int32),
Title: row.Title.String,
Description: row.Description.String,
}
}
games[i] = Game{
GameId: int(row.GameID),
State: GameState(row.State),
DisplayName: row.DisplayName,
DurationSeconds: int(row.DurationSeconds),
StartedAt: startedAt,
Problem: problem,
}
}
return GetGames200JSONResponse{
Games: games,
}, nil
} else {
gameRows, err := h.q.ListGamesForPlayer(ctx, int32(*playerId))
if err != nil {
return nil, echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
games := make([]Game, len(gameRows))
for i, row := range gameRows {
var startedAt *int
if row.StartedAt.Valid {
startedAtTimestamp := int(row.StartedAt.Time.Unix())
startedAt = &startedAtTimestamp
}
var problem *Problem
if row.ProblemID.Valid {
if !row.Title.Valid || !row.Description.Valid {
panic("inconsistent data")
}
problem = &Problem{
ProblemId: int(row.ProblemID.Int32),
Title: row.Title.String,
Description: row.Description.String,
}
}
games[i] = Game{
GameId: int(row.GameID),
State: GameState(row.State),
DisplayName: row.DisplayName,
DurationSeconds: int(row.DurationSeconds),
StartedAt: startedAt,
Problem: problem,
}
}
return GetGames200JSONResponse{
Games: games,
}, nil
}
}
func _assertJwtPayloadIsCompatibleWithJWTClaims() {
var c auth.JWTClaims
var p JwtPayload
p.UserId = c.UserID
p.Username = c.Username
p.DisplayName = c.DisplayName
p.IconPath = c.IconPath
p.IsAdmin = c.IsAdmin
_ = p
}
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)
}
}
}
}
|