-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
339 lines (304 loc) · 10.3 KB
/
main.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package main
import (
"net/http"
"strconv"
"fmt"
"os"
"encoding/json"
"encoding/hex"
"strings"
"time"
"crypto/rand"
"golang.org/x/crypto/bcrypt"
"github.com/Greeshmanth1909/chirpy/database"
"github.com/golang-jwt/jwt/v5"
"github.com/joho/godotenv"
)
func main() {
// Load environment variables from dot file
godotenv.Load()
jwtSecret := os.Getenv("JWT_SECRET")
hits.jwtSecret = jwtSecret
serveMux := http.NewServeMux()
var server http.Server
server.Addr = "localhost:8080"
server.Handler = serveMux
// add handler to root
serveMux.Handle("/app/", http.StripPrefix("/app/", hits.middlewareMetricsInc(http.FileServer(http.Dir(".")))))
serveMux.HandleFunc("GET /api/healthz", readyness)
serveMux.HandleFunc("GET /admin/metrics", metricsHandler)
serveMux.HandleFunc("/api/reset", resetHandler)
serveMux.HandleFunc("POST /api/chirps", valChirpHandler)
serveMux.HandleFunc("GET /api/chirps", getChirps)
serveMux.HandleFunc("GET /api/chirps/{chirpid}", getChirpById)
serveMux.HandleFunc("POST /api/users", postUsers)
serveMux.HandleFunc("PUT /api/users", updateUsers)
serveMux.HandleFunc("POST /api/login", loginUsers)
serveMux.HandleFunc("POST /api/refresh", refresh)
serveMux.HandleFunc("POST /api/revoke", revoke)
server.ListenAndServe()
}
func readyness(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
}
// Type to assess metrics
type apiConfig struct {
fileserverHits int
jwtSecret string
}
var hits apiConfig
/* middlewareMetricsInc is a middle ware function that runs before the root handler is called, it counts the number of times the
site was visited */
func (cfg *apiConfig) middlewareMetricsInc(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
cfg.fileserverHits += 1
next.ServeHTTP(w, r)
})
}
/* metricsHandler writes the fileserverHits from apiConfig to the response body*/
func metricsHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write([]byte(fmt.Sprintf("<html><body><h1>Welcome, Chirpy Admin</h1><p>Chirpy has been visited %v times!</p></body></html>", hits.fileserverHits)))
}
/* Reset function handler resets the hits count to zero*/
func resetHandler(w http.ResponseWriter, r *http.Request) {
hits.fileserverHits = 0
w.WriteHeader(http.StatusOK)
}
var db = database.NewDB("/Users/geechu/Desktop/Programing/Projects/chirpy/database.json")
/* valChirpHandler validates the post request from the user */
func valChirpHandler(w http.ResponseWriter, r *http.Request) {
type getData struct {
Body string `json:"body"`
}
type sendData struct {
Error string `json:"error"`
Valid bool `json:"valid"`
Cleaned_body string `json:"cleaned_body"`
}
get := getData{}
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&get)
if err != nil {
fmt.Println("error")
}
bodyStr := get.Body
if len(bodyStr) > 140 {
// encode response body
response := sendData{"Chirp is too long", false, ""}
dat, err := json.Marshal(response)
if err != nil {
fmt.Println("error encoding json")
return
}
w.WriteHeader(400)
w.Write([]byte(dat))
return
}
chirp := db.CreateChirp(rmProfane(bodyStr))
dat, err := json.Marshal(chirp)
w.WriteHeader(201)
w.Write([]byte(dat))
db.Write(chirp)
}
// rmProfane replaces certain words with ****
func rmProfane(opinion string) string {
forbiddenWords := []string{"kerfuffle", "sharbert", "fornax"}
lowerCaseOpinion := strings.ToLower(opinion)
opinionSplit := strings.Split(opinion, " ")
for _, val := range forbiddenWords {
splitstr := strings.Split(lowerCaseOpinion, " ")
for i, k := range splitstr {
if k == val {
opinionSplit[i] = "****"
}
}
}
return strings.Join(opinionSplit, " ")
}
/* getChirps function reads json from the database and returns it*/
func getChirps(w http.ResponseWriter, r *http.Request) {
dat, err := db.Read()
if err != nil {
w.WriteHeader(200)
w.Write([]byte("No chirps in the database yet"))
return
}
var s []database.Chirp
for _, val := range dat.Chirps {
s = append(s, val)
}
data, err := json.Marshal(s)
w.Write(data)
return
}
/* getChirpById function returns the requested chirp with a status code of 200 */
func getChirpById(w http.ResponseWriter, r *http.Request) {
id, _ := strconv.Atoi(r.PathValue("chirpid"))
data, err := db.Read()
if err != nil {
fmt.Println("llll")
return
}
if id > len(data.Chirps) {
w.WriteHeader(404)
return
}
id -= 1
dat, _ := json.Marshal(data.Chirps[id])
w.Write(dat)
}
type Email struct {
Email string `json:"email"`
Password string `json:"password"`
Expires_in_seconds int `json:"expires_in_seconds"`
}
type User struct {
Id int `json:"id"`
Email string `json:"email"`
Token string `json:"token"`
Refresh_token string `json:"refresh_token"`
}
/* postUsers function adds a user to the database and responds with the username and its corresponding id */
func postUsers(w http.ResponseWriter, r *http.Request) {
var email Email
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&email)
if err != nil {
fmt.Println("couldn't unmarshal request body")
}
id, e := db.CreateUser(email.Email, email.Password)
user := User{id, e, "", ""}
dat, _ := json.Marshal(user)
w.WriteHeader(201)
w.Write(dat)
}
/* loginUsers logs a user in if they exist in the db and password mathes stored hash */
func loginUsers(w http.ResponseWriter, r *http.Request) {
var req Email
decoder := json.NewDecoder(r.Body)
decoder.Decode(&req)
dat, err := db.Read()
if err != nil {
w.WriteHeader(401)
return
}
for _, val := range dat.Users {
if val.Email == req.Email {
err = bcrypt.CompareHashAndPassword(val.Hash, []byte(req.Password))
if err != nil {
w.WriteHeader(401)
return
} else {
// Generate refresh token
c := 32
b := make([]byte, c)
rand.Read(b)
ref_token := hex.EncodeToString(b)
w.WriteHeader(200)
var user User
user.Id = val.Id
user.Email = val.Email
user.Refresh_token = ref_token
var claims jwt.RegisteredClaims
claims.Issuer = "chirpy"
claims.IssuedAt = jwt.NewNumericDate(time.Now())
if req.Expires_in_seconds != 0 {
claims.ExpiresAt = jwt.NewNumericDate(time.Now().Add(time.Duration(req.Expires_in_seconds) * time.Second))
}
// add refresh token to the database
db.AddRefToken(val.Id, ref_token)
claims.Subject = string(val.Id)
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
jwt, _ := token.SignedString([]byte(hits.jwtSecret))
user.Token = jwt
dat, _ := json.Marshal(user)
w.Write(dat)
return
}
}
}
w.WriteHeader(401)
}
/* updateUsers function verifies the jwt and updates a user's email */
func updateUsers(w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("Authorization")
if token == "" {
w.WriteHeader(404)
return
}
token = strings.TrimPrefix(token, "Bearer ")
var claims jwt.RegisteredClaims
_ , err := jwt.ParseWithClaims(token, &claims, func(token *jwt.Token) (interface{}, error) {
return []byte(hits.jwtSecret), nil
})
if err != nil {
w.WriteHeader(401)
return
}
// read and update the database
i, _ := claims.GetSubject()
id, _ := strconv.Atoi(i)
// get fields that need to be updated
var user Email
decoder := json.NewDecoder(r.Body)
decoder.Decode(&user)
// user now has email and password
// Update username
db.UpdateUser(user.Email, user.Password, id)
var updatedUser User
updatedUser.Id = id + 1
updatedUser.Email = user.Email
data, _ := json.Marshal(updatedUser)
w.Write(data)
}
/* refresh function verifies the refresh token and generates a new jwt */
func refresh(w http.ResponseWriter, r *http.Request) {
auth := r.Header.Get("Authorization")
auth = strings.TrimPrefix(auth, "Bearer ")
dat, _ := db.Read()
for _, val := range dat.Users {
if val.Refresh_token == auth {
// Generate new jwt
w.WriteHeader(200)
var user User
user.Id = val.Id
user.Email = val.Email
user.Refresh_token = auth
var claims jwt.RegisteredClaims
claims.Issuer = "chirpy"
claims.IssuedAt = jwt.NewNumericDate(time.Now())
claims.ExpiresAt = jwt.NewNumericDate(time.Now().Add(time.Duration(60) * time.Minute))
// add refresh token to the database
claims.Subject = string(val.Id)
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
jwt, _ := token.SignedString([]byte(hits.jwtSecret))
user.Token = jwt
dat, _ := json.Marshal(user)
w.Write(dat)
return
}
}
w.WriteHeader(401)
}
/* revoke function removes the token */
func revoke(w http.ResponseWriter, r *http.Request) {
auth := r.Header.Get("Authorization")
auth = strings.TrimPrefix(auth, "Bearer ")
var user database.User
dat, _ := db.Read()
for _, val := range dat.Users {
if val.Refresh_token == auth {
user.Id = val.Id
user.Email = val.Email
user.Hash = val.Hash
user.Refresh_token = ""
}
}
mapId := user.Id - 1
dat.Users[mapId] = user
db.WriteDb(dat)
w.WriteHeader(204)
}