generated from fallion/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add required-scopes option for stricter scope validations
- Loading branch information
1 parent
7e7e7ef
commit 001d0e8
Showing
9 changed files
with
91 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package text | ||
|
||
import ( | ||
"errors" | ||
) | ||
|
||
var errMissingRequiredScope = errors.New("required scope missing") | ||
|
||
// RequiredScopeChecker creates case sensitive strict checker, validating that | ||
// provided scope matches one from required scopes list | ||
func RequiredScopeChecker(requiredScopes []string) func(string) error { | ||
return func(scope string) error { | ||
if len(requiredScopes) < 1 { | ||
return nil | ||
} | ||
|
||
for _, rs := range requiredScopes { | ||
if scope == rs { | ||
return nil | ||
} | ||
} | ||
|
||
return errMissingRequiredScope | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package text | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestRequiredScopesChecker(t *testing.T) { | ||
tests := map[error][]struct { | ||
scope string | ||
requiredScopes []string | ||
}{ | ||
nil: { | ||
{"ci", []string{}}, | ||
{"ui", nil}, | ||
{"ci", []string{"ci", "project-1", "repo", "ui"}}, | ||
{"ci", []string{"ui", "project-1", "repo", "ci"}}, | ||
{"repo", []string{"repo"}}, | ||
}, | ||
errMissingRequiredScope: { | ||
{"SECURITY", []string{"security", "ci", "ui"}}, | ||
{"SeCuRiTy", []string{"security", "ci", "ui"}}, | ||
{"sth", []string{"security", "ci", "ui"}}, | ||
{"se curity", []string{"security", "ci", "ui"}}, | ||
{"", []string{"repo"}}, | ||
}, | ||
} | ||
|
||
for expected, testCases := range tests { | ||
for _, testCase := range testCases { | ||
err := RequiredScopeChecker(testCase.requiredScopes)(testCase.scope) | ||
assert.Equal(t, expected, err) | ||
} | ||
} | ||
} |