-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5cf2428
commit ded2c83
Showing
13 changed files
with
347 additions
and
2 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
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,23 @@ | ||
package register | ||
|
||
import "regexp" | ||
|
||
var ( | ||
emailRegex = regexp.MustCompile(`^[a-z0-9._%+\-]+@[a-z0-9.\-]+\.[a-z]{2,4}$`) | ||
) | ||
|
||
type validationErrors map[string]string | ||
|
||
type Request struct { | ||
Identity string `json:"identity"` | ||
} | ||
|
||
func (r *Request) Validate() (bool, validationErrors) { | ||
if !emailRegex.MatchString(r.Identity) { | ||
return false, validationErrors{ | ||
"identity": "identity is not a valid email address", | ||
} | ||
} | ||
|
||
return true, nil | ||
} |
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,5 @@ | ||
package register | ||
|
||
type Response struct { | ||
ValidationErrors validationErrors `json:"errors,omitempty"` | ||
} |
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,92 @@ | ||
package register | ||
|
||
import ( | ||
"bytes" | ||
"encoding/base64" | ||
"errors" | ||
"github.com/khanzadimahdi/testproject/application/auth" | ||
"github.com/khanzadimahdi/testproject/domain" | ||
"github.com/khanzadimahdi/testproject/domain/user" | ||
"github.com/khanzadimahdi/testproject/infrastructure/jwt" | ||
"time" | ||
) | ||
|
||
type UseCase struct { | ||
userRepository user.Repository | ||
jwt *jwt.JWT | ||
mailer domain.Mailer | ||
mailFrom string | ||
} | ||
|
||
func NewUseCase( | ||
userRepository user.Repository, | ||
JWT *jwt.JWT, | ||
mailer domain.Mailer, | ||
mailFrom string, | ||
) *UseCase { | ||
return &UseCase{ | ||
userRepository: userRepository, | ||
jwt: JWT, | ||
mailer: mailer, | ||
mailFrom: mailFrom, | ||
} | ||
} | ||
|
||
func (uc *UseCase) Register(request Request) (*Response, error) { | ||
if ok, validation := request.Validate(); !ok { | ||
return &Response{ | ||
ValidationErrors: validation, | ||
}, nil | ||
} | ||
|
||
exists, err := uc.IdentityExists(request.Identity) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if exists { | ||
return &Response{ | ||
ValidationErrors: map[string]string{ | ||
"identity": "user with given email already exists", | ||
}, | ||
}, nil | ||
} | ||
|
||
resetPasswordToken, err := uc.registrationToken(request.Identity) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resetPasswordToken = base64.URLEncoding.EncodeToString([]byte(resetPasswordToken)) | ||
|
||
var msg bytes.Buffer | ||
if _, err := msg.WriteString(resetPasswordToken); err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := uc.mailer.SendMail(uc.mailFrom, request.Identity, "Registration", msg.Bytes()); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &Response{}, nil | ||
} | ||
|
||
func (uc *UseCase) IdentityExists(identity string) (bool, error) { | ||
_, err := uc.userRepository.GetOneByIdentity(identity) | ||
if errors.Is(err, domain.ErrNotExists) { | ||
return true, nil | ||
} | ||
|
||
return false, err | ||
} | ||
|
||
func (uc *UseCase) registrationToken(identity string) (string, error) { | ||
b := jwt.NewClaimsBuilder() | ||
b.SetSubject(identity) | ||
b.SetNotBefore(time.Now()) | ||
b.SetExpirationTime(time.Now().Add(24 * time.Hour)) | ||
b.SetIssuedAt(time.Now()) | ||
b.SetAudience([]string{auth.RegistrationToken}) | ||
|
||
return uc.jwt.Generate(b.Build()) | ||
} |
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 @@ | ||
package register |
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,15 @@ | ||
package verify | ||
|
||
type validationErrors map[string]string | ||
|
||
type Request struct { | ||
Token string `json:"token"` | ||
Name string `json:"name"` | ||
Username string `json:"username"` | ||
Password string `json:"password"` | ||
Repassword string `json:"repassword"` | ||
} | ||
|
||
func (r *Request) Validate() (bool, validationErrors) { | ||
return true, nil | ||
} |
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,5 @@ | ||
package verify | ||
|
||
type Response struct { | ||
ValidationErrors validationErrors `json:"errors,omitempty"` | ||
} |
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,110 @@ | ||
package verify | ||
|
||
import ( | ||
"crypto/rand" | ||
"errors" | ||
|
||
"github.com/khanzadimahdi/testproject/application/auth" | ||
"github.com/khanzadimahdi/testproject/domain" | ||
"github.com/khanzadimahdi/testproject/domain/password" | ||
"github.com/khanzadimahdi/testproject/domain/user" | ||
"github.com/khanzadimahdi/testproject/infrastructure/jwt" | ||
) | ||
|
||
type UseCase struct { | ||
userRepository user.Repository | ||
hasher password.Hasher | ||
jwt *jwt.JWT | ||
} | ||
|
||
func NewUseCase(userRepository user.Repository, hasher password.Hasher, JWT *jwt.JWT) *UseCase { | ||
return &UseCase{ | ||
userRepository: userRepository, | ||
hasher: hasher, | ||
jwt: JWT, | ||
} | ||
} | ||
|
||
func (uc *UseCase) Verify(request Request) (*Response, error) { | ||
if ok, validation := request.Validate(); !ok { | ||
return &Response{ | ||
ValidationErrors: validation, | ||
}, nil | ||
} | ||
|
||
claims, err := uc.jwt.Verify(request.Token) | ||
if err != nil { | ||
return &Response{ | ||
ValidationErrors: validationErrors{ | ||
"token": err.Error(), | ||
}, | ||
}, nil | ||
} | ||
|
||
if audiences, err := claims.GetAudience(); err != nil || len(audiences) == 0 || audiences[0] != auth.RegistrationToken { | ||
return &Response{ | ||
ValidationErrors: validationErrors{ | ||
"token": "registration token is not valid", | ||
}, | ||
}, nil | ||
} | ||
|
||
identity, err := claims.GetSubject() | ||
if err != nil { | ||
return &Response{ | ||
ValidationErrors: validationErrors{ | ||
"token": err.Error(), | ||
}, | ||
}, nil | ||
} | ||
|
||
if exists, err := uc.IdentityExists(identity); err != nil { | ||
return nil, err | ||
} else if exists { | ||
return &Response{ | ||
ValidationErrors: map[string]string{ | ||
"identity": "user with given email already exists", | ||
}, | ||
}, nil | ||
} | ||
|
||
if exists, err := uc.IdentityExists(request.Username); err != nil { | ||
return nil, err | ||
} else if exists { | ||
return &Response{ | ||
ValidationErrors: map[string]string{ | ||
"user": "user with given username already exists", | ||
}, | ||
}, nil | ||
} | ||
|
||
salt := make([]byte, 64) | ||
if _, err := rand.Read(salt); err != nil { | ||
return nil, err | ||
} | ||
|
||
u := user.User{ | ||
Name: request.Name, | ||
Username: request.Username, | ||
Email: identity, | ||
PasswordHash: password.Hash{ | ||
Value: uc.hasher.Hash([]byte(request.Password), salt), | ||
Salt: salt, | ||
}, | ||
} | ||
|
||
if err := uc.userRepository.Save(&u); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &Response{}, nil | ||
} | ||
|
||
func (uc *UseCase) IdentityExists(identity string) (bool, error) { | ||
_, err := uc.userRepository.GetOneByIdentity(identity) | ||
if errors.Is(err, domain.ErrNotExists) { | ||
return true, nil | ||
} | ||
|
||
return false, err | ||
} |
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 @@ | ||
package verify |
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,43 @@ | ||
package auth | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"net/http" | ||
|
||
"github.com/khanzadimahdi/testproject/application/auth/register" | ||
"github.com/khanzadimahdi/testproject/domain" | ||
) | ||
|
||
type registerHandler struct { | ||
useCase *register.UseCase | ||
} | ||
|
||
func NewRegisterHandler(useCase *register.UseCase) *registerHandler { | ||
return ®isterHandler{ | ||
useCase: useCase, | ||
} | ||
} | ||
|
||
func (h *registerHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { | ||
var request register.Request | ||
if err := json.NewDecoder(r.Body).Decode(&request); err != nil { | ||
rw.WriteHeader(http.StatusBadRequest) | ||
return | ||
} | ||
|
||
response, err := h.useCase.Register(request) | ||
|
||
switch true { | ||
case errors.Is(err, domain.ErrNotExists): | ||
rw.WriteHeader(http.StatusNotFound) | ||
case err != nil: | ||
rw.WriteHeader(http.StatusInternalServerError) | ||
case len(response.ValidationErrors) > 0: | ||
rw.WriteHeader(http.StatusBadRequest) | ||
json.NewEncoder(rw).Encode(response) | ||
default: | ||
rw.Header().Add("Content-Type", "application/json") | ||
rw.WriteHeader(http.StatusNoContent) | ||
} | ||
} |
Oops, something went wrong.