-
Notifications
You must be signed in to change notification settings - Fork 34
/
generate.go
221 lines (209 loc) · 6.79 KB
/
generate.go
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package security
import (
"path/filepath"
"strings"
"goa.design/goa/codegen"
"goa.design/goa/codegen/service"
goadesign "goa.design/goa/design"
"goa.design/goa/eval"
"goa.design/plugins/security/design"
// Initializes the HTTP generator
_ "goa.design/plugins/security/http"
)
type (
// ServiceData contains the data necessary to render the secure endpoints
// constructor.
ServiceData struct {
// Name is the name of the service.
Name string
// VarName is the Go name of the service interface.
VarName string
// PkgName is the name of the service package.
PkgName string
// EndpointsVarName is the Go endpoints struct name.
EndpointsVarName string
// Methods list the endpoint constructors.
Methods []*MethodData
}
// MethodData contains the data necessry to render
// endpoints.
MethodData struct {
// VarName is the goified name of the constructor method. It
// differs to the name generated by goa when the endpoint
// defines security scheme.
VarName string
// NonSecureVarName is the goified name of constructor method
// generated by goa.
NonSecureVarName string
// FieldName is the name of the corresponding endpoints struct
// field.
FieldName string
// ServiceName is the name of the corresponding service.
ServiceName string
// MethodName is the name of the corresponding service method.
MethodName string
// Requirements lists the security requirements that apply to
// the secured method.
Requirements []*design.SecurityExpr
}
)
// Register the plugin Generator functions.
func init() {
codegen.RegisterPlugin("gen", Generate)
codegen.RegisterPlugin("example", Example)
}
// Generate produces server code that enforce the security requirements defined
// in the design. Generate also produces client code that makes it possible to
// provide the required security artifacts. Finally Generate also generate code
// that initializes the context given to the service methods with security
// information.
func Generate(genpkg string, roots []eval.Root, files []*codegen.File) ([]*codegen.File, error) {
for _, root := range roots {
switch r := root.(type) {
case *goadesign.RootExpr:
for _, s := range r.Services {
if f := SecureEndpointFile(genpkg, s); f != nil {
files = append(files, f)
}
}
}
}
return files, nil
}
// SecureEndpointFile returns the file containing the secure endpoint
// definitions.
func SecureEndpointFile(genpkg string, svc *goadesign.ServiceExpr) *codegen.File {
data := BuildSecureServiceData(svc)
path := filepath.Join(codegen.Gendir, codegen.SnakeCase(svc.Name), "security.go")
header := codegen.Header(
svc.Name+" service security",
data.PkgName,
[]*codegen.ImportSpec{
{Path: "context"},
{Path: "goa.design/goa"},
{Path: "goa.design/plugins/security"},
})
init := &codegen.SectionTemplate{
Name: "secure-endpoint-init",
Source: secureEndpointsInitT,
Data: data,
FuncMap: codegen.TemplateFuncs(),
}
sections := []*codegen.SectionTemplate{header, init}
for _, m := range data.Methods {
if len(m.Requirements) == 0 {
continue
}
sections = append(sections, &codegen.SectionTemplate{
Source: secureEndpointContextT,
Data: m,
})
}
if len(sections) == 1 {
return nil
}
return &codegen.File{
Path: path,
SectionTemplates: sections,
}
}
// Example modified the generated main function so that the secured endpoints
// context gets initialized with the security requirements.
func Example(genpkg string, roots []eval.Root, files []*codegen.File) ([]*codegen.File, error) {
for _, f := range files {
for _, s := range f.Section("service-main") {
s.Source = strings.Replace(
s.Source,
"{{ .Service.PkgName }}Endpoints = {{ .Service.PkgName }}.NewEndpoints({{ .Service.PkgName }}Svc)",
"{{ .Service.PkgName }}Endpoints = {{ .Service.PkgName }}.NewSecureEndpoints({{ .Service.PkgName }}Svc)",
1,
)
}
}
return files, nil
}
// BuildSecureServiceData builds the data needed to render the secured endpoints
// struct constructor and the secure endpoint methods.
func BuildSecureServiceData(svc *goadesign.ServiceExpr) *ServiceData {
s := service.Services.Get(svc.Name)
data := &ServiceData{
Name: svc.Name,
PkgName: s.PkgName,
VarName: service.ServiceInterfaceName,
EndpointsVarName: service.EndpointsStructName,
}
for _, m := range svc.Methods {
reqs := design.Requirements(svc.Name, m.Name)
varn := s.Method(m.Name).VarName
cn := "New" + varn
if len(reqs) > 0 {
cn = "Secure" + varn
}
data.Methods = append(data.Methods, &MethodData{
VarName: cn,
NonSecureVarName: "New" + varn + "Endpoint",
FieldName: varn,
ServiceName: svc.Name,
MethodName: m.Name,
Requirements: reqs,
})
}
return data
}
// input: securedServiceData
const secureEndpointsInitT = `{{ printf "NewSecure%s wraps the methods of a %s service with security scheme aware endpoints." .EndpointsVarName .Name | comment }}
func NewSecure{{ .EndpointsVarName }}(s {{ .VarName }}) *{{ .EndpointsVarName }} {
return &{{ .EndpointsVarName }}{
{{- range .Methods }}
{{ .FieldName }}: {{ .VarName }}{{ if .Requirements }}({{ .NonSecureVarName }}{{ end }}(s){{ if .Requirements }}){{ end }},
{{- end }}
}
}
`
// input: securedServiceMethodData
const secureEndpointContextT = `{{ printf "%s returns an endpoint function which initializes the context with the security requirements for the method %q of service %q." .VarName .MethodName .ServiceName | comment }}
func {{ .VarName }}(ep goa.Endpoint) goa.Endpoint {
reqs := []*security.Requirement{
{{- range $req := .Requirements }}
&security.Requirement{
{{- if $req.Scopes }}
RequiredScopes: []string{ {{- range $req.Scopes }}{{ printf "%q" . }}, {{ end }} },
{{- end }}
Schemes: []*security.Scheme{
{{- range $scheme := .Schemes }}
&security.Scheme{
Kind: security.SchemeKind({{ $scheme.Kind }}),
Name: {{ printf "%q" $scheme.SchemeName }},
{{- if .Scopes }}
Scopes: []string{ {{- range $scheme.Scopes }}{{ printf "%q" .Name }}, {{ end }} },
{{- end }}
{{- if .Flows }}
Flows: []*security.OAuthFlow{
{{- range $flow := .Flows }}
&security.OAuthFlow{
Kind: security.FlowKind({{ $flow.Kind }}),
{{- if .AuthorizationURL }}
AuthorizationURL: {{ printf "%q" .AuthorizationURL }},
{{- end }}
{{- if .TokenURL }}
TokenURL: {{ printf "%q" .TokenURL }},
{{- end }}
{{- if .RefreshURL }}
RefreshURL: {{ printf "%q" .RefreshURL }},
{{- end }}
},
{{- end }}
},
{{- end }}
},
{{- end }}
},
},
{{- end }}
}
return func(ctx context.Context, req interface{}) (interface{}, error) {
ctx = context.WithValue(ctx, security.ContextKey, reqs)
return ep(ctx, req)
}
}
`