-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
api: Configurable permissions (#1314)
* API Permissions * dont make perms pointer * add pre- and post-hooks * add the first real migration * fix migrator picking up non-instance kvs * add func for activation * fix api tests * load user for token auth * add storage client tests * add separate call for migrations * fix api * make session duration configurable
- Loading branch information
Showing
24 changed files
with
616 additions
and
67 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,32 @@ | ||
package auth | ||
|
||
import ( | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
const wildcard = "*" | ||
|
||
func (ap *AuthProvider) checkPermission(req *http.Request, u User) bool { | ||
var longestMatch *Permission | ||
for _, perm := range u.Permissions { | ||
if strings.HasSuffix(perm.Path, wildcard) && strings.HasPrefix(req.URL.Path, strings.TrimSuffix(perm.Path, wildcard)) { | ||
if longestMatch == nil || len(perm.Path) > len(longestMatch.Path) { | ||
longestMatch = &perm | ||
} | ||
} else if perm.Path == req.URL.Path { | ||
if longestMatch == nil || len(perm.Path) > len(longestMatch.Path) { | ||
longestMatch = &perm | ||
} | ||
} | ||
} | ||
if longestMatch == nil { | ||
return false | ||
} | ||
for _, meth := range longestMatch.Methods { | ||
if strings.EqualFold(meth, req.Method) { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
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,48 @@ | ||
package auth | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func mustRequest(meth string, url string) *http.Request { | ||
req, err := http.NewRequest(meth, url, nil) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return req | ||
} | ||
|
||
func TestPermission_Fixed(t *testing.T) { | ||
ap := AuthProvider{} | ||
assert.True(t, ap.checkPermission(mustRequest("get", "/foo/bar"), User{ | ||
Permissions: []Permission{ | ||
{ | ||
Path: "/foo/bar", | ||
Methods: []string{"get", "post"}, | ||
}, | ||
{ | ||
Path: "/foo/ba", | ||
Methods: []string{"post"}, | ||
}, | ||
{ | ||
Path: "/foo", | ||
Methods: []string{"head"}, | ||
}, | ||
}, | ||
})) | ||
} | ||
|
||
func TestPermission_Wildcard(t *testing.T) { | ||
ap := AuthProvider{} | ||
assert.True(t, ap.checkPermission(mustRequest("get", "/foo/bar"), User{ | ||
Permissions: []Permission{ | ||
{ | ||
Path: "/foo/*", | ||
Methods: []string{"get"}, | ||
}, | ||
}, | ||
})) | ||
} |
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
Oops, something went wrong.