Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add OIDC Schema format as per spec #287

Merged
merged 4 commits into from
Jan 19, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 18 additions & 8 deletions openapi3/security_scheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ var _ jsonpointer.JSONPointable = (*SecuritySchemes)(nil)
type SecurityScheme struct {
ExtensionProps

Type string `json:"type,omitempty" yaml:"type,omitempty"`
Description string `json:"description,omitempty" yaml:"description,omitempty"`
Name string `json:"name,omitempty" yaml:"name,omitempty"`
In string `json:"in,omitempty" yaml:"in,omitempty"`
Scheme string `json:"scheme,omitempty" yaml:"scheme,omitempty"`
BearerFormat string `json:"bearerFormat,omitempty" yaml:"bearerFormat,omitempty"`
Flows *OAuthFlows `json:"flows,omitempty" yaml:"flows,omitempty"`
Type string `json:"type,omitempty" yaml:"type,omitempty"`
Description string `json:"description,omitempty" yaml:"description,omitempty"`
Name string `json:"name,omitempty" yaml:"name,omitempty"`
In string `json:"in,omitempty" yaml:"in,omitempty"`
Scheme string `json:"scheme,omitempty" yaml:"scheme,omitempty"`
BearerFormat string `json:"bearerFormat,omitempty" yaml:"bearerFormat,omitempty"`
Flows *OAuthFlows `json:"flows,omitempty" yaml:"flows,omitempty"`
OpenIdConnectUrl string `json:"openIdConnectUrl,omitempty" yaml:"openIdConnectUrl,omitempty"`
}

func NewSecurityScheme() *SecurityScheme {
Expand All @@ -49,6 +50,13 @@ func NewCSRFSecurityScheme() *SecurityScheme {
}
}

func NewOIDCSecurityScheme(oidcUrl string) *SecurityScheme {
return &SecurityScheme{
Type: "openIdConnect",
OpenIdConnectUrl: oidcUrl,
}
}

func NewJWTSecurityScheme() *SecurityScheme {
return &SecurityScheme{
Type: "http",
Expand Down Expand Up @@ -114,7 +122,9 @@ func (ss *SecurityScheme) Validate(c context.Context) error {
case "oauth2":
hasFlow = true
case "openIdConnect":
return fmt.Errorf("Support for security schemes with type '%v' has not been implemented", ss.Type)
if ss.OpenIdConnectUrl != "" {
fenollp marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Errorf("No OIDC URL found for openIdConnect security scheme %q", ss.Name)
}
default:
return fmt.Errorf("Security scheme 'type' can't be '%v'", ss.Type)
}
Expand Down
20 changes: 20 additions & 0 deletions openapi3/security_scheme_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,24 @@ var securitySchemeExamples = []securitySchemeExample{
`),
valid: true,
},
{
title: "OIDC Type With URL",
raw: []byte(`
{
"type": "openIdConnect",
"openIdConnectUrl": "https://example.com/.well-known/openid-configuration"
}
`),
valid: true,
},
{
title: "OIDC Type Without URL",
raw: []byte(`
{
"type": "openIdConnect",
"openIdConnectUrl": ""
}
`),
valid: false,
},
}