-
Notifications
You must be signed in to change notification settings - Fork 4
/
auth.go
53 lines (48 loc) · 1.47 KB
/
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
48
49
50
51
52
53
package endpoints
import (
"fmt"
"net/http"
"github.com/burxtx/gin-microservice-boilerplate/app/config"
"github.com/gin-gonic/gin"
"github.com/gorilla/sessions"
)
type AuthEndpoint struct {
}
func (a *AuthEndpoint) Logout(c *gin.Context) {
cfg := config.GetConfig()
secret := cfg.GetString("http.secret")
session_domain := cfg.GetString("http.session_domain")
store := sessions.NewCookieStore([]byte(secret))
callback := fmt.Sprintf("%s://%s%s", "http", c.Request.Host, "/")
cas := fmt.Sprintf("%s/logout?service=%s", cfg.GetString("auth.cas"), callback)
session, err := store.Get(c.Request, session_domain)
if err != nil {
http.Error(c.Writer, err.Error(), http.StatusInternalServerError)
}
var msg []byte
if session != nil {
session.Options.MaxAge = -1
saveErr := session.Save(c.Request, c.Writer)
if saveErr != nil {
http.Error(c.Writer, saveErr.Error(), http.StatusInternalServerError)
}
http.Redirect(c.Writer, c.Request, cas, 302)
} else {
msg = []byte("already logout!")
}
c.JSON(200, gin.H{"msg": msg})
return
}
func (a *AuthEndpoint) GetCurrentUser(c *gin.Context) {
cfg := config.GetConfig()
secret := cfg.GetString("http.secret")
session_domain := cfg.GetString("http.session_domain")
store := sessions.NewCookieStore([]byte(secret))
session, err := store.Get(c.Request, session_domain)
if err != nil {
http.Error(c.Writer, err.Error(), http.StatusInternalServerError)
return
}
user := session.Values["user"]
c.JSON(200, gin.H{"user": user})
}