v2 swagger scopes added to what-changed.

Added hashing functions to all v2 and v3 models that handle security, thats the next target.
This commit is contained in:
Dave Shanley
2022-10-23 14:23:30 -04:00
parent fcd4a0f57d
commit 3bc0a3a577
12 changed files with 367 additions and 6 deletions

View File

@@ -4,9 +4,12 @@
package v3
import (
"crypto/sha256"
"fmt"
"github.com/pb33f/libopenapi/datamodel/low"
"github.com/pb33f/libopenapi/index"
"gopkg.in/yaml.v3"
"strings"
)
// OAuthFlows represents a low-level OpenAPI 3+ OAuthFlows object.
@@ -52,7 +55,27 @@ func (o *OAuthFlows) Build(root *yaml.Node, idx *index.SpecIndex) error {
}
o.AuthorizationCode = v
return nil
}
// Hash will return a consistent SHA256 Hash of the OAuthFlow object
func (o *OAuthFlows) Hash() [32]byte {
var f []string
if !o.Implicit.IsEmpty() {
f = append(f, low.GenerateHashString(o.Implicit.Value))
}
if !o.Password.IsEmpty() {
f = append(f, low.GenerateHashString(o.Password.Value))
}
if !o.ClientCredentials.IsEmpty() {
f = append(f, low.GenerateHashString(o.ClientCredentials.Value))
}
if !o.AuthorizationCode.IsEmpty() {
f = append(f, low.GenerateHashString(o.AuthorizationCode.Value))
}
for k := range o.Extensions {
f = append(f, fmt.Sprintf("%s-%v", k.Value, o.Extensions[k].Value))
}
return sha256.Sum256([]byte(strings.Join(f, "|")))
}
// OAuthFlow represents a low-level OpenAPI 3+ OAuthFlow object.
@@ -80,3 +103,24 @@ func (o *OAuthFlow) Build(root *yaml.Node, idx *index.SpecIndex) error {
o.Extensions = low.ExtractExtensions(root)
return nil
}
// Hash will return a consistent SHA256 Hash of the OAuthFlow object
func (o *OAuthFlow) Hash() [32]byte {
var f []string
if !o.AuthorizationUrl.IsEmpty() {
f = append(f, o.AuthorizationUrl.Value)
}
if !o.TokenUrl.IsEmpty() {
f = append(f, o.TokenUrl.Value)
}
if !o.RefreshUrl.IsEmpty() {
f = append(f, o.RefreshUrl.Value)
}
for i := range o.Scopes.Value {
f = append(f, fmt.Sprintf("%s-%s", i.Value, o.Scopes.Value[i].Value))
}
for k := range o.Extensions {
f = append(f, fmt.Sprintf("%s-%v", k.Value, o.Extensions[k].Value))
}
return sha256.Sum256([]byte(strings.Join(f, "|")))
}