softplayer-backend/api/v1/accounts.go

118 lines
3.2 KiB
Go

package v1
import (
"context"
"git.badhouseplants.net/softplayer/softplayer-backend/internal/controllers"
"git.badhouseplants.net/softplayer/softplayer-backend/internal/helpers/email"
"git.badhouseplants.net/softplayer/softplayer-go-proto/pkg/accounts"
"github.com/golang/protobuf/ptypes/empty"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"google.golang.org/protobuf/types/known/emptypb"
ctrl "sigs.k8s.io/controller-runtime"
)
func NewAccountRPCImpl(contoller ctrl.Manager, hashCost int16, email *email.EmailConf, devMode bool) *AccountsServer {
return &AccountsServer{
Controller: contoller,
Params: &controllers.AccountParams{
HashCost: hashCost,
},
emailConfig: *email,
devMode: devMode,
}
}
type AccountsServer struct {
accounts.UnimplementedAccountsServer
Controller ctrl.Manager
Params *controllers.AccountParams
emailConfig email.EmailConf
// When dev mode is enabled, email won't be sent, instead the code will be returned in metadata
devMode bool
}
func (a *AccountsServer) SignUp(ctx context.Context, in *accounts.AccountWithPassword) (*accounts.AccountFullWithToken, error) {
data := populateData(in.Data.GetName(), in.Password.GetPassword(), in.Data.GetEmail())
acc := populateAccount(data, a.Controller)
if err := acc.Create(ctx); err != nil {
return nil, err
}
return &accounts.AccountFullWithToken{
Id: &accounts.AccountId{
Id: acc.Data.UUID,
},
Data: &accounts.AccountData{
Name: acc.Data.Username,
Email: acc.Data.Email,
},
Token: acc.Token,
}, nil
}
func (a *AccountsServer) SignIn(ctx context.Context, in *accounts.AccountWithPassword) (*accounts.AccountFullWithToken, error) {
data := populateData(in.Data.GetName(), in.Password.GetPassword(), in.Data.GetEmail())
acc := populateAccount(data, a.Controller)
if err := acc.Login(ctx); err != nil {
return nil, err
}
return &accounts.AccountFullWithToken{
Id: &accounts.AccountId{
Id: acc.Data.UUID,
},
Data: &accounts.AccountData{
Name: acc.Data.Username,
Email: acc.Data.Email,
},
Token: acc.Token,
}, nil
}
func (a *AccountsServer) ResetPassword(ctx context.Context, in *accounts.AccountData) (*empty.Empty, error) {
data := populateData(in.GetName(), "", in.GetEmail())
acc := populateAccount(data, a.Controller)
code, err := acc.ResetPassword(ctx, a.emailConfig)
if err != nil {
return nil, err
}
if a.devMode {
header := metadata.Pairs("code", code)
if err := grpc.SendHeader(ctx, header); err != nil {
return nil, err
}
}
return &emptypb.Empty{}, nil
}
func (acc *AccountsServer) NewPassword(ctx context.Context, in *accounts.AccountWithPasswordAndCode) (*empty.Empty, error) {
data := populateData(in.Data.GetName(), in.Password.GetPassword(), in.Data.GetEmail())
account := populateAccount(data, acc.Controller)
if err := account.NewPassword(ctx, in.GetCode()); err != nil {
return nil, err
}
return &emptypb.Empty{}, nil
}
func populateData(username, password, email string) *controllers.AccountData {
return &controllers.AccountData{
Username: username,
Password: password,
Email: email,
}
}
func populateAccount(data *controllers.AccountData, controller ctrl.Manager) *controllers.Account {
return &controllers.Account{
Controller: controller,
Data: data,
}
}