fix some security issues
This commit is contained in:
@@ -44,7 +44,7 @@ func (cfg *apiConfig) handlerUsersCreate(w http.ResponseWriter, r *http.Request)
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
user, err := cfg.DB.GetUserByAPIKey(r.Context(), apiKey)
|
user, err := cfg.DB.GetUser(r.Context(), apiKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
respondWithError(w, http.StatusInternalServerError, "Couldn't get user")
|
respondWithError(w, http.StatusInternalServerError, "Couldn't get user")
|
||||||
|
|||||||
@@ -40,13 +40,13 @@ func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
const getUserByAPIKey = `-- name: GetUserByAPIKey :one
|
const getUser = `-- name: GetUser :one
|
||||||
|
|
||||||
SELECT id, created_at, updated_at, name, api_key FROM users WHERE api_key = ?
|
SELECT id, created_at, updated_at, name, api_key FROM users WHERE api_key = ?
|
||||||
`
|
`
|
||||||
|
|
||||||
func (q *Queries) GetUserByAPIKey(ctx context.Context, apiKey string) (User, error) {
|
func (q *Queries) GetUser(ctx context.Context, apiKey string) (User, error) {
|
||||||
row := q.db.QueryRowContext(ctx, getUserByAPIKey, apiKey)
|
row := q.db.QueryRowContext(ctx, getUser, apiKey)
|
||||||
var i User
|
var i User
|
||||||
err := row.Scan(
|
err := row.Scan(
|
||||||
&i.ID,
|
&i.ID,
|
||||||
|
|||||||
5
main.go
5
main.go
@@ -27,7 +27,10 @@ type apiConfig struct {
|
|||||||
var staticFiles embed.FS
|
var staticFiles embed.FS
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
godotenv.Load(".env")
|
err := godotenv.Load(".env")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Error loading .env file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
port := os.Getenv("PORT")
|
port := os.Getenv("PORT")
|
||||||
if port == "" {
|
if port == "" {
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ func (cfg *apiConfig) middlewareAuth(handler authedHandler) http.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
user, err := cfg.DB.GetUserByAPIKey(r.Context(), apiKey)
|
user, err := cfg.DB.GetUser(r.Context(), apiKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
respondWithError(w, http.StatusNotFound, "Couldn't get user")
|
respondWithError(w, http.StatusNotFound, "Couldn't get user")
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -9,6 +9,6 @@ VALUES (
|
|||||||
);
|
);
|
||||||
--
|
--
|
||||||
|
|
||||||
-- name: GetUserByAPIKey :one
|
-- name: GetUser :one
|
||||||
SELECT * FROM users WHERE api_key = ?;
|
SELECT * FROM users WHERE api_key = ?;
|
||||||
--
|
--
|
||||||
|
|||||||
Reference in New Issue
Block a user