generated from denpeshkov/go-template
-
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
edec93c
commit bd7dd38
Showing
13 changed files
with
324 additions
and
29 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
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 |
---|---|---|
@@ -1,4 +1,6 @@ | ||
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= | ||
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= | ||
golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc= | ||
golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg= | ||
golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk= | ||
golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM= |
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,89 @@ | ||
package greenlight | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"net/mail" | ||
"unicode/utf8" | ||
|
||
"golang.org/x/crypto/bcrypt" | ||
) | ||
|
||
// User represents a user. | ||
type User struct { | ||
ID int64 `json:"id"` | ||
Name string `json:"name"` | ||
Email string `json:"email"` | ||
Password Password `json:"-"` | ||
Activated bool `json:"activated"` | ||
Version int `json:"-"` | ||
} | ||
|
||
// Valid returns an error if the validation fails, otherwise nil. | ||
func (u *User) Valid() error { | ||
err := NewInvalidError("User is invalid.") | ||
|
||
if u.ID < 0 { | ||
err.AddViolationMsg("ID", "Must be greater or equal to 0.") | ||
} | ||
|
||
if u.Name == "" { | ||
err.AddViolationMsg("Name", "Must be provided.") | ||
} | ||
if utf8.RuneCount([]byte(u.Name)) > 500 { | ||
err.AddViolationMsg("Name", "Must not be more than 500 characters long.") | ||
} | ||
|
||
if u.Email == "" { | ||
err.AddViolationMsg("Email", "Must be provided.") | ||
} | ||
if _, e := mail.ParseAddress(u.Email); e != nil { | ||
err.AddViolationMsg("Email", "Is invalid.") | ||
} | ||
|
||
if len(u.Password) == 0 { | ||
err.AddViolationMsg("Password", "Must be provided.") | ||
} | ||
|
||
if len(err.violations) != 0 { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// Password represents a hash of the user password. | ||
type Password []byte | ||
|
||
// NewPasswords generates a hashed password from the plaintext password. | ||
func NewPassword(plaintext string) (Password, error) { | ||
op := "greenlight.NewPassword" | ||
|
||
hash, err := bcrypt.GenerateFromPassword([]byte(plaintext), 12) | ||
if err != nil { | ||
return nil, fmt.Errorf("%s: %w", op, err) | ||
} | ||
return hash, nil | ||
} | ||
|
||
// Matches tests whether the provided plaintext password matches the hashed password. | ||
func (p *Password) Matches(plaintext string) (bool, error) { | ||
op := "greenlight.password.Matches" | ||
|
||
if err := bcrypt.CompareHashAndPassword(*p, []byte(plaintext)); err != nil { | ||
switch { | ||
case errors.Is(err, bcrypt.ErrMismatchedHashAndPassword): | ||
return false, nil | ||
default: | ||
return false, fmt.Errorf("%s: %w", op, err) | ||
} | ||
} | ||
return true, nil | ||
} | ||
|
||
// UserService is a service for managing users. | ||
type UserService interface { | ||
Get(ctx context.Context, email string) (*User, error) | ||
Create(ctx context.Context, u *User) error | ||
Update(ctx context.Context, u *User) 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
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,77 @@ | ||
package http | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/denpeshkov/greenlight/internal/greenlight" | ||
) | ||
|
||
func (s *Server) registerUserHandlers() { | ||
s.router.HandleFunc("POST /v1/users", s.handleUserCreate) | ||
} | ||
|
||
// handleUserCreate handles requests to create (register) a user. | ||
func (s *Server) handleUserCreate(w http.ResponseWriter, r *http.Request) { | ||
op := "http.Server.handleUserCreate" | ||
|
||
var req struct { | ||
Name string `json:"name"` | ||
Email string `json:"email"` | ||
Password string `json:"password"` | ||
} | ||
if err := s.readRequest(w, r, &req); err != nil { | ||
s.Error(w, r, fmt.Errorf("%s: %w", op, err)) | ||
return | ||
} | ||
|
||
err := greenlight.NewInvalidError("User is invalid.") | ||
if req.Password == "" { | ||
err.AddViolationMsg("Password", "Must be provided.") | ||
} | ||
if len(req.Password) < 8 { | ||
err.AddViolationMsg("Password", "Must be at least 8 characters long.") | ||
} | ||
if len(req.Password) > 72 { | ||
err.AddViolationMsg("Password", "Must not be more than 72 bytes long.") | ||
} | ||
if len(err.Violations()) != 0 { | ||
s.Error(w, r, err) | ||
return | ||
} | ||
|
||
u := &greenlight.User{ | ||
Name: req.Name, | ||
Email: req.Email, | ||
Activated: false, | ||
} | ||
pass, errPas := greenlight.NewPassword(req.Password) | ||
if errPas != nil { | ||
s.Error(w, r, fmt.Errorf("%s: %w", op, err)) | ||
} | ||
u.Password = pass | ||
|
||
if err := u.Valid(); err != nil { | ||
s.Error(w, r, err) | ||
return | ||
} | ||
if err := s.UserService.Create(r.Context(), u); err != nil { | ||
s.Error(w, r, fmt.Errorf("%s: %w", op, err)) | ||
return | ||
} | ||
|
||
resp := struct { | ||
ID int64 `json:"id"` | ||
Name string `json:"name"` | ||
Email string `json:"email"` | ||
}{ | ||
ID: u.ID, | ||
Name: u.Name, | ||
Email: u.Email, | ||
} | ||
|
||
if err := s.sendResponse(w, r, http.StatusCreated, resp, nil); err != nil { | ||
s.Error(w, r, fmt.Errorf("%s: %w", op, err)) | ||
return | ||
} | ||
} |
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 @@ | ||
DROP TABLE IF EXISTS users; |
10 changes: 10 additions & 0 deletions
10
internal/postgres/migrations/005_create_users_table.up.sql
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,10 @@ | ||
CREATE EXTENSION IF NOT EXISTS citext; | ||
|
||
CREATE TABLE IF NOT EXISTS users ( | ||
id bigserial PRIMARY KEY, | ||
name text NOT NULL, | ||
email citext UNIQUE NOT NULL, | ||
password_hash bytea NOT NULL, | ||
activated bool NOT NULL, | ||
version integer NOT NULL DEFAULT 1 | ||
); |
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
Oops, something went wrong.