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 (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/go-oauth2/oauth2/v4"
|
||||
"github.com/nspcc-dev/neo-go/pkg/util"
|
||||
"log/slog"
|
||||
)
|
||||
|
||||
type InMemoryClient struct {
|
||||
|
@ -68,18 +68,21 @@ func (model StorageClientInfo) GetSecret() string {
|
|||
}
|
||||
|
||||
func (model StorageClientInfo) GetDomain() string {
|
||||
client, _ := GetInMemoryClient(model.Id)
|
||||
return client.Domain
|
||||
//client, _ := GetInMemoryClient(model.Id)
|
||||
//return client.Domain
|
||||
return ""
|
||||
}
|
||||
|
||||
func (model StorageClientInfo) IsPublic() bool {
|
||||
client, _ := GetInMemoryClient(model.Id)
|
||||
return client.IsPublic
|
||||
//client, _ := GetInMemoryClient(model.Id)
|
||||
//return client.IsPublic
|
||||
return false
|
||||
}
|
||||
|
||||
func (model StorageClientInfo) GetUserID() string {
|
||||
client, _ := GetInMemoryClient(model.Id)
|
||||
return client.UserID
|
||||
//client, _ := GetInMemoryClient(model.Id)
|
||||
//return client.UserID
|
||||
return model.Id
|
||||
}
|
||||
|
||||
type IBlockchainStorage interface {
|
||||
|
@ -112,6 +115,13 @@ func (storage BlockchainStorage) Set(clt oauth2.ClientInfo) error {
|
|||
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)
|
||||
if err != nil {
|
||||
_, _, err = storage.contract.CreateUser(clt.GetID(), password)
|
||||
|
@ -124,10 +134,14 @@ func (storage BlockchainStorage) Set(clt oauth2.ClientInfo) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (storage BlockchainStorage) Delete(id string) error {
|
||||
func (storage BlockchainStorage) Delete(id string) (err error) {
|
||||
// should we use hash and ValidUntilBlock?
|
||||
|
||||
_, _, res := storage.contract.DeleteUser(id)
|
||||
return res
|
||||
if res != nil {
|
||||
err = res
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (storage BlockchainStorage) CheckPassword(id string, secret string) (bool, error) {
|
||||
|
|
|
@ -54,3 +54,10 @@ func HashSecret(secret string) string {
|
|||
|
||||
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 (
|
||||
"auth-server/logic"
|
||||
"context"
|
||||
"net/url"
|
||||
"strconv"
|
||||
|
||||
"github.com/go-oauth2/oauth2/v4/errors"
|
||||
|
@ -30,22 +31,24 @@ func main() {
|
|||
slog.Info("ContractCheckSum: " + config.ContractCheckSum)
|
||||
slog.Info("AuthServerPort: " + strconv.Itoa(config.AuthServerPort))
|
||||
slog.Info("WalletFile: " + config.WalletFile)
|
||||
slog.Info("EndpointUrl: " + config.EndpointUrl)
|
||||
slog.Info("AccountSecret: " + config.AccountSecret)
|
||||
|
||||
manager := manage.NewDefaultManager()
|
||||
manager.SetAuthorizeCodeTokenCfg(manage.DefaultAuthorizeCodeTokenCfg)
|
||||
|
||||
// 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]
|
||||
// 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 {
|
||||
log.Fatal("Wallet decryption failed")
|
||||
}
|
||||
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(?)
|
||||
rpcClient, _ := rpcclient.New(context.Background(), config.EndpointUrl, rpcclient.Options{})
|
||||
rpcActor, _ := actor.NewSimple(rpcClient, fileWallet.Accounts[0])
|
||||
|
@ -78,8 +81,9 @@ func main() {
|
|||
http.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {
|
||||
id := r.URL.Query().Get("client_id")
|
||||
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 {
|
||||
msg := "Credentials verification failed"
|
||||
slog.Warn(msg + " for client with id: " + id)
|
||||
|
@ -87,6 +91,12 @@ func main() {
|
|||
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
|
||||
})
|
||||
|
||||
|
@ -104,6 +114,7 @@ func main() {
|
|||
return
|
||||
}
|
||||
|
||||
/* redundant
|
||||
// add client's credentials to in memory storage
|
||||
err = logic.AddInMemoryClient(id, "", "", false)
|
||||
if err != nil {
|
||||
|
@ -111,6 +122,7 @@ func main() {
|
|||
writer.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
*/
|
||||
|
||||
// add client's credentials to blockchain
|
||||
err = blockchainStorage.Set(&logic.StorageClientInfo{
|
||||
|
@ -137,14 +149,26 @@ func main() {
|
|||
id := r.Header.Get("client_id")
|
||||
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 {
|
||||
slog.Error(errorMessage+" (caused by blockchain) with id: "+id, err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(errorMessage))
|
||||
}
|
||||
|
||||
/* redundant
|
||||
logic.DeleteInMemoryClient(id)
|
||||
*/
|
||||
|
||||
}, srv))
|
||||
|
||||
|
@ -154,21 +178,37 @@ func main() {
|
|||
secret := r.Header.Get("new_client_secret")
|
||||
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 {
|
||||
slog.Error(errorMessage+" for client with id: "+id, err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(errorMessage))
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
/* 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
|
||||
err = blockchainStorage.Set(&logic.StorageClientInfo{
|
||||
|
|
Loading…
Reference in a new issue