this repo has no description
13
fork

Configure Feed

Select the types of activity you want to include in your feed.

oauth scope string parsing

+57
+35
permissions/oauthscope.go
··· 1 + package permissions 2 + 3 + import ( 4 + "fmt" 5 + "strings" 6 + ) 7 + 8 + // High-level helper for parsing a space-delimited OAuth scope string in to a set of permissions. 9 + // 10 + // If the 'atproto' scope is not included, this will return an error. Otherwise invalid permission scope strings are simply ignored. 11 + func ParseOAuthScope(scope string) ([]Permission, error) { 12 + 13 + foundAtproto := false 14 + perms := []Permission{} 15 + 16 + parts := strings.Split(scope, " ") 17 + for _, p := range parts { 18 + if p == "" { 19 + continue 20 + } 21 + if p == "atproto" { 22 + foundAtproto = true 23 + continue 24 + } 25 + perm, err := ParsePermissionString(p) 26 + if err != nil { 27 + continue 28 + } 29 + perms = append(perms, *perm) 30 + } 31 + if !foundAtproto { 32 + return nil, fmt.Errorf("required 'atproto' scope not found") 33 + } 34 + return perms, nil 35 + }
+22
permissions/oauthscope_test.go
··· 1 + package permissions 2 + 3 + import ( 4 + "testing" 5 + 6 + "github.com/stretchr/testify/assert" 7 + ) 8 + 9 + func TestParseOAuthScope(t *testing.T) { 10 + assert := assert.New(t) 11 + 12 + perms, err := ParseOAuthScope("") 13 + assert.Error(err) 14 + 15 + perms, err = ParseOAuthScope("atproto repo:*") 16 + assert.NoError(err) 17 + assert.Equal(1, len(perms)) 18 + 19 + perms, err = ParseOAuthScope("atproto rpc:asdf") 20 + assert.NoError(err) 21 + assert.Equal(0, len(perms)) 22 + }