-
Notifications
You must be signed in to change notification settings - Fork 28
/
security.go
174 lines (162 loc) · 5.26 KB
/
security.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
package openapi3edit
import (
"errors"
"fmt"
"strings"
oas3 "github.com/getkin/kin-openapi/openapi3"
"github.com/grokify/mogo/type/maputil"
"github.com/grokify/spectrum/openapi3"
)
const (
SecuritySchemeApikeyDefaultName = "ApiKeyAuth"
SecuritySchemeBearertokenDefaultName = "BearerAuth"
SchemeHTTP = "http"
TokenTypeBearer = "bearer"
)
// SecuritySchemeAddBearertoken adds bearer token auth
// to spec and operations.
func SecuritySchemeAddBearertoken(spec *openapi3.Spec, schemeName, bearerFormat string, inclTags, skipTags []string) {
schemeName = strings.TrimSpace(schemeName)
if len(schemeName) == 0 {
schemeName = SecuritySchemeBearertokenDefaultName
}
SecuritySchemeBearertokenAddDefinition(spec, schemeName, bearerFormat)
SecuritySchemeBearertokenAddOperationsByTags(spec, schemeName, inclTags, skipTags)
}
func SecuritySchemeBearertokenAddOperationsByTags(spec *openapi3.Spec, schemeName string, inclTags, skipTags []string) {
inclTagsMap := map[string]int{}
for _, tag := range inclTags {
tag = strings.ToLower(strings.TrimSpace(tag))
if len(tag) > 0 {
inclTagsMap[tag] = 1
}
}
skipTagsMap := map[string]int{}
for _, tag := range skipTags {
tag = strings.ToLower(strings.TrimSpace(tag))
if len(tag) > 0 {
skipTagsMap[tag] = 1
}
}
openapi3.VisitOperations(spec, func(skipPath string, skipMethod string, op *oas3.Operation) {
addSecurity := false
for _, tag := range op.Tags {
tag = strings.ToLower(strings.TrimSpace(tag))
if _, ok := inclTagsMap[tag]; ok {
addSecurity = true
}
if len(inclTagsMap) == 0 {
if len(skipTagsMap) == 0 {
addSecurity = true
} else if _, ok := skipTagsMap[tag]; !ok {
addSecurity = true
}
}
}
if addSecurity {
SecuritySchemeAddOperation(op, schemeName, []string{})
}
})
}
// SecuritySchemeAddOperation adds a scheme name and value to an operation.
func SecuritySchemeAddOperation(op *oas3.Operation, schemeName string, schemeValue []string) {
if op.Security == nil {
op.Security = &oas3.SecurityRequirements{}
}
op.Security.With(
oas3.SecurityRequirement(
map[string][]string{
schemeName: schemeValue}))
}
func SecuritySchemeBearertokenAddDefinition(spec *openapi3.Spec, schemeName, bearerFormat string) {
schemeName = strings.TrimSpace(schemeName)
bearerFormat = strings.TrimSpace(bearerFormat)
if len(schemeName) == 0 {
schemeName = SecuritySchemeBearertokenDefaultName
}
scheme := &oas3.SecuritySchemeRef{
Value: &oas3.SecurityScheme{
Type: SchemeHTTP,
Scheme: TokenTypeBearer}}
if len(bearerFormat) > 0 {
scheme.Value.BearerFormat = bearerFormat
}
spec.Components.SecuritySchemes[schemeName] = scheme
}
// AddAPIKey adds an API Key definition to the spec.
// https://swagger.io/docs/specification/authentication/api-keys/
func SecuritySchemeApikeyAddDefinition(spec *openapi3.Spec, schemeName, location, name string) error {
schemeName = strings.TrimSpace(schemeName)
location = strings.TrimSpace(location)
name = strings.TrimSpace(name)
if len(schemeName) == 0 {
schemeName = SecuritySchemeApikeyDefaultName
}
if len(location) == 0 {
return errors.New("API Key Security Scheme Location cannot be empty. Must be one of: [\"header\", \"query\", \"cookie\"]")
} else if location != "header" && location != "query" && location != "cookie" {
return fmt.Errorf("API Key Security Scheme Invalid Location [%s], must be one of: [\"header\", \"query\", \"cookie\"]", location)
}
if len(name) == 0 {
return errors.New("API Key Security Scheme name cannot be empty.")
}
if spec.Components.SecuritySchemes == nil {
spec.Components.SecuritySchemes = map[string]*oas3.SecuritySchemeRef{}
}
spec.Components.SecuritySchemes[schemeName] = &oas3.SecuritySchemeRef{
Value: &oas3.SecurityScheme{
Type: "apiKey",
In: location,
Name: name,
},
}
return nil
}
func SecuritySchemeApikeyAddOperations(spec *openapi3.Spec, tags []string, keyName string) {
keyName = strings.TrimSpace(keyName)
if len(keyName) == 0 {
keyName = SecuritySchemeApikeyDefaultName
}
tagsMap := map[string]int{}
for _, tagName := range tags {
tagName = strings.TrimSpace(tagName)
tagsMap[tagName] = 1
}
openapi3.VisitOperations(spec, func(skipPath, skipMethod string, op *oas3.Operation) {
if !maputil.MapSliceIntersectionExists(tagsMap, op.Tags) {
return
}
SecuritySchemeAddOperation(op, keyName, []string{})
})
}
/*
func MapSliceIntersection(haystack map[string]int, needles []string, unique bool) []string {
if unique {
needles = stringsutil.SliceCondenseSpace(needles, true, false)
}
matches := []string{}
for _, needle := range needles {
if _, ok := haystack[needle]; ok {
matches = append(matches, needle)
}
}
return matches
}
*/
func MapSliceIntersectionExists(haystack map[string]int, needles []string) bool {
for _, needle := range needles {
if _, ok := haystack[needle]; ok {
return true
}
}
return false
}
// RemoveOperationsSecurity removes the security property
// for all operations. It is useful when building a spec
// to get individual specs to validate before setting the
// correct security property.
func RemoveOperationsSecurity(spec *openapi3.Spec) {
openapi3.VisitOperations(spec, func(skipPath, skipMethod string, op *oas3.Operation) {
op.Security = &oas3.SecurityRequirements{}
})
}