-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* progress with jwt token generation * progress with Login service; interface segregation refactor, check if everything behaves correctly; adding tests * refactor with interface segregation and interface composition done properly * progress with ed25519 key generation; still fails at unmarshalling * interface segregation and composition working; testing working; login and jwt token generation working; pending auth middleware and public rsa pkcs8 key workflows
- Loading branch information
Showing
31 changed files
with
640 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,3 +22,6 @@ go.work | |
|
||
# Environment variables | ||
.env | ||
|
||
# Config | ||
config.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package dtos | ||
|
||
import ( | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
type LoginDTO struct { | ||
Email string | ||
Password string | ||
Token *string | ||
} | ||
|
||
// Validator | ||
func (dto *LoginDTO) Validate() bool { | ||
rfc2822EmailPattern := `[a-z0-9!#$%&'*+/=?^_{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?` | ||
trimmedEmail := strings.TrimSpace((*dto).Email) | ||
trimmedPassword := strings.TrimSpace((*dto).Password) | ||
|
||
isOk, _ := regexp.MatchString(rfc2822EmailPattern, trimmedEmail) | ||
|
||
// TODO: check password for certain special characters | ||
if !isOk || len(trimmedPassword) < 8 || len(trimmedPassword) > 16 { | ||
return false | ||
} | ||
|
||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package dtos | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestLoginValidateShouldReturnFalseIfEmailIsNil(t *testing.T) { | ||
// Arrange | ||
loginDTO := LoginDTO{ | ||
Password: "aaaaaaaa", | ||
} | ||
|
||
// Act | ||
result := loginDTO.Validate() | ||
|
||
// Assert | ||
assert.False(t, result) | ||
} | ||
|
||
func TestLoginValidateShouldReturnFalseIfEmailIsEmpty(t *testing.T) { | ||
// Arrange | ||
loginDTO := LoginDTO{ | ||
Email: "", | ||
Password: "aaaaaaaa", | ||
} | ||
|
||
// Act | ||
result := loginDTO.Validate() | ||
|
||
// Assert | ||
assert.False(t, result) | ||
} | ||
|
||
func TestLoginValidateShouldReturnFalseIfEmailIsMalformed(t *testing.T) { | ||
// Arrange | ||
loginDTO := LoginDTO{ | ||
Email: "[email protected]", | ||
Password: "aaaaaaaa", | ||
} | ||
|
||
// Act | ||
result := loginDTO.Validate() | ||
|
||
// Assert | ||
assert.False(t, result) | ||
} | ||
|
||
func TestLoginValidateShouldReturnFalseIfPasswordIsNil(t *testing.T) { | ||
// Arrange | ||
loginDTO := LoginDTO{ | ||
Email: "[email protected]", | ||
} | ||
|
||
// Act | ||
result := loginDTO.Validate() | ||
|
||
// Assert | ||
assert.False(t, result) | ||
} | ||
|
||
func TestLoginValidateShouldReturnFalseIfPasswordIsEmpty(t *testing.T) { | ||
// Arrange | ||
loginDTO := LoginDTO{ | ||
Email: "[email protected]", | ||
Password: "", | ||
} | ||
|
||
// Act | ||
result := loginDTO.Validate() | ||
|
||
// Assert | ||
assert.False(t, result) | ||
} | ||
|
||
func TestLoginValidateShouldReturnFalseIfPasswordIsShorterThanEightCharacters(t *testing.T) { | ||
// Arrange | ||
loginDTO := LoginDTO{ | ||
Email: "[email protected]", | ||
Password: "aaaa", | ||
} | ||
|
||
// Act | ||
result := loginDTO.Validate() | ||
|
||
// Assert | ||
assert.False(t, result) | ||
} | ||
|
||
func TestLoginValidateShouldReturnFalseIfPasswordIsLongerThanSixteenCharacters(t *testing.T) { | ||
// Arrange | ||
loginDTO := LoginDTO{ | ||
Email: "[email protected]", | ||
Password: "aaaaaaaaaaaaaaaaa", | ||
} | ||
|
||
// Act | ||
result := loginDTO.Validate() | ||
|
||
// Assert | ||
assert.False(t, result) | ||
} | ||
|
||
func TestLoginValidateShouldReturnTrueIfAllRequiredValuesArePresent(t *testing.T) { | ||
// Arrange | ||
loginDTO := LoginDTO{ | ||
Email: "[email protected]", | ||
Password: "aaaaaaaaaaa", | ||
} | ||
|
||
// Act | ||
result := loginDTO.Validate() | ||
|
||
// Assert | ||
assert.True(t, result) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package services | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
"github.com/golang-jwt/jwt/v5" | ||
"todoapp.com/application/dtos" | ||
"todoapp.com/application/errors" | ||
"todoapp.com/domain/interfaces" | ||
"todoapp.com/domain/models" | ||
) | ||
|
||
type LoginServiceImpl struct { | ||
userRepository interfaces.UsersRepositoryEmail | ||
config *models.Config | ||
} | ||
|
||
func NewLoginService(userRepository interfaces.UsersRepositoryEmail, config *models.Config) *LoginServiceImpl { | ||
return &LoginServiceImpl{ | ||
userRepository: userRepository, | ||
config: config, | ||
} | ||
} | ||
|
||
func (ls *LoginServiceImpl) Login(context context.Context, login *dtos.LoginDTO) error { | ||
dbUser := ls.userRepository.GetByEmail(context, &login.Email) | ||
|
||
if dbUser.Email == "" { | ||
return errors.Errors{}.ItemNotFoundError("User") | ||
} | ||
if dbUser.Password != (*login).Password { | ||
return errors.Errors{}.IncorrectPasswordError() | ||
} | ||
|
||
return ls.GenerateToken(login) | ||
} | ||
|
||
func (ls *LoginServiceImpl) GenerateToken(login *dtos.LoginDTO) error { | ||
fmt.Println(ls.config.PrivateKeyPath) | ||
fmt.Println("Llego aquí") | ||
encodedKey, error := os.ReadFile(ls.config.PrivateKeyPath) | ||
if error != nil { | ||
return errors.Errors{}.CannotReadFileError("OPENSSH private key") | ||
} | ||
|
||
privateKey, error := jwt.ParseRSAPrivateKeyFromPEM(encodedKey) | ||
if error != nil { | ||
return errors.Errors{}.ItemNotParsedError("OPENSSH private key") | ||
} | ||
|
||
claims := jwt.MapClaims{ | ||
"email": login.Email, | ||
"expiration": time.Now().UTC().Add(time.Hour * 24).Unix(), | ||
} | ||
token := jwt.NewWithClaims(jwt.SigningMethodRS256, claims) | ||
|
||
signedToken, error := token.SignedString(privateKey) | ||
login.Token = &signedToken | ||
|
||
return error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package services | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"todoapp.com/application/dtos" | ||
"todoapp.com/domain/models" | ||
) | ||
|
||
func TestLoginGenerateTokenShouldReturnTokenAndNoError(t *testing.T) { | ||
// Arrange | ||
testLoginDTO := &dtos.LoginDTO{ | ||
Email: "[email protected]", | ||
Password: "aaaaaaaa", | ||
} | ||
testConfig := &models.Config{ | ||
PrivateKeyPath: "../../testing/id_rsa", | ||
PublicKeyPath: "../../testing/id_rsa.pub", | ||
} | ||
MockedUsersRepository := new(MockedUsersRepository) | ||
|
||
// Act | ||
testLoginService := NewLoginService(MockedUsersRepository, testConfig) | ||
error := testLoginService.GenerateToken(testLoginDTO) | ||
|
||
// Assert | ||
assert.Nil(t, error) | ||
assert.NotNil(t, testLoginDTO.Token) | ||
assert.NotEmpty(t, testLoginDTO.Token) | ||
} | ||
|
||
func TestLoginLoginShouldReturnErrorIfGivenPasswordDoesNotMatch(t *testing.T) { | ||
// Arrange | ||
id1 := uint(1) | ||
testUser := models.User{ | ||
ID: &id1, | ||
Name: "test 1", | ||
Email: "[email protected]", | ||
Password: "aaaaaaaa", | ||
} | ||
testLoginDTO := &dtos.LoginDTO{ | ||
Email: "[email protected]", | ||
Password: "bbbbb", | ||
} | ||
testContext := context.Background() | ||
MockedUserRepository := new(MockedUsersRepository) | ||
MockedUserRepository.mock.On("GetByEmail", testContext, &testLoginDTO.Email).Return(testUser) | ||
testConfig := &models.Config{ | ||
PrivateKeyPath: "../../testing/id_rsa", | ||
PublicKeyPath: "../../testing/id_rsa.pub", | ||
} | ||
|
||
// Act | ||
testLoginService := NewLoginService(MockedUserRepository, testConfig) | ||
error := testLoginService.Login(testContext, testLoginDTO) | ||
|
||
// Assert | ||
assert.NotNil(t, error) | ||
} | ||
|
||
func TestLoginLoginShouldReturnErrorIfUserIsNotFound(t *testing.T) { | ||
// Arrange | ||
id1 := uint(1) | ||
testUser := models.User{ | ||
ID: &id1, | ||
Name: "test 1", | ||
Email: "[email protected]", | ||
Password: "aaaaaaaa", | ||
} | ||
testLoginDTO := &dtos.LoginDTO{ | ||
Email: "[email protected]", | ||
Password: "aaaaaaaa", | ||
} | ||
testContext := context.Background() | ||
MockedUserRepository := new(MockedUsersRepository) | ||
MockedUserRepository.mock.On("GetByEmail", testContext, &testLoginDTO.Email).Return(testUser) | ||
testConfig := &models.Config{ | ||
PrivateKeyPath: "../../testing/id_rsa", | ||
PublicKeyPath: "../../testing/id_rsa.pub", | ||
} | ||
|
||
// Act | ||
testLoginService := NewLoginService(MockedUserRepository, testConfig) | ||
error := testLoginService.Login(testContext, testLoginDTO) | ||
|
||
// Assert | ||
assert.NotNil(t, error) | ||
} | ||
|
||
func TestLoginLoginShouldNotReturnAnyErrorsIfEverythingIsOk(t *testing.T) { | ||
// Arrange | ||
id1 := uint(1) | ||
testUser := models.User{ | ||
ID: &id1, | ||
Name: "test 1", | ||
Email: "[email protected]", | ||
Password: "aaaaaaaa", | ||
} | ||
testLoginDTO := &dtos.LoginDTO{ | ||
Email: "[email protected]", | ||
Password: "aaaaaaaa", | ||
} | ||
testContext := context.Background() | ||
MockedUserRepository := new(MockedUsersRepository) | ||
MockedUserRepository.mock.On("GetByEmail", testContext, &testLoginDTO.Email).Return(testUser) | ||
testConfig := &models.Config{ | ||
PrivateKeyPath: "../../testing/id_rsa", | ||
PublicKeyPath: "../../testing/id_rsa.pub", | ||
} | ||
|
||
// Act | ||
testLoginService := NewLoginService(MockedUserRepository, testConfig) | ||
error := testLoginService.Login(testContext, testLoginDTO) | ||
|
||
// Assert | ||
assert.Nil(t, error) | ||
} |
Oops, something went wrong.