aboutsummaryrefslogtreecommitdiffhomepage
path: root/backend/api/handler_wrapper.go
blob: 9f524e9b81c9b112aafd28a04ddbf79885be31d6 (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
// 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 = (*HandlerWrapper)(nil)

type HandlerWrapper struct {
	impl Handler
}

func NewHandler(queries *db.Queries, hubs GameHubsInterface) *HandlerWrapper {
	return &HandlerWrapper{
		impl: Handler{
			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 *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) 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) 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.impl.GetToken(ctx, request, user)
}

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