-
Notifications
You must be signed in to change notification settings - Fork 2
/
oauth2_handler_logger.go
126 lines (102 loc) · 3.9 KB
/
oauth2_handler_logger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package oauth
import (
"context"
"net/http"
"github.com/go-oauth2/oauth2/v4"
)
type (
// handlerLogger is a decorator for oauth.handler.
handlerLogger struct {
Handler
log oauthLogger
}
oauthLogger interface {
Debugf(format string, args ...interface{})
Infof(format string, args ...interface{})
Errorf(format string, args ...interface{})
}
)
// NewHandlerLogger returns a new handlerLogger.
func NewHandlerLogger(h Handler, log oauthLogger) Handler {
return &handlerLogger{
Handler: h,
log: log,
}
}
// ClientAuthorizedHandler check the client is authorized or not
// and logs the request and response.
func (h *handlerLogger) ClientAuthorizedHandler(clientID string, grant oauth2.GrantType) (allowed bool, err error) {
h.log.Debugf("ClientAuthorizedHandler: clientID=%s, grant=%s", clientID, grant)
allowed, err = h.Handler.ClientAuthorizedHandler(clientID, grant)
if err != nil {
h.log.Errorf("ClientAuthorizedHandler: %v", err)
return false, err
}
h.log.Debugf("ClientAuthorizedHandler: allowed=%t", allowed)
return allowed, nil
}
// ClientScopeHandler check the scope of the client
// and logs the request and response.
func (h *handlerLogger) ClientScopeHandler(tgr *oauth2.TokenGenerateRequest) (allowed bool, err error) {
h.log.Debugf("ClientScopeHandler: request=%+v", tgr)
allowed, err = h.Handler.ClientScopeHandler(tgr)
if err != nil {
h.log.Errorf("ClientScopeHandler: %v", err)
return false, err
}
h.log.Debugf("ClientScopeHandler: allowed=%t", allowed)
return allowed, nil
}
// AuthorizeScopeHandler logs the request and response.
func (h *handlerLogger) AuthorizeScopeHandler(w http.ResponseWriter, r *http.Request) (scope string, err error) {
h.log.Debugf("AuthorizeScopeHandler: request=%+v", r)
scope, err = h.Handler.AuthorizeScopeHandler(w, r)
if err != nil {
h.log.Errorf("AuthorizeScopeHandler: %v", err)
return "", err
}
h.log.Debugf("AuthorizeScopeHandler: scope=%s", scope)
return scope, nil
}
// RefreshingScopeHandler check the scope of the refreshing token
// and logs the request and response.
func (h *handlerLogger) RefreshingScopeHandler(tgr *oauth2.TokenGenerateRequest, oldScope string) (allowed bool, err error) {
h.log.Debugf("RefreshingScopeHandler: request=%+v", tgr)
allowed, err = h.Handler.RefreshingScopeHandler(tgr, oldScope)
if err != nil {
h.log.Errorf("RefreshingScopeHandler: %v", err)
return false, err
}
h.log.Debugf("RefreshingScopeHandler: allowed=%t", allowed)
return allowed, nil
}
// UserAuthorizationHandler logs the request and response.
func (h *handlerLogger) UserAuthorizationHandler(w http.ResponseWriter, r *http.Request) (userID string, err error) {
h.log.Debugf("UserAuthorizationHandler: request=%+v", r)
userID, err = h.Handler.UserAuthorizationHandler(w, r)
if err != nil {
h.log.Errorf("UserAuthorizationHandler: %v", err)
return "", err
}
h.log.Debugf("UserAuthorizationHandler: user_id=%s", userID)
return userID, nil
}
// PasswordAuthorizationHandler get user id from username and password
// and logs the request and response.
func (h *handlerLogger) PasswordAuthorizationHandler(ctx context.Context, clientID, username, password string) (userID string, err error) {
h.log.Debugf("PasswordAuthorizationHandler: clientID=%s, username=%s, password=%s", clientID, username, password)
userID, err = h.Handler.PasswordAuthorizationHandler(ctx, clientID, username, password)
if err != nil {
h.log.Errorf("PasswordAuthorizationHandler: %v", err)
return "", err
}
h.log.Debugf("PasswordAuthorizationHandler: user_id=%s", userID)
return userID, nil
}
// ExtensionFieldsHandler logs the request and response.
func (h *handlerLogger) ExtensionFieldsHandler(ti oauth2.TokenInfo) (fieldsValue map[string]interface{}) {
h.log.Debugf("ExtensionFieldsHandler: token_info=%+v", ti)
fieldsValue = h.Handler.ExtensionFieldsHandler(ti)
h.log.Debugf("ExtensionFieldsHandler: fields_value=%+v", fieldsValue)
return fieldsValue
}