-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.go
129 lines (109 loc) · 3.24 KB
/
auth.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
package main
import (
"chirpy/internal/auth"
"chirpy/internal/database"
"encoding/json"
"errors"
"net/http"
"strconv"
"time"
)
func (cfg *apiConfig) handleLogin(w http.ResponseWriter, r *http.Request) {
type parameters struct {
Email string `json:"email"`
Password string `json:"password"`
}
type response struct {
User
Token string `json:"token"`
RefreshToken string `json:"refresh_token"`
}
decoder := json.NewDecoder(r.Body)
params := parameters{}
err := decoder.Decode(¶ms)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Couldn't decode parameters")
return
}
user, err := cfg.DB.GetUserByEmail(params.Email)
if err != nil {
respondWithError(w, http.StatusNotFound, "Couldn't find user")
return
}
err = auth.CheckPasswordHash(params.Password, user.HashedPassword)
if err != nil {
respondWithError(w, http.StatusUnauthorized, "Wrong credentials")
return
}
token, err := auth.MakeJWT(user.ID, "chirpy-access", cfg.jwtSecret, time.Duration(1*time.Hour))
if err != nil {
respondWithError(w, http.StatusUnauthorized, "Error making JWT Access Token")
return
}
refreshToken, err := auth.MakeJWT(user.ID, "chirpy-refresh", cfg.jwtSecret, time.Duration(60*24*time.Hour))
if err != nil {
respondWithError(w, http.StatusUnauthorized, "Error making JWT Refresh Token")
return
}
respondWithJSON(
w,
http.StatusOK,
response{
User: User{ID: user.ID, Email: user.Email, IsChirpyRed: user.IsChirpyRed},
Token: token,
RefreshToken: refreshToken,
},
)
}
func (cfg *apiConfig) handleRefresh(w http.ResponseWriter, r *http.Request) {
type response struct {
Token string `json:"token"`
}
token, err := auth.GetBearerToken(r.Header)
if err != nil {
respondWithError(w, http.StatusUnauthorized, "Couldn't find JWT")
return
}
subject, err := auth.ValidateRefreshJWT(token, cfg.jwtSecret)
if err != nil {
respondWithError(w, http.StatusUnauthorized, "Couldn't Validate JWT")
return
}
if _, err := cfg.DB.GetRevocation(token); !errors.Is(err, database.ErrNotExist) {
respondWithError(w, http.StatusUnauthorized, "This token is revoked")
return
}
userID, err := strconv.Atoi(subject)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Couldn't parse user ID")
return
}
accessToken, err := auth.MakeJWT(userID, "chirpy-access", cfg.jwtSecret, time.Duration(1*time.Hour))
if err != nil {
respondWithError(w, http.StatusUnauthorized, "Error making JWT Access Token")
return
}
respondWithJSON(w, http.StatusOK, response{Token: accessToken})
}
func (cfg *apiConfig) handleRevoke(w http.ResponseWriter, r *http.Request) {
token, err := auth.GetBearerToken(r.Header)
if err != nil {
respondWithError(w, http.StatusUnauthorized, "Couldn't find JWT")
return
}
_, err = cfg.DB.GetRevocation(token)
if err == nil {
respondWithError(w, http.StatusBadRequest, "Token was already revoked")
return
}
if err != database.ErrNotExist {
respondWithError(w, http.StatusInternalServerError, "Error fetching revocation")
return
}
_, err = cfg.DB.CreateRevocation(token)
if err != nil {
respondWithError(w, http.StatusInternalServerError, "Error revoking token")
return
}
respondWithJSON(w, http.StatusOK, "Token revoked successfuly")
}