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
|
package openapi3_test
import (
"context"
"testing"
"github.com/getkin/kin-openapi/openapi3"
"github.com/stretchr/testify/require"
)
type securitySchemeExample struct {
title string
raw []byte
valid bool
}
func TestSecuritySchemaExample(t *testing.T) {
for _, example := range securitySchemeExamples {
t.Run(example.title, testSecuritySchemaExample(t, example))
}
}
func testSecuritySchemaExample(t *testing.T, e securitySchemeExample) func(*testing.T) {
return func(t *testing.T) {
var err error
ss := &openapi3.SecurityScheme{}
err = ss.UnmarshalJSON(e.raw)
require.NoError(t, err)
err = ss.Validate(context.TODO())
if e.valid {
require.NoError(t, err)
} else {
require.Error(t, err)
}
}
}
// from https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#fixed-fields-23
var securitySchemeExamples = []securitySchemeExample{
{
title: "Basic Authentication Sample",
raw: []byte(`
{
"type": "http",
"scheme": "basic"
}
`),
valid: true,
},
{
title: "API Key Sample",
raw: []byte(`
{
"type": "apiKey",
"name": "api_key",
"in": "header"
}
`),
valid: true,
},
{
title: "JWT Bearer Sample",
raw: []byte(`
{
"type": "http",
"scheme": "bearer",
"bearerFormat": "JWT"
}
`),
valid: true,
},
{
title: "Implicit OAuth2 Sample",
raw: []byte(`
{
"type": "oauth2",
"flows": {
"implicit": {
"authorizationUrl": "https://example.com/api/oauth/dialog",
"scopes": {
"write:pets": "modify pets in your account",
"read:pets": "read your pets"
}
}
}
}
`),
valid: true,
},
{
title: "OAuth Flow Object Sample",
raw: []byte(`
{
"type": "oauth2",
"flows": {
"implicit": {
"authorizationUrl": "https://example.com/api/oauth/dialog",
"scopes": {
"write:pets": "modify pets in your account",
"read:pets": "read your pets"
}
},
"authorizationCode": {
"authorizationUrl": "https://example.com/api/oauth/dialog",
"tokenUrl": "https://example.com/api/oauth/token",
"scopes": {
"write:pets": "modify pets in your account",
"read:pets": "read your pets"
}
}
}
}
`),
valid: true,
},
{
title: "OAuth Flow Object clientCredentials/password",
raw: []byte(`
{
"type": "oauth2",
"flows": {
"clientCredentials": {
"tokenUrl": "https://example.com/api/oauth/token",
"scopes": {
"write:pets": "modify pets in your account"
}
},
"password": {
"tokenUrl": "https://example.com/api/oauth/token",
"scopes": {
"read:pets": "read your pets"
}
}
}
}
`),
valid: true,
},
{
title: "Invalid Basic",
raw: []byte(`
{
"type": "https",
"scheme": "basic"
}
`),
valid: false,
},
{
title: "Apikey Cookie",
raw: []byte(`
{
"type": "apiKey",
"in": "cookie",
"name": "somecookie"
}
`),
valid: true,
},
}
|