-
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.
Merge pull request #5 from go-park-mail-ru/PRI-3
Pri-3: autorization
- Loading branch information
Showing
18 changed files
with
481 additions
and
7 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
.idea | ||
.bin | ||
.bin | ||
*.env |
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,18 @@ | ||
version: "3.9" | ||
services: | ||
db: | ||
container_name: postgres | ||
image: postgres:latest | ||
restart: always | ||
volumes: | ||
- type: volume | ||
source: postgresdb-data | ||
target: /var/lib/postgresql/data | ||
env_file: | ||
- ./.env | ||
ports: | ||
- ${DB_PORT}:5432 | ||
|
||
volumes: | ||
postgresdb-data: | ||
driver: local |
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,2 +1,10 @@ | ||
github.com/golang-jwt/jwt/v5 v5.2.0 h1:d/ix8ftRUorsN+5eMIlF4T6J8CAt9rch3My2winC1Jw= | ||
github.com/golang-jwt/jwt/v5 v5.2.0/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk= | ||
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= | ||
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= | ||
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= | ||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= | ||
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= | ||
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= | ||
github.com/satori/uuid v1.2.0 h1:6TFY4nxn5XwBx0gDfzbEMCNT6k4N/4FNIuN8RACZ0KI= | ||
github.com/satori/uuid v1.2.0/go.mod h1:B8HLsPLik/YNn6KKWVMDJ8nzCL8RP5WyfsnmvnAEwIU= |
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,9 @@ | ||
package models | ||
|
||
// UserLoginData represents user information for login and signup | ||
type UserLoginData struct { | ||
// Login stands for users nickname | ||
Login string `json:"login"` | ||
// Password stands for users password | ||
Password string `json:"password"` | ||
} |
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,6 +1,15 @@ | ||
package models | ||
|
||
import ( | ||
"github.com/satori/uuid" | ||
) | ||
|
||
// User represents user information | ||
type User struct { | ||
Login string | ||
PasswordHash string | ||
// ID uniquely identifies the user. | ||
ID uuid.UUID `json:"id"` | ||
// Login is the username of the user. | ||
Login string `json:"login"` | ||
// PasswordHash is the hashed password of the user. | ||
PasswordHash string `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,83 @@ | ||
package delivery | ||
|
||
import ( | ||
"2024_1_TeaStealers/internal/models" | ||
"2024_1_TeaStealers/internal/pkg/auth" | ||
"2024_1_TeaStealers/internal/pkg/middleware" | ||
"2024_1_TeaStealers/internal/pkg/utils" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
// AuthHandler handles HTTP requests for user authentication. | ||
type AuthHandler struct { | ||
// uc represents the usecase interface for authentication. | ||
uc auth.AuthUsecase | ||
} | ||
|
||
// NewAuthHandler creates a new instance of AuthHandler. | ||
func NewAuthHandler(uc auth.AuthUsecase) *AuthHandler { | ||
return &AuthHandler{uc: uc} | ||
} | ||
|
||
// SignUp handles the request for registering a new user. | ||
func (h *AuthHandler) SignUp(w http.ResponseWriter, r *http.Request) { | ||
data := models.UserLoginData{} | ||
|
||
if err := utils.ReadRequestData(r, &data); err != nil { | ||
utils.WriteError(w, http.StatusBadRequest, "incorrect data format") | ||
return | ||
} | ||
|
||
newUser, token, exp, err := h.uc.SignUp(r.Context(), &data) | ||
if err != nil { | ||
utils.WriteError(w, http.StatusBadRequest, err.Error()) | ||
return | ||
} | ||
|
||
http.SetCookie(w, tokenCookie(middleware.CookieName, token, exp)) | ||
|
||
if err = utils.WriteResponse(w, http.StatusCreated, newUser); err != nil { | ||
utils.WriteError(w, http.StatusInternalServerError, err.Error()) | ||
} | ||
} | ||
|
||
// Login handles the request for user login. | ||
func (h *AuthHandler) Login(w http.ResponseWriter, r *http.Request) { | ||
data := models.UserLoginData{} | ||
if err := utils.ReadRequestData(r, &data); err != nil { | ||
utils.WriteError(w, http.StatusBadRequest, err.Error()) | ||
return | ||
} | ||
|
||
user, token, exp, err := h.uc.Login(r.Context(), &data) | ||
if err != nil { | ||
utils.WriteError(w, http.StatusBadRequest, "incorrect password or login") | ||
} | ||
|
||
http.SetCookie(w, tokenCookie(middleware.CookieName, token, exp)) | ||
|
||
if err = utils.WriteResponse(w, http.StatusOK, user); err != nil { | ||
utils.WriteError(w, http.StatusInternalServerError, err.Error()) | ||
} | ||
} | ||
|
||
// Logout handles the request for user logout. | ||
func (h *AuthHandler) Logout(w http.ResponseWriter, r *http.Request) { | ||
http.SetCookie(w, &http.Cookie{ | ||
Name: middleware.CookieName, | ||
Value: "", | ||
Path: "/", | ||
}) | ||
} | ||
|
||
// tokenCookie creates a new cookie for storing the authentication token. | ||
func tokenCookie(name, token string, exp time.Time) *http.Cookie { | ||
return &http.Cookie{ | ||
Name: name, | ||
Value: token, | ||
Expires: exp, | ||
Path: "/", | ||
HttpOnly: 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 |
---|---|---|
@@ -1 +1,20 @@ | ||
package auth | ||
|
||
import ( | ||
"2024_1_TeaStealers/internal/models" | ||
"context" | ||
"time" | ||
) | ||
|
||
// AuthUsecase represents the usecase interface for authentication. | ||
type AuthUsecase interface { | ||
SignUp(context.Context, *models.UserLoginData) (*models.User, string, time.Time, error) | ||
Login(context.Context, *models.UserLoginData) (*models.User, string, time.Time, error) | ||
} | ||
|
||
// AuthRepo represents the repository interface for authentication. | ||
type AuthRepo interface { | ||
CreateUser(ctx context.Context, newUser *models.User) error | ||
CheckUser(ctx context.Context, login string, passwordHash string) (*models.User, error) | ||
GetUserByLogin(cts context.Context, login string) (*models.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package repo | ||
|
||
import ( | ||
"2024_1_TeaStealers/internal/models" | ||
"context" | ||
"database/sql" | ||
"errors" | ||
) | ||
|
||
// AuthRepo represents a repository for authentication. | ||
type AuthRepo struct { | ||
db *sql.DB | ||
} | ||
|
||
// NewRepository creates a new instance of AuthRepo. | ||
func NewRepository(db *sql.DB) *AuthRepo { | ||
return &AuthRepo{db: db} | ||
} | ||
|
||
// CreateUser creates a new user in the database. | ||
func (r *AuthRepo) CreateUser(ctx context.Context, user *models.User) error { | ||
insert := `INSERT INTO users (id, login, password_hash) VALUES ($1, $2, $3, $4)` | ||
|
||
if _, err := r.db.ExecContext(ctx, insert, user.ID, user.Login, user.PasswordHash); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// GetUserByLogin retrieves a user from the database by their login. | ||
func (r *AuthRepo) GetUserByLogin(ctx context.Context, login string) (*models.User, error) { | ||
query := `SELECT id, login FROM users WHERE login = $1` | ||
|
||
res := r.db.QueryRowContext(ctx, query, login) | ||
|
||
user := &models.User{ | ||
Login: login, | ||
} | ||
if err := res.Scan(&user.ID, &user.Login, &user.PasswordHash); err != nil { | ||
return nil, err | ||
} | ||
|
||
return user, nil | ||
} | ||
|
||
// CheckUser checks if the user with the given login and password hash exists in the database. | ||
func (r *AuthRepo) CheckUser(ctx context.Context, login string, passwordHash string) (*models.User, error) { | ||
user, err := r.GetUserByLogin(ctx, login) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if user.PasswordHash != passwordHash { | ||
return nil, errors.New("wrong password") | ||
} | ||
|
||
return user, 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,64 @@ | ||
package usecase | ||
|
||
import ( | ||
"2024_1_TeaStealers/internal/models" | ||
"2024_1_TeaStealers/internal/pkg/auth" | ||
"2024_1_TeaStealers/internal/pkg/jwt" | ||
"context" | ||
"crypto/sha1" | ||
"encoding/hex" | ||
"github.com/satori/uuid" | ||
"time" | ||
) | ||
|
||
// AuthUsecase represents the usecase for authentication. | ||
type AuthUsecase struct { | ||
repo auth.AuthRepo | ||
} | ||
|
||
// NewAuthUsecase creates a new instance of AuthUsecase. | ||
func NewAuthUsecase(repo auth.AuthRepo) *AuthUsecase { | ||
return &AuthUsecase{repo: repo} | ||
} | ||
|
||
// SignUp handles the user registration process. | ||
func (u *AuthUsecase) SignUp(ctx context.Context, data *models.UserLoginData) (*models.User, string, time.Time, error) { | ||
newUser := &models.User{ | ||
ID: uuid.NewV4(), | ||
Login: data.Login, | ||
PasswordHash: generateHashString(data.Password), | ||
} | ||
|
||
if err := u.repo.CreateUser(ctx, newUser); err != nil { | ||
return nil, "", time.Now(), err | ||
} | ||
|
||
token, exp, err := jwt.GenerateToken(newUser) | ||
if err != nil { | ||
return nil, "", time.Now(), err | ||
} | ||
|
||
return newUser, token, exp, nil | ||
} | ||
|
||
// Login handles the user login process. | ||
func (u *AuthUsecase) Login(ctx context.Context, data *models.UserLoginData) (*models.User, string, time.Time, error) { | ||
user, err := u.repo.CheckUser(ctx, data.Login, generateHashString(data.Password)) | ||
if err != nil { | ||
return nil, "", time.Now(), err | ||
} | ||
|
||
token, exp, err := jwt.GenerateToken(user) | ||
if err != nil { | ||
return nil, "", time.Now(), err | ||
} | ||
|
||
return user, token, exp, nil | ||
} | ||
|
||
// generateHashString returns a hash string for the given input string. | ||
func generateHashString(s string) string { | ||
h := sha1.New() | ||
h.Write([]byte(s)) | ||
return hex.EncodeToString(h.Sum(nil)) | ||
} |
Oops, something went wrong.