From aa5fd309549e84fde4eca7ad120dbc3b59d33673 Mon Sep 17 00:00:00 2001 From: Ernst Widerberg Date: Fri, 22 Oct 2021 08:28:31 +0200 Subject: Enable login screen with mocked JWT server --- README.md | 5 ++++- jwt_mock.go | 30 ++++++++++++++++++++++++++++++ src/components/App.js | 4 ++-- src/components/Login.js | 3 +-- 4 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 jwt_mock.go diff --git a/README.md b/README.md index b961dfe..ed1a91e 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,10 @@ ## Development setup -Run `soc_collector` and set env var `COLLECTOR_URL`. +Env vars: +- `COLLECTOR_URL` +- `JWT_URL` +- `PER_PAGE` The commit pointed to by the `soc_collector` submodule is compatible with `main` in this repo. diff --git a/jwt_mock.go b/jwt_mock.go new file mode 100644 index 0000000..e4a46f5 --- /dev/null +++ b/jwt_mock.go @@ -0,0 +1,30 @@ +package main + +import ( + "fmt" + "log" + "net/http" +) + +func main() { + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Access-Control-Allow-Origin", "*") + w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS, POST, PUT") + w.Header().Set("Access-Control-Allow-Headers", "Authorization") + + if r.Method == "OPTIONS" { + w.WriteHeader(http.StatusNoContent) + return + } + + auth := r.Header.Get("Authorization") + if auth != "Basic dXNyOnB3ZA==" { // btoa("usr:pwd") + w.WriteHeader(http.StatusUnauthorized) + return + } + + fmt.Fprint(w, "{\"access_token\": \"JWT_TOKEN_PLACEHOLDER\"}") + }) + + log.Fatal(http.ListenAndServe(":8080", nil)) +} diff --git a/src/components/App.js b/src/components/App.js index 9ead9c5..636c672 100644 --- a/src/components/App.js +++ b/src/components/App.js @@ -56,8 +56,8 @@ class App extends React.Component { clearToken={this.clearToken} /> ); - // if (this.state.token === null) - // return ; + if (this.state.token === null) + return ; return (
diff --git a/src/components/Login.js b/src/components/Login.js index 209871e..9b45817 100644 --- a/src/components/Login.js +++ b/src/components/Login.js @@ -26,8 +26,7 @@ class Login extends React.Component { // NOTE: btoa() limits email, password to ASCII login(email, password) { - const url = process.env.JWT_URL + "/api/v1.0/auth"; - fetch(url, { + fetch(process.env.JWT_URL, { method: "POST", headers: { Authorization: "Basic " + btoa(email + ":" + password) } }) -- cgit v1.1