-
Notifications
You must be signed in to change notification settings - Fork 26
/
authentication.go
41 lines (32 loc) · 928 Bytes
/
authentication.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
package utils
import (
"github.com/gorilla/context"
"github.com/gorilla/sessions"
"github.com/stevenleeg/gobb/config"
"github.com/stevenleeg/gobb/models"
"net/http"
)
var Store *sessions.CookieStore
func GetCookieStore(r *http.Request) *sessions.CookieStore {
if Store == nil {
cookie_key, _ := config.Config.GetString("gobb", "cookie_key")
Store = sessions.NewCookieStore([]byte(cookie_key))
}
return Store
}
func GetCurrentUser(r *http.Request) *models.User {
cached := context.Get(r, "user")
if cached != nil {
return cached.(*models.User)
}
session, _ := GetCookieStore(r).Get(r, "sirsid")
if session.Values["username"] == nil || session.Values["password"] == nil {
return nil
}
err, current_user := models.AuthenticateUser(session.Values["username"].(string), session.Values["password"].(string))
if err != nil {
return nil
}
context.Set(r, "user", current_user)
return current_user
}