Skip to content

Commit

Permalink
Feature no auth (#21)
Browse files Browse the repository at this point in the history
* print no auth config

* print req

* print method and path

* print method and path

* respond with ok for OPTIONS request

* return bad request if route not found

* just return if route not found

* enable cors

* enable cors

* send ok response on OPTIONS method

* use library to enable cors

* refactor

* print auth

* print auth

* check if required auth is nil

* print auth bearer token

* undo print auth bearer token

* add return statement after error

* remove unnecessary checks
  • Loading branch information
stkr89 authored Feb 12, 2021
1 parent 2bfa83e commit 5ce36b3
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 10 deletions.
6 changes: 3 additions & 3 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func getAuthCheckers() []func(r *http.Request, route *route) (string, int) {
}

func checkCustomHeaders(r *http.Request, route *route) (string, int) {
if route.Auth != nil && route.Auth.Custom != nil {
if route.Auth.Custom != nil {
custom := route.Auth.Custom
for k, v := range custom {
headerVal := r.Header.Get(k)
Expand All @@ -50,7 +50,7 @@ func checkCustomHeaders(r *http.Request, route *route) (string, int) {
func checkBearerTokenAuth(r *http.Request, route *route) (string, int) {
header := r.Header.Get("Authorization")

if route.Auth != nil && route.Auth.BearerToken != nil {
if route.Auth.BearerToken != nil {
bearer := route.Auth.BearerToken
if header == "" || header != "Bearer "+bearer.Token {
log.Println(AuthorizationBearerTokenError)
Expand All @@ -64,7 +64,7 @@ func checkBearerTokenAuth(r *http.Request, route *route) (string, int) {
func checkBasicAuth(r *http.Request, route *route) (string, int) {
header := r.Header.Get("Authorization")

if route.Auth != nil && route.Auth.Basic != nil {
if route.Auth.Basic != nil {
basic := route.Auth.Basic
if header == "" || header != basicAuthHeaderValue(basic.Username, basic.Password) {
log.Println(AuthorizationBasicError)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.14

require (
github.com/go-playground/validator/v10 v10.4.1
github.com/rs/cors v1.7.0
github.com/stkr89/skeleton v1.1.1
github.com/stretchr/testify v1.6.1
gopkg.in/yaml.v2 v2.4.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stkr89/skeleton v1.0.1 h1:9sNcuJ+2D+do9Due4pMFk7gvNY8i+UtqdXpPh2QkGEo=
github.com/stkr89/skeleton v1.0.1/go.mod h1:ifLb+vlb8qreSzIfpmQmH4p4I2gKkHh4xewoA4pkdVI=
github.com/rs/cors v1.7.0 h1:+88SsELBHx5r+hZ8TCkggzSstaWNbDvThkVK8H6f9ik=
github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
github.com/stkr89/skeleton v1.1.1 h1:2nLzo80jMVtFYMrbtcpAIjE6rT0r/JKAN/IuecSiUN0=
github.com/stkr89/skeleton v1.1.1/go.mod h1:ifLb+vlb8qreSzIfpmQmH4p4I2gKkHh4xewoA4pkdVI=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
12 changes: 8 additions & 4 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@ func handleAll(w http.ResponseWriter, req *http.Request) {

r := routeConfigMap[getRouteMapKey(req.Method, req.URL.Path)]

errStr, statusCode := checkAuthorization(req, r)
if errStr != "" {
http.Error(w, errStr, statusCode)
return
if r.Auth != nil {
errStr, statusCode := checkAuthorization(req, r)
if errStr != "" {
http.Error(w, errStr, statusCode)
return
}
}

err := checkBody(r.Body, req.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

if r.Response != nil {
Expand All @@ -49,6 +52,7 @@ func sendResponse(w http.ResponseWriter, r *route) {
respStr, err := json.Marshal(r.Response)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

w.Header().Add("Content-Type", "application/json")
Expand Down
4 changes: 3 additions & 1 deletion server.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package esme

import (
"github.com/rs/cors"
"log"
"net/http"
)
Expand All @@ -22,7 +23,8 @@ func Serve(port string, paths ...string) {

func launchServer(port string) {
m := http.NewServeMux()
s := http.Server{Addr: ":" + port, Handler: m}
handler := cors.Default().Handler(m)
s := http.Server{Addr: ":" + port, Handler: handler}

m.HandleFunc("/", handleAll)
m.HandleFunc("/shutdown", handleShutdown(port, &s))
Expand Down

0 comments on commit 5ce36b3

Please sign in to comment.