Skip to content
This repository has been archived by the owner on Aug 16, 2022. It is now read-only.

refactor: signup #65

Merged
merged 25 commits into from
Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions internal/usecase/interactor/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,8 @@ func (i *User) Fetch(ctx context.Context, ids []id.UserID, operator *usecase.Ope
func (i *User) Signup(ctx context.Context, inp interfaces.SignupParam) (u *user.User, _ *user.Team, err error) {
var team *user.Team
var tx repo.Tx
var encodedPass []byte
var email, name string
var auth user.Auth
var auth *user.Auth

if inp.Secret != nil && inp.Sub != nil {
// Auth0
Expand Down Expand Up @@ -139,7 +138,7 @@ func (i *User) Signup(ctx context.Context, inp interfaces.SignupParam) (u *user.
}
name = ui.Name
email = ui.Email
auth = user.AuthFromAuth0Sub(*inp.Sub)
auth = user.AuthFromAuth0Sub(*inp.Sub).Ref()

} else if inp.Name != nil && inp.Email != nil && inp.Password != nil {
if *inp.Name == "" {
Expand Down Expand Up @@ -170,14 +169,12 @@ func (i *User) Signup(ctx context.Context, inp interfaces.SignupParam) (u *user.
if existed != nil {
return nil, nil, errors.New("existed user email")
}
encodedPass, err = user.EncodePassword(*inp.Password)

if err != nil {
return nil, nil, err
}
name = *inp.Name
email = *inp.Email
auth = user.GenReearthSub(8)
}

// Check if team already exists
Expand All @@ -196,7 +193,7 @@ func (i *User) Signup(ctx context.Context, inp interfaces.SignupParam) (u *user.
Email: name,
Name: email,
Sub: auth,
Password: encodedPass,
Password: *inp.Password,
Lang: inp.Lang,
Theme: inp.Theme,
UserID: inp.UserID,
Expand Down
17 changes: 7 additions & 10 deletions pkg/user/auth.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package user

import (
"math/rand"
"strings"
)

Expand All @@ -22,16 +21,14 @@ func (a Auth) IsAuth0() bool {
return a.Provider == "auth0"
}

func GenReearthSub(length int) Auth {
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")

b := make([]rune, length)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
func (a Auth) Ref() *Auth {
a2 := a
return &a2
}

return Auth{
func GenReearthSub(userID string) *Auth {
return &Auth{
Provider: "reearth",
Sub: string(b),
Sub: userID,
}
}
11 changes: 9 additions & 2 deletions pkg/user/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

type Builder struct {
u *User
p *string
}

func New() *Builder {
Expand All @@ -17,6 +18,12 @@ func (b *Builder) Build() (*User, error) {
if id.ID(b.u.id).IsNil() {
return nil, id.ErrInvalidID
}
if b.p != nil {
err := b.u.SetPassword(*b.p)
if err != nil {
mimoham24 marked this conversation as resolved.
Show resolved Hide resolved
return nil, ErrEncodingPass
}
}
return b.u, nil
}

Expand Down Expand Up @@ -48,8 +55,8 @@ func (b *Builder) Email(email string) *Builder {
return b
}

func (b *Builder) Password(password []byte) *Builder {
b.u.password = password
func (b *Builder) Password(password string) *Builder {
b.p = &password
return b
}

Expand Down
9 changes: 6 additions & 3 deletions pkg/user/initializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
type InitParams struct {
Email string
Name string
Sub Auth
Password []byte
Sub *Auth
Password string
rot1024 marked this conversation as resolved.
Show resolved Hide resolved
Lang *language.Tag
Theme *Theme
UserID *id.UserID
Expand All @@ -30,12 +30,15 @@ func Init(p InitParams) (*User, *Team, error) {
t := ThemeDefault
p.Theme = &t
}
if p.Sub == nil {
p.Sub = GenReearthSub(p.UserID.String())
}

u, err := New().
ID(*p.UserID).
Name(p.Name).
Email(p.Email).
Auths([]Auth{p.Sub}).
Auths([]Auth{*p.Sub}).
Lang(*p.Lang).
Password(p.Password).
Theme(*p.Theme).
Expand Down
2 changes: 1 addition & 1 deletion pkg/user/initializer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func TestInit(t *testing.T) {
u, t, err := Init(InitParams{
Email: tc.Email,
Name: tc.Username,
Sub: tc.Sub,
Sub: &tc.Sub,
UserID: tc.UID,
TeamID: tc.TID,
})
Expand Down
35 changes: 34 additions & 1 deletion pkg/user/user.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package user

import (
"errors"

"github.com/matthewhartstonge/argon2"
mimoham24 marked this conversation as resolved.
Show resolved Hide resolved
"github.com/reearth/reearth-backend/pkg/id"
"golang.org/x/text/language"
)

var (
ErrEncodingPass = errors.New("error encoding password")
mimoham24 marked this conversation as resolved.
Show resolved Hide resolved
)

type User struct {
id id.UserID
name string
Expand Down Expand Up @@ -121,11 +127,38 @@ func (u *User) ClearAuths() {
u.auths = []Auth{}
}

func EncodePassword(pass string) ([]byte, error) {
func (u *User) SetPassword(pass string) error {
p, err := encodePassword(pass)
if err != nil {
return err
}
u.password = p
return nil
mimoham24 marked this conversation as resolved.
Show resolved Hide resolved
}

func (u *User) MatchPassword(pass string) (bool, error) {
return verifyPassword(pass, u.password)
mimoham24 marked this conversation as resolved.
Show resolved Hide resolved
mimoham24 marked this conversation as resolved.
Show resolved Hide resolved
}

func encodePassword(pass string) ([]byte, error) {
argon := argon2.DefaultConfig()
encodedPass, err := argon.HashEncoded([]byte(pass))
if err != nil {
return nil, err
}
return encodedPass, nil
}

func verifyPassword(toVerify string, encoded []byte) (bool, error) {
raw, err := argon2.Decode(encoded)
if err != nil {
return false, err
}

ok, err := raw.Verify([]byte(toVerify))
if err != nil {
return false, err
}

return ok, nil
}