fix api methods
This commit is contained in:
parent
b931a8ac18
commit
ddfae53e76
3 changed files with 86 additions and 25 deletions
|
@ -3,9 +3,9 @@ package logic
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/go-oauth2/oauth2/v4"
|
"github.com/go-oauth2/oauth2/v4"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/util"
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
||||||
|
"log/slog"
|
||||||
)
|
)
|
||||||
|
|
||||||
type InMemoryClient struct {
|
type InMemoryClient struct {
|
||||||
|
@ -68,18 +68,21 @@ func (model StorageClientInfo) GetSecret() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (model StorageClientInfo) GetDomain() string {
|
func (model StorageClientInfo) GetDomain() string {
|
||||||
client, _ := GetInMemoryClient(model.Id)
|
//client, _ := GetInMemoryClient(model.Id)
|
||||||
return client.Domain
|
//return client.Domain
|
||||||
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func (model StorageClientInfo) IsPublic() bool {
|
func (model StorageClientInfo) IsPublic() bool {
|
||||||
client, _ := GetInMemoryClient(model.Id)
|
//client, _ := GetInMemoryClient(model.Id)
|
||||||
return client.IsPublic
|
//return client.IsPublic
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (model StorageClientInfo) GetUserID() string {
|
func (model StorageClientInfo) GetUserID() string {
|
||||||
client, _ := GetInMemoryClient(model.Id)
|
//client, _ := GetInMemoryClient(model.Id)
|
||||||
return client.UserID
|
//return client.UserID
|
||||||
|
return model.Id
|
||||||
}
|
}
|
||||||
|
|
||||||
type IBlockchainStorage interface {
|
type IBlockchainStorage interface {
|
||||||
|
@ -112,6 +115,13 @@ func (storage BlockchainStorage) Set(clt oauth2.ClientInfo) error {
|
||||||
return decodeErr
|
return decodeErr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
_, _, _ = storage.contract.CreateUser(clt.GetID(), password)
|
||||||
|
slog.Warn("Recovered. Error during Set operation in BlockchainStorage:\n", r)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
_, _, err := storage.contract.UpdateUser(clt.GetID(), password)
|
_, _, err := storage.contract.UpdateUser(clt.GetID(), password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_, _, err = storage.contract.CreateUser(clt.GetID(), password)
|
_, _, err = storage.contract.CreateUser(clt.GetID(), password)
|
||||||
|
@ -124,10 +134,14 @@ func (storage BlockchainStorage) Set(clt oauth2.ClientInfo) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (storage BlockchainStorage) Delete(id string) error {
|
func (storage BlockchainStorage) Delete(id string) (err error) {
|
||||||
// should we use hash and ValidUntilBlock?
|
// should we use hash and ValidUntilBlock?
|
||||||
|
|
||||||
_, _, res := storage.contract.DeleteUser(id)
|
_, _, res := storage.contract.DeleteUser(id)
|
||||||
return res
|
if res != nil {
|
||||||
|
err = res
|
||||||
|
}
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (storage BlockchainStorage) CheckPassword(id string, secret string) (bool, error) {
|
func (storage BlockchainStorage) CheckPassword(id string, secret string) (bool, error) {
|
||||||
|
|
|
@ -54,3 +54,10 @@ func HashSecret(secret string) string {
|
||||||
|
|
||||||
return hex.EncodeToString(hashBytes)
|
return hex.EncodeToString(hashBytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func AddDefaultClientCredentialsIfNotExists(id string) error {
|
||||||
|
if _, ok := clients[id]; ok {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return AddInMemoryClient(id, "", "", false)
|
||||||
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"auth-server/logic"
|
"auth-server/logic"
|
||||||
"context"
|
"context"
|
||||||
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/go-oauth2/oauth2/v4/errors"
|
"github.com/go-oauth2/oauth2/v4/errors"
|
||||||
|
@ -30,22 +31,24 @@ func main() {
|
||||||
slog.Info("ContractCheckSum: " + config.ContractCheckSum)
|
slog.Info("ContractCheckSum: " + config.ContractCheckSum)
|
||||||
slog.Info("AuthServerPort: " + strconv.Itoa(config.AuthServerPort))
|
slog.Info("AuthServerPort: " + strconv.Itoa(config.AuthServerPort))
|
||||||
slog.Info("WalletFile: " + config.WalletFile)
|
slog.Info("WalletFile: " + config.WalletFile)
|
||||||
|
slog.Info("EndpointUrl: " + config.EndpointUrl)
|
||||||
|
slog.Info("AccountSecret: " + config.AccountSecret)
|
||||||
|
|
||||||
manager := manage.NewDefaultManager()
|
manager := manage.NewDefaultManager()
|
||||||
manager.SetAuthorizeCodeTokenCfg(manage.DefaultAuthorizeCodeTokenCfg)
|
manager.SetAuthorizeCodeTokenCfg(manage.DefaultAuthorizeCodeTokenCfg)
|
||||||
|
|
||||||
// contract integration
|
// contract integration
|
||||||
fileWallet, _ := wallet.NewWalletFromFile(config.WalletFile)
|
fileWallet, err := wallet.NewWalletFromFile(config.WalletFile)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln("Wallet loading failed.", err)
|
||||||
|
}
|
||||||
|
|
||||||
acc := fileWallet.Accounts[0]
|
acc := fileWallet.Accounts[0]
|
||||||
// FIXME: account password should be in the config: FIXED
|
|
||||||
// FIXME: Idk which password and url we should use
|
|
||||||
if err := acc.Decrypt(config.AccountSecret, keys.NEP2ScryptParams()); err != nil {
|
if err := acc.Decrypt(config.AccountSecret, keys.NEP2ScryptParams()); err != nil {
|
||||||
log.Fatal("Wallet decryption failed")
|
log.Fatal("Wallet decryption failed")
|
||||||
}
|
}
|
||||||
defer fileWallet.Close()
|
defer fileWallet.Close()
|
||||||
|
|
||||||
// FIXME: endpoint url should be in the config FIXED
|
|
||||||
// FIXME: Idk which password and url we should use
|
|
||||||
// In idea we need an rpc-server(?)
|
// In idea we need an rpc-server(?)
|
||||||
rpcClient, _ := rpcclient.New(context.Background(), config.EndpointUrl, rpcclient.Options{})
|
rpcClient, _ := rpcclient.New(context.Background(), config.EndpointUrl, rpcclient.Options{})
|
||||||
rpcActor, _ := actor.NewSimple(rpcClient, fileWallet.Accounts[0])
|
rpcActor, _ := actor.NewSimple(rpcClient, fileWallet.Accounts[0])
|
||||||
|
@ -78,8 +81,9 @@ func main() {
|
||||||
http.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {
|
||||||
id := r.URL.Query().Get("client_id")
|
id := r.URL.Query().Get("client_id")
|
||||||
secret := r.URL.Query().Get("client_secret")
|
secret := r.URL.Query().Get("client_secret")
|
||||||
|
secret_hash := logic.HashSecret(secret)
|
||||||
|
|
||||||
_, err := blockchainStorage.CheckPassword(id, logic.HashSecret(secret))
|
_, err := blockchainStorage.CheckPassword(id, secret_hash)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
msg := "Credentials verification failed"
|
msg := "Credentials verification failed"
|
||||||
slog.Warn(msg + " for client with id: " + id)
|
slog.Warn(msg + " for client with id: " + id)
|
||||||
|
@ -87,6 +91,12 @@ func main() {
|
||||||
w.Write([]byte(msg))
|
w.Write([]byte(msg))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
r.Form = make(url.Values)
|
||||||
|
r.Form.Add("client_id", id)
|
||||||
|
r.Form.Add("client_secret", secret_hash)
|
||||||
|
r.Form.Add("grant_type", r.URL.Query().Get("grant_type"))
|
||||||
|
r.Form.Add("scope", r.URL.Query().Get("scope"))
|
||||||
|
|
||||||
srv.HandleTokenRequest(w, r) // verifying secret
|
srv.HandleTokenRequest(w, r) // verifying secret
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -104,6 +114,7 @@ func main() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* redundant
|
||||||
// add client's credentials to in memory storage
|
// add client's credentials to in memory storage
|
||||||
err = logic.AddInMemoryClient(id, "", "", false)
|
err = logic.AddInMemoryClient(id, "", "", false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -111,6 +122,7 @@ func main() {
|
||||||
writer.WriteHeader(http.StatusInternalServerError)
|
writer.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
// add client's credentials to blockchain
|
// add client's credentials to blockchain
|
||||||
err = blockchainStorage.Set(&logic.StorageClientInfo{
|
err = blockchainStorage.Set(&logic.StorageClientInfo{
|
||||||
|
@ -137,14 +149,26 @@ func main() {
|
||||||
id := r.Header.Get("client_id")
|
id := r.Header.Get("client_id")
|
||||||
errorMessage := "Fault during deleting client"
|
errorMessage := "Fault during deleting client"
|
||||||
|
|
||||||
err := blockchainStorage.Delete(id)
|
// check whether client exists
|
||||||
|
_, err := blockchainStorage.GetByID(context.Background(), id)
|
||||||
|
if err != nil {
|
||||||
|
msg := "Client not found with id: " + id
|
||||||
|
slog.Warn(msg)
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
w.Write([]byte(msg))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = blockchainStorage.Delete(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error(errorMessage+" (caused by blockchain) with id: "+id, err)
|
slog.Error(errorMessage+" (caused by blockchain) with id: "+id, err)
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
w.Write([]byte(errorMessage))
|
w.Write([]byte(errorMessage))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* redundant
|
||||||
logic.DeleteInMemoryClient(id)
|
logic.DeleteInMemoryClient(id)
|
||||||
|
*/
|
||||||
|
|
||||||
}, srv))
|
}, srv))
|
||||||
|
|
||||||
|
@ -154,21 +178,37 @@ func main() {
|
||||||
secret := r.Header.Get("new_client_secret")
|
secret := r.Header.Get("new_client_secret")
|
||||||
errorMessage := "Fault during secret reset"
|
errorMessage := "Fault during secret reset"
|
||||||
|
|
||||||
err := blockchainStorage.Delete(id)
|
// check whether client exists
|
||||||
|
_, err := blockchainStorage.GetByID(context.Background(), id)
|
||||||
|
if err != nil {
|
||||||
|
msg := "Client not found with id: " + id
|
||||||
|
slog.Warn(msg)
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
w.Write([]byte(msg))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = blockchainStorage.Delete(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error(errorMessage+" for client with id: "+id, err)
|
slog.Error(errorMessage+" for client with id: "+id, err)
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
w.Write([]byte(errorMessage))
|
w.Write([]byte(errorMessage))
|
||||||
}
|
}
|
||||||
|
|
||||||
// add client with new credentials to in memory storage
|
/* redundant
|
||||||
err = logic.AddInMemoryClient(id, "", "", false)
|
logic.DeleteInMemoryClient(id)
|
||||||
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))
|
// add client with new credentials to in memory storage
|
||||||
return
|
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{
|
||||||
|
|
Loading…
Reference in a new issue