-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_login_post.go
executable file
·127 lines (102 loc) · 3.01 KB
/
handler_login_post.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
package handlers
import (
"encoding/json"
"net/http"
"gopkg.in/mgo.v2"
"github.com/IlijevskiSymphony/symphonyGopher/server/configuration"
"github.com/IlijevskiSymphony/symphonyGopher/server/partners"
"github.com/Sirupsen/logrus"
"github.com/gorilla/sessions"
)
type LoginPost struct {
Email string `json:"email"`
Password string `json:"password"`
}
type LoginResponse struct {
Email string `json:"email"`
Status partners.Status `json:"status"`
}
type HandlerLoginPost struct {
Configuration configuration.Configuration
SessionFn func() *mgo.Session
Store *sessions.CookieStore
}
func (h HandlerLoginPost) ServeHTTP(w http.ResponseWriter, req *http.Request) {
decoder := json.NewDecoder(req.Body)
var posted LoginPost
if err := decoder.Decode(&posted); err != nil {
logrus.Infof("Cannot decode settings. Error: %s", err)
w.WriteHeader(http.StatusBadRequest)
return
}
if !partners.ValidateEmailFormat(posted.Email) {
logrus.Infof("Email format invalid.")
w.WriteHeader(http.StatusBadRequest)
return
}
if !partners.ValidatePasswordFormat(posted.Password) {
logrus.Infof("Password format invalid.")
w.WriteHeader(http.StatusBadRequest)
return
}
partnersRepo := partners.NewRepository(h.SessionFn())
defer partnersRepo.Close()
p, err := partnersRepo.FindByEmail(posted.Email)
if err != nil {
logrus.Infof("Cannot find partner. Error: %s", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
if p == nil {
logrus.Info("Partner with given email doesn't exists")
w.WriteHeader(http.StatusUnauthorized)
return
}
passwordHash := partners.Hash(posted.Password + p.Salt)
if p.PasswordHash == passwordHash {
if p != nil && p.Status == partners.StatusNotVerified {
response := LoginResponse{p.Email, partners.StatusNotVerified}
jsonResponse, err := json.Marshal(response)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(jsonResponse)
return
}
session, err := h.Store.Get(req, "login-session")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
session.Values["id"] = p.ID
session.Save(req, w)
if p != nil && p.Status == partners.StatusVerified {
response := LoginResponse{p.Email, partners.StatusVerified}
jsonResponse, err := json.Marshal(response)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(jsonResponse)
return
}
if p != nil && p.Status == partners.StatusActive {
response := LoginResponse{p.Email, partners.StatusActive}
jsonResponse, err := json.Marshal(response)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(jsonResponse)
return
}
} else {
logrus.Infof("Invalid password")
w.WriteHeader(http.StatusUnauthorized)
return
}
}