Skip to content

Commit

Permalink
fix: status code related to api key (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
kokoichi206 committed Nov 26, 2022
1 parent 3f3b43c commit 88824c7
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 24 deletions.
11 changes: 8 additions & 3 deletions api/blogs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@ func (server *Server) getAllBlogs(w http.ResponseWriter, r *http.Request) {

key := r.FormValue("key")

if !server.isApiKeyValid(key) {
w.WriteHeader(http.StatusForbidden)
fmt.Fprint(w, ErrorJson("No valid api key"))
if err := server.isApiKeyValid(key); err != nil {
if err == sql.ErrNoRows {
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprint(w, ErrorJson("No valid api key"))
return
}
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, ErrorJson("Error while reading api key from DB"))
return
}

Expand Down
13 changes: 8 additions & 5 deletions api/formations.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ func (server *Server) getAllFormations(w http.ResponseWriter, r *http.Request) {

key := r.FormValue("key")

if !server.isApiKeyValid(key) {
fmt.Printf("getAllFormations: access with invalid api key")
// return error message
w.WriteHeader(http.StatusForbidden)
fmt.Fprint(w, ErrorJson("No valid api key"))
if err := server.isApiKeyValid(key); err != nil {
if err == sql.ErrNoRows {
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprint(w, ErrorJson("No valid api key"))
return
}
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, ErrorJson("Error while reading api key from DB"))
return
}

Expand Down
12 changes: 8 additions & 4 deletions api/members.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@ func (server *Server) getAllMembers(w http.ResponseWriter, r *http.Request) {

key := r.FormValue("key")

if !server.isApiKeyValid(key) {
// return error message
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprint(w, ErrorJson("No valid api key"))
if err := server.isApiKeyValid(key); err != nil {
if err == sql.ErrNoRows {
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprint(w, ErrorJson("No valid api key"))
return
}
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, ErrorJson("Error while reading api key from DB"))
return
}

Expand Down
13 changes: 9 additions & 4 deletions api/positions.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api

import (
"database/sql"
"encoding/json"
"fmt"
"net/http"
Expand All @@ -15,10 +16,14 @@ func (server *Server) getPositions(w http.ResponseWriter, r *http.Request) {

key := r.FormValue("key")

if !server.isApiKeyValid(key) {
// return error message
w.WriteHeader(http.StatusForbidden)
fmt.Fprint(w, ErrorJson("No valid api key"))
if err := server.isApiKeyValid(key); err != nil {
if err == sql.ErrNoRows {
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprint(w, ErrorJson("No valid api key"))
return
}
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, ErrorJson("Error while reading api key from DB"))
return
}

Expand Down
13 changes: 8 additions & 5 deletions api/songs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ func (server *Server) getAllSongs(w http.ResponseWriter, r *http.Request) {

key := r.FormValue("key")

if !server.isApiKeyValid(key) {
fmt.Printf("getAllSongs: access with invalid api key")
// return error message
w.WriteHeader(http.StatusForbidden)
fmt.Fprint(w, ErrorJson("No valid api key"))
if err := server.isApiKeyValid(key); err != nil {
if err == sql.ErrNoRows {
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprint(w, ErrorJson("No valid api key"))
return
}
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, ErrorJson("Error while reading api key from DB"))
return
}

Expand Down
10 changes: 7 additions & 3 deletions api/validations.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package api

func (server *Server) isApiKeyValid(key string) bool {
import (
"errors"
)

func (server *Server) isApiKeyValid(key string) error {
if key == "" {
return false
return errors.New("API key is empty")
}
_, err := server.querier.FindApiKeyByName(key)
return err == nil
return err
}

0 comments on commit 88824c7

Please sign in to comment.