feat: authsession service
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
// Package getsession implements the trusted internal read use case for one
|
||||
// device session.
|
||||
package getsession
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"galaxy/authsession/internal/ports"
|
||||
"galaxy/authsession/internal/service/shared"
|
||||
)
|
||||
|
||||
// Input describes one trusted internal get-session request.
|
||||
type Input struct {
|
||||
// DeviceSessionID identifies the session that should be read.
|
||||
DeviceSessionID string
|
||||
}
|
||||
|
||||
// Result describes one trusted internal get-session response.
|
||||
type Result struct {
|
||||
// Session stores the frozen internal read-model DTO.
|
||||
Session shared.Session
|
||||
}
|
||||
|
||||
// Service executes the trusted internal get-session use case against the
|
||||
// configured ports.
|
||||
type Service struct {
|
||||
sessionStore ports.SessionStore
|
||||
}
|
||||
|
||||
// New returns a get-session service wired to sessionStore.
|
||||
func New(sessionStore ports.SessionStore) (*Service, error) {
|
||||
if sessionStore == nil {
|
||||
return nil, fmt.Errorf("getsession: session store must not be nil")
|
||||
}
|
||||
|
||||
return &Service{sessionStore: sessionStore}, nil
|
||||
}
|
||||
|
||||
// Execute loads one source-of-truth session and projects it into the frozen
|
||||
// internal read DTO shape.
|
||||
func (s *Service) Execute(ctx context.Context, input Input) (Result, error) {
|
||||
deviceSessionID, err := shared.ParseDeviceSessionID(input.DeviceSessionID)
|
||||
if err != nil {
|
||||
return Result{}, err
|
||||
}
|
||||
|
||||
record, err := s.sessionStore.Get(ctx, deviceSessionID)
|
||||
if err != nil {
|
||||
switch {
|
||||
case errors.Is(err, ports.ErrNotFound):
|
||||
return Result{}, shared.SessionNotFound()
|
||||
default:
|
||||
return Result{}, shared.ServiceUnavailable(err)
|
||||
}
|
||||
}
|
||||
|
||||
session, err := shared.ToSession(record)
|
||||
if err != nil {
|
||||
return Result{}, shared.InternalError(err)
|
||||
}
|
||||
|
||||
return Result{Session: session}, nil
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package getsession
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/stretchr/testify/require"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"galaxy/authsession/internal/domain/common"
|
||||
"galaxy/authsession/internal/domain/devicesession"
|
||||
"galaxy/authsession/internal/service/shared"
|
||||
"galaxy/authsession/internal/testkit"
|
||||
)
|
||||
|
||||
func TestExecuteReturnsMappedSession(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
store := &testkit.InMemorySessionStore{}
|
||||
record := activeSessionFixture("device-session-1", "user-1", time.Unix(10, 0).UTC())
|
||||
if err := store.Create(context.Background(), record); err != nil {
|
||||
require.Failf(t, "test failed", "Create() returned error: %v", err)
|
||||
}
|
||||
|
||||
service, err := New(store)
|
||||
if err != nil {
|
||||
require.Failf(t, "test failed", "New() returned error: %v", err)
|
||||
}
|
||||
|
||||
result, err := service.Execute(context.Background(), Input{DeviceSessionID: " device-session-1 "})
|
||||
if err != nil {
|
||||
require.Failf(t, "test failed", "Execute() returned error: %v", err)
|
||||
}
|
||||
if result.Session.DeviceSessionID != "device-session-1" {
|
||||
require.Failf(t, "test failed", "Execute().Session.DeviceSessionID = %q, want %q", result.Session.DeviceSessionID, "device-session-1")
|
||||
}
|
||||
if result.Session.CreatedAt != time.Unix(10, 0).UTC().Format(time.RFC3339) {
|
||||
require.Failf(t, "test failed", "Execute().Session.CreatedAt = %q", result.Session.CreatedAt)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExecuteReturnsSessionNotFound(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
service, err := New(&testkit.InMemorySessionStore{})
|
||||
if err != nil {
|
||||
require.Failf(t, "test failed", "New() returned error: %v", err)
|
||||
}
|
||||
|
||||
_, err = service.Execute(context.Background(), Input{DeviceSessionID: "missing"})
|
||||
if shared.CodeOf(err) != shared.ErrorCodeSessionNotFound {
|
||||
require.Failf(t, "test failed", "Execute() error code = %q, want %q", shared.CodeOf(err), shared.ErrorCodeSessionNotFound)
|
||||
}
|
||||
}
|
||||
|
||||
func activeSessionFixture(deviceSessionID string, userID string, createdAt time.Time) devicesession.Session {
|
||||
key, err := common.NewClientPublicKey(make([]byte, 32))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return devicesession.Session{
|
||||
ID: common.DeviceSessionID(deviceSessionID),
|
||||
UserID: common.UserID(userID),
|
||||
ClientPublicKey: key,
|
||||
Status: devicesession.StatusActive,
|
||||
CreatedAt: createdAt,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user