github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/pkg/scope/directive.go (about) 1 package scope 2 3 import ( 4 "context" 5 6 "github.com/kyma-incubator/compass/components/director/pkg/str" 7 8 "github.com/99designs/gqlgen/graphql" 9 "github.com/pkg/errors" 10 ) 11 12 // ScopesGetter missing godoc 13 //go:generate mockery --name=ScopesGetter --output=automock --outpkg=automock --case=underscore --disable-version-string 14 type ScopesGetter interface { 15 GetRequiredScopes(scopesDefinition string) ([]string, error) 16 } 17 18 // ScopesMismatchErrorProvider missing godoc 19 type ScopesMismatchErrorProvider interface { 20 Error([]string, []string) error 21 } 22 23 type directive struct { 24 scopesGetter ScopesGetter 25 errorProvider ScopesMismatchErrorProvider 26 } 27 28 // NewDirective missing godoc 29 func NewDirective(getter ScopesGetter, errorProvider ScopesMismatchErrorProvider) *directive { 30 return &directive{ 31 scopesGetter: getter, 32 errorProvider: errorProvider, 33 } 34 } 35 36 // VerifyScopes missing godoc 37 func (d *directive) VerifyScopes(ctx context.Context, _ interface{}, next graphql.Resolver, scopesDefinition string) (interface{}, error) { 38 actualScopes, err := LoadFromContext(ctx) 39 if err != nil { 40 return nil, err 41 } 42 requiredScopes, err := d.scopesGetter.GetRequiredScopes(scopesDefinition) 43 if err != nil { 44 return nil, errors.Wrap(err, "while getting required scopes") 45 } 46 47 if !str.Matches(actualScopes, requiredScopes) { 48 return nil, d.errorProvider.Error(requiredScopes, actualScopes) 49 } 50 return next(ctx) 51 }