1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package petstore
16
17 import (
18 goerrors "errors"
19 "io"
20 "net/http"
21 "strings"
22 gotest "testing"
23
24 "github.com/go-openapi/errors"
25 "github.com/go-openapi/loads"
26 "github.com/go-openapi/runtime"
27 testingutil "github.com/go-openapi/runtime/internal/testing"
28 "github.com/go-openapi/runtime/middleware/untyped"
29 "github.com/go-openapi/runtime/security"
30 "github.com/go-openapi/runtime/yamlpc"
31 "github.com/stretchr/testify/require"
32 )
33
34 const (
35 apiPrincipal = "admin"
36 apiUser = "topuser"
37 otherUser = "anyother"
38 )
39
40
41 func NewAPI(t gotest.TB) (*loads.Document, *untyped.API) {
42 spec, err := loads.Analyzed(testingutil.PetStoreJSONMessage, "")
43 require.NoError(t, err)
44 api := untyped.NewAPI(spec)
45
46 api.RegisterConsumer("application/json", runtime.JSONConsumer())
47 api.RegisterProducer("application/json", runtime.JSONProducer())
48 api.RegisterConsumer("application/xml", new(stubConsumer))
49 api.RegisterProducer("application/xml", new(stubProducer))
50 api.RegisterProducer("text/plain", new(stubProducer))
51 api.RegisterProducer("text/html", new(stubProducer))
52 api.RegisterConsumer("application/x-yaml", yamlpc.YAMLConsumer())
53 api.RegisterProducer("application/x-yaml", yamlpc.YAMLProducer())
54
55 api.RegisterAuth("basic", security.BasicAuth(func(username, password string) (interface{}, error) {
56 switch {
57 case username == apiPrincipal && password == apiPrincipal:
58 return apiPrincipal, nil
59 case username == apiUser && password == apiUser:
60 return apiUser, nil
61 case username == otherUser && password == otherUser:
62 return otherUser, nil
63 default:
64 return nil, errors.Unauthenticated("basic")
65 }
66 }))
67 api.RegisterAuth("apiKey", security.APIKeyAuth("X-API-KEY", "header", func(token string) (interface{}, error) {
68 if token == "token123" {
69 return apiPrincipal, nil
70 }
71 return nil, errors.Unauthenticated("token")
72 }))
73 api.RegisterAuthorizer(runtime.AuthorizerFunc(func(r *http.Request, user interface{}) error {
74 if r.Method == http.MethodPost && strings.HasPrefix(r.URL.Path, "/api/pets") && user.(string) != apiPrincipal {
75 if user.(string) == apiUser {
76 return errors.CompositeValidationError(errors.New(errors.InvalidTypeCode, "unauthorized"))
77 }
78
79 return goerrors.New("unauthorized")
80 }
81 return nil
82 }))
83 api.RegisterOperation("get", "/pets", new(stubOperationHandler))
84 api.RegisterOperation("post", "/pets", new(stubOperationHandler))
85 api.RegisterOperation("delete", "/pets/{id}", new(stubOperationHandler))
86 api.RegisterOperation("get", "/pets/{id}", new(stubOperationHandler))
87
88 api.Models["pet"] = func() interface{} { return new(Pet) }
89 api.Models["newPet"] = func() interface{} { return new(Pet) }
90 api.Models["tag"] = func() interface{} { return new(Tag) }
91
92 return spec, api
93 }
94
95
96 func NewRootAPI(t gotest.TB) (*loads.Document, *untyped.API) {
97 spec, err := loads.Analyzed(testingutil.RootPetStoreJSONMessage, "")
98 require.NoError(t, err)
99 api := untyped.NewAPI(spec)
100
101 api.RegisterConsumer("application/json", runtime.JSONConsumer())
102 api.RegisterProducer("application/json", runtime.JSONProducer())
103 api.RegisterConsumer("application/xml", new(stubConsumer))
104 api.RegisterProducer("application/xml", new(stubProducer))
105 api.RegisterProducer("text/plain", new(stubProducer))
106 api.RegisterProducer("text/html", new(stubProducer))
107 api.RegisterConsumer("application/x-yaml", yamlpc.YAMLConsumer())
108 api.RegisterProducer("application/x-yaml", yamlpc.YAMLProducer())
109
110 api.RegisterAuth("basic", security.BasicAuth(func(username, password string) (interface{}, error) {
111 if username == apiPrincipal && password == apiPrincipal {
112 return apiPrincipal, nil
113 }
114 return nil, errors.Unauthenticated("basic")
115 }))
116 api.RegisterAuth("apiKey", security.APIKeyAuth("X-API-KEY", "header", func(token string) (interface{}, error) {
117 if token == "token123" {
118 return apiPrincipal, nil
119 }
120 return nil, errors.Unauthenticated("token")
121 }))
122 api.RegisterAuthorizer(security.Authorized())
123 api.RegisterOperation("get", "/pets", new(stubOperationHandler))
124 api.RegisterOperation("post", "/pets", new(stubOperationHandler))
125 api.RegisterOperation("delete", "/pets/{id}", new(stubOperationHandler))
126 api.RegisterOperation("get", "/pets/{id}", new(stubOperationHandler))
127
128 api.Models["pet"] = func() interface{} { return new(Pet) }
129 api.Models["newPet"] = func() interface{} { return new(Pet) }
130 api.Models["tag"] = func() interface{} { return new(Tag) }
131
132 return spec, api
133 }
134
135
136 type Tag struct {
137 ID int64
138 Name string
139 }
140
141
142 type Pet struct {
143 ID int64
144 Name string
145 PhotoURLs []string
146 Status string
147 Tags []Tag
148 }
149
150 type stubConsumer struct {
151 }
152
153 func (s *stubConsumer) Consume(_ io.Reader, _ interface{}) error {
154 return nil
155 }
156
157 type stubProducer struct {
158 }
159
160 func (s *stubProducer) Produce(_ io.Writer, _ interface{}) error {
161 return nil
162 }
163
164 type stubOperationHandler struct {
165 }
166
167 func (s *stubOperationHandler) ParameterModel() interface{} {
168 return nil
169 }
170
171 func (s *stubOperationHandler) Handle(_ interface{}) (interface{}, error) {
172 return map[string]interface{}{}, nil
173 }
174
View as plain text