All checks were successful
ci/woodpecker/push/build Pipeline was successful
Signed-off-by: Nikolai Rodionov <allanger@badhouseplants.net>
99 lines
3.3 KiB
Go
99 lines
3.3 KiB
Go
package v1
|
|
|
|
import (
|
|
"context"
|
|
|
|
"gitea.badhouseplants.net/softplayer/softplayer-backend/internal/authorization"
|
|
"gitea.badhouseplants.net/softplayer/softplayer-backend/internal/controllers"
|
|
accounts "gitea.badhouseplants.net/softplayer/softplayer-go-proto/pkg/accounts/v1"
|
|
"github.com/golang/protobuf/ptypes/empty"
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/metadata"
|
|
"google.golang.org/grpc/status"
|
|
"google.golang.org/protobuf/types/known/emptypb"
|
|
)
|
|
|
|
func NewAccountNoAuthRPCImpl(
|
|
accountsCtrl *controllers.AccountController,
|
|
authorizationCtrl *authorization.AuthController,
|
|
) *AccountsNoAuthServer {
|
|
return &AccountsNoAuthServer{
|
|
accountsCtrl: accountsCtrl,
|
|
authorizationCtrl: authorizationCtrl,
|
|
}
|
|
}
|
|
|
|
type AccountsNoAuthServer struct {
|
|
accounts.UnimplementedAccountsNoAuthServiceServer
|
|
accountsCtrl *controllers.AccountController
|
|
authorizationCtrl *authorization.AuthController
|
|
}
|
|
|
|
func (a *AccountsNoAuthServer) SignIn(ctx context.Context, in *accounts.SignInRequest) (*empty.Empty, error) {
|
|
id, err := a.accountsCtrl.Login(ctx, in.GetEmail(), in.GetPassword())
|
|
if err != nil {
|
|
return nil, status.Error(codes.Aborted, "Couldn't create a user")
|
|
}
|
|
accessToken, _, err := a.authorizationCtrl.GenerateToken(id, authorization.TokenTypeAccess)
|
|
if err != nil {
|
|
return nil, status.Error(codes.Aborted, "Couldn't generate an access token")
|
|
}
|
|
|
|
refreshToken, tokenID, err := a.authorizationCtrl.GenerateToken(id, authorization.TokenTypeRefresh)
|
|
if err != nil {
|
|
return nil, status.Error(codes.Aborted, "Couldn't generate an access token")
|
|
}
|
|
|
|
session := &authorization.Session{UserID: id}
|
|
|
|
if err := a.authorizationCtrl.SaveSession(ctx, tokenID, session); err != nil {
|
|
return nil, status.Error(codes.Aborted, "Couldn't store session")
|
|
}
|
|
header := metadata.New(map[string]string{
|
|
"X-Access-Token": accessToken,
|
|
"X-Refresh-Token": refreshToken,
|
|
})
|
|
if err := grpc.SetHeader(ctx, header); err != nil {
|
|
return nil, status.Error(codes.Aborted, "Couldn't set metadata")
|
|
}
|
|
return &emptypb.Empty{}, nil
|
|
}
|
|
|
|
// Create a new account in Softplayer
|
|
func (a *AccountsNoAuthServer) SignUp(ctx context.Context, in *accounts.SignUpRequest) (*empty.Empty, error) {
|
|
data := &controllers.AccountData{
|
|
Password: in.GetPassword(),
|
|
Email: in.GetEmail(),
|
|
}
|
|
id, err := a.accountsCtrl.Create(ctx, data)
|
|
if err != nil {
|
|
return nil, status.Error(codes.Aborted, "Couldn't create a user")
|
|
}
|
|
|
|
accessToken, _, err := a.authorizationCtrl.GenerateToken(id, authorization.TokenTypeAccess)
|
|
if err != nil {
|
|
return nil, status.Error(codes.Aborted, "Couldn't generate an access token")
|
|
}
|
|
|
|
refreshToken, tokenID, err := a.authorizationCtrl.GenerateToken(id, authorization.TokenTypeRefresh)
|
|
if err != nil {
|
|
return nil, status.Error(codes.Aborted, "Couldn't generate an access token")
|
|
}
|
|
|
|
session := &authorization.Session{UserID: id}
|
|
|
|
if err := a.authorizationCtrl.SaveSession(ctx, tokenID, session); err != nil {
|
|
return nil, status.Error(codes.Aborted, "Couldn't store session")
|
|
}
|
|
|
|
header := metadata.New(map[string]string{
|
|
"X-Access-Token": accessToken,
|
|
"X-Refresh-Token": refreshToken,
|
|
})
|
|
if err := grpc.SetHeader(ctx, header); err != nil {
|
|
return nil, status.Error(codes.Aborted, "Couldn't set metadata")
|
|
}
|
|
return &emptypb.Empty{}, nil
|
|
}
|