87 lines
3.0 KiB
Go
87 lines
3.0 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 *server) 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, accountStatus(err)
|
|
}
|
|
return &pb.OpenAccountSessionResponse{Identity: identityProto(value.Identity), Token: value.Token, ExpiresAt: timestamp(value.Expires)}, nil
|
|
}
|
|
|
|
func (s *server) RequireFreshAccountSession(ctx context.Context, r *pb.RequireFreshAccountSessionRequest) (*pb.RequireFreshAccountSessionResponse, error) {
|
|
store, err := s.accountSecurity()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &pb.RequireFreshAccountSessionResponse{}, accountStatus(store.RequireFreshAccountSession(ctx, r.Token))
|
|
}
|
|
|
|
func (s *server) 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, accountStatus(err)
|
|
}
|
|
return &pb.CreateAccountRecoveryResponse{Code: code}, nil
|
|
}
|
|
|
|
func (s *server) RecoverAccount(ctx context.Context, r *pb.RecoverAccountRequest) (*pb.RecoverAccountResponse, error) {
|
|
store, err := s.accountSecurity()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &pb.RecoverAccountResponse{}, accountStatus(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.client.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.client.RequireFreshAccountSession(ctx, &pb.RequireFreshAccountSessionRequest{Token: token})
|
|
return accountClientError(err)
|
|
}
|
|
|
|
func (c *Client) CreateAccountRecovery(ctx context.Context) (string, error) {
|
|
r, err := c.client.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.client.RecoverAccount(ctx, &pb.RecoverAccountRequest{Email: email, Code: code, Password: password})
|
|
return accountClientError(err)
|
|
}
|