feat: we made it better...aka i did too many things and don't remember
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/alexedwards/argon2id"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func HashPassword(password string) (string, error) {
|
||||
return argon2id.CreateHash(password, argon2id.DefaultParams)
|
||||
}
|
||||
|
||||
func CheckPasswordHash(password string, hash string) (bool, error) {
|
||||
return argon2id.ComparePasswordAndHash(password, hash)
|
||||
}
|
||||
|
||||
func MakeJWT(userID uuid.UUID, tokenSecret []byte, expiresIn time.Duration) (string, error) {
|
||||
token := jwt.New(jwt.GetSigningMethod(jwt.SigningMethodHS256.Name))
|
||||
|
||||
token.Claims = &jwt.RegisteredClaims{
|
||||
Issuer: "chirpy-access",
|
||||
IssuedAt: jwt.NewNumericDate(time.Now()),
|
||||
ExpiresAt: jwt.NewNumericDate(time.Now().Add(expiresIn)),
|
||||
Subject: userID.String(),
|
||||
}
|
||||
|
||||
return token.SignedString(tokenSecret)
|
||||
}
|
||||
|
||||
func ValidateJWT(tokenString string, tokenSecret []byte) (uuid.UUID, error) {
|
||||
token, err := jwt.ParseWithClaims(tokenString, &jwt.RegisteredClaims{}, func(t *jwt.Token) (any, error) {
|
||||
return tokenSecret, nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return uuid.Nil, err
|
||||
}
|
||||
|
||||
userId, err := token.Claims.GetSubject()
|
||||
|
||||
if err != nil {
|
||||
return uuid.Nil, err
|
||||
}
|
||||
|
||||
validUserId, err := uuid.Parse(userId)
|
||||
|
||||
if err != nil {
|
||||
return uuid.Nil, err
|
||||
}
|
||||
|
||||
return validUserId, nil
|
||||
}
|
||||
|
||||
func GetBearerToken(headers http.Header) (string, error) {
|
||||
authorizationHeader := headers.Get("Authorization")
|
||||
|
||||
if strings.TrimSpace(authorizationHeader) == "" {
|
||||
return "", errors.New("Missing Authorization header")
|
||||
}
|
||||
|
||||
token := strings.TrimPrefix(authorizationHeader, "Bearer ")
|
||||
|
||||
return token, nil
|
||||
}
|
||||
|
||||
func MakeRefreshToken() string {
|
||||
bytes := make([]byte, 32)
|
||||
rand.Read(bytes)
|
||||
return hex.EncodeToString(bytes)
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package auth_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/StevanFreeborn/chirpy/internal/auth"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func TestJwtCreationAndValidation(t *testing.T) {
|
||||
userId := uuid.New()
|
||||
secret := []byte("RTCK2UTcOUkiswdrClC6Z3KmmEq/+QicpD9iRx7J0qY=")
|
||||
|
||||
jwtString, err := auth.MakeJWT(userId, secret, time.Hour)
|
||||
validatedUserId, err := auth.ValidateJWT(jwtString, secret)
|
||||
|
||||
if userId != validatedUserId {
|
||||
t.Fatalf("received %s expected %s: %v", validatedUserId, userId, err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user