neowolves/auth-server/logic/tools.go
2024-01-15 17:49:10 +03:00

63 lines
1.3 KiB
Go

package logic
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"github.com/go-oauth2/oauth2/v4/server"
"net/http"
"os"
)
type Config struct {
ContractCheckSum string `json:"ContractCheckSum"`
AuthServerPort int `json:"AuthServerPort"`
WalletFile string `json:"WalletFile"`
EndpointUrl string `json:"EndpointUrl"`
AccountSecret string `json:"AccountSecret"`
}
func LoadConfig(pathToJsonConfig string) (Config, error) {
var config Config
file, err := os.Open(pathToJsonConfig)
if err != nil {
return config, err
}
defer file.Close()
decoder := json.NewDecoder(file)
err = decoder.Decode(&config)
if err != nil {
return config, err
}
return config, nil
}
func ValidateToken(f http.HandlerFunc, srv *server.Server) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := srv.ValidationBearerToken(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
f.ServeHTTP(w, r)
})
}
func HashSecret(secret string) string {
encoder := sha256.New()
encoder.Write([]byte(secret))
hashBytes := encoder.Sum(nil)
return hex.EncodeToString(hashBytes)
}
func AddDefaultClientCredentialsIfNotExists(id string) error {
if _, ok := clients[id]; ok {
return nil
}
return AddInMemoryClient(id, "", "", false)
}