/
auth.go
47 lines (39 loc) · 848 Bytes
/
auth.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
package middleware
import (
"github.com/go-martini/martini"
"net/http"
)
// setup not need check authorize URL
//
// urls := []string{"/login", "/users", "/firewall"}
// m.Use(middleware.AuthRequest(urls...))
//
func AuthRequest(disUrls ...string) martini.Handler {
return func(ctx martini.Context, c *Context, r *http.Request) {
var (
match bool
)
if authored := c.S.Get("Authorized"); authored != nil {
if u := c.S.Get("AuthInfo"); u != nil {
c.Set("AuthData", u)
ctx.Next()
return
}
}
urlLen := len(disUrls)
if urlLen < 1 {
disUrls[0] = "/login"
urlLen = 1
}
for _, url := range disUrls {
if c.R.RequestURI == url {
match = true
break
}
}
if !match && c.R.RequestURI != "/login" && c.R.RequestURI != "/users/logined" {
c.Redirect(disUrls[0])
}
ctx.Next()
}
}