Skip to content

Commit

Permalink
Merge pull request #26 from marcos-dev88/develop
Browse files Browse the repository at this point in the history
develop
  • Loading branch information
marcos-dev88 authored Feb 7, 2022
2 parents 960e420 + 6a74df9 commit 6919222
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 142 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ create-pass: build
@./bin/passgen generate -l=$(l)

strong-password:
docker build -t go-passtrong ./docker
docker build -t go-passtrong -f ./docker/Dockerfile_StrongPass .
docker run go-passtrong:latest
@docker rmi -f go-passtrong >/dev/null 2>&1
@docker rm $$(docker ps -a -f status=exited -q) -f >/dev/null 2>&1
Expand Down
8 changes: 4 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: '3.4'
services:
passgen-app:
build:
dockerfile: ./docker/server/Dockerfile
dockerfile: docker/Dockerfile_Server
context: .
container_name: go-password-generator-app
depends_on:
Expand All @@ -12,7 +12,7 @@ services:
ports:
- "8095:8095"
networks:
net:
password_gen_net:
ipv4_address: 172.20.0.2

mongodb:
Expand All @@ -25,11 +25,11 @@ services:
- "27018:27017"
command: --auth
networks:
net:
password_gen_net:
ipv4_address: 172.20.0.3

networks:
net:
password_gen_net:
driver: bridge
ipam:
config:
Expand Down
File renamed without changes.
File renamed without changes.
24 changes: 10 additions & 14 deletions domain/entity/passwordgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,10 @@ func (p *PasswordGen) Validate(body []byte) error {
"has_special_char": false,
}


if err := json.Unmarshal(body, &expectedBody); err != nil {
return err
}

if expectedBody["length"] == 0 {
return errors.New("expected a json key 'length' with a value: 8, 16, 32 or 64")
}

if reflect.TypeOf(expectedBody["length"]).Kind() != reflect.Float64 {
return errors.New(fmt.Sprintf("expected a float64 type and %v given, use an integer value example: 32", reflect.TypeOf(expectedBody["length"]).Kind()))
}
Expand All @@ -86,7 +81,6 @@ func (p *PasswordGen) Validate(body []byte) error {
return errors.New(fmt.Sprintf("expected a boolean type and %v given, use 'true' or 'false'", reflect.TypeOf(expectedBody["has_letter"]).Kind()))
}


var conditionsSlice []bool
lengthValue := expectedBody["length"].(float64)
hasNumberValue := expectedBody["has_number"].(bool)
Expand All @@ -105,15 +99,17 @@ func (p *PasswordGen) Validate(body []byte) error {
return nil
}

