-
Notifications
You must be signed in to change notification settings - Fork 14
/
auth.go
55 lines (49 loc) · 1.78 KB
/
auth.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
package swagno
// https://swagger.io/specification/v2/#security-definitions-object
// SetBasicAuth sets the basic authentication security definition in the Swagger object.
// For more information, refer to: https://swagger.io/specification/v2/#basic-authentication-sample
func (s Swagger) SetBasicAuth(description ...string) {
desc := "Basic Authentication"
if len(description) > 0 {
desc = description[0]
}
s.SecurityDefinitions["basicAuth"] = securityDefinition{
Type: "basic",
Description: desc,
}
}
// SetApiKeyAuth sets the API key authentication security definition in the Swagger object.
// For more information, refer to: https://swagger.io/specification/v2/#api-key-sample
func (s Swagger) SetApiKeyAuth(name string, in string, description ...string) {
desc := "API Key Authentication"
if len(description) > 0 {
desc = description[0]
}
s.SecurityDefinitions[name] = securityDefinition{
Type: "apiKey",
Name: name,
In: in,
Description: desc,
}
}
// SetOAuth2Auth sets the OAuth2 authentication security definition in the Swagger object.
// For more information, refer to: https://swagger.io/specification/v2/#implicit-oauth2-sample
func (s Swagger) SetOAuth2Auth(name string, flow string, authorizationUrl string, tokenUrl string, scopes map[string]string, description ...string) {
desc := "OAuth2 Authentication"
if len(description) > 0 {
desc = description[0]
}
definition := securityDefinition{
Type: "oauth2",
Flow: flow,
Scopes: scopes,
Description: desc,
}
if flow == "implicit" || flow == "accessCode" {
definition.AuthorizationUrl = authorizationUrl
}
if flow == "password" || flow == "accessCode" || flow == "application" {
definition.TokenUrl = tokenUrl
}
s.SecurityDefinitions[name] = definition
}