-
Notifications
You must be signed in to change notification settings - Fork 5
/
user_resource.go
91 lines (82 loc) · 2.74 KB
/
user_resource.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
package main
import (
log "github.com/Sirupsen/logrus"
"github.com/emicklei/go-restful"
"github.com/satori/go.uuid"
"golang.org/x/crypto/bcrypt"
"encoding/json"
"fmt"
)
const ONE_TIME_PIN_USE = `
<html>
</head>
<body>
<h1>One time pin garage door opener</h1>
<br>
<p>By clicking the button below it will open the garage door. The garage door will automatically close %v seconds after clicking the button</p>
<br>
<br>
<form name="myform" action="/garage/one-time-pin/%s" method="post">
<button>Open</button>
</form>
</body>
</html>
`
type UserResource struct {
userDao UserDao
pinDao PinDao
}
func (this UserResource) register(container *restful.Container) {
ws := new(restful.WebService)
ws.Path("/user").
Consumes(restful.MIME_JSON).
Produces(restful.MIME_JSON)
ws.Route(ws.POST("login").To(this.login))
ws.Route(ws.POST("one-time-pin").To(this.oneTimePin))
ws.Route(ws.GET("one-time-pin/{oneTimePin}").To(this.useOneTimePin))
container.Add(ws)
}
func (this UserResource) oneTimePin(request *restful.Request, response *restful.Response) {
token := request.HeaderParameter("X-Auth-Token")
user := this.userDao.getUserByToken(token)
log.Debugf("%s is creating new one time pin", user.Email)
pin, err := this.pinDao.newOneTimePin(user)
if err != nil {
log.WithError(err).Error("Could not create one time pin")
response.WriteHeader(500)
}
pinMap := map[string]interface{}{
"pin": pin,
}
payload, err := json.Marshal(pinMap)
if err != nil {
log.WithError(err).Error("Could not marshell one time pin")
response.WriteHeader(500)
}
response.Write(payload)
}
func (this UserResource) useOneTimePin(request *restful.Request, response *restful.Response) {
oneTimePin := request.PathParameter("oneTimePin")
response.ResponseWriter.WriteHeader(200)
response.ResponseWriter.Write([]byte(fmt.Sprintf(ONE_TIME_PIN_USE, TIME_TO_CLOSE.Seconds(), oneTimePin)))
}
func (this UserResource) login(request *restful.Request, response *restful.Response) {
loginUser := new(User)
request.ReadEntity(&loginUser)
user := this.userDao.getUserByEmail(loginUser.Email)
err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(loginUser.Password))
if err == bcrypt.ErrMismatchedHashAndPassword {
log.Infof("Login failed for [%s]", user.Email)
response.WriteErrorString(400, "400: Incorrect username or passwords")
} else if err != nil {
log.Infof("Login failed for [%s]", user.Email)
log.Errorf("%v", err)
response.WriteErrorString(400, "400: Incorrect username or passwords")
} else {
log.Infof("Login successful for [%s]", user.Email)
user.Token = uuid.Must(uuid.NewV4()).String()
this.userDao.setToken(user)
response.Header().Set("X-Auth-Token", user.Token)
log.Debugf("Setting X-Auth-Token to [%s]", user.Token)
}
}