aboutsummaryrefslogtreecommitdiffhomepage
path: root/backend/gen/api_handler_wrapper_gen.go
blob: 7fd34b29018726836098418aad39d240f46fd26c (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
157
158
159
160
161
162
163
164
165
166
167
168
package main

import (
	"bytes"
	"flag"
	"go/ast"
	"go/format"
	"go/parser"
	"go/token"
	"os"
	"slices"
	"strings"
	"text/template"
)

func main() {
	inputFile := flag.String("i", "", "input file")
	outputFile := flag.String("o", "", "output file")
	flag.Parse()

	if inputFile == nil || *inputFile == "" || outputFile == nil || *outputFile == "" {
		flag.PrintDefaults()
		os.Exit(1)
	}

	// Parse the input file
	fileSet := token.NewFileSet()
	parsedFile, err := parser.ParseFile(fileSet, *inputFile, nil, parser.SkipObjectResolution)
	if err != nil {
		panic(err)
	}

	// Find methods in StrictServerInterface
	var methods []string
	for _, decl := range parsedFile.Decls {
		genDecl, ok := decl.(*ast.GenDecl)
		if !ok {
			continue
		}
		for _, spec := range genDecl.Specs {
			typeSpec, ok := spec.(*ast.TypeSpec)
			if !ok {
				continue
			}
			if typeSpec.Name.Name != "StrictServerInterface" {
				continue
			}
			interfaceType, ok := typeSpec.Type.(*ast.InterfaceType)
			if !ok {
				continue
			}
			for _, method := range interfaceType.Methods.List {
				if len(method.Names) != 0 {
					methods = append(methods, method.Names[0].Name)
				}
			}
		}
	}
	if len(methods) == 0 {
		panic("StrictServerInterface not found")
	}
	slices.Sort(methods)

	type TemplateParameter struct {
		Name              string
		RequiresLogin     bool
		RequiresAdminRole bool
	}
	templateParameters := make([]TemplateParameter, len(methods))
	for i, method := range methods {
		templateParameters[i] = TemplateParameter{
			Name:              method,
			RequiresLogin:     method != "PostLogin",
			RequiresAdminRole: strings.Contains(method, "Admin"),
		}
	}

	// Generate code.
	tmpl, err := template.New("code").Parse(templateText)
	if err != nil {
		panic(err)
	}

	var buf bytes.Buffer
	err = tmpl.Execute(&buf, templateParameters)
	if err != nil {
		panic(err)
	}

	formatted, err := format.Source(buf.Bytes())
	if err != nil {
		panic(err)
	}

	err = os.WriteFile(*outputFile, formatted, 0644)
	if err != nil {
		panic(err)
	}
}

const templateText = `// 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
}

{{ range . }}
	func (h *ApiHandlerWrapper) {{ .Name }}(ctx context.Context, request {{ .Name }}RequestObject) ({{ .Name }}ResponseObject, error) {
		{{ if .RequiresLogin -}}
			user, err := parseJWTClaimsFromAuthorizationHeader(request.Params.Authorization)
			if err != nil {
				return {{ .Name }}401JSONResponse{
					UnauthorizedJSONResponse: UnauthorizedJSONResponse{
						Message: "Unauthorized",
					},
				}, nil
			}
			{{ if .RequiresAdminRole -}}
				if !user.IsAdmin {
					return {{ .Name }}403JSONResponse{
						ForbiddenJSONResponse: ForbiddenJSONResponse{
							Message: "Forbidden",
						},
					}, nil
				}
			{{ end -}}
			return h.innerHandler.{{ .Name }}(ctx, request, user)
		{{ else -}}
			return h.innerHandler.{{ .Name }}(ctx, request)
		{{ end -}}
	}
{{ end }}
`