-
Notifications
You must be signed in to change notification settings - Fork 0
/
authpro.go
303 lines (268 loc) · 7.72 KB
/
authpro.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
package authpro
import (
"crypto/rand"
"encoding/json"
"errors"
"fmt"
"github.com/go-chi/chi/v5"
"github.com/gofrs/uuid/v5"
"github.com/golang-jwt/jwt/v5"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/joho/godotenv"
"github.com/o1egl/paseto"
"golang.org/x/crypto/bcrypt"
"golang.org/x/crypto/chacha20poly1305"
"log"
"log/slog"
"net/http"
"os"
"strings"
"time"
)
type auth struct {
DB *pgxpool.Pool
W http.ResponseWriter
R *http.Request
}
type Auth interface {
TokenJWT(claimName string, userId uuid.UUID) (string, string, error)
ParseJWT(claimName string, tokenString string) (string, float64, error)
ExtractJWT() (string, error)
MiddlewarePerm(claimName string) bool
HashPassword(password string) ([]byte, error)
ComparePassword(userPassword []byte, dtoPassword string) error
RandomPassword(length int) (string, error)
UUID(param string) (uuid.UUID, error)
MiddlewareApiKey(apiKeyName, apiValueName string, r *http.Request) (bool, string)
}
func NewAuth(w http.ResponseWriter, r *http.Request) Auth {
return &auth{
W: w,
R: r,
}
}
const (
Content = "Content-Type"
AppJson = "application/json"
RandChar = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
)
func (a *auth) TokenJWT(claimName string, userId uuid.UUID) (string, string, error) {
DotENV()
var err error
privateKey := []byte(os.Getenv("JWT_KEY_SECRET"))
privateRefreshKey := []byte(os.Getenv("JWT_REFRESH_KEY_SECRET"))
token := jwt.New(jwt.SigningMethodHS256)
claims := token.Claims.(jwt.MapClaims)
claims[claimName] = userId
claims["exp"] = time.Now().Add(time.Hour * 2).Unix()
accessTokenString, errAuthToken := token.SignedString(privateKey)
if errAuthToken != nil {
err = errAuthToken
}
refreshToken := jwt.New(jwt.SigningMethodHS256)
refreshClaims := refreshToken.Claims.(jwt.MapClaims)
refreshClaims["ref_"+claimName] = userId
refreshClaims["ref_exp"] = time.Now().Add(time.Hour * 2).Unix()
refreshTokenString, errRefreshToken := refreshToken.SignedString(privateRefreshKey)
if errRefreshToken != nil {
err = errRefreshToken
}
return accessTokenString, refreshTokenString, err
}
func (a *auth) ParseJWT(paramId string, tokenString string) (string, float64, error) {
DotENV()
var err error
jwtKey := os.Getenv("JWT_KEY_SECRET")
token, errParse := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
err = errors.New(fmt.Sprintf("unexpected signing method: %v", token.Header["alg"]))
slog.Error(err.Error(), "error parse token")
}
secretKey := []byte(jwtKey)
return secretKey, nil
})
if errParse != nil {
err = errParse
slog.Error(errParse.Error(), "error parse token")
}
claims, ok := token.Claims.(jwt.MapClaims)
if !ok {
err = errors.New(strings.ToLower("error claim token"))
slog.Error(err.Error(), "error claim token")
}
jwtUserId := claims[paramId].(string)
jwtExp := claims["exp"].(float64)
return jwtUserId, jwtExp, err
}
func (a *auth) ExtractJWT() (string, error) {
authHeader := a.R.Header.Get("Authorization")
var err error
errHeader := checkAuthHeader(authHeader)
authHeaderParts := strings.Split(authHeader, " ")
errBearer := checkBearer(authHeaderParts)
if len(authHeaderParts) < 2 {
err = errors.New(fmt.Sprintf("%s: %s", "error token not found", errHeader))
slog.Error(err.Error(), "error token not found")
}
if errBearer != nil {
err = errBearer
slog.Error(errBearer.Error(), "error bearer token")
}
tokenString := authHeaderParts[1]
return tokenString, err
}
func checkAuthHeader(authHeader string) error {
if len(authHeader) <= 5 {
return errors.New("length of authHeader exceeds 5 characters token is not valid")
}
return nil
}
func checkBearer(str []string) error {
if len(str) != 2 || str[0] != "Bearer" {
return errors.New("invalid bearer token")
}
return nil
}
func (a *auth) MiddlewarePerm(paramId string) bool {
userId := chi.URLParam(a.R, paramId)
jwtUserId := a.R.Context().Value("jwt_" + paramId).(string)
if userId != jwtUserId {
response := map[string]interface{}{
"data": "ERROR PERMISSION",
"message": "You are not authorize",
"statusCode": http.StatusUnauthorized,
"success": false,
}
a.W.Header().Set(Content, AppJson)
a.W.WriteHeader(http.StatusUnauthorized)
err := json.NewEncoder(a.W).Encode(response)
if err != nil {
slog.Error(err.Error())
http.Error(a.W, err.Error(), http.StatusInternalServerError)
}
return false
}
return true
}
func (a *auth) MiddlewareApiKey(apiKeyName, apiValueName string, r *http.Request) (bool, string) {
b := true
var message string
getApiHeader := r.Header.Get(apiKeyName)
if getApiHeader == "" {
b = false
message = "error header is empty"
}
apiKeyHeader := strings.Split(getApiHeader, " ")
apiHeaderValue := apiKeyHeader[0]
if len(apiHeaderValue) <= 0 {
b = false
message = "error header has no length"
}
if apiValueName != apiHeaderValue {
b = false
message = "invalid api key"
}
return b, message
}
func (a *auth) UUID(param string) (uuid.UUID, error) {
var err error
id := chi.URLParam(a.R, param)
UUID, errParseUUID := uuid.FromString(id)
if errParseUUID != nil {
err = errors.New("error parse uuid")
return uuid.Nil, err
}
return UUID, err
}
func (a *auth) HashPassword(password string) ([]byte, error) {
var errHash error
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
errHash = err
slog.Error(err.Error(), "error hash password")
}
return hashedPassword, errHash
}
func (a *auth) ComparePassword(userPassword []byte, dtoPassword string) error {
err := bcrypt.CompareHashAndPassword(userPassword, []byte(dtoPassword))
return err
}
func (a *auth) RandomPassword(length int) (string, error) {
var errRand error
buffer := make([]byte, length)
_, err := rand.Read(buffer)
if err != nil {
errRand = err
slog.Error(err.Error(), fmt.Sprintf("error random crypto: %s", err.Error()))
}
charsetLength := len(RandChar)
for i := 0; i < length; i++ {
buffer[i] = RandChar[int(buffer[i])%charsetLength]
}
return string(buffer), errRand
}
//Paseto
type PasetoMaker struct {
paseto *paseto.V2
symmetricKey []byte
}
type Payload struct {
ID uuid.UUID `json:"ID"`
UserID uuid.UUID `json:"userID"`
IssuedAt time.Time `json:"issued_at"`
ExpiredAt time.Time `json:"expired_at"`
}
func NewPasetoMaker(symmetricKey string) (*PasetoMaker, error) {
if len(symmetricKey) != chacha20poly1305.KeySize {
return nil, fmt.Errorf("invalid key size: must be exactly %d characters", chacha20poly1305.KeySize)
}
maker := &PasetoMaker{
paseto: paseto.NewV2(),
symmetricKey: []byte(symmetricKey),
}
return maker, nil
}
func NewPasetoPayload(userID uuid.UUID, duration time.Duration) (*Payload, error) {
tokenID, err := uuid.NewV4()
if err != nil {
return nil, err
}
payload := &Payload{
ID: tokenID,
UserID: userID,
IssuedAt: time.Now(),
ExpiredAt: time.Now().Add(duration),
}
return payload, nil
}
func (maker *PasetoMaker) CreatePasetoToken(userID uuid.UUID, duration time.Duration) (string, error) {
payload, err := NewPasetoPayload(userID, duration)
if err != nil {
return "", err
}
return maker.paseto.Encrypt(maker.symmetricKey, payload, nil)
}
func (payload *Payload) Valid() error {
if time.Now().After(payload.ExpiredAt) {
return errors.New("expired")
}
return nil
}
func (maker *PasetoMaker) VerifyPasetoToken(token string) (*Payload, error) {
payload := &Payload{}
err := maker.paseto.Decrypt(token, maker.symmetricKey, payload, nil)
if err != nil {
return nil, errors.New("invalid token")
}
err = payload.Valid()
if err != nil {
return nil, err
}
return payload, nil
}
func DotENV() {
err := godotenv.Load(".env")
if err != nil {
log.Fatal(err, "error load .env")
}
}