Skip to content

Commit d793cf7

Browse files
committed
auth 为一个函数
1 parent 6b819fc commit d793cf7

File tree

3 files changed

+7
-23
lines changed

3 files changed

+7
-23
lines changed

http/mock/auth.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func NewMockAuther() router.Auther {
2020

2121
type mockAuther struct{}
2222

23-
func (m *mockAuther) Auth(r *http.Request) (authInfo interface{}, err error) {
23+
func (m *mockAuther) Auth(r *http.Request, entry router.Entry) (authInfo interface{}, err error) {
2424
authHeader := r.Header.Get("Authorization")
2525
if authHeader == "" {
2626
return nil, exception.NewUnauthorized("Authorization missed in header")
@@ -38,7 +38,3 @@ func (m *mockAuther) Auth(r *http.Request) (authInfo interface{}, err error) {
3838
}
3939
return access, nil
4040
}
41-
42-
func (m *mockAuther) Permission(authInfo interface{}, entry router.Entry) error {
43-
return nil
44-
}

http/router/auther.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import "net/http"
66
// Header 用于鉴定身份
77
// Entry 用于鉴定权限
88
type Auther interface {
9-
Auth(*http.Request) (authInfo interface{}, err error)
10-
Permission(authInfo interface{}, entry Entry) (err error)
9+
Auth(req *http.Request, entry Entry) (authInfo interface{}, err error)
1110
}
1211

1312
// The AutherFunc type is an adapter to allow the use of

http/router/httprouter/httprouter.go

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"net/http"
66
"strings"
77

8-
"github.com/infraboard/mcube/exception"
98
"github.com/infraboard/mcube/http/context"
109
"github.com/infraboard/mcube/http/response"
1110
"github.com/infraboard/mcube/http/router"
@@ -146,22 +145,12 @@ func (r *httpRouter) addHandler(method, path string, h http.Handler) {
146145
return
147146
}
148147

149-
if entry.AuthEnable {
150-
ai, err := r.auther.Auth(req)
151-
if err != nil {
152-
response.Failed(w, exception.NewUnauthorized(err.Error()))
153-
return
154-
}
155-
authInfo = ai
156-
}
157-
158-
if entry.PermissionEnable {
159-
err := r.auther.Permission(authInfo, *entry.Entry)
160-
if err != nil {
161-
response.Failed(w, exception.NewPermissionDeny(err.Error()))
162-
return
163-
}
148+
ai, err := r.auther.Auth(req, *entry.Entry)
149+
if err != nil {
150+
response.Failed(w, err)
151+
return
164152
}
153+
authInfo = ai
165154
}
166155

167156
rc := context.GetContext(req)

0 commit comments

Comments
 (0)