-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
39 lines (33 loc) · 986 Bytes
/
user.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
package main
import (
"crypto/sha1"
"encoding/base64"
"github.com/gorilla/sessions"
"github.com/justinas/nosurf"
"net/http"
)
// User for JSON schema
type User struct {
Username string `json:"Username"`
Email string `json:"Email"`
Password string `json:"Password"`
}
// Session for JSON schema
type Session struct {
UserID string `json:"UserId"`
Expires int64 `json:"Expires"`
}
// GetUserData fetches user session data
func GetUserData(cs *sessions.CookieStore, req *http.Request) (string, string) {
session, _ := cs.Get(req, "magnet_session")
return session.Values["username"].(string), session.Values["user_id"].(string)
}
func cryptPassword(password, salt string) string {
hash := sha1.New()
hash.Write([]byte(password + salt))
return string(base64.URLEncoding.EncodeToString(hash.Sum(nil)))
}
// RequestNewToken writes out nosurf token
func RequestNewToken(r *http.Request, w http.ResponseWriter) {
WriteJSONResponse(200, false, nosurf.Token(r), r, w)
}