added auth service to go backend

This commit is contained in:
2025-01-03 20:45:39 +00:00
parent 98fb6942fe
commit d0d88de15c
10 changed files with 297 additions and 9 deletions
@@ -0,0 +1,122 @@
package repository
import (
"actatempus_backend/internal/domain/app_error"
"actatempus_backend/internal/domain/repository"
"context"
"errors"
"fmt"
"sync"
"time"
E "github.com/IBM/fp-go/either"
"github.com/golang-jwt/jwt/v5"
)
type InMemoryAuthRepository struct {
secretKey string
sessionCache map[string]string
mu sync.RWMutex
}
func NewInMemoryAuthRepository(secretKey string) repository.AuthRepository {
return &InMemoryAuthRepository{
secretKey: secretKey,
sessionCache: make(map[string]string),
mu: sync.RWMutex{},
}
}
func (r *InMemoryAuthRepository) GenerateToken(ctx context.Context) func(userID string) E.Either[error, string] {
return func(userID string) E.Either[error, string] {
token, err := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"userID": userID,
"exp": time.Now().Add(24 * time.Hour).Unix(),
}).SignedString([]byte(r.secretKey))
// add token to cache
r.mu.Lock()
defer r.mu.Unlock() // unlock after function returns
r.sessionCache[token] = userID
return E.TryCatch(
token,
err,
func(e error) error {
return app_error.NewInternalError(fmt.Errorf("could not generate token: %w", e))
},
)
}
}
func (r *InMemoryAuthRepository) ValidateToken(ctx context.Context) func(token string) E.Either[error, string] {
return func(token string) E.Either[error, string] {
// Separate value and error generation for TryCatch
userID, err := func() (string, error) {
parsedToken, err := jwt.Parse(token, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, errors.New("invalid signing method")
}
return []byte(r.secretKey), nil
})
if err != nil {
return "", err
}
if claims, ok := parsedToken.Claims.(jwt.MapClaims); ok && parsedToken.Valid {
userID, ok := claims["userID"].(string)
if !ok {
return "", app_error.NewAuthError(fmt.Sprintf("invalid token claims"))
}
// Check the cache
r.mu.RLock()
defer r.mu.RUnlock()
if cachedUserID, exists := r.sessionCache[token]; exists && cachedUserID == userID {
return userID, nil
}
// print chache to see if it is working
fmt.Println(r.sessionCache)
return "", app_error.NewAuthError(fmt.Sprintf("token not found in cache"))
}
return "", app_error.NewAuthError(fmt.Sprintf("invalid token"))
}()
// Use TryCatch with separated value and error
return E.TryCatch(
userID,
err,
func(e error) error {
return e
},
)
}
}
func (r *InMemoryAuthRepository) RevokeToken(ctx context.Context) func(token string) E.Either[error, string] {
return func(token string) E.Either[error, string] {
// Separate value and error generation for TryCatch
userID, err := func() (string, error) {
r.mu.Lock()
defer r.mu.Unlock()
if userID, exists := r.sessionCache[token]; exists {
delete(r.sessionCache, token)
return userID, nil
}
return "", app_error.NewAuthError(fmt.Sprintf("token not found"))
}()
// Use TryCatch with separated value and error
return E.TryCatch(
userID,
err,
func(e error) error {
return e
},
)
}
}
@@ -0,0 +1,118 @@
package services
import (
"actatempus_backend/internal/application/services/dto"
"actatempus_backend/internal/domain/app_error"
"actatempus_backend/internal/domain/entities"
"actatempus_backend/internal/domain/repository"
"actatempus_backend/internal/infrastructure/data"
"net/http"
E "github.com/IBM/fp-go/either"
F "github.com/IBM/fp-go/function"
"github.com/gin-gonic/gin"
)
// AuthService handles authentication-related HTTP requests.
type AuthService struct {
authRepository repository.AuthRepository
userRepository repository.UserRepository
}
// NewAuthService creates a new instance of AuthService.
func NewAuthService(authRepo repository.AuthRepository,
userRepository repository.UserRepository) *AuthService {
return &AuthService{
authRepository: authRepo,
userRepository: userRepository,
}
}
// RegisterRoutes registers the authentication-related routes.
func (s *AuthService) RegisterRoutes(router *gin.RouterGroup) {
router.POST("/login", s.Login)
router.POST("/validate", s.Validate)
router.POST("/revoke", s.Revoke)
}
// Login handles user login and token generation.
func (s *AuthService) Login(c *gin.Context) {
var loginRequest dto.LoginRequestDTO
if err := c.ShouldBindJSON(&loginRequest); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
F.Pipe4(
loginRequest.Email,
s.userRepository.FindByEmail(c.Request.Context()),
E.Chain[error](
func(user entities.User) E.Either[error, entities.User] {
hashedPassword := data.GenerateSecureHash(loginRequest.Password)
if user.Password != hashedPassword {
return E.Left[entities.User, error](app_error.NewAuthError("Invalid password"))
}
return E.Right[error](user)
},
),
E.Chain(func(user entities.User) E.Either[error, dto.TokenResponseDTO] {
return F.Pipe2(
user.ID,
s.authRepository.GenerateToken(c.Request.Context()),
E.Map[error](func(token string) dto.TokenResponseDTO {
return dto.TokenResponseDTO{
Token: token,
UserID: user.ID,
}
}),
)
}),
E.Fold(
HandleError(c),
HandleSuccess[dto.TokenResponseDTO](c, http.StatusOK),
),
)
}
// Validate handles token validation.
func (s *AuthService) Validate(c *gin.Context) {
var tokenRequest dto.TokenRequestDTO
if err := c.ShouldBindJSON(&tokenRequest); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
F.Pipe2(
s.authRepository.ValidateToken(c.Request.Context())(tokenRequest.Token),
E.Map[error](func(userID string) dto.TokenResponseDTO {
return dto.TokenResponseDTO{
Token: tokenRequest.Token,
UserID: userID,
}
}),
E.Fold(
HandleError(c),
HandleSuccess[dto.TokenResponseDTO](c, http.StatusOK),
),
)
}
// Revoke handles token revocation.
func (s *AuthService) Revoke(c *gin.Context) {
var tokenRequest dto.TokenRequestDTO
if err := c.ShouldBindJSON(&tokenRequest); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
F.Pipe2(
s.authRepository.RevokeToken(c.Request.Context())(tokenRequest.Token),
E.Map[error](func(userID string) gin.H {
return gin.H{"message": "Token revoked", "user_id": userID}
}),
E.Fold(
HandleError(c),
HandleSuccess[gin.H](c, http.StatusOK),
),
)
}
@@ -0,0 +1,18 @@
package dto
// TokenResponseDTO represents the response for a token generation or validation.
type TokenResponseDTO struct {
Token string `json:"token"`
UserID string `json:"user_id"`
}
// TokenRequestDTO represents a request for operations involving tokens.
type TokenRequestDTO struct {
Token string `json:"token"`
}
// LoginRequestDTO represents the login request.
type LoginRequestDTO struct {
Email string `json:"email"`
Password string `json:"password"`
}