diff --git a/claims/perms.go b/claims/perms.go index a2d65d8..7409e83 100644 --- a/claims/perms.go +++ b/claims/perms.go @@ -1,10 +1,12 @@ package claims import ( + "bufio" "encoding/json" "github.com/becheran/wildmatch-go" "gopkg.in/yaml.v3" "sort" + "strings" ) type PermStorage struct { @@ -15,6 +17,16 @@ func NewPermStorage() *PermStorage { return new(PermStorage).setup() } +func ParsePermStorage(perms string) *PermStorage { + ps := NewPermStorage() + sc := bufio.NewScanner(strings.NewReader(perms)) + sc.Split(bufio.ScanWords) + for sc.Scan() { + ps.Set(sc.Text()) + } + return ps +} + func (p *PermStorage) setup() *PermStorage { if p.values == nil { p.values = make(map[string]struct{}) diff --git a/claims/perms_test.go b/claims/perms_test.go index a046177..015c5b1 100644 --- a/claims/perms_test.go +++ b/claims/perms_test.go @@ -6,6 +6,14 @@ import ( "testing" ) +func TestParsePermStorage(t *testing.T) { + t.Parallel() + ps := ParsePermStorage("mjwt:test mjwt:test2") + if _, ok := ps.values["mjwt:test"]; !ok { + assert.Fail(t, "perm not set") + } +} + func TestPermStorage_Set(t *testing.T) { t.Parallel() ps := NewPermStorage()