Skip to content

Commit

Permalink
feat: add bearer token auth for /decentralized and /rss routers (#436)
Browse files Browse the repository at this point in the history
  • Loading branch information
pseudoyu authored Aug 2, 2024
1 parent 182001c commit ee2e96b
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 16 deletions.
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ type Operator struct {
type Server struct {
Endpoint string `mapstructure:"endpoint"`
GlobalIndexerEndpoint string `mapstructure:"global_indexer_endpoint"`
AccessToken string `mapstructure:"access_token"`
}

type Component struct {
Expand Down
6 changes: 5 additions & 1 deletion config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ discovery:
server:
endpoint: https://node.mydomain.com/
global_indexer_endpoint: https://gi.rss3.dev/
access_token: test
endpoints:
ethereum:
url: https://rpc.ankr.com/eth
Expand Down Expand Up @@ -98,7 +99,8 @@ component:
},
"server": {
"endpoint": "https://node.mydomain.com/",
"global_indexer_endpoint": "https://gi.rss3.dev/"
"global_indexer_endpoint": "https://gi.rss3.dev/",
"access_token": "test"
}
},
"database": {
Expand Down Expand Up @@ -184,6 +186,7 @@ url = "https://rpc.ankr.com/eth"
[discovery.server]
endpoint = "https://node.mydomain.com/"
global_indexer_endpoint = "https://gi.rss3.dev/"
access_token = "test"
[database]
driver = "cockroachdb"
Expand Down Expand Up @@ -262,6 +265,7 @@ var configFileExpected = &File{
Server: &Server{
Endpoint: "https://node.mydomain.com/",
GlobalIndexerEndpoint: "https://gi.rss3.dev/",
AccessToken: "test",
},
},
Component: &Component{
Expand Down
36 changes: 36 additions & 0 deletions docs/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
"tags": [
"Decentralized"
],
"security": [
{
"bearerAuth": []
}
],
"parameters": [
{
"$ref": "#/components/parameters/activity_id_path"
Expand Down Expand Up @@ -58,6 +63,11 @@
"tags": [
"Decentralized"
],
"security": [
{
"bearerAuth": []
}
],
"parameters": [
{
"$ref": "#/components/parameters/account_path"
Expand Down Expand Up @@ -119,6 +129,11 @@
"tags": [
"Decentralized"
],
"security": [
{
"bearerAuth": []
}
],
"requestBody": {
"$ref": "#/components/requestBodies/BatchGetAccountsActivities"
},
Expand All @@ -142,6 +157,11 @@
"tags": [
"Decentralized"
],
"security": [
{
"bearerAuth": []
}
],
"parameters": [
{
"$ref": "#/components/parameters/network_path"
Expand Down Expand Up @@ -200,6 +220,11 @@
"tags": [
"Decentralized"
],
"security": [
{
"bearerAuth": []
}
],
"parameters": [
{
"$ref": "#/components/parameters/platform_path"
Expand Down Expand Up @@ -258,6 +283,11 @@
"tags": [
"RSS"
],
"security": [
{
"bearerAuth": []
}
],
"parameters": [
{
"$ref": "#/components/parameters/rss_path"
Expand Down Expand Up @@ -321,6 +351,12 @@
}
},
"components": {
"securitySchemes": {
"bearerAuth": {
"type": "http",
"scheme": "bearer"
}
},
"parameters": {
"activity_id_path": {
"description": "Retrieve details for the specified activity ID",
Expand Down
26 changes: 14 additions & 12 deletions internal/node/broadcaster/broadcaster.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ import (

func (b *Broadcaster) Register(ctx context.Context) error {
request := RegisterNodeRequest{
Address: b.config.Discovery.Operator.EvmAddress,
Signature: b.config.Discovery.Operator.Signature,
Endpoint: b.config.Discovery.Server.Endpoint,
Stream: b.config.Stream,
Config: b.config.Component,
Type: b.config.Type,
Address: b.config.Discovery.Operator.EvmAddress,
Signature: b.config.Discovery.Operator.Signature,
Endpoint: b.config.Discovery.Server.Endpoint,
AccessToken: b.config.Discovery.Server.AccessToken,
Stream: b.config.Stream,
Config: b.config.Component,
Type: b.config.Type,
}

var response any
Expand Down Expand Up @@ -104,12 +105,13 @@ func (b *Broadcaster) sendRequest(ctx context.Context, path string, values url.V
}

type RegisterNodeRequest struct {
Address common.Address `json:"address" validate:"required"`
Signature string `json:"signature" validate:"required"`
Endpoint string `json:"endpoint" validate:"required"`
Stream *config.Stream `json:"stream,omitempty"`
Config *config.Component `json:"config,omitempty"`
Type string `json:"type" validate:"required"`
Address common.Address `json:"address" validate:"required"`
Signature string `json:"signature" validate:"required"`
Endpoint string `json:"endpoint" validate:"required"`
AccessToken string `json:"access_token,omitempty"`
Stream *config.Stream `json:"stream,omitempty"`
Config *config.Component `json:"config,omitempty"`
Type string `json:"type" validate:"required"`
}

type NodeHeartbeatRequest struct {
Expand Down
4 changes: 4 additions & 0 deletions internal/node/component/decentralized/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/rss3-network/node/internal/constant"
"github.com/rss3-network/node/internal/database"
"github.com/rss3-network/node/internal/node/component"
"github.com/rss3-network/node/internal/node/component/middleware"
"github.com/rss3-network/node/provider/ethereum/etherface"
"github.com/samber/lo"
"go.opentelemetry.io/otel"
Expand Down Expand Up @@ -55,6 +56,9 @@ func NewComponent(_ context.Context, apiServer *echo.Echo, config *config.File,

group := apiServer.Group(fmt.Sprintf("/%s", Name))

// Add middleware for bearer token authentication
group.Use(middleware.BearerAuth(config.Discovery.Server.AccessToken))

group.GET("/tx/:id", c.GetActivity)
group.GET("/:account", c.GetAccountActivities)
group.GET("/network/:network", c.GetNetworkActivities)
Expand Down
35 changes: 35 additions & 0 deletions internal/node/component/middleware/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package middleware

import (
"net/http"
"strings"

"github.com/labstack/echo/v4"
)

// BearerAuth middleware for bearer token authentication
func BearerAuth(accessToken string) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
authHeader := c.Request().Header.Get("Authorization")
if authHeader == "" {
return echo.NewHTTPError(http.StatusUnauthorized, "Missing Authorization header")
}

// Check if the header starts with "Bearer "
if !strings.HasPrefix(authHeader, "Bearer ") {
return echo.NewHTTPError(http.StatusUnauthorized, "Invalid Authorization header format")
}

// Extract the token
token := strings.TrimPrefix(authHeader, "Bearer ")

// Verify the token
if token != accessToken {
return echo.NewHTTPError(http.StatusUnauthorized, "Invalid access token")
}

return next(c)
}
}
}
10 changes: 8 additions & 2 deletions internal/node/component/rss/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/rss3-network/node/config"
"github.com/rss3-network/node/internal/constant"
"github.com/rss3-network/node/internal/node/component"
"github.com/rss3-network/node/internal/node/component/middleware"
"github.com/rss3-network/node/schema/worker"
"github.com/rss3-network/protocol-go/schema/network"
"go.opentelemetry.io/otel"
Expand All @@ -18,6 +19,7 @@ import (
)

type Component struct {
config *config.File
httpClient *http.Client
rsshub *configx
counter metric.Int64Counter
Expand All @@ -39,20 +41,24 @@ func (h *Component) Name() string {

var _ component.Component = (*Component)(nil)

func NewComponent(_ context.Context, apiServer *echo.Echo, config []*config.Module) component.Component {
func NewComponent(_ context.Context, apiServer *echo.Echo, config *config.File) component.Component {
c := &Component{
config: config,
httpClient: http.DefaultClient,
}

group := apiServer.Group(fmt.Sprintf("/%s", Name))

// Add middleware for bearer token authentication
group.Use(middleware.BearerAuth(config.Discovery.Server.AccessToken))

group.GET("/*", c.Handler)

if err := c.InitMeter(); err != nil {
panic(err)
}

for _, conf := range config {
for _, conf := range config.Component.RSS {
if conf.Network == network.RSS {
c.rsshub = &configx{
id: conf.ID,
Expand Down
2 changes: 1 addition & 1 deletion internal/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func NewCoreService(ctx context.Context, config *config.File, databaseClient dat
node.components = append(node.components, &infoComponent)

if len(config.Component.RSS) > 0 {
rssComponent := rss.NewComponent(ctx, apiServer, config.Component.RSS)
rssComponent := rss.NewComponent(ctx, apiServer, config)
node.components = append(node.components, &rssComponent)
}

Expand Down

0 comments on commit ee2e96b

Please sign in to comment.