-
-
Notifications
You must be signed in to change notification settings - Fork 225
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
77 additions
and
2 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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package ladon | ||
|
||
// AuditLogger tracks denied and granted authorizations. | ||
type AuditLogger interface { | ||
OnDeniedAuthorization(request *Request, pool Policies, deciders Policies) | ||
OnGrantedAuthorization(request *Request, pool Policies, deciders Policies) | ||
} | ||
|
||
// AuditLoggerNoOp is the default AuditLogger, that tracks nothing. | ||
type AuditLoggerNoOp struct{} | ||
|
||
func (*AuditLoggerNoOp) OnDeniedAuthorization(r *Request, p Policies, d Policies) {} | ||
func (*AuditLoggerNoOp) OnGrantedAuthorization(r *Request, p Policies, d Policies) {} | ||
|
||
var DefaultAuditLogger = &AuditLoggerNoOp{} |
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,37 @@ | ||
package ladon_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/ory/ladon" | ||
. "github.com/ory/ladon/manager/memory" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
) | ||
|
||
type MockAuditLogger struct { | ||
mock.Mock | ||
} | ||
|
||
func (v *MockAuditLogger) OnDeniedAuthorization(r *Request, p Policies, d Policies) { | ||
v.Called(r, p, d) | ||
} | ||
|
||
func (v *MockAuditLogger) OnGrantedAuthorization(r *Request, p Policies, d Policies) { | ||
v.Called(r, p, d) | ||
} | ||
|
||
func TestAuditLogger(t *testing.T) { | ||
auditLogger := &MockAuditLogger{} | ||
auditLogger.On("OnDeniedAuthorization", mock.Anything, mock.Anything, mock.Anything) | ||
|
||
warden := &Ladon{ | ||
Manager: NewMemoryManager(), | ||
AuditLogger: auditLogger, | ||
} | ||
|
||
r := &Request{} | ||
assert.NotNil(t, warden.IsAllowed(r)) | ||
|
||
auditLogger.AssertCalled(t, "OnDeniedAuthorization", r, Policies{}, Policies{}) | ||
} |
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