aboutsummaryrefslogtreecommitdiffhomepage
path: root/backend/api/handler_wrapper.go
blob: 939e37a7f224be045e37fa1d464c90d84434db3b (plain)
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
// Code generated by go generate; DO NOT EDIT.

package api

import (
	"context"
	"errors"
	"strings"

	"github.com/nsfisis/iosdc-japan-2024-albatross/backend/auth"
	"github.com/nsfisis/iosdc-japan-2024-albatross/backend/db"
)

var _ StrictServerInterface = (*ApiHandlerWrapper)(nil)

type ApiHandlerWrapper struct {
	innerHandler ApiHandler
}

func NewHandler(queries *db.Queries, hubs GameHubsInterface) *ApiHandlerWrapper {
	return &ApiHandlerWrapper{
		innerHandler: ApiHandler{
			q:    queries,
			hubs: hubs,
		},
	}
}

func parseJWTClaimsFromAuthorizationHeader(authorization string) (*auth.JWTClaims, error) {
	const prefix = "Bearer "
	if !strings.HasPrefix(authorization, prefix) {
		return nil, errors.New("invalid authorization header")
	}
	token := authorization[len(prefix):]
	claims, err := auth.ParseJWT(token)
	if err != nil {
		return nil, err
	}
	return claims, nil
}

func (h *ApiHandlerWrapper) AdminGetGame(ctx context.Context, request AdminGetGameRequestObject) (AdminGetGameResponseObject, error) {
	user, err := parseJWTClaimsFromAuthorizationHeader(request.Params.Authorization)
	if err != nil {
		return AdminGetGame401JSONResponse{
			UnauthorizedJSONResponse: UnauthorizedJSONResponse{
				Message: "Unauthorized",
			},
		}, nil
	}
	if !user.IsAdmin {
		return AdminGetGame403JSONResponse{
			ForbiddenJSONResponse: ForbiddenJSONResponse{
				Message: "Forbidden",
			},
		}, nil
	}
	return h.innerHandler.AdminGetGame(ctx, request, user)
}

func (h *ApiHandlerWrapper) AdminGetGames(ctx context.Context, request AdminGetGamesRequestObject) (AdminGetGamesResponseObject, error) {
	user, err := parseJWTClaimsFromAuthorizationHeader(request.Params.Authorization)
	if err != nil {
		return AdminGetGames401JSONResponse{
			UnauthorizedJSONResponse: UnauthorizedJSONResponse{
				Message: "Unauthorized",
			},
		}, nil
	}
	if !user.IsAdmin {
		return AdminGetGames403JSONResponse{
			ForbiddenJSONResponse: ForbiddenJSONResponse{
				Message: "Forbidden",
			},
		}, nil
	}
	return h.innerHandler.AdminGetGames(ctx, request, user)
}

func (h *ApiHandlerWrapper) AdminGetUsers(ctx context.Context, request AdminGetUsersRequestObject) (AdminGetUsersResponseObject, error) {
	user, err := parseJWTClaimsFromAuthorizationHeader(request.Params.Authorization)
	if err != nil {
		return AdminGetUsers401JSONResponse{
			UnauthorizedJSONResponse: UnauthorizedJSONResponse{
				Message: "Unauthorized",
			},
		}, nil
	}
	if !user.IsAdmin {
		return AdminGetUsers403JSONResponse{
			ForbiddenJSONResponse: ForbiddenJSONResponse{
				Message: "Forbidden",
			},
		}, nil
	}
	return h.innerHandler.AdminGetUsers(ctx, request, user)
}

func (h *ApiHandlerWrapper) AdminPutGame(ctx context.Context, request AdminPutGameRequestObject) (AdminPutGameResponseObject, error) {
	user, err := parseJWTClaimsFromAuthorizationHeader(request.Params.Authorization)
	if err != nil {
		return AdminPutGame401JSONResponse{
			UnauthorizedJSONResponse: UnauthorizedJSONResponse{
				Message: "Unauthorized",
			},
		}, nil
	}
	if !user.IsAdmin {
		return AdminPutGame403JSONResponse{
			ForbiddenJSONResponse: ForbiddenJSONResponse{
				Message: "Forbidden",
			},
		}, nil
	}
	return h.innerHandler.AdminPutGame(ctx, request, user)
}

func (h *ApiHandlerWrapper) GetGame(ctx context.Context, request GetGameRequestObject) (GetGameResponseObject, error) {
	user, err := parseJWTClaimsFromAuthorizationHeader(request.Params.Authorization)
	if err != nil {
		return GetGame401JSONResponse{
			UnauthorizedJSONResponse: UnauthorizedJSONResponse{
				Message: "Unauthorized",
			},
		}, nil
	}
	return h.innerHandler.GetGame(ctx, request, user)
}

func (h *ApiHandlerWrapper) GetGames(ctx context.Context, request GetGamesRequestObject) (GetGamesResponseObject, error) {
	user, err := parseJWTClaimsFromAuthorizationHeader(request.Params.Authorization)
	if err != nil {
		return GetGames401JSONResponse{
			UnauthorizedJSONResponse: UnauthorizedJSONResponse{
				Message: "Unauthorized",
			},
		}, nil
	}
	return h.innerHandler.GetGames(ctx, request, user)
}

func (h *ApiHandlerWrapper) GetToken(ctx context.Context, request GetTokenRequestObject) (GetTokenResponseObject, error) {
	user, err := parseJWTClaimsFromAuthorizationHeader(request.Params.Authorization)
	if err != nil {
		return GetToken401JSONResponse{
			UnauthorizedJSONResponse: UnauthorizedJSONResponse{
				Message: "Unauthorized",
			},
		}, nil
	}
	return h.innerHandler.GetToken(ctx, request, user)
}

func (h *ApiHandlerWrapper) PostLogin(ctx context.Context, request PostLoginRequestObject) (PostLoginResponseObject, error) {
	return h.innerHandler.PostLogin(ctx, request)
}