fix: gofmt: with -s argument
This commit is contained in:
parent
f4f49e6ae6
commit
140fbb8b23
124
api/auth.go
124
api/auth.go
|
@ -1,62 +1,62 @@
|
||||||
package api
|
package api
|
||||||
|
|
||||||
// Auth can be used by http middleware to
|
// Auth can be used by http middleware to
|
||||||
// 1) consult required roles in @Auth.Required
|
// 1) consult required roles in @Auth.Required
|
||||||
// 2) update active roles in @Auth.Active
|
// 2) update active roles in @Auth.Active
|
||||||
type Auth struct {
|
type Auth struct {
|
||||||
// required roles for this request
|
// required roles for this request
|
||||||
// - the first dimension of the array reads as a OR
|
// - the first dimension of the array reads as a OR
|
||||||
// - the second dimension reads as a AND
|
// - the second dimension reads as a AND
|
||||||
//
|
//
|
||||||
// Example:
|
// Example:
|
||||||
// [ [A, B], [C, D] ] reads: roles (A and B) or (C and D) are required
|
// [ [A, B], [C, D] ] reads: roles (A and B) or (C and D) are required
|
||||||
//
|
//
|
||||||
// Warning: must not be mutated
|
// Warning: must not be mutated
|
||||||
Required [][]string
|
Required [][]string
|
||||||
|
|
||||||
// active roles to be updated by authentication
|
// active roles to be updated by authentication
|
||||||
// procedures (e.g. jwt)
|
// procedures (e.g. jwt)
|
||||||
Active []string
|
Active []string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Granted returns whether the authorization is granted
|
// Granted returns whether the authorization is granted
|
||||||
// i.e. Auth.Active fulfills Auth.Required
|
// i.e. Auth.Active fulfills Auth.Required
|
||||||
func (a *Auth) Granted() bool {
|
func (a *Auth) Granted() bool {
|
||||||
var nothingRequired = true
|
var nothingRequired = true
|
||||||
|
|
||||||
// first dimension: OR ; at least one is valid
|
// first dimension: OR ; at least one is valid
|
||||||
for _, required := range a.Required {
|
for _, required := range a.Required {
|
||||||
// empty list
|
// empty list
|
||||||
if len(required) < 1 {
|
if len(required) < 1 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
nothingRequired = false
|
nothingRequired = false
|
||||||
|
|
||||||
// second dimension: AND ; all required must be fulfilled
|
// second dimension: AND ; all required must be fulfilled
|
||||||
if a.fulfills(required) {
|
if a.fulfills(required) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nothingRequired
|
return nothingRequired
|
||||||
}
|
}
|
||||||
|
|
||||||
// returns whether Auth.Active fulfills (contains) all @required roles
|
// returns whether Auth.Active fulfills (contains) all @required roles
|
||||||
func (a *Auth) fulfills(required []string) bool {
|
func (a *Auth) fulfills(required []string) bool {
|
||||||
for _, requiredRole := range required {
|
for _, requiredRole := range required {
|
||||||
var found = false
|
var found = false
|
||||||
for _, activeRole := range a.Active {
|
for _, activeRole := range a.Active {
|
||||||
if activeRole == requiredRole {
|
if activeRole == requiredRole {
|
||||||
found = true
|
found = true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// missing role -> fail
|
// missing role -> fail
|
||||||
if !found {
|
if !found {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// all @required are fulfilled
|
// all @required are fulfilled
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
228
api/auth_test.go
228
api/auth_test.go
|
@ -1,114 +1,114 @@
|
||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestCombination(t *testing.T) {
|
func TestCombination(t *testing.T) {
|
||||||
tcases := []struct {
|
tcases := []struct {
|
||||||
Name string
|
Name string
|
||||||
Required [][]string
|
Required [][]string
|
||||||
Active []string
|
Active []string
|
||||||
Granted bool
|
Granted bool
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
Name: "no requirement none given",
|
Name: "no requirement none given",
|
||||||
Required: [][]string{},
|
Required: [][]string{},
|
||||||
Active: []string{},
|
Active: []string{},
|
||||||
Granted: true,
|
Granted: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "empty requirements none given",
|
Name: "empty requirements none given",
|
||||||
Required: [][]string{{}},
|
Required: [][]string{{}},
|
||||||
Active: []string{},
|
Active: []string{},
|
||||||
Granted: true,
|
Granted: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "no requirement 1 given",
|
Name: "no requirement 1 given",
|
||||||
Required: [][]string{},
|
Required: [][]string{},
|
||||||
Active: []string{"a"},
|
Active: []string{"a"},
|
||||||
Granted: true,
|
Granted: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "no requirement some given",
|
Name: "no requirement some given",
|
||||||
Required: [][]string{},
|
Required: [][]string{},
|
||||||
Active: []string{"a", "b"},
|
Active: []string{"a", "b"},
|
||||||
Granted: true,
|
Granted: true,
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
Name: "1 required none given",
|
Name: "1 required none given",
|
||||||
Required: [][]string{{"a"}},
|
Required: [][]string{{"a"}},
|
||||||
Active: []string{},
|
Active: []string{},
|
||||||
Granted: false,
|
Granted: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "1 required fulfilled",
|
Name: "1 required fulfilled",
|
||||||
Required: [][]string{{"a"}},
|
Required: [][]string{{"a"}},
|
||||||
Active: []string{"a"},
|
Active: []string{"a"},
|
||||||
Granted: true,
|
Granted: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "1 required mismatch",
|
Name: "1 required mismatch",
|
||||||
Required: [][]string{{"a"}},
|
Required: [][]string{{"a"}},
|
||||||
Active: []string{"b"},
|
Active: []string{"b"},
|
||||||
Granted: false,
|
Granted: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "2 required none gien",
|
Name: "2 required none gien",
|
||||||
Required: [][]string{{"a", "b"}},
|
Required: [][]string{{"a", "b"}},
|
||||||
Active: []string{},
|
Active: []string{},
|
||||||
Granted: false,
|
Granted: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "2 required other given",
|
Name: "2 required other given",
|
||||||
Required: [][]string{{"a", "b"}},
|
Required: [][]string{{"a", "b"}},
|
||||||
Active: []string{"c"},
|
Active: []string{"c"},
|
||||||
Granted: false,
|
Granted: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "2 required one given",
|
Name: "2 required one given",
|
||||||
Required: [][]string{{"a", "b"}},
|
Required: [][]string{{"a", "b"}},
|
||||||
Active: []string{"a"},
|
Active: []string{"a"},
|
||||||
Granted: false,
|
Granted: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "2 required fulfilled",
|
Name: "2 required fulfilled",
|
||||||
Required: [][]string{{"a", "b"}},
|
Required: [][]string{{"a", "b"}},
|
||||||
Active: []string{"a", "b"},
|
Active: []string{"a", "b"},
|
||||||
Granted: true,
|
Granted: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "2 or 2 required first fulfilled",
|
Name: "2 or 2 required first fulfilled",
|
||||||
Required: [][]string{{"a", "b"}, {"c", "d"}},
|
Required: [][]string{{"a", "b"}, {"c", "d"}},
|
||||||
Active: []string{"a", "b"},
|
Active: []string{"a", "b"},
|
||||||
Granted: true,
|
Granted: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "2 or 2 required second fulfilled",
|
Name: "2 or 2 required second fulfilled",
|
||||||
Required: [][]string{{"a", "b"}, {"c", "d"}},
|
Required: [][]string{{"a", "b"}, {"c", "d"}},
|
||||||
Active: []string{"c", "d"},
|
Active: []string{"c", "d"},
|
||||||
Granted: true,
|
Granted: true,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tcase := range tcases {
|
for _, tcase := range tcases {
|
||||||
t.Run(tcase.Name, func(t *testing.T) {
|
t.Run(tcase.Name, func(t *testing.T) {
|
||||||
|
|
||||||
auth := Auth{
|
auth := Auth{
|
||||||
Required: tcase.Required,
|
Required: tcase.Required,
|
||||||
Active: tcase.Active,
|
Active: tcase.Active,
|
||||||
}
|
}
|
||||||
|
|
||||||
// all right
|
// all right
|
||||||
if tcase.Granted == auth.Granted() {
|
if tcase.Granted == auth.Granted() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if tcase.Granted && !auth.Granted() {
|
if tcase.Granted && !auth.Granted() {
|
||||||
t.Fatalf("expected granted authorization")
|
t.Fatalf("expected granted authorization")
|
||||||
}
|
}
|
||||||
t.Fatalf("unexpected granted authorization")
|
t.Fatalf("unexpected granted authorization")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue