WIP: Something is going on
This commit is contained in:
parent
58c1b91916
commit
782e762019
@ -4,36 +4,30 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
|
|
||||||
"git.badhouseplants.net/softplayer/softplayer-backend/internal/controllers"
|
"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"
|
"git.badhouseplants.net/softplayer/softplayer-go-proto/pkg/accounts"
|
||||||
|
|
||||||
// "google.golang.org/protobuf/types/known/emptypb"
|
|
||||||
ctrl "sigs.k8s.io/controller-runtime"
|
ctrl "sigs.k8s.io/controller-runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewAccountRPCImpl(contoller ctrl.Manager, emailConfig email.EmailConf) *AccountsServer {
|
func NewAccountRPCImpl(contoller ctrl.Manager, hashCost int16) *AccountsServer {
|
||||||
return &AccountsServer{
|
return &AccountsServer{
|
||||||
Controller: contoller,
|
Controller: contoller,
|
||||||
EmailConfig: emailConfig,
|
Params: &controllers.AccountParams{
|
||||||
|
HashCost: hashCost,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type AccountsServer struct {
|
type AccountsServer struct {
|
||||||
accounts.UnimplementedAccountsServer
|
accounts.UnimplementedAccountsServer
|
||||||
EmailConfig email.EmailConf
|
|
||||||
Controller ctrl.Manager
|
Controller ctrl.Manager
|
||||||
|
Params *controllers.AccountParams
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *AccountsServer) SignUp(ctx context.Context, in *accounts.AccountWithPassword) (*accounts.AccountFullWithToken, error) {
|
func (a *AccountsServer) SignUp(ctx context.Context, in *accounts.AccountWithPassword) (*accounts.AccountFullWithToken, error) {
|
||||||
data := controllers.AccountData{
|
data := populateData(in.Data.GetName(), in.Password.GetPassword(), in.Data.GetEmail())
|
||||||
Username: in.Data.GetName(),
|
acc := populateAccount(data, a.Controller)
|
||||||
Password: in.Password.GetPassword(),
|
|
||||||
Email: in.Data.GetEmail(),
|
|
||||||
}
|
|
||||||
acc := controllers.Account{
|
|
||||||
Controller: a.Controller,
|
|
||||||
Data: &data,
|
|
||||||
}
|
|
||||||
if err := acc.Create(ctx); err != nil {
|
if err := acc.Create(ctx); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -51,18 +45,13 @@ func (a *AccountsServer) SignUp(ctx context.Context, in *accounts.AccountWithPas
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *AccountsServer) SignIn(ctx context.Context, in *accounts.AccountWithPassword) (*accounts.AccountFullWithToken, error) {
|
func (a *AccountsServer) SignIn(ctx context.Context, in *accounts.AccountWithPassword) (*accounts.AccountFullWithToken, error) {
|
||||||
data := controllers.AccountData{
|
data := populateData(in.Data.GetName(), in.Password.GetPassword(), in.Data.GetEmail())
|
||||||
Username: in.Data.GetName(),
|
acc := populateAccount(data, a.Controller)
|
||||||
Password: in.Password.GetPassword(),
|
|
||||||
Email: in.Data.GetEmail(),
|
|
||||||
}
|
|
||||||
acc := controllers.Account{
|
|
||||||
Controller: a.Controller,
|
|
||||||
Data: &data,
|
|
||||||
}
|
|
||||||
if err := acc.Login(ctx); err != nil {
|
if err := acc.Login(ctx); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &accounts.AccountFullWithToken{
|
return &accounts.AccountFullWithToken{
|
||||||
Id: &accounts.AccountId{
|
Id: &accounts.AccountId{
|
||||||
Id: acc.Data.UUID,
|
Id: acc.Data.UUID,
|
||||||
@ -75,10 +64,18 @@ func (a *AccountsServer) SignIn(ctx context.Context, in *accounts.AccountWithPas
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
//func (a *AccountsServer) ValidateEmail(ctx context.Context, in *accounts.AccountDataWithEmailCode) (*emptypb.Empty, error) {
|
func populateData(username, password, email string) *controllers.AccountData {
|
||||||
// data := controllers.AccountData {
|
return &controllers.AccountData{
|
||||||
// Username: in.Data.GetName(),
|
Username: username,
|
||||||
// Email: in.Data.GetEmail(),
|
Password: password,
|
||||||
// }
|
Email: email,
|
||||||
// acc := controllers.Account {}
|
}
|
||||||
//}
|
}
|
||||||
|
|
||||||
|
func populateAccount(data *controllers.AccountData, controller ctrl.Manager) *controllers.Account {
|
||||||
|
return &controllers.Account{
|
||||||
|
Controller: controller,
|
||||||
|
Data: data,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -7,8 +7,8 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"git.badhouseplants.net/softplayer/softplayer-backend/internal/helpers/hash"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"golang.org/x/crypto/bcrypt"
|
|
||||||
corev1 "k8s.io/api/core/v1"
|
corev1 "k8s.io/api/core/v1"
|
||||||
rbacv1 "k8s.io/api/rbac/v1"
|
rbacv1 "k8s.io/api/rbac/v1"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
@ -28,6 +28,7 @@ type Account struct {
|
|||||||
type AccountParams struct {
|
type AccountParams struct {
|
||||||
HashCost int16
|
HashCost int16
|
||||||
}
|
}
|
||||||
|
|
||||||
type AccountData struct {
|
type AccountData struct {
|
||||||
Username string
|
Username string
|
||||||
Password string
|
Password string
|
||||||
@ -35,17 +36,7 @@ type AccountData struct {
|
|||||||
UUID string
|
UUID string
|
||||||
}
|
}
|
||||||
|
|
||||||
func HashPassword(password string) (string, error) {
|
func waitUntilCreated(ctx context.Context, client client.Client, obj client.Object, attemps int, timeout time.Duration) error {
|
||||||
bytes, err := bcrypt.GenerateFromPassword([]byte(password), 1)
|
|
||||||
return string(bytes), err
|
|
||||||
}
|
|
||||||
|
|
||||||
func CheckPasswordHash(password, hash string) bool {
|
|
||||||
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
|
|
||||||
return err == nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func waitUntilCreated(ctx context.Context, client client.Client ,obj client.Object, attemps int, timeout time.Duration) error {
|
|
||||||
log.Printf("Waiting %d", attemps)
|
log.Printf("Waiting %d", attemps)
|
||||||
if err := client.Get(ctx, types.NamespacedName{
|
if err := client.Get(ctx, types.NamespacedName{
|
||||||
Namespace: obj.GetNamespace(),
|
Namespace: obj.GetNamespace(),
|
||||||
@ -53,7 +44,7 @@ func waitUntilCreated(ctx context.Context, client client.Client ,obj client.Obje
|
|||||||
}, obj); err != nil {
|
}, obj); err != nil {
|
||||||
if attemps > 0 {
|
if attemps > 0 {
|
||||||
time.Sleep(timeout)
|
time.Sleep(timeout)
|
||||||
waitUntilCreated(ctx, client, obj, attemps - 1, timeout)
|
waitUntilCreated(ctx, client, obj, attemps-1, timeout)
|
||||||
} else {
|
} else {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -66,7 +57,7 @@ func (acc *Account) Create(ctx context.Context) error {
|
|||||||
|
|
||||||
acc.Data.UUID = uuid.New().String()
|
acc.Data.UUID = uuid.New().String()
|
||||||
log.Println(acc.Data.UUID)
|
log.Println(acc.Data.UUID)
|
||||||
passwordHash, err := HashPassword(acc.Data.Password)
|
passwordHash, err := hash.HashPassword(acc.Data.Password, int(acc.Params.HashCost))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -81,11 +72,10 @@ func (acc *Account) Create(ctx context.Context) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := waitUntilCreated(ctx, client, &namespace, 10, time.Millisecond * 50); err != nil {
|
if err := waitUntilCreated(ctx, client, &namespace, 10, time.Millisecond*50); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if err := client.Get(ctx, types.NamespacedName{
|
if err := client.Get(ctx, types.NamespacedName{
|
||||||
Name: acc.Data.UUID,
|
Name: acc.Data.UUID,
|
||||||
}, &namespace); err != nil {
|
}, &namespace); err != nil {
|
||||||
@ -132,7 +122,6 @@ func (acc *Account) Create(ctx context.Context) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
sa := &corev1.ServiceAccount{
|
sa := &corev1.ServiceAccount{
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
Name: acc.Data.UUID,
|
Name: acc.Data.UUID,
|
||||||
@ -190,7 +179,7 @@ func (acc *Account) Create(ctx context.Context) error {
|
|||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := waitUntilCreated(ctx, client, saSec, 10, time.Millisecond * 50); err != nil {
|
if err := waitUntilCreated(ctx, client, saSec, 10, time.Millisecond*50); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -204,7 +193,7 @@ func (acc *Account) Create(ctx context.Context) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (acc *Account) Login (ctx context.Context) error {
|
func (acc *Account) Login(ctx context.Context) error {
|
||||||
client := acc.Controller.GetClient()
|
client := acc.Controller.GetClient()
|
||||||
sec := &corev1.Secret{}
|
sec := &corev1.Secret{}
|
||||||
if err := client.Get(ctx, types.NamespacedName{
|
if err := client.Get(ctx, types.NamespacedName{
|
||||||
@ -213,7 +202,7 @@ func (acc *Account) Login (ctx context.Context) error {
|
|||||||
}, sec); err != nil {
|
}, sec); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if !CheckPasswordHash(acc.Data.Password, string(sec.Data["password"])){
|
if !hash.CheckPasswordHash(acc.Data.Password, string(sec.Data["password"])) {
|
||||||
err := errors.New("wrong password")
|
err := errors.New("wrong password")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -231,13 +220,13 @@ func (acc *Account) Login (ctx context.Context) error {
|
|||||||
}
|
}
|
||||||
var err error
|
var err error
|
||||||
acc.Token, err = acc.getToken(ctx, saSec)
|
acc.Token, err = acc.getToken(ctx, saSec)
|
||||||
if err != nil{
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (acc *Account) getToken (ctx context.Context, saSec *corev1.Secret) (string, error) {
|
func (acc *Account) getToken(ctx context.Context, saSec *corev1.Secret) (string, error) {
|
||||||
client := acc.Controller.GetClient()
|
client := acc.Controller.GetClient()
|
||||||
if err := client.Get(ctx, types.NamespacedName{
|
if err := client.Get(ctx, types.NamespacedName{
|
||||||
Namespace: acc.Data.UUID,
|
Namespace: acc.Data.UUID,
|
||||||
@ -247,4 +236,3 @@ func (acc *Account) getToken (ctx context.Context, saSec *corev1.Secret) (string
|
|||||||
}
|
}
|
||||||
return string(saSec.Data["token"]), nil
|
return string(saSec.Data["token"]), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
13
internal/controllers/email.go.tmp
Normal file
13
internal/controllers/email.go.tmp
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
# package controllers
|
||||||
|
|
||||||
|
import "context"
|
||||||
|
|
||||||
|
type EmailSvc struct {}
|
||||||
|
|
||||||
|
type EmailData strict {
|
||||||
|
UserID string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (svc *EmailSvc) SendVerification(ctx context.Context) {
|
||||||
|
|
||||||
|
}
|
@ -11,7 +11,7 @@ type EmailConf struct {
|
|||||||
SmtpPort string
|
SmtpPort string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *EmailConf) SendEmail (to string, message string )error {
|
func (e *EmailConf) SendEmail(to string, message string) error {
|
||||||
messageByte := []byte(message)
|
messageByte := []byte(message)
|
||||||
auth := smtp.PlainAuth("", e.From, e.Password, e.SmtpHost)
|
auth := smtp.PlainAuth("", e.From, e.Password, e.SmtpHost)
|
||||||
|
|
||||||
|
@ -11,4 +11,3 @@ func CheckPasswordHash(password, hash string) bool {
|
|||||||
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
|
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
|
||||||
return err == nil
|
return err == nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
3
main.go
3
main.go
@ -42,14 +42,13 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func server(params Serve) error {
|
func server(params Serve) error {
|
||||||
controller, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{})
|
controller, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
go func(){
|
go func() {
|
||||||
controller.Start(context.Background())
|
controller.Start(context.Background())
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user