-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.go
163 lines (135 loc) · 4 KB
/
session.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"context"
"encoding/gob"
"net/http"
"strings"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"github.com/kurrik/oauth1a"
log "github.com/sirupsen/logrus"
)
type Session struct {
UserName string
TwitterName string
TwitterID string
Oauth *oauth1a.UserConfig
}
func init() {
gob.Register(Session{})
}
const sessionKey = "s"
func GetSession(c *gin.Context) *Session {
defaultSession := sessions.Default(c)
s := defaultSession.Get(sessionKey)
if s == nil {
return new(Session)
}
ss := s.(Session)
return &ss
}
func SaveSession(c *gin.Context, s *Session) {
defaultSession := sessions.Default(c)
defaultSession.Set(sessionKey, &s)
defaultSession.Save()
}
func ClearSession(c *gin.Context) {
session := sessions.Default(c)
session.Clear()
session.Save()
}
func SuccessFlash(c *gin.Context, value string) {
defaultSession := sessions.Default(c)
defaultSession.AddFlash(value, "success")
defaultSession.Save()
}
func WarningFlash(c *gin.Context, value string) {
defaultSession := sessions.Default(c)
defaultSession.AddFlash(value, "warning")
defaultSession.Save()
}
func ErrorFlash(c *gin.Context, value string) {
defaultSession := sessions.Default(c)
defaultSession.AddFlash(value, "error")
defaultSession.Save()
}
func GetFlashes(c *gin.Context) map[string][]string {
defaultSession := sessions.Default(c)
f := map[string][]string{
"success": flashes(defaultSession.Flashes("success")),
"error": flashes(defaultSession.Flashes("error")),
"warning": flashes(defaultSession.Flashes("warning")),
"info": flashes(defaultSession.Flashes("info")),
}
defaultSession.Save() // saves the fact that we got flashes
return f
}
func flashes(f []interface{}) []string {
converted := make([]string, len(f))
for i := 0; i < len(f); i++ {
converted[i] = f[i].(string)
}
return converted
}
func (s *Session) SignedIn() bool {
return s != nil && (s.TwitterName != "" || s.UserName != "")
}
func SignInHandler(c *gin.Context) {
session := GetSession(c)
session.Oauth = &oauth1a.UserConfig{}
err := session.Oauth.GetRequestToken(context.Background(), service, http.DefaultClient)
if err != nil {
log.Debugf("Could not get request token: %v", err)
c.String(http.StatusInternalServerError, "Problem getting the request token")
c.Abort()
return
}
url, err := session.Oauth.GetAuthorizeURL(service)
if err != nil {
log.Debugf("Could not get authorization URL: %v", err)
c.String(http.StatusInternalServerError, "Problem getting the authorization URL")
c.Abort()
return
}
SaveSession(c, session)
log.Debugf("Redirecting user to %v\n", url)
c.Redirect(http.StatusFound, url)
}
func SignOutHandler(c *gin.Context) {
ClearSession(c)
c.Redirect(http.StatusFound, "/")
}
func CallbackHandler(c *gin.Context) {
log.Debugf("Callback hit") //. %v current sessions.\n", len(sessions))
session := GetSession(c)
if session.Oauth == nil || session.Oauth.RequestTokenKey == "" {
log.Tracef("No user config in session")
c.String(http.StatusBadRequest, "error: no session found")
c.Abort()
return
}
token, verifier, err := session.Oauth.ParseAuthorize(c.Request, service)
if err != nil {
log.Tracef("Could not parse authorization: %v", err)
c.String(http.StatusInternalServerError, "error: could not parse authorization")
c.Abort()
return
}
err = session.Oauth.GetAccessToken(context.Background(), token, verifier, service, http.DefaultClient)
if err != nil {
log.Tracef("Error getting access token: %v", err)
c.String(http.StatusInternalServerError, "error: could not get access token")
c.Abort()
return
}
session.TwitterName = session.Oauth.AccessValues.Get("screen_name")
session.TwitterID = session.Oauth.AccessValues.Get("user_id")
session.UserName = strings.ToLower(session.TwitterName)
session.Oauth = nil
if localDevMode {
// session.TwitterName = "GRINTESTING" // login as this user, for dev
session.UserName = strings.ToLower(session.TwitterName)
}
SaveSession(c, session)
c.Redirect(http.StatusFound, "/signin-redirect")
}