diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..0f8103e --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,15 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Launch Package", + "type": "go", + "request": "launch", + "mode": "auto", + "program": "${workspaceFolder}" + } + ] +} \ No newline at end of file diff --git a/db_init.sql b/db_init.sql new file mode 100644 index 0000000..d17d1cd --- /dev/null +++ b/db_init.sql @@ -0,0 +1,8 @@ +create table users ( + id int generated always as identity, + login text unique not null, + secret text not null, + name text not null, + + primary key (id) +); \ No newline at end of file diff --git a/dockerfile b/dockerfile new file mode 100644 index 0000000..e1d6c8a --- /dev/null +++ b/dockerfile @@ -0,0 +1,13 @@ +FROM golang:1.22 + +WORKDIR /usr/src/app + +COPY go.mod go.sum ./ +RUN go mod download && go mod verify + +EXPOSE 8080 + +COPY . . +RUN go build -v -o /usr/local/bin/app ./... + +CMD ["app"] \ No newline at end of file diff --git a/main.go b/main.go index 7c5da9c..ee4376b 100644 --- a/main.go +++ b/main.go @@ -4,7 +4,6 @@ import ( "backend/src" "crypto/rand" "crypto/rsa" - "crypto/x509" "github.com/gin-gonic/gin" "github.com/jackc/pgx" @@ -16,10 +15,10 @@ func main() { if err != nil { panic(err) } - keyBytes, err := x509.MarshalPKCS8PrivateKey(key) - if err != nil { - panic(err) - } + // keyBytes, err := x509.MarshalPKCS8PrivateKey(key) + // if err != nil { + // panic(err) + // } connConf, err := pgx.ParseConnectionString("postgres://postgres:postgres@localhost:5432/postgres") if err != nil { @@ -31,7 +30,7 @@ func main() { panic(err) } - jwtUtil := src.NewJwtUtil(string(keyBytes)) + jwtUtil := src.NewJwtUtil(key) bcryptUtil := src.NewBcrypt() db := src.NewDB(sqlDb) userService := src.NewUserService(src.UserServiceDeps{ @@ -46,7 +45,7 @@ func main() { userGroup := r.Group("/user") userGroup.POST("/create", src.NewUserCreateHandler(userService)) - userGroup.POST("/login", src.NewUserCreateHandler(userService)) + userGroup.POST("/login", src.NewUserLoginHandler(userService)) dummyGroup := r.Group("/dummy") dummyGroup.Use(src.NewAuthMiddleware(userService)) diff --git a/src/db.go b/src/db.go index 56aa320..79af35a 100644 --- a/src/db.go +++ b/src/db.go @@ -21,7 +21,7 @@ type dbImpl struct { } func (d *dbImpl) CreateUser(ctx context.Context, dto UserDTO) (*UserDTO, error) { - query := `insert into users (login, secret, name) values (?, ?, ?) returning id;` + query := `insert into users (login, secret, name) values ($1, $2, $3) returning id;` row := d.db.QueryRowContext(ctx, query, dto.Login, dto.Secret, dto.Name) id := "" @@ -38,11 +38,11 @@ func (d *dbImpl) CreateUser(ctx context.Context, dto UserDTO) (*UserDTO, error) } func (d *dbImpl) GetUserById(ctx context.Context, id string) (*UserDTO, error) { - query := `select (id, login, secret, name) from users where id = ?;` + query := `select id, login, secret, name from users where id = $1;` row := d.db.QueryRowContext(ctx, query, id) dto := &UserDTO{} - err := row.Scan(dto) + err := row.Scan(&dto.Id, &dto.Login, &dto.Secret, &dto.Name) if err == nil { return dto, nil } @@ -54,11 +54,11 @@ func (d *dbImpl) GetUserById(ctx context.Context, id string) (*UserDTO, error) { } func (d *dbImpl) GetUserByLogin(ctx context.Context, login string) (*UserDTO, error) { - query := `select (id, login, secret, name) from users where login = ?;` + query := `select id, login, secret, name from users where login = $1;` row := d.db.QueryRowContext(ctx, query, login) dto := &UserDTO{} - err := row.Scan(dto) + err := row.Scan(&dto.Id, &dto.Login, &dto.Secret, &dto.Name) if err == nil { return dto, nil } diff --git a/src/jwt.go b/src/jwt.go index dfeb3e9..54aff46 100644 --- a/src/jwt.go +++ b/src/jwt.go @@ -1,6 +1,7 @@ package src import ( + "crypto/rsa" "fmt" "github.com/golang-jwt/jwt/v5" @@ -16,19 +17,19 @@ type JwtUtil interface { Parse(tokenStr string) (JwtPayload, error) } -func NewJwtUtil(privateKey string) JwtUtil { +func NewJwtUtil(privateKey *rsa.PrivateKey) JwtUtil { return &jwtUtil{ privateKey: privateKey, } } type jwtUtil struct { - privateKey string + privateKey *rsa.PrivateKey } func (j *jwtUtil) Create(user UserDTO) (string, error) { - payload := JwtPayload{UserId: user.Id} - token := jwt.NewWithClaims(&jwt.SigningMethodHMAC{}, payload) + payload := &JwtPayload{UserId: user.Id} + token := jwt.NewWithClaims(jwt.SigningMethodRS256, payload) tokenStr, err := token.SignedString(j.privateKey) if err != nil { return "", err @@ -37,15 +38,15 @@ func (j *jwtUtil) Create(user UserDTO) (string, error) { } func (j *jwtUtil) Parse(tokenStr string) (JwtPayload, error) { - token, err := jwt.ParseWithClaims(tokenStr, JwtPayload{}, func(t *jwt.Token) (interface{}, error) { - return j.privateKey, nil + token, err := jwt.ParseWithClaims(tokenStr, &JwtPayload{}, func(t *jwt.Token) (interface{}, error) { + return &j.privateKey.PublicKey, nil }) if err != nil { return JwtPayload{}, err } - if payload, ok := token.Claims.(JwtPayload); ok { - return payload, nil + if payload, ok := token.Claims.(*JwtPayload); ok { + return *payload, nil } return JwtPayload{}, fmt.Errorf("cant get payload") diff --git a/src/user_create_handler.go b/src/user_create_handler.go index 72b8ad9..5fcbe4f 100644 --- a/src/user_create_handler.go +++ b/src/user_create_handler.go @@ -32,11 +32,11 @@ func NewUserCreateHandler(userService UserService) gin.HandlerFunc { Name: params.Name, }) if err == ErrUserExists { - ctx.AbortWithError(400, err) + ctx.Data(400, "plain/text", []byte(err.Error())) return } if err != nil { - ctx.AbortWithError(500, err) + ctx.Data(500, "plain/text", []byte(err.Error())) return }