-
Notifications
You must be signed in to change notification settings - Fork 72
/
oas_security_gen.go
211 lines (202 loc) · 7.01 KB
/
oas_security_gen.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
// Code generated by ogen, DO NOT EDIT.
package api
import (
"context"
"net/http"
"strings"
"github.com/go-faster/errors"
"github.com/ogen-go/ogen/ogenerrors"
)
// SecurityHandler is handler for security parameters.
type SecurityHandler interface {
// HandleBasicAuth handles basicAuth security.
HandleBasicAuth(ctx context.Context, operationName string, t BasicAuth) (context.Context, error)
// HandleBearerToken handles bearerToken security.
HandleBearerToken(ctx context.Context, operationName string, t BearerToken) (context.Context, error)
// HandleCookieKey handles cookieKey security.
HandleCookieKey(ctx context.Context, operationName string, t CookieKey) (context.Context, error)
// HandleCustom handles custom security.
HandleCustom(ctx context.Context, operationName string, req *http.Request) (context.Context, error)
// HandleHeaderKey handles headerKey security.
HandleHeaderKey(ctx context.Context, operationName string, t HeaderKey) (context.Context, error)
// HandleQueryKey handles queryKey security.
HandleQueryKey(ctx context.Context, operationName string, t QueryKey) (context.Context, error)
}
func findAuthorization(h http.Header, prefix string) (string, bool) {
v, ok := h["Authorization"]
if !ok {
return "", false
}
for _, vv := range v {
scheme, value, ok := strings.Cut(vv, " ")
if !ok || !strings.EqualFold(scheme, prefix) {
continue
}
return value, true
}
return "", false
}
func (s *Server) securityBasicAuth(ctx context.Context, operationName string, req *http.Request) (context.Context, bool, error) {
var t BasicAuth
if _, ok := findAuthorization(req.Header, "Basic"); !ok {
return ctx, false, nil
}
username, password, ok := req.BasicAuth()
if !ok {
return nil, false, errors.New("invalid basic auth")
}
t.Username = username
t.Password = password
rctx, err := s.sec.HandleBasicAuth(ctx, operationName, t)
if errors.Is(err, ogenerrors.ErrSkipServerSecurity) {
return nil, false, nil
} else if err != nil {
return nil, false, err
}
return rctx, true, err
}
func (s *Server) securityBearerToken(ctx context.Context, operationName string, req *http.Request) (context.Context, bool, error) {
var t BearerToken
token, ok := findAuthorization(req.Header, "Bearer")
if !ok {
return ctx, false, nil
}
t.Token = token
rctx, err := s.sec.HandleBearerToken(ctx, operationName, t)
if errors.Is(err, ogenerrors.ErrSkipServerSecurity) {
return nil, false, nil
} else if err != nil {
return nil, false, err
}
return rctx, true, err
}
func (s *Server) securityCookieKey(ctx context.Context, operationName string, req *http.Request) (context.Context, bool, error) {
var t CookieKey
const parameterName = "api_key"
var value string
switch cookie, err := req.Cookie(parameterName); {
case err == nil: // if NO error
value = cookie.Value
case errors.Is(err, http.ErrNoCookie):
return ctx, false, nil
default:
return nil, false, errors.Wrap(err, "get cookie value")
}
t.APIKey = value
rctx, err := s.sec.HandleCookieKey(ctx, operationName, t)
if errors.Is(err, ogenerrors.ErrSkipServerSecurity) {
return nil, false, nil
} else if err != nil {
return nil, false, err
}
return rctx, true, err
}
func (s *Server) securityCustom(ctx context.Context, operationName string, req *http.Request) (context.Context, bool, error) {
t := req
rctx, err := s.sec.HandleCustom(ctx, operationName, t)
if errors.Is(err, ogenerrors.ErrSkipServerSecurity) {
return nil, false, nil
} else if err != nil {
return nil, false, err
}
return rctx, true, err
}
func (s *Server) securityHeaderKey(ctx context.Context, operationName string, req *http.Request) (context.Context, bool, error) {
var t HeaderKey
const parameterName = "X-Api-Key"
value := req.Header.Get(parameterName)
if value == "" {
return ctx, false, nil
}
t.APIKey = value
rctx, err := s.sec.HandleHeaderKey(ctx, operationName, t)
if errors.Is(err, ogenerrors.ErrSkipServerSecurity) {
return nil, false, nil
} else if err != nil {
return nil, false, err
}
return rctx, true, err
}
func (s *Server) securityQueryKey(ctx context.Context, operationName string, req *http.Request) (context.Context, bool, error) {
var t QueryKey
const parameterName = "api_key"
q := req.URL.Query()
if !q.Has(parameterName) {
return ctx, false, nil
}
value := q.Get(parameterName)
t.APIKey = value
rctx, err := s.sec.HandleQueryKey(ctx, operationName, t)
if errors.Is(err, ogenerrors.ErrSkipServerSecurity) {
return nil, false, nil
} else if err != nil {
return nil, false, err
}
return rctx, true, err
}
// SecuritySource is provider of security values (tokens, passwords, etc.).
type SecuritySource interface {
// BasicAuth provides basicAuth security value.
BasicAuth(ctx context.Context, operationName string) (BasicAuth, error)
// BearerToken provides bearerToken security value.
BearerToken(ctx context.Context, operationName string) (BearerToken, error)
// CookieKey provides cookieKey security value.
CookieKey(ctx context.Context, operationName string) (CookieKey, error)
// Custom provides custom security value.
Custom(ctx context.Context, operationName string, req *http.Request) error
// HeaderKey provides headerKey security value.
HeaderKey(ctx context.Context, operationName string) (HeaderKey, error)
// QueryKey provides queryKey security value.
QueryKey(ctx context.Context, operationName string) (QueryKey, error)
}
func (s *Client) securityBasicAuth(ctx context.Context, operationName string, req *http.Request) error {
t, err := s.sec.BasicAuth(ctx, operationName)
if err != nil {
return errors.Wrap(err, "security source \"BasicAuth\"")
}
req.SetBasicAuth(t.Username, t.Password)
return nil
}
func (s *Client) securityBearerToken(ctx context.Context, operationName string, req *http.Request) error {
t, err := s.sec.BearerToken(ctx, operationName)
if err != nil {
return errors.Wrap(err, "security source \"BearerToken\"")
}
req.Header.Set("Authorization", "Bearer "+t.Token)
return nil
}
func (s *Client) securityCookieKey(ctx context.Context, operationName string, req *http.Request) error {
t, err := s.sec.CookieKey(ctx, operationName)
if err != nil {
return errors.Wrap(err, "security source \"CookieKey\"")
}
req.AddCookie(&http.Cookie{
Name: "api_key",
Value: t.APIKey,
})
return nil
}
func (s *Client) securityCustom(ctx context.Context, operationName string, req *http.Request) error {
if err := s.sec.Custom(ctx, operationName, req); err != nil {
return errors.Wrap(err, "security source \"Custom\"")
}
return nil
}
func (s *Client) securityHeaderKey(ctx context.Context, operationName string, req *http.Request) error {
t, err := s.sec.HeaderKey(ctx, operationName)
if err != nil {
return errors.Wrap(err, "security source \"HeaderKey\"")
}
req.Header.Set("X-Api-Key", t.APIKey)
return nil
}
func (s *Client) securityQueryKey(ctx context.Context, operationName string, req *http.Request) error {
t, err := s.sec.QueryKey(ctx, operationName)
if err != nil {
return errors.Wrap(err, "security source \"QueryKey\"")
}
q := req.URL.Query()
q.Set("api_key", t.APIKey)
req.URL.RawQuery = q.Encode()
return nil
}