Compare commits

...

2 commits

Author SHA1 Message Date
9e02980415 modify html 2024-01-15 20:19:23 +03:00
cbcf3067cb feat: server login & register forms 2024-01-15 18:51:51 +03:00
5 changed files with 129 additions and 40 deletions

View file

@ -1,7 +1,7 @@
{ {
"ContractCheckSum": "none", "ContractCheckSum": "5e4375b9e8214ed4183659114735a94ac26033d7",
"AuthServerPort": 9096, "AuthServerPort": 9096,
"WalletFile": "none", "WalletFile": "../../frostfs-aio/morph/node-wallet.json",
"EndpointUrl": "url", "EndpointUrl": "http://localhost:30333",
"AccountSecret": "one" "AccountSecret": "one"
} }

View file

@ -3,9 +3,6 @@ package main
import ( import (
"auth-server/logic" "auth-server/logic"
"context" "context"
"net/url"
"strconv"
"github.com/go-oauth2/oauth2/v4/errors" "github.com/go-oauth2/oauth2/v4/errors"
"github.com/go-oauth2/oauth2/v4/manage" "github.com/go-oauth2/oauth2/v4/manage"
"github.com/go-oauth2/oauth2/v4/server" "github.com/go-oauth2/oauth2/v4/server"
@ -15,10 +12,12 @@ import (
"github.com/nspcc-dev/neo-go/pkg/rpcclient/actor" "github.com/nspcc-dev/neo-go/pkg/rpcclient/actor"
"github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/wallet" "github.com/nspcc-dev/neo-go/pkg/wallet"
"log" "log"
"log/slog" "log/slog"
"net/http" "net/http"
"net/url"
"os"
"strconv"
) )
func main() { func main() {
@ -97,12 +96,12 @@ func main() {
r.Form.Add("grant_type", r.URL.Query().Get("grant_type")) r.Form.Add("grant_type", r.URL.Query().Get("grant_type"))
r.Form.Add("scope", r.URL.Query().Get("scope")) r.Form.Add("scope", r.URL.Query().Get("scope"))
srv.HandleTokenRequest(w, r) // verifying secret srv.HandleTokenRequest(w, r) // grants access token
}) })
http.HandleFunc("/register", func(writer http.ResponseWriter, request *http.Request) { http.HandleFunc("/register", func(writer http.ResponseWriter, request *http.Request) {
id := request.Header.Get("client_id") id := request.URL.Query().Get("client_id")
secret := request.Header.Get("client_secret") secret := request.URL.Query().Get("client_secret")
// check whether client exists // check whether client exists
_, err := blockchainStorage.GetByID(context.Background(), id) _, err := blockchainStorage.GetByID(context.Background(), id)
@ -114,16 +113,6 @@ func main() {
return return
} }
/* redundant
// add client's credentials to in memory storage
err = logic.AddInMemoryClient(id, "", "", false)
if err != nil {
slog.Error("Fault during setting client credentials", err)
writer.WriteHeader(http.StatusInternalServerError)
return
}
*/
// add client's credentials to blockchain // add client's credentials to blockchain
err = blockchainStorage.Set(&logic.StorageClientInfo{ err = blockchainStorage.Set(&logic.StorageClientInfo{
Id: id, Id: id,
@ -136,7 +125,8 @@ func main() {
return return
} }
writer.WriteHeader(http.StatusOK) redirectURL := "/login.html"
http.Redirect(writer, request, redirectURL, http.StatusSeeOther)
}) })
// for tests, can access only with valid token (when logged in) // for tests, can access only with valid token (when logged in)
@ -166,10 +156,6 @@ func main() {
w.Write([]byte(errorMessage)) w.Write([]byte(errorMessage))
} }
/* redundant
logic.DeleteInMemoryClient(id)
*/
}, srv)) }, srv))
// can access only with valid token (when logged in), deletes client and creates new one with another secret // can access only with valid token (when logged in), deletes client and creates new one with another secret
@ -195,21 +181,6 @@ func main() {
w.Write([]byte(errorMessage)) w.Write([]byte(errorMessage))
} }
/* redundant
logic.DeleteInMemoryClient(id)
*/
/*
// add client with new credentials to in memory storage
err = logic.AddInMemoryClient(id, "", "", false)
if err != nil {
slog.Error(errorMessage+" (caused by in memory storage) for client with id: "+id, err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(errorMessage))
return
}
*/
// add client with new credentials to blockchain // add client with new credentials to blockchain
err = blockchainStorage.Set(&logic.StorageClientInfo{ err = blockchainStorage.Set(&logic.StorageClientInfo{
Id: id, Id: id,
@ -224,5 +195,24 @@ func main() {
} }
}, srv)) }, srv))
http.HandleFunc("/login.html", func(w http.ResponseWriter, r *http.Request) {
outputHTML(w, r, "static/login.html")
})
http.HandleFunc("/register.html", func(w http.ResponseWriter, r *http.Request) {
outputHTML(w, r, "static/register.html")
})
log.Fatal(http.ListenAndServe(":"+strconv.Itoa(config.AuthServerPort), nil)) log.Fatal(http.ListenAndServe(":"+strconv.Itoa(config.AuthServerPort), nil))
} }
func outputHTML(w http.ResponseWriter, req *http.Request, filename string) {
file, err := os.Open(filename)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
defer file.Close()
fi, _ := file.Stat()
http.ServeContent(w, req, file.Name(), fi.ModTime(), file)
}

View file

@ -0,0 +1,59 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login</title>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="//code.jquery.com/jquery-2.2.4.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h1>Sign In</h1>
<form id="loginForm" action="/login" method="GET">
<input type="hidden" name="grant_type" value="client_credentials" />
<input type="hidden" name="scope" value="all" />
<div class="form-group">
<label for="client_id">User Name</label>
<input type="text" class="form-control" name="client_id" required placeholder="Please enter your user name">
</div>
<div class="form-group">
<label for="client_secret">Password</label>
<input type="password" class="form-control" name="client_secret" placeholder="Please enter your password">
</div>
<button type="submit" class="btn btn-success">Sign In</button>
</form>
<form id="registerForm" action="/register.html" method="GET">
<button type="submit" class="btn btn-primary">Register</button>
</form>
</div>
<script>
function submitFormWithReferer(formId) {
var form = document.getElementById(formId);
var referrer = document.referrer || "";
form.insertAdjacentHTML('beforeend', '<input type="hidden" name="referer" value="' + referrer + '">');
form.submit();
}
document.getElementById("loginForm").addEventListener("submit", function (event) {
event.preventDefault();
submitFormWithReferer("loginForm");
});
document.getElementById("registerForm").addEventListener("submit", function (event) {
event.preventDefault();
submitFormWithReferer("registerForm");
});
</script>
</body>
</html>
<style>
.btn-primary {
margin-top: 10px;
}
</style>

View file

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Redirect</title>
<meta http-equiv="refresh" content="0;url=http://localhost:9096/login.html">
</head>
<body>
<p>If you are not redirected, <a href="http://localhost:9096/login.html">click here</a>.</p>
</body>
</html>

View file

@ -0,0 +1,29 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login</title>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="//code.jquery.com/jquery-2.2.4.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h1>Sign Up</h1>
<form action="/register" method="GET">
<div class="form-group">
<label for="client_id">User Name</label>
<input type="text" class="form-control" name="client_id" required placeholder="Please enter your user name">
</div>
<div class="form-group">
<label for="client_secret">Password</label>
<input type="password" class="form-control" name="client_secret" placeholder="Please enter your password">
</div>
<button type="submit" class="btn btn-success">Register</button>
</form>
</div>
</body>
</html>