func validateLength (lengthValue float64) error {
if (lengthValue == 8) ||
(lengthValue == 16) ||
(lengthValue == 32) ||
(lengthValue == 64) {
return nil
}else{
return errors.New(fmt.Sprintf("length field must receive one of this numbers: 8, 16, 32, 64 and %v given", lengthValue))
func validateLength(lengthValue float64) error {

if lengthValue < 8 {
return errors.New(fmt.Sprintf("length field must have a value bigger than 8 and %v given", lengthValue))
}

if lengthValue > 64 {
return errors.New(fmt.Sprintf("length field must have a value lower than 64 and %v given", lengthValue))
}

return nil
}

func validateConditions(conditions []bool) error {
Expand Down
135 changes: 67 additions & 68 deletions domain/service/service_passwordgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package service

import (
"errors"
"github.com/marcos-dev88/go-password-generator/domain/entity"
"math/rand"
"sync"
"time"

"github.com/marcos-dev88/go-password-generator/domain/entity"
)

type Service interface {
Expand All @@ -29,11 +30,10 @@ func NewService(passGen entity.PasswordGenerator) *service {
func (s *service) GeneratePasswordByLength(length int, passCharacters []rune) (string, error) {
rand.Seed(time.Now().UnixNano())
randomCharArray := make([]rune, length)
passwordListChannel := make(chan []string)
var passwordList []string

// Channels
inCH, outCH, returnCH, errorCH, errDoneCH := make(chan string, 3), make(chan string, 3), make(chan string, 3), make(chan error), make(chan error)
passwordListChannel := make(chan []string)

wg := sync.WaitGroup{}
wg.Add(2)
Expand Down Expand Up @@ -71,52 +71,10 @@ func (s *service) GeneratePasswordByLength(length int, passCharacters []rune) (s
close(inCH)
}()

// Checking the duplicated passwords and removing them
removeDuplicatedPasswords := func(inputChan chan string, outputChan chan string) {
var previousPassword string
for actualPassword := range inputChan {
if actualPassword != previousPassword {
previousPassword = actualPassword
outputChan <- actualPassword
}
}
close(outputChan)
}

getPasswords := func(outputChan <-chan string, newWg *sync.WaitGroup) {
defer newWg.Done()

for v := range outCH {
passwordList = append(passwordList, v)
}
passwordListChannel <- passwordList
}

// Send passwords to channel
sendPasswordsToChan := func(receiveCh chan string, newWg *sync.WaitGroup) {
defer newWg.Done()

go func() {
for _, v := range <-passwordListChannel {
receiveCh <- v
}
close(receiveCh)
}()
}

// Get errors from CH, this way we could handle errors
getErrFromCh := func(errCh chan error, newWg *sync.WaitGroup) {
defer newWg.Done()
select {
case err := <-errorCH:
errDoneCH <- err
}
}

go removeDuplicatedPasswords(inCH, outCH)
go getPasswords(outCH, &wg)
go sendPasswordsToChan(returnCH, &wg)
go getErrFromCh(errorCH, &wg)
go getPasswords(passwordListChannel, outCH, &wg)
go returnAllPasswords(passwordListChannel, returnCH, &wg)
go chanErrorHandler(errorCH, errDoneCH, &wg)

select {
case generatedPassword := <-returnCH:
Expand All @@ -134,21 +92,65 @@ func (s *service) GeneratePasswordByLength(length int, passCharacters []rune) (s
}
}

// Checking the duplicated passwords and removing them
func removeDuplicatedPasswords(inputChannel, outputChannel chan string) {
var previousPassword string
for actualPassword := range inputChannel {
if actualPassword != previousPassword {
previousPassword = actualPassword
outputChannel <- actualPassword
}
}
close(outputChannel)
}

// getPasswords: Get all generated passswords and send it to an array
func getPasswords(generatedPasswods chan []string, outputSlice chan string, wg *sync.WaitGroup) {
defer wg.Done()

var passwordList []string

for v := range outputSlice {
passwordList = append(passwordList, v)
}

generatedPasswods <- passwordList
}

func returnAllPasswords(passwordList chan []string, receiveCh chan string, newWg *sync.WaitGroup) {
defer newWg.Done()

go func() {
for _, v := range <-passwordList {
receiveCh <- v
}
close(receiveCh)
}()
}

// Get errors from CH, this way we could handle errors
func chanErrorHandler(errorInput chan error, errorOutput chan error, newWg *sync.WaitGroup) {
defer newWg.Done()
select {
case err := <-errorInput:
errorOutput <- err
}
}

// CheckSpecialCharAndLettersQuantity - It Checks password's special characters and letters according to its length
func (s *service) CheckSpecialCharAndLettersQuantity(password *entity.PasswordGen) bool {
passwordLetters := s.passGen.GetPasswordLetters(password.Password)
passwordSpecialChars := s.passGen.GetPasswordSpecialChars(password.Password)

switch password.Length {
case 8:
if len(passwordLetters) < 3 && len(passwordSpecialChars) < 3 {
if password.Length >= 8 && password.Length < 16 {
if len(passwordLetters) < 2 && len(passwordSpecialChars) < 2 {
return false
}
case 16:
} else if password.Length >= 16 && password.Length < 32 {
if len(passwordLetters) < 5 && len(passwordSpecialChars) < 5 {
return false
}
case 32:
} else if password.Length >= 32 && password.Length <= 64 {
if len(passwordLetters) < 6 && len(passwordSpecialChars) < 6 {
return false
}
Expand All @@ -161,16 +163,15 @@ func (s *service) CheckSpecialCharAndNumbersQuantity(password *entity.PasswordGe
passwordNumbers := s.passGen.GetPasswordNumbers(password.Password)
passwordSpecialChars := s.passGen.GetPasswordSpecialChars(password.Password)

switch password.Length {
case 8:
if len(passwordNumbers) < 3 && len(passwordSpecialChars) < 3 {
if password.Length >= 8 && password.Length < 16 {
if len(passwordNumbers) < 2 && len(passwordSpecialChars) < 2 {
return false
}
case 16:
} else if password.Length >= 16 && password.Length < 32 {
if len(passwordNumbers) < 5 && len(passwordSpecialChars) < 5 {
return false
}
case 32:
} else if password.Length >= 32 && password.Length <= 64 {
if len(passwordNumbers) < 6 && len(passwordSpecialChars) < 6 {
return false
}
Expand All @@ -183,16 +184,15 @@ func (s *service) CheckLettersAndNumbersQuantity(password *entity.PasswordGen) b
passwordNumbers := s.passGen.GetPasswordNumbers(password.Password)
passwordLetters := s.passGen.GetPasswordLetters(password.Password)

switch password.Length {
case 8:
if len(passwordNumbers) < 3 && len(passwordLetters) < 3 {
if password.Length >= 8 && password.Length < 16 {
if len(passwordNumbers) < 2 && len(passwordLetters) < 2 {
return false
}
case 16:
} else if password.Length >= 16 && password.Length < 32 {
if len(passwordNumbers) < 5 && len(passwordLetters) < 5 {
return false
}
case 32:
} else if password.Length >= 32 && password.Length <= 64 {
if len(passwordNumbers) < 6 && len(passwordLetters) < 6 {
return false
}
Expand All @@ -206,16 +206,15 @@ func (s *service) CheckAllCharsQuantity(password *entity.PasswordGen) bool {
passwordLetters := s.passGen.GetPasswordLetters(password.Password)
passwordSpecialChars := s.passGen.GetPasswordSpecialChars(password.Password)

switch password.Length {
case 8:
if len(passwordNumbers) < 2 && len(passwordLetters) < 2 && len(passwordSpecialChars) < 2 {
if password.Length >= 8 && password.Length < 16 {
if len(passwordNumbers) < 1 && len(passwordLetters) < 1 && len(passwordSpecialChars) < 1 {
return false
}
case 16:
} else if password.Length >= 16 && password.Length < 32 {
if len(passwordNumbers) < 3 && len(passwordLetters) < 3 && len(passwordSpecialChars) < 3 {
return false
}
case 32:
} else if password.Length >= 32 && password.Length <= 64 {
if len(passwordNumbers) < 6 && len(passwordLetters) < 6 && len(passwordSpecialChars) < 6 {
return false
}
Expand Down
Loading

0 comments on commit 6919222

Please sign in to comment.