fix some security issues

This commit is contained in:
wagslane
2023-06-06 13:33:22 -06:00
parent 307c73e1fb
commit 54f294659f
5 changed files with 10 additions and 7 deletions

View File

@@ -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")

View File

@@ -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,

View File

@@ -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 == "" {

View File

@@ -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

View File

@@ -9,6 +9,6 @@ VALUES (
); );
-- --
-- name: GetUserByAPIKey :one -- name: GetUser :one
SELECT * FROM users WHERE api_key = ?; SELECT * FROM users WHERE api_key = ?;
-- --