This repository has been archived by the owner on Jun 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler_auth.go
229 lines (218 loc) · 6.11 KB
/
handler_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
224
225
226
227
228
229
package main
import (
"database/sql"
"github.com/dgrijalva/jwt-go"
"github.com/gin-gonic/gin"
"github.com/lib/pq"
"github.com/pkg/errors"
"github.com/pquerna/otp/totp"
"golang.org/x/crypto/bcrypt"
"time"
)
var signMethod jwt.SigningMethod = jwt.SigningMethodHS256
// The relams we allow a user to set on a new API key
var UserKeygenRealms = []string{"trade", "withdraw"}
// The relams we give to conventional logins through the web interface
var JWTRealms = []string{"trade", "withdraw", "keygen", "tfa"}
type User struct {
Username string
ID int
Password string
TFAEnabled bool `db:"tfa_enabled"`
TFACode string `db:"tfa_code"`
}
func (q *NgWebAPI) postTFA(c *gin.Context) {
type TwoFactoReq struct {
Code string `validate:"required"`
}
var req TwoFactoReq
if !q.BindValid(c, &req) {
return
}
userID := c.GetInt("userID")
var user User
err := q.db.QueryRowx(
"SELECT tfa_code, tfa_enabled FROM users WHERE id = $1",
userID).StructScan(&user)
if err != nil {
q.apiException(c, 500, errors.WithStack(err), SQLError)
return
}
valid := totp.Validate(req.Code, user.TFACode)
if !valid {
q.apiError(c, 400, APIError{
Code: "invalid_code",
Title: "The two factor code provided is not valid"})
return
}
if !user.TFAEnabled {
res, err := q.db.Exec(`UPDATE users SET tfa_enabled = true WHERE id = $1`, userID)
if err != nil {
q.apiException(c, 500, errors.WithStack(err), SQLError)
return
}
affect, err := res.RowsAffected()
if affect < 1 {
q.apiException(c, 500, errors.WithStack(err), SQLError)
return
}
}
tokenString, err := q.createToken(user.Username, userID, JWTRealms)
q.apiSuccess(c, 200, res{"token": tokenString})
}
func (q *NgWebAPI) postTFASetup(c *gin.Context) {
userID := c.GetInt("userID")
user := make(map[string]interface{})
err := q.db.QueryRowx(
"SELECT id, email, tfa_enabled FROM users WHERE id = $1",
userID).MapScan(user)
if err != nil {
q.apiException(c, 500, errors.WithStack(err), SQLError)
return
}
if user["tfa_enabled"].(bool) == true {
q.apiError(c, 400, APIError{
Code: "already_setup",
Title: "Two factor authentication is already setup"})
return
}
key, err := totp.Generate(totp.GenerateOpts{
Issuer: "NgWebAPI test",
AccountName: user["email"].(string),
})
if err != nil {
q.apiException(c, 500, errors.WithStack(err), APIError{
Code: "keygen_err",
Title: "Failed to generate new two factor key for unknown reason"})
return
}
result, err := q.db.Exec(
`UPDATE users SET tfa_code = $1 WHERE id = $2 AND tfa_enabled = false`,
key.Secret(), userID)
if err != nil {
q.apiException(c, 500, errors.WithStack(err), SQLError)
return
}
affect, err := result.RowsAffected()
if affect < 1 {
// Only occurs in edge case
q.apiError(c, 400, APIError{
Code: "already_setup",
Title: "Two factor authentication is already setup"})
return
}
q.apiSuccess(c, 200, res{"tfa_enroll_string": key.String()})
}
func (q *NgWebAPI) postLogin(c *gin.Context) {
type LoginReq struct {
Username string `json:"username" validate:"required"`
Password string `json:"password" validate:"required"`
}
var req LoginReq
var err error
if !q.BindValid(c, &req) {
return
}
var user User
err = q.db.QueryRowx(
"SELECT id, username, password, tfa_enabled FROM users WHERE username = $1",
req.Username).StructScan(&user)
if err != nil {
if err == sql.ErrNoRows {
q.apiError(c, 403, APIError{
Code: "invalid_auth",
Title: "Invalid username or password provided"})
} else {
q.apiException(c, 500, errors.WithStack(err), SQLError)
}
return
}
err = bcrypt.CompareHashAndPassword(
[]byte(user.Password), []byte(req.Password))
if err != nil {
q.apiError(c, 403, APIError{
Code: "invalid_auth",
Title: "Invalid username or password provided"})
return
}
// Only given them read access and tfa auth endpoint access if they have
// tfa enabled. Otherwise full access
var realms []string
if user.TFAEnabled {
realms = []string{"tfa"}
} else {
realms = JWTRealms
}
tokenString, err := q.createToken(user.Username, user.ID, realms)
if err != nil {
q.apiException(c, 500, errors.WithStack(err), APIError{
Code: "tokengen_err",
Title: "Failed to generate JWT token unknown reason"})
return
}
q.apiSuccess(c, 200, res{
"user_id": user.ID,
"username": user.Username,
"token": tokenString,
"tfa_enabled": user.TFAEnabled,
})
}
func (q *NgWebAPI) createToken(username string, id int, realms []string) (string, error) {
expire := time.Now().Add(time.Hour).Unix()
token := jwt.NewWithClaims(signMethod, customClaims{
Username: username,
UserID: id,
StandardClaims: jwt.StandardClaims{
ExpiresAt: expire,
},
})
tokenString, err := token.SignedString([]byte(q.config.GetString("JWTSecret")))
if err != nil {
return "", err
}
return tokenString, nil
}
func (q *NgWebAPI) postRegister(c *gin.Context) {
type RegisterReq struct {
Username string `validate:"required,alphanum,lt=32"`
Email *string `validate:"omitempty,email" json:"omitempty"`
Password string `validate:"required,gt=8,lt=128"`
}
var req RegisterReq
if !q.BindValid(c, &req) {
return
}
bcryptPassword, err := bcrypt.GenerateFromPassword([]byte(req.Password), 6)
if err != nil {
q.apiException(c, 500, errors.WithStack(err), APIError{
Code: "bcrypt_error",
Title: "Unable to hash password for unknown reason"})
return
}
var userID int
err = q.db.QueryRow(
`INSERT INTO users (username, email, password)
VALUES ($1, $2, $3) RETURNING id`,
req.Username, req.Email, bcryptPassword).Scan(&userID)
if err != nil {
if pqe, ok := err.(*pq.Error); ok && pqe.Code == "23505" {
if pqe.Constraint == "unique_email" {
q.apiError(c, 400, APIError{
Code: "email_taken",
Title: "Email address already in use"})
return
}
if pqe.Constraint == "unique_username" {
q.apiError(c, 400, APIError{
Code: "username_taken",
Title: "Username already in use"})
return
}
q.apiException(c, 500, errors.WithStack(err), SQLError)
} else {
q.apiException(c, 500, errors.WithStack(err), SQLError)
}
return
}
q.apiSuccess(c, 201, res{"id": userID})
}