Files
archie/internal/infrastructure/staterpc/account_security.go
T

87 lines
3.1 KiB
Go

package staterpc
import (
"context"
"time"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
pb "github.com/samcharles93/archie-core/internal/contracts/state/v1"
"github.com/samcharles93/archie-core/internal/domain/identity"
)
func (s *server) accountSecurity() (identity.AccountSecurity, error) {
store, ok := s.deps.Accounts.(identity.AccountSecurity)
if !ok {
return nil, status.Error(codes.Unavailable, "account security unavailable")
}
return store, nil
}
func (s *accountServer) OpenAccountSession(ctx context.Context, r *pb.OpenAccountSessionRequest) (*pb.OpenAccountSessionResponse, error) {
store, err := s.accountSecurity()
if err != nil {
return nil, err
}
value, err := store.OpenAccountSession(ctx, timeValue(r.ExpiresAt))
if err != nil {
return nil, s.accountStatus(ctx, err)
}
return &pb.OpenAccountSessionResponse{Identity: identityProto(value.Identity), Token: value.Token, ExpiresAt: timestamp(value.Expires)}, nil
}
func (s *accountServer) RequireFreshAccountSession(ctx context.Context, r *pb.RequireFreshAccountSessionRequest) (*pb.RequireFreshAccountSessionResponse, error) {
store, err := s.accountSecurity()
if err != nil {
return nil, err
}
return &pb.RequireFreshAccountSessionResponse{}, s.accountStatus(ctx, store.RequireFreshAccountSession(ctx, r.Token))
}
func (s *accountServer) CreateAccountRecovery(ctx context.Context, _ *pb.CreateAccountRecoveryRequest) (*pb.CreateAccountRecoveryResponse, error) {
store, err := s.accountSecurity()
if err != nil {
return nil, err
}
code, err := store.CreateAccountRecovery(ctx)
if err != nil {
return nil, s.accountStatus(ctx, err)
}
return &pb.CreateAccountRecoveryResponse{Code: code}, nil
}
func (s *accountServer) RecoverAccount(ctx context.Context, r *pb.RecoverAccountRequest) (*pb.RecoverAccountResponse, error) {
store, err := s.accountSecurity()
if err != nil {
return nil, err
}
return &pb.RecoverAccountResponse{}, s.accountStatus(ctx, store.RecoverAccount(ctx, r.Email, r.Code, r.Password))
}
func (c *Client) OpenAccountSession(ctx context.Context, expires time.Time) (identity.AccountSession, error) {
r, err := c.account.OpenAccountSession(ctx, &pb.OpenAccountSessionRequest{ExpiresAt: timestamp(expires)})
if err != nil {
return identity.AccountSession{}, accountClientError(err)
}
return identity.AccountSession{Identity: identityValue(r.Identity), Token: r.Token, Expires: timeValue(r.ExpiresAt)}, nil
}
func (c *Client) RequireFreshAccountSession(ctx context.Context, token string) error {
_, err := c.account.RequireFreshAccountSession(ctx, &pb.RequireFreshAccountSessionRequest{Token: token})
return accountClientError(err)
}
func (c *Client) CreateAccountRecovery(ctx context.Context) (string, error) {
r, err := c.account.CreateAccountRecovery(ctx, &pb.CreateAccountRecoveryRequest{})
if err != nil {
return "", accountClientError(err)
}
return r.Code, nil
}
func (c *Client) RecoverAccount(ctx context.Context, email, code, password string) error {
_, err := c.account.RecoverAccount(ctx, &pb.RecoverAccountRequest{Email: email, Code: code, Password: password})
return accountClientError(err)
}