-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauth-routes.go
144 lines (124 loc) · 3.62 KB
/
auth-routes.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
package main
import (
"encoding/json"
"net/http"
"github.com/gorilla/sessions"
"golang.org/x/crypto/bcrypt"
)
/* Authentication Methods
-------------------------------------------*/
type userRequest struct {
Name string `json:"name"`
Bio string `json:"bio"`
Password string `json:"password"`
Role string `json:"role"`
Hosts []string `json:"hosts"`
}
type loginRequest struct {
Name string `json:"name"`
Password string `json:"password"`
Remember bool `json:"remember"`
}
type userResponse struct {
ID string `json:"id"`
Name string `json:"name"`
Bio string `json:"bio"`
}
type authenticatedUserResponse struct {
ID string `json:"id"`
Name string `json:"name"`
Bio string `json:"bio"`
Role string `json:"role"`
Hosts []string `json:"hosts"`
}
func login(host string) handlerFuncType {
return func(w http.ResponseWriter, r *http.Request) {
// Short term session
session, err := store.Get(r, DefaultSession)
if err != nil {
throwError(w, err.Error(), http.StatusInternalServerError)
return
}
userIDI := session.Values["userID"]
userNameI := session.Values["userName"]
userBioI := session.Values["userBio"]
userRoleI := session.Values["userRole"]
userHostsI := session.Values["userHosts"]
if userIDI != nil {
userID, ok := userIDI.(string)
userName := userNameI.(string)
userBio := userBioI.(string)
userRole := userRoleI.(string)
userHosts := userHostsI.([]string)
if ok && len(userID) > 0 {
json.NewEncoder(w).Encode(authenticatedUserResponse{userID, userName, userBio, userRole, userHosts})
return
}
}
// Get request body
var requestData loginRequest
decoder := json.NewDecoder(r.Body)
if decoder.Decode(&requestData) != nil {
throwError(w, "Sent JSON body does is not of correct type", http.StatusBadRequest)
return
}
defer r.Body.Close()
user := UserDatabase.getUserByName(requestData.Name)
if user != nil {
if !user.inHost(host) {
throwError(w, "Invalid login data", http.StatusForbidden)
return
}
err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(requestData.Password))
if err == nil {
session.Values["version"] = DefaultSessionVersion
session.Values["userName"] = user.Name
session.Values["userID"] = user.ID
session.Values["userBio"] = user.Bio
session.Values["userRole"] = user.Role
session.Values["userHosts"] = user.Hosts
session.Values["userPassword"] = user.Password
session.Options = &sessions.Options{
Path: "/",
Secure: false,
HttpOnly: false,
MaxAge: 60 * 60,
}
if Config.env == "production" {
// session.Options.Domain = host
session.Options.Secure = true
}
if requestData.Remember {
oneYear := 60 * 60 * 24 * 365
session.Options.MaxAge = oneYear
}
err = session.Save(r, w)
if err != nil {
throwError(w, err.Error(), http.StatusInternalServerError)
return
}
json.NewEncoder(w).Encode(authenticatedUserResponse{user.ID, user.Name, user.Bio, user.Role, user.Hosts})
return
}
}
throwError(w, "Invalid login data", http.StatusForbidden)
}
}
func logout(w http.ResponseWriter, r *http.Request) {
session, err := store.Get(r, DefaultSession)
if err != nil {
throwError(w, err.Error(), http.StatusInternalServerError)
return
}
session.Values["userID"] = nil
session.Values["userName"] = nil
session.Values["userRole"] = nil
session.Values["userHosts"] = nil
session.Options.MaxAge = -1
err = session.Save(r, w)
if err != nil {
throwError(w, err.Error(), http.StatusInternalServerError)
return
}
json.NewEncoder(w).Encode(struct{}{})
}