add: meta proxy

This commit is contained in:
Mohamed Sohail 2022-05-31 11:35:49 +03:00
parent 7fc0e0681b
commit a3127af026
Signed by: kamikazechaser
GPG Key ID: 7DD45520C01CD85D
8 changed files with 86 additions and 14 deletions

View File

@ -6,6 +6,7 @@ import (
"strings"
batch_balance "github.com/grassrootseconomics/cic-go/batch_balance"
"github.com/grassrootseconomics/cic-go/meta"
cic_net "github.com/grassrootseconomics/cic-go/net"
"github.com/ethereum/go-ethereum/common"
@ -40,6 +41,12 @@ type config struct {
Enabled bool `koan:"enabled"`
}
Syncers map[string]string `koanf:"syncers"`
Meta struct {
Endpoint string `koanf:"meta"`
}
Jwt struct {
Secret string `koanf:"secret"`
}
}
type queries struct {
@ -111,6 +118,10 @@ func loadCicNet(tokenIndex common.Address) error {
return nil
}
func loadCicMeta(metaEndpoint string) {
metaClient = meta.NewCicMeta(metaEndpoint)
}
func loadBatchBalance(balanceResolver common.Address) error {
var err error

View File

@ -8,6 +8,7 @@ import (
"time"
batch_balance "github.com/grassrootseconomics/cic-go/batch_balance"
"github.com/grassrootseconomics/cic-go/meta"
cic_net "github.com/grassrootseconomics/cic-go/net"
"github.com/grassrootseconomics/cic-go/provider"
"github.com/hibiken/asynq"
@ -28,6 +29,7 @@ var (
rpcProvider *provider.Provider
cicnetClient *cic_net.CicNet
batchBalance *batch_balance.BatchBalance
metaClient *meta.CicMeta
rClient asynq.RedisConnOpt
)
@ -59,9 +61,10 @@ func init() {
}
if err := parseRedis(conf.Db.Redis); err != nil {
log.Fatal().Err(err).Msg("could not parse redis connection string")
}
loadCicMeta(conf.Meta.Endpoint)
}
func main() {

View File

@ -22,7 +22,7 @@ func initHTTPServer() *echo.Echo {
dashboard.InitDashboardApi(server, db, preparedQueries.dashboard)
public.InitPublicApi(server, db, batchBalance, cicnetClient, preparedQueries.public)
admin.InitAdminApi(server, db, preparedQueries.admin, "test")
admin.InitAdminApi(server, db, preparedQueries.admin, metaClient, conf.Jwt.Secret)
return server
}

View File

@ -25,4 +25,10 @@ enabled = true
[syncers]
cache = "@every 20s"
ussd = "@every 30s"
token = "@every 1m"
token = "@every 1m"
[meta]
endpoint = "http://cic-meta-server:8000"
[jwt]
secret = "0xd34db33f"

1
go.mod
View File

@ -42,6 +42,7 @@ require (
github.com/jackc/pgtype v1.11.0 // indirect
github.com/jackc/puddle v1.2.1 // indirect
github.com/labstack/gommon v0.3.1 // indirect
github.com/mapaiva/vcard-go v1.2.0 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect

2
go.sum
View File

@ -393,6 +393,8 @@ github.com/lmittmann/w3 v0.7.0 h1:z3Z7OeLAQ/oHWK8k5U5SH+G2BD6ZbsdrBktUytDzG78=
github.com/lmittmann/w3 v0.7.0/go.mod h1:8F9LNaa0D/k68rnCdTtBsMon8ucpvlS9fmPVXp7Uz7M=
github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mapaiva/vcard-go v1.2.0 h1:w/Jmdow8CDE3T/6SBSTt1HBTDxZmQ39a415hCzElAN8=
github.com/mapaiva/vcard-go v1.2.0/go.mod h1:OedyhOJ8gUHmfp6iI3+2bf0kzQetFRpD625G9NCEB6Y=
github.com/matryer/moq v0.0.0-20190312154309-6cfb0558e1bd/go.mod h1:9ELz6aaclSIGnZBoaSLZ3NAl1VTufbOrXBPvtcy6WiQ=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ=

View File

@ -4,6 +4,7 @@ import (
"net/http"
"github.com/golang-jwt/jwt"
"github.com/grassrootseconomics/cic-go/meta"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/labstack/echo/v4"
"github.com/nleof/goyesql"
@ -13,11 +14,12 @@ import (
type api struct {
db *pgxpool.Pool
q goyesql.Queries
m *meta.CicMeta
jwtKey []byte
}
func InitAdminApi(e *echo.Echo, db *pgxpool.Pool, queries goyesql.Queries, jwtKey string) {
api := newApi(db, queries, jwtKey)
func InitAdminApi(e *echo.Echo, db *pgxpool.Pool, queries goyesql.Queries, metaClient *meta.CicMeta, jwtKey string) {
api := newApi(db, queries, metaClient, jwtKey)
auth := e.Group(("/auth"))
g := e.Group("/admin")
@ -29,13 +31,15 @@ func InitAdminApi(e *echo.Echo, db *pgxpool.Pool, queries goyesql.Queries, jwtKe
g.Use(api.dwCoreMiddleware)
g.Use(api.verifyAuthMiddleware)
g.GET("/protected", handleProtectedResource)
g.GET("/meta-proxy/:address", handleMetaProxy)
}
func newApi(db *pgxpool.Pool, queries goyesql.Queries, jwtKey string) *api {
func newApi(db *pgxpool.Pool, queries goyesql.Queries, metaClient *meta.CicMeta, jwtKey string) *api {
log.Info().Msgf("%s inj", jwtKey)
return &api{
db: db,
q: queries,
m: metaClient,
jwtKey: []byte(jwtKey),
}
}
@ -45,6 +49,7 @@ func (a *api) dwCoreMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
c.Set("api", &api{
db: a.db,
q: a.q,
m: a.m,
jwtKey: a.jwtKey,
})
return next(c)
@ -53,7 +58,6 @@ func (a *api) dwCoreMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
func (a *api) verifyAuthMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
log.Info().Msgf("%v", c.Cookies())
cookie, err := c.Cookie("_ge_auth")
if err != nil {
return c.String(http.StatusForbidden, "auth cookie missing")
@ -65,10 +69,7 @@ func (a *api) verifyAuthMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
return a.jwtKey, nil
})
if err != nil {
if err == jwt.ErrSignatureInvalid {
return c.String(http.StatusUnauthorized, "jwt signature validation failed")
}
return c.String(http.StatusBadRequest, "jwt bad request")
return c.String(http.StatusUnauthorized, "jwt validation failed")
}
if !token.Valid {
return c.String(http.StatusUnauthorized, "jwt invalid")

View File

@ -1,11 +1,59 @@
package admin
import (
"bytes"
"encoding/base64"
"net/http"
"strings"
"github.com/grassrootseconomics/cic-go/meta"
"github.com/labstack/echo/v4"
"github.com/mapaiva/vcard-go"
)
func handleProtectedResource(c echo.Context) error {
return c.String(http.StatusOK, "unlocked")
type metaRes struct {
Person meta.PersonResponse `json:"person"`
Name string `json:"name"`
}
func handleMetaProxy(c echo.Context) error {
var (
api = c.Get("api").(*api)
address = c.Param("address")
)
person, err := api.m.GetPersonMetadata(address)
if err != nil {
if strings.Contains(err.Error(), "404") {
return c.String(http.StatusNotFound, "meta resource not found")
} else {
return err
}
}
vCard, err := parseVCard(person.VCard)
if err != nil {
return err
}
return c.JSON(http.StatusOK, &metaRes{
Person: person,
Name: vCard.FormattedName,
})
}
func parseVCard(encodedVCard string) (vcard.VCard, error) {
data, err := base64.StdEncoding.DecodeString(encodedVCard)
if err != nil {
return vcard.VCard{}, err
}
reader := bytes.NewReader(data)
vCards, err := vcard.GetVCardsByReader(reader)
if err != nil {
return vcard.VCard{}, nil
}
return vCards[0], nil
}