-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
141 lines (109 loc) · 3.1 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package support
import (
"encoding/json"
"time"
"github.com/gofiber/fiber/v2/log"
"github.com/refiber/framework/constant"
)
func (s *support) NewAuthenticatedUserSession(user interface{}) error {
session, err := s.session.Get(s.GetCtx())
if err != nil {
return err
}
// before reset the session, get redirection from the session, then save redirection to the new session
var redirection string
_redirection := session.Get(constant.KeyRedirection + session.ID())
if _redirection != nil {
redirection = _redirection.(string)
}
session.Reset()
session.SetExpiry((time.Hour * 24) * 7)
if redirection != "" {
session.Set(constant.KeyRedirection+session.ID(), redirection)
}
sessionKey := constant.KeyAuth + session.ID()
buf, _ := json.Marshal(user)
session.Set(sessionKey, buf)
if err := session.Save(); err != nil {
return err
}
return nil
}
func (s *support) GetAuthenticatedUserSession(user interface{}) error {
session, err := s.session.Get(s.GetCtx())
if err != nil {
return err
}
raw := session.Get(constant.KeyAuth + session.ID())
if data, ok := raw.([]byte); ok {
if err := json.Unmarshal(data, &user); err != nil {
return err
}
}
return nil
}
func (s *support) UpdateAuthenticatedUserSession(user interface{}) error {
session, err := s.session.Get(s.GetCtx())
if err != nil {
return err
}
buf, _ := json.Marshal(user)
session.Set(constant.KeyAuth+session.ID(), buf)
if err := session.Save(); err != nil {
return err
}
return nil
}
func (s *support) DestroyAuthenticatedUserSession() error {
session, err := s.session.Get(s.GetCtx())
if err != nil {
return err
}
session.Reset()
return nil
}
func getRedirectLocation(s Refiber) (location *string, err error) {
if session, err := s.GetSession().Get(s.GetCtx()); err == nil {
key := constant.KeyRedirection + session.ID()
data := session.Get(key)
if redirectLocation, ok := data.(string); ok && redirectLocation != "" {
session.Delete(key)
if err := session.Save(); err != nil {
return nil, err
}
return &redirectLocation, nil
}
}
return nil, err
}
func AuthRedirection(s Refiber, location string) error {
var l *string
l = &location
if redirectLocation, err := getRedirectLocation(s); redirectLocation != nil {
if err != nil {
log.Errorf("refiber.auth.AuthRedirection:", err)
}
l = redirectLocation
}
return s.Redirect().To(*l).Now()
}
func AuthRedirectionWithMessage(s Refiber, location string, messageType MessageType, message string) error {
var l *string
l = &location
if redirectLocation, err := getRedirectLocation(s); redirectLocation != nil {
if err != nil {
log.Errorf("refiber.auth.AuthRedirection:", err)
}
l = redirectLocation
}
return s.Redirect().To(*l).WithMessage(messageType, message).Now()
}
func AuthLoginPage(location string, s Refiber) error {
if session, err := s.GetSession().Get(s.GetCtx()); err == nil {
session.Set(constant.KeyRedirection+session.ID(), s.GetCtx().OriginalURL())
if err := session.Save(); err != nil {
log.Errorw("refiber.support.auth.AuthLoginPage: failed to save session")
}
}
return s.GetCtx().Redirect(location)
}