-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
168 lines (155 loc) · 4.25 KB
/
api.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"encoding/json"
"fmt"
"github.com/julienschmidt/httprouter"
"github.com/nu7hatch/gouuid"
"golang.org/x/crypto/bcrypt"
"google.golang.org/appengine"
"google.golang.org/appengine/datastore"
"google.golang.org/appengine/log"
"google.golang.org/appengine/memcache"
"io/ioutil"
"net/http"
"time"
)
func checkUserName(res http.ResponseWriter, req *http.Request, _ httprouter.Params) {
ctx := appengine.NewContext(req)
bs, err := ioutil.ReadAll(req.Body)
sbs := string(bs)
log.Infof(ctx, "REQUEST BODY: %v", sbs)
var user User
key := datastore.NewKey(ctx, "Users", sbs, 0, nil)
err = datastore.Get(ctx, key, &user)
// if there is an err, there is NO user
log.Infof(ctx, "ERR: %v", err)
if err != nil {
// there is an err, there is a NO user
fmt.Fprint(res, "false")
return
} else {
fmt.Fprint(res, "true")
}
}
func createUser(res http.ResponseWriter, req *http.Request, _ httprouter.Params) {
ctx := appengine.NewContext(req)
hashedPass, err := bcrypt.GenerateFromPassword([]byte(req.FormValue("password")), bcrypt.DefaultCost)
if err != nil {
log.Errorf(ctx, "error creating password: %v", err)
http.Error(res, err.Error(), 500)
return
}
user := User{
Email: req.FormValue("email"),
UserName: req.FormValue("userName"),
Password: string(hashedPass),
}
key := datastore.NewKey(ctx, "Users", user.UserName, 0, nil)
key, err = datastore.Put(ctx, key, &user)
if err != nil {
log.Errorf(ctx, "error adding todo: %v", err)
http.Error(res, err.Error(), 500)
return
}
createSession(res, req, user)
// redirect
http.Redirect(res, req, "/", 302)
}
func loginProcess(res http.ResponseWriter, req *http.Request, _ httprouter.Params) {
ctx := appengine.NewContext(req)
key := datastore.NewKey(ctx, "Users", req.FormValue("userName"), 0, nil)
var user User
err := datastore.Get(ctx, key, &user)
if err != nil || bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(req.FormValue("password"))) != nil {
// failure logging in
var sd SessionData
sd.LoginFail = true
tpl.ExecuteTemplate(res, "login.html", sd)
return
} else {
user.UserName = req.FormValue("userName")
// success logging in
createSession(res, req, user)
// redirect
http.Redirect(res, req, "/", 302)
}
}
func createSession(res http.ResponseWriter, req *http.Request, user User) {
ctx := appengine.NewContext(req)
// SET COOKIE
id, _ := uuid.NewV4()
cookie := &http.Cookie{
Name: "session",
Value: id.String(),
Path: "/",
// twenty minute session:
MaxAge: 60 * 20,
// UNCOMMENT WHEN DEPLOYED:
// Secure: true,
// HttpOnly: true,
}
http.SetCookie(res, cookie)
// SET MEMCACHE session data (sd)
json, err := json.Marshal(user)
if err != nil {
log.Errorf(ctx, "error marshalling during user creation: %v", err)
http.Error(res, err.Error(), 500)
return
}
sd := memcache.Item{
Key: id.String(),
Value: json,
}
memcache.Set(ctx, &sd)
}
func logout(res http.ResponseWriter, req *http.Request, _ httprouter.Params) {
ctx := appengine.NewContext(req)
cookie, err := req.Cookie("session")
// cookie is not set
if err != nil {
http.Redirect(res, req, "/", 302)
return
}
// clear memcache
sd := memcache.Item{
Key: cookie.Value,
Value: []byte(""),
Expiration: time.Duration(1 * time.Microsecond),
}
memcache.Set(ctx, &sd)
// clear the cookie
cookie.MaxAge = -1
http.SetCookie(res, cookie)
// redirect
http.Redirect(res, req, "/", 302)
}
func tweetProcess(res http.ResponseWriter, req *http.Request, _ httprouter.Params) {
ctx := appengine.NewContext(req)
memItem, err := getSession(req)
if err != nil {
log.Infof(ctx, "Attempt to post tweet from logged out user")
http.Error(res, "You must be logged in", http.StatusForbidden)
return
}
// declare a variable of type user
// initialize user with values from memcache item
var user User
json.Unmarshal(memItem.Value, &user)
// declare a variable of type tweet
// initialize it with values
log.Infof(ctx, user.UserName)
tweet := Tweet{
Msg: req.FormValue("tweet"),
Time: time.Now(),
UserName: user.UserName,
}
// put in datastore
err = putTweet(req, &user, &tweet)
if err != nil {
log.Errorf(ctx, "error adding todo: %v", err)
http.Error(res, err.Error(), 500)
return
}
// redirect
http.Redirect(res, req, "/", 302)
}