-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support wildcard scopes in M2M auth (#184)
- Loading branch information
1 parent
3a2826f
commit 2139d95
Showing
7 changed files
with
171 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
package config | ||
|
||
const APIVersion = "14.2.0" | ||
const APIVersion = "15.1.0" |
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,49 @@ | ||
package shared | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/stytchauth/stytch-go/v15/stytch/consumer/m2m" | ||
"github.com/stytchauth/stytch-go/v15/stytch/stytcherror" | ||
) | ||
|
||
func PerformM2MAuthorizationCheck(params m2m.ScopeAuthorizationFuncParams) error { | ||
clientScopes := map[string][]string{} | ||
for _, scope := range params.HasScopes { | ||
action := scope | ||
resource := "-" | ||
if strings.Contains(scope, ":") { | ||
parts := strings.SplitN(scope, ":", 2) | ||
action = parts[0] | ||
resource = parts[1] | ||
} | ||
clientScopes[action] = append(clientScopes[action], resource) | ||
} | ||
|
||
for _, requiredScope := range params.RequiredScopes { | ||
requiredAction := requiredScope | ||
requiredResource := "-" | ||
if strings.Contains(requiredScope, ":") { | ||
parts := strings.SplitN(requiredScope, ":", 2) | ||
requiredAction = parts[0] | ||
requiredResource = parts[1] | ||
} | ||
resources, ok := clientScopes[requiredAction] | ||
if !ok { | ||
return stytcherror.NewM2MPermissionError() | ||
} | ||
|
||
found := false | ||
for _, resource := range resources { | ||
if resource == "*" || resource == requiredResource { | ||
found = true | ||
break | ||
} | ||
} | ||
|
||
if !found { | ||
return stytcherror.NewM2MPermissionError() | ||
} | ||
} | ||
return nil | ||
} |
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,78 @@ | ||
package shared_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stytchauth/stytch-go/v15/stytch/consumer/m2m" | ||
"github.com/stytchauth/stytch-go/v15/stytch/shared" | ||
) | ||
|
||
func TestPerformM2MAuthorizationCheck(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
has []string | ||
needs []string | ||
expectError bool | ||
}{ | ||
{ | ||
name: "basic", | ||
has: []string{"read:users", "write:users"}, | ||
needs: []string{"read:users"}, | ||
expectError: false, | ||
}, | ||
{ | ||
name: "multiple required scopes", | ||
has: []string{"read:users", "write:users", "read:books"}, | ||
needs: []string{"read:users", "read:books"}, | ||
expectError: false, | ||
}, | ||
{ | ||
name: "simple scopes", | ||
has: []string{"read_users", "write_users"}, | ||
needs: []string{"read_users"}, | ||
expectError: false, | ||
}, | ||
{ | ||
name: "wildcard resource", | ||
has: []string{"read:*", "write:*"}, | ||
needs: []string{"read:users"}, | ||
expectError: false, | ||
}, | ||
{ | ||
name: "missing required scope", | ||
has: []string{"read:users"}, | ||
needs: []string{"write:users"}, | ||
expectError: true, | ||
}, | ||
{ | ||
name: "missing required scope with wildcard", | ||
has: []string{"read:users", "write:*"}, | ||
needs: []string{"delete:books"}, | ||
expectError: true, | ||
}, | ||
{ | ||
name: "has simple scope and wants specific scope", | ||
has: []string{"read"}, | ||
needs: []string{"read:users"}, | ||
expectError: true, | ||
}, | ||
{ | ||
name: "has specific scope and wants simple scope", | ||
has: []string{"read:users"}, | ||
needs: []string{"read"}, | ||
expectError: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := shared.PerformM2MAuthorizationCheck(m2m.ScopeAuthorizationFuncParams{ | ||
HasScopes: tt.has, | ||
RequiredScopes: tt.needs, | ||
}) | ||
if (err != nil) != tt.expectError { | ||
t.Errorf("PerformM2MAuthorizationCheck(%v, %v) error = %v, expectError %v", tt.has, tt.needs, err, tt.expectError) | ||
} | ||
}) | ||
} | ||
} |
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