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

package api

import (
	"context"
	"errors"
	"strings"

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

var _ StrictServerInterface = (*HandlerWrapper)(nil)

type HandlerWrapper struct {
	impl Handler
}

func NewHandler(queries *db.Queries, hub GameHubInterface) *HandlerWrapper {
	return &HandlerWrapper{
		impl: Handler{
			q:   queries,
			hub: hub,
		},
	}
}

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 *HandlerWrapper) 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.impl.GetGame(ctx, request, user)
}

func (h *HandlerWrapper) GetGamePlayLatestState(ctx context.Context, request GetGamePlayLatestStateRequestObject) (GetGamePlayLatestStateResponseObject, error) {
	user, err := parseJWTClaimsFromAuthorizationHeader(request.Params.Authorization)
	if err != nil {
		return GetGamePlayLatestState401JSONResponse{
			UnauthorizedJSONResponse: UnauthorizedJSONResponse{
				Message: "Unauthorized",
			},
		}, nil
	}
	return h.impl.GetGamePlayLatestState(ctx, request, user)
}

func (h *HandlerWrapper) GetGameWatchLatestStates(ctx context.Context, request GetGameWatchLatestStatesRequestObject) (GetGameWatchLatestStatesResponseObject, error) {
	user, err := parseJWTClaimsFromAuthorizationHeader(request.Params.Authorization)
	if err != nil {
		return GetGameWatchLatestStates401JSONResponse{
			UnauthorizedJSONResponse: UnauthorizedJSONResponse{
				Message: "Unauthorized",
			},
		}, nil
	}
	return h.impl.GetGameWatchLatestStates(ctx, request, user)
}

func (h *HandlerWrapper) GetGameWatchRanking(ctx context.Context, request GetGameWatchRankingRequestObject) (GetGameWatchRankingResponseObject, error) {
	user, err := parseJWTClaimsFromAuthorizationHeader(request.Params.Authorization)
	if err != nil {
		return GetGameWatchRanking401JSONResponse{
			UnauthorizedJSONResponse: UnauthorizedJSONResponse{
				Message: "Unauthorized",
			},
		}, nil
	}
	return h.impl.GetGameWatchRanking(ctx, request, user)
}

func (h *HandlerWrapper) 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.impl.GetGames(ctx, request, user)
}

func (h *HandlerWrapper) GetTournament(ctx context.Context, request GetTournamentRequestObject) (GetTournamentResponseObject, error) {
	user, err := parseJWTClaimsFromAuthorizationHeader(request.Params.Authorization)
	if err != nil {
		return GetTournament401JSONResponse{
			UnauthorizedJSONResponse: UnauthorizedJSONResponse{
				Message: "Unauthorized",
			},
		}, nil
	}
	return h.impl.GetTournament(ctx, request, user)
}

func (h *HandlerWrapper) PostGamePlayCode(ctx context.Context, request PostGamePlayCodeRequestObject) (PostGamePlayCodeResponseObject, error) {
	user, err := parseJWTClaimsFromAuthorizationHeader(request.Params.Authorization)
	if err != nil {
		return PostGamePlayCode401JSONResponse{
			UnauthorizedJSONResponse: UnauthorizedJSONResponse{
				Message: "Unauthorized",
			},
		}, nil
	}
	return h.impl.PostGamePlayCode(ctx, request, user)
}

func (h *HandlerWrapper) PostGamePlaySubmit(ctx context.Context, request PostGamePlaySubmitRequestObject) (PostGamePlaySubmitResponseObject, error) {
	user, err := parseJWTClaimsFromAuthorizationHeader(request.Params.Authorization)
	if err != nil {
		return PostGamePlaySubmit401JSONResponse{
			UnauthorizedJSONResponse: UnauthorizedJSONResponse{
				Message: "Unauthorized",
			},
		}, nil
	}
	return h.impl.PostGamePlaySubmit(ctx, request, user)
}

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