renommage lassistanoque
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/labstack/echo/v5"
|
||||
"trankilou.fr/lassistanoque/backend/internal/domain"
|
||||
)
|
||||
|
||||
type Credentials struct {
|
||||
Method string // "password" ou "oidc"
|
||||
Email string
|
||||
Password string
|
||||
OIDCCode string
|
||||
}
|
||||
|
||||
type Registration struct {
|
||||
Method string `json:"method"` // "password" ou "oidc"
|
||||
Email string `json:"email"`
|
||||
Firstname string `json:"firstname"`
|
||||
Lastname string `json:"lastname"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
type Session struct {
|
||||
User *domain.User
|
||||
AccessToken string
|
||||
RefreshToken string
|
||||
ExpiresAt time.Time
|
||||
}
|
||||
|
||||
const ContextUserIDKey = "userID"
|
||||
const ContextEmailKey = "userEmail"
|
||||
const ContextNameKey = "userName"
|
||||
const ContextAdminKey = "admin"
|
||||
|
||||
// Interface définie ici car c'est un port propre au cas d'usage "auth"
|
||||
type Authenticator interface {
|
||||
Authenticate(ctx context.Context, creds Credentials) (*Session, error)
|
||||
Register(ctx context.Context, registration Registration) (*Session, error)
|
||||
}
|
||||
|
||||
type TokenManager interface {
|
||||
GenerateAccessToken(user *domain.User) (string, time.Time, error)
|
||||
GenerateRefreshToken(userID string) (string, time.Time, error)
|
||||
ParseAndValidate(tokenString string) (*domain.User, error)
|
||||
TokenMiddleware(next echo.HandlerFunc) echo.HandlerFunc
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"trankilou.fr/lassistanoque/backend/internal/domain"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrRegistrationNotAllowed = errors.New("registration not allowed")
|
||||
ErrMethodUnknown = errors.New("auth method unknown")
|
||||
ErrUnauthorized = errors.New("unauthorized")
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
authenticators map[string]Authenticator // "password" -> ..., "oidc" -> ...
|
||||
userRepository domain.UserRepository
|
||||
settingsRepository domain.SettingsRepository
|
||||
}
|
||||
|
||||
func NewService(
|
||||
settingsRepository domain.SettingsRepository,
|
||||
userRepository domain.UserRepository,
|
||||
authenticators map[string]Authenticator,
|
||||
) *Service {
|
||||
return &Service{
|
||||
authenticators: authenticators,
|
||||
userRepository: userRepository,
|
||||
settingsRepository: settingsRepository,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) Login(ctx context.Context, creds Credentials) (*Session, error) {
|
||||
authn, ok := s.authenticators[creds.Method]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("auth method unsupported: %s", creds.Method)
|
||||
}
|
||||
return authn.Authenticate(ctx, creds)
|
||||
}
|
||||
|
||||
func (s *Service) Register(registration Registration) (*Session, error) {
|
||||
settings, err := s.settingsRepository.GetSettings()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !settings.RegisterEnabled {
|
||||
return nil, ErrRegistrationNotAllowed
|
||||
}
|
||||
if authenticator, ok := s.authenticators[registration.Method]; ok {
|
||||
return authenticator.Register(context.Background(), registration)
|
||||
} else {
|
||||
return nil, ErrMethodUnknown
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) Status(session *Session) (*domain.User, error) {
|
||||
return nil, nil
|
||||
}
|
||||
Reference in New Issue
Block a user