This repository has been archived by the owner on Jun 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
auth.go
223 lines (194 loc) · 6.65 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
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
package controllers
import (
"log"
"net/http"
"github.com/alanqchen/Bear-Post/backend/app"
"github.com/alanqchen/Bear-Post/backend/models"
"github.com/alanqchen/Bear-Post/backend/repositories"
"github.com/alanqchen/Bear-Post/backend/services"
"github.com/alanqchen/Bear-Post/backend/util"
"gopkg.in/ezzarghili/recaptcha-go.v4"
)
// AuthController holds what's necessary for authentication
type AuthController struct {
App *app.App
repositories.UserRepository
jwtService services.JWTAuthService
}
// NewAuthController returns an AuthController struct given the App, user repository, and JWT service
func NewAuthController(a *app.App, us repositories.UserRepository, jwtService services.JWTAuthService) *AuthController {
return &AuthController{a, us, jwtService}
}
// Authenticate will log in a user
func (ac *AuthController) Authenticate(w http.ResponseWriter, r *http.Request) {
j, err := GetJSON(r.Body)
if err != nil {
NewAPIError(&APIError{false, "Invalid request", http.StatusBadRequest}, w)
return
}
username, err := j.GetString("username")
if err != nil {
NewAPIError(&APIError{false, "username is required", http.StatusBadRequest}, w)
return
}
pw, err := j.GetString("password")
if err != nil {
NewAPIError(&APIError{false, "Password is required", http.StatusBadRequest}, w)
return
}
/* NO LONGER REQUIRE A EMAIL
if ok := util.IsEmail(email); !ok {
NewAPIError(&APIError{false, "You must provide a valid email address", http.StatusBadRequest}, w)
return
}
*/
u, err := ac.UserRepository.FindByUsername(username)
if err != nil {
log.Println("[BAD LOGIN] - username:", username)
NewAPIError(&APIError{false, "Incorrect email or password", http.StatusBadRequest}, w)
return
}
if ok := u.CheckPassword(pw); !ok {
log.Printf("[BAD LOGIN] - username: %v password %v", username, pw)
NewAPIError(&APIError{false, "Incorrect email or password", http.StatusBadRequest}, w)
return
}
tokens, err := ac.jwtService.GenerateTokens(u)
if err != nil {
NewAPIError(&APIError{false, "Something went wrong", http.StatusBadRequest}, w)
return
}
authUser := &models.AuthUser{
User: u,
Admin: u.Admin,
}
data := struct {
Tokens *services.Tokens `json:"tokens"`
User *models.AuthUser `json:"user"`
}{
tokens,
authUser,
}
log.Println("[LOGIN SUCCESS] - username:", username)
NewAPIResponse(&APIResponse{Success: true, Message: "Login successful", Data: data}, w, http.StatusOK)
}
// Logout will log out a user
func (ac *AuthController) Logout(w http.ResponseWriter, r *http.Request) {
tokenString, err := services.GetTokenFromRequest(&ac.App.Config, r)
if err != nil {
NewAPIError(&APIError{false, "Something went wrong", http.StatusBadRequest}, w)
return
}
tokenHash, err := services.ExtractTokenHash(&ac.App.Config, tokenString)
if err != nil {
NewAPIError(&APIError{false, "Something went wrong", http.StatusBadRequest}, w)
return
}
/*jti, err := services.ExtractJti(&ac.App.Config, tokenString)
if err != nil {
NewAPIError(&APIError{false, "Something went wrong", http.StatusBadRequest}, w)
return
}*/
keys := ac.App.Redis.Keys("*" + tokenHash + ".*")
for _, token := range keys.Val() {
err := ac.App.Redis.Del(token).Err()
if err != nil {
log.Printf("Could not delete token: %s ; error: %v", token, err)
NewAPIError(&APIError{false, "Something went wrong", http.StatusBadRequest}, w)
return
}
}
NewAPIResponse(&APIResponse{Success: true, Message: "Logout successful"}, w, http.StatusOK)
}
// LogoutAll logs out of all users
func (ac *AuthController) LogoutAll(w http.ResponseWriter, r *http.Request) {
uid, err := services.UserIDFromContext(r.Context())
if err != nil {
NewAPIError(&APIError{false, "Something went wrong", http.StatusInternalServerError}, w)
return
}
keys := ac.App.Redis.Keys("*." + uid + ".*")
for _, token := range keys.Val() {
err := ac.App.Redis.Del(token).Err()
if err != nil {
log.Printf("Could not delete token: %s ; error: %v", token, err)
}
}
NewAPIResponse(&APIResponse{Success: true, Message: "Logout successful"}, w, http.StatusOK)
}
// RefreshTokens will refresh JWT tokens if the given refresh token is valid
func (ac *AuthController) RefreshTokens(w http.ResponseWriter, r *http.Request) {
tokenString, err := services.GetRefreshTokenFromRequest(&ac.App.Config, r)
if err != nil {
NewAPIError(&APIError{false, "Something went wrong", http.StatusInternalServerError}, w)
return
}
uid, err := services.UserIDFromContext(r.Context())
if err != nil {
NewAPIError(&APIError{false, "Something went wrong", http.StatusInternalServerError}, w)
return
}
tokenHash, err := services.ExtractRefreshTokenHash(&ac.App.Config, tokenString)
if err != nil {
NewAPIError(&APIError{false, "Something went wrong", http.StatusBadRequest}, w)
return
}
u, err := ac.UserRepository.FindByIDDetailed(uid)
if err != nil {
NewAPIError(&APIError{false, "Could not find user", http.StatusBadRequest}, w)
return
}
tokens, err := ac.jwtService.GenerateTokens(u)
if err != nil {
NewAPIError(&APIError{false, "Something went wrong", http.StatusBadRequest}, w)
return
}
keys := ac.App.Redis.Keys("*" + tokenHash + ".*")
for _, token := range keys.Val() {
err := ac.App.Redis.Del(token).Err()
if err != nil {
log.Printf("Could not delete token: %s ; error: %v", token, err)
NewAPIError(&APIError{false, "Something went wrong", http.StatusBadRequest}, w)
return
}
}
authUser := &models.AuthUser{
User: u,
Admin: u.Admin,
}
data := struct {
Tokens *services.Tokens `json:"tokens"`
User *models.AuthUser `json:"user"`
}{
tokens,
authUser,
}
log.Println("[AUTH] Created new refresh token for user", u.Username)
NewAPIResponse(&APIResponse{Success: true, Message: "Refresh successful", Data: data}, w, http.StatusOK)
}
// VerifyCaptcha verifies a clients reCaptcha response token is valid
func (ac *AuthController) VerifyCaptcha(w http.ResponseWriter, r *http.Request) {
j, err := GetJSON(r.Body)
if err != nil {
NewAPIError(&APIError{false, "Invalid request", http.StatusBadRequest}, w)
return
}
token, err := j.GetString("token")
if err != nil {
NewAPIError(&APIError{false, "token is required", http.StatusBadRequest}, w)
return
}
remoteIP := util.GetIP(r)
if remoteIP == "" {
err = ac.App.Recaptcha.Verify(token)
} else {
err = ac.App.Recaptcha.VerifyWithOptions(token, recaptcha.VerifyOption{RemoteIP: remoteIP})
}
if err != nil {
log.Println("[BAD AUTH] Failed recaptcha verification", err)
NewAPIError(&APIError{false, "Failed to verify token", http.StatusBadRequest}, w)
return
}
log.Println("[AUTH] Passed reCaptcha verification")
NewAPIResponse(&APIResponse{Success: true, Message: "reCaptcha verification successful"}, w, http.StatusOK)
}