Skip to content

Commit

Permalink
go-aah/aah#162 added reason struct for authorization failed
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevatkm committed May 24, 2018
1 parent 4c1d753 commit 1613d94
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
31 changes: 14 additions & 17 deletions authz/authorization_info.go
Expand Up @@ -132,24 +132,21 @@ func (a *AuthorizationInfo) IsPermittedAllp(permissions ...*Permission) bool {
return true
}

// String method is stringer interface implementation.
func (a AuthorizationInfo) String() string {
var str string
if len(a.roles) > 0 {
str += "Roles[" + strings.Join(a.roles, "|") + "]"
} else {
str += "Roles[]"
}
// Roles method returns roles in the string format.
func (a *AuthorizationInfo) Roles() string {
return strings.Join(a.roles, ", ")
}

if len(a.permissions) > 0 {
var ps []string
for _, p := range a.permissions {
ps = append(ps, p.String())
}
str += " Permissions[" + strings.Join(ps, "|") + "]"
} else {
str += " Permissions[]"
// Permissions method returns permissions in the string format.
func (a *AuthorizationInfo) Permissions() string {
var ps []string
for _, p := range a.permissions {
ps = append(ps, p.String())
}
return strings.Join(ps, "|")
}

return str
// String method is stringer interface implementation.
func (a AuthorizationInfo) String() string {
return "Roles[" + a.Roles() + "] Permissions[" + a.Permissions() + "]"
}
13 changes: 13 additions & 0 deletions authz/authz.go
Expand Up @@ -6,6 +6,7 @@ package authz

import (
"errors"
"fmt"

"aahframework.org/config.v0"
"aahframework.org/security.v0/authc"
Expand All @@ -27,6 +28,18 @@ type Authorizer interface {
GetAuthorizationInfo(authcInfo *authc.AuthenticationInfo) *AuthorizationInfo
}

// Reason struct used to represent authorization failed details.
type Reason struct {
Func string
Expected string
Got string
}

// String method is Stringer interface
func (r Reason) String() string {
return fmt.Sprintf("reason(func=%s expected=%s got=%s)", r.Func, r.Expected, r.Got)
}

//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Unexported methods
//___________________________________
Expand Down
2 changes: 1 addition & 1 deletion subject_test.go
Expand Up @@ -61,7 +61,7 @@ func TestSecuritySubject(t *testing.T) {

str := sub.String()
assert.True(t, strings.Contains(str, "user@sample.com"))
assert.True(t, strings.Contains(str, "Roles[role1|role2|role3|role4]"))
assert.True(t, strings.Contains(str, "Roles[role1, role2, role3, role4]"))
assert.True(t, strings.Contains(str, "Permissions[newsletter:read,write]"))

// Session
Expand Down

0 comments on commit 1613d94

Please sign in to comment.