-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c506465
commit 153477b
Showing
6 changed files
with
125 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package server | ||
|
||
import ( | ||
"net/http" | ||
) | ||
|
||
func BearerAuthMiddleware(next http.Handler, token string) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
|
||
headerValue := r.Header.Get("Authorization") | ||
if headerValue == "" { | ||
http.Error(w, "Authorization header is required", http.StatusUnauthorized) | ||
return | ||
} | ||
|
||
expectedToken := "Bearer " + token | ||
if headerValue != expectedToken { | ||
http.Error(w, "Invalid token", http.StatusUnauthorized) | ||
return | ||
} | ||
|
||
next.ServeHTTP(w, r) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package server_test | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/riesinger/plausible-exporter/server" | ||
) | ||
|
||
func TestValidToken(t *testing.T) { | ||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
}) | ||
|
||
testToken := "secret-token" | ||
|
||
req, _ := http.NewRequest("GET", "/", nil) | ||
req.Header.Set("Authorization", "Bearer "+testToken) | ||
rr := httptest.NewRecorder() | ||
|
||
handlerWithMiddleware := server.BearerAuthMiddleware(handler, testToken) | ||
handlerWithMiddleware.ServeHTTP(rr, req) | ||
|
||
if status := rr.Code; status != http.StatusOK { | ||
t.Errorf("Handler returned wrong status code: got %v want %v", status, http.StatusOK) | ||
} | ||
} | ||
|
||
func TestInvalidToken(t *testing.T) { | ||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
t.Error("Handler should not be called with an invalid token") | ||
}) | ||
|
||
testToken := "secret-token" | ||
|
||
req, _ := http.NewRequest("GET", "/", nil) | ||
req.Header.Set("Authorization", "Bearer wrong-token") | ||
rr := httptest.NewRecorder() | ||
|
||
handlerWithMiddleware := server.BearerAuthMiddleware(handler, testToken) | ||
handlerWithMiddleware.ServeHTTP(rr, req) | ||
|
||
if status := rr.Code; status != http.StatusUnauthorized { | ||
t.Errorf("Handler returned wrong status code: got %v want %v", status, http.StatusUnauthorized) | ||
} | ||
} | ||
|
||
func TestMissingToken(t *testing.T) { | ||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
t.Error("Handler should not be called without a token") | ||
}) | ||
|
||
testToken := "secret-token" | ||
|
||
req, _ := http.NewRequest("GET", "/", nil) | ||
rr := httptest.NewRecorder() | ||
|
||
handlerWithMiddleware := server.BearerAuthMiddleware(handler, testToken) | ||
handlerWithMiddleware.ServeHTTP(rr, req) | ||
|
||
if status := rr.Code; status != http.StatusUnauthorized { | ||
t.Errorf("Handler returned wrong status code: got %v want %v", status, http.StatusUnauthorized) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters