-
Notifications
You must be signed in to change notification settings - Fork 0
/
routers.go
98 lines (84 loc) · 2.42 KB
/
routers.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
package main
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
)
// 设置请求的合法域名
func pathVerify(c *gin.Context) {
// fmt.Println(c.Request.RequestURI)
}
// 加载CAS登录页面
func casLoginPage(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", nil)
}
// 检测登录状态
func casCheckLoadingState(c *gin.Context) {
cookie, err := c.Cookie(CasLoinTicketName)
fmt.Println(cookie)
// service := c.DefaultQuery("service", "")
loginState := CheckLoadingState(cookie)
if err == nil && loginState == nil {
// 这里的ticket应该重新生成
// c.Redirect(http.StatusMovedPermanently, service+"?ticket="+cookie)
c.JSON(http.StatusOK, gin.H{
"message": "登录成功了",
"ticket": cookie,
})
} else {
c.JSON(http.StatusBadRequest, gin.H{
"message": "请重新登陆",
})
}
}
// cas 登入
func casLogin(c *gin.Context) {
params := PraseLoginInfoFromRequest(c)
isAllowedDomain := checkAllowedDomains(params.Service)
isAllowedUser := params.Check()
if isAllowedDomain && isAllowedUser {
// 这里应该有个生成 ticket 的操作 目前以 uuid 替代
uidTicket := getUUid()
InsertMapData(params.Service, uidTicket, params.Username)
// 登陆成功时向登陆页面写入 cookie
c.SetCookie(CasLoinTicketName, uidTicket, 100000, "/cas", ".ywhabc.com", false, false)
c.Redirect(http.StatusMovedPermanently, params.Service+"?ticket="+uidTicket)
} else if isAllowedUser && !isAllowedDomain {
c.JSON(http.StatusBadGateway, gin.H{
"message": "域名不合法",
})
} else {
c.String(http.StatusBadRequest, "登录出错")
}
}
// cas 登出
func casLogout(c *gin.Context) {
cookie, err := c.Cookie(CasLoinTicketName)
message := "成功登出"
state := RemoveLoginHistory(cookie)
if state != nil || err != nil {
message = "登出失败"
}
c.JSON(http.StatusOK, gin.H{
"message": message,
})
}
// 非法页面跳转到登录页面
func redirect(c *gin.Context) {
fmt.Println("非法页面")
c.Redirect(http.StatusMovedPermanently, "/cas/loginPage")
}
// 登出接口
func router() {
r := gin.Default()
r.LoadHTMLFiles("./html/index.html")
g := r.Group("/cas")
g.Use(pathVerify)
g.GET("/loginPage", casLoginPage)
g.GET("/checkLoginState", casCheckLoadingState)
g.GET("/login", casLogin)
g.GET("/logout", casLogout)
r.GET("/", redirect)
g.GET("/", redirect)
r.Run(":889")
}