Skip to content

Commit

Permalink
Use JWT verifier in API server (#365)
Browse files Browse the repository at this point in the history
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **New Features**
	- Added JWT-based authentication to the API server.
	- Introduced conditional JWT verification for server initialization.

- **Improvements**
	- Enhanced server authentication capabilities.
- Enabled flexible JWT verification across different server
configurations.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
neekolas authored Jan 6, 2025
1 parent 9d1a2bb commit ab9d996
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
19 changes: 16 additions & 3 deletions pkg/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package api

import (
"context"
"github.com/xmtp/xmtpd/pkg/interceptors/server"
"net"
"strings"
"sync"
"time"

"github.com/xmtp/xmtpd/pkg/authn"
"github.com/xmtp/xmtpd/pkg/interceptors/server"

"google.golang.org/grpc/reflection"

prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
Expand Down Expand Up @@ -41,6 +43,7 @@ func NewAPIServer(
listenAddress string,
enableReflection bool,
registrationFunc RegistrationFunc,
jwtVerifier authn.JWTVerifier,
) (*ApiServer, error) {
grpcListener, err := net.Listen("tcp", listenAddress)

Expand All @@ -67,8 +70,18 @@ func NewAPIServer(
return nil, err
}

unary := []grpc.UnaryServerInterceptor{prometheus.UnaryServerInterceptor}
stream := []grpc.StreamServerInterceptor{prometheus.StreamServerInterceptor}
unary := []grpc.UnaryServerInterceptor{
prometheus.UnaryServerInterceptor,
}
stream := []grpc.StreamServerInterceptor{
prometheus.StreamServerInterceptor,
}

if jwtVerifier != nil {
interceptor := server.NewAuthInterceptor(jwtVerifier, log)
unary = append(unary, interceptor.Unary())
stream = append(stream, interceptor.Stream())
}

options := []grpc.ServerOption{
grpc.ChainUnaryInterceptor(unary...),
Expand Down
11 changes: 10 additions & 1 deletion pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package server
import (
"context"
"database/sql"
"github.com/xmtp/xmtpd/pkg/mlsvalidate"
"net"
"os"
"os/signal"
"syscall"

"github.com/xmtp/xmtpd/pkg/authn"
"github.com/xmtp/xmtpd/pkg/mlsvalidate"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/xmtp/xmtpd/pkg/api/message"
Expand Down Expand Up @@ -203,12 +205,19 @@ func startAPIServer(
return nil
}

var jwtVerifier authn.JWTVerifier

if s.nodeRegistry != nil && s.registrant != nil {
jwtVerifier = authn.NewRegistryVerifier(s.nodeRegistry, s.registrant.NodeID())
}

s.apiServer, err = api.NewAPIServer(
s.ctx,
log,
listenAddress,
options.Reflection.Enable,
serviceRegistrationFunc,
jwtVerifier,
)
if err != nil {
return err
Expand Down
4 changes: 4 additions & 0 deletions pkg/testutils/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/xmtp/xmtpd/pkg/api"
"github.com/xmtp/xmtpd/pkg/api/message"
"github.com/xmtp/xmtpd/pkg/api/payer"
"github.com/xmtp/xmtpd/pkg/authn"
"github.com/xmtp/xmtpd/pkg/db/queries"
"github.com/xmtp/xmtpd/pkg/mocks/blockchain"
mocks "github.com/xmtp/xmtpd/pkg/mocks/registry"
Expand Down Expand Up @@ -78,6 +79,8 @@ func NewTestAPIServer(t *testing.T) (*api.ApiServer, *sql.DB, func()) {
require.NoError(t, err)
mockMessagePublisher := blockchain.NewMockIBlockchainPublisher(t)

jwtVerifier := authn.NewRegistryVerifier(mockRegistry, registrant.NodeID())

serviceRegistrationFunc := func(grpcServer *grpc.Server) error {
replicationService, err := message.NewReplicationApiService(
ctx,
Expand Down Expand Up @@ -107,6 +110,7 @@ func NewTestAPIServer(t *testing.T) (*api.ApiServer, *sql.DB, func()) {
"localhost:0", /*listenAddress*/
true, /*enableReflection*/
serviceRegistrationFunc,
jwtVerifier,
)
require.NoError(t, err)

Expand Down

0 comments on commit ab9d996

Please sign in to comment.