Skip to content

Commit db1ab22

Browse files
committed
feat: use session token for API auth
Signed-off-by: Aurora Gaffney <aurora@blinklabs.io>
1 parent a42b89d commit db1ab22

8 files changed

Lines changed: 762 additions & 347 deletions

File tree

cmd/vpn-indexer/main.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,19 @@ func main() {
157157
var crlInstance *crl.Crl
158158
var wgClient *wireguard.Client
159159
var s3Client *client.Client
160+
var jwtIssuer *jwt.Issuer
161+
162+
// Initialize the JWT issuer up front: it signs browser session tokens for
163+
// every protocol (so /api/auth/session is available regardless of protocol)
164+
// and also authenticates the indexer to the WireGuard container. The key
165+
// file config is shared (VPN_WG_JWT_KEY_FILE).
166+
jwtIssuer, err = jwt.NewIssuer(cfg.Vpn.WGJWTKeyFile)
167+
if err != nil {
168+
slog.Error(
169+
fmt.Sprintf("failed to initialize JWT issuer: %s", err),
170+
)
171+
os.Exit(1)
172+
}
160173

161174
switch cfg.Vpn.Protocol {
162175
case "openvpn":
@@ -181,15 +194,6 @@ func main() {
181194
// Initialize WireGuard components if protocol is wireguard
182195
slog.Info("initializing WireGuard components")
183196

184-
// Initialize JWT issuer for WG container authentication
185-
jwtIssuer, err := jwt.NewIssuer(cfg.Vpn.WGJWTKeyFile)
186-
if err != nil {
187-
slog.Error(
188-
fmt.Sprintf("failed to initialize JWT issuer: %s", err),
189-
)
190-
os.Exit(1)
191-
}
192-
193197
// Initialize WG container client
194198
wgClient = wireguard.NewClient(cfg.Vpn.WGContainerURL, jwtIssuer)
195199

@@ -252,7 +256,7 @@ func main() {
252256
}
253257

254258
// Start API listener
255-
if err := api.Start(cfg, db, caInstance, wgClient, s3Client); err != nil {
259+
if err := api.Start(cfg, db, caInstance, wgClient, s3Client, jwtIssuer); err != nil {
256260
slog.Error(
257261
"failed to start API:",
258262
"error",

internal/api/api.go

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/blinklabs-io/vpn-indexer/internal/client"
2626
"github.com/blinklabs-io/vpn-indexer/internal/config"
2727
"github.com/blinklabs-io/vpn-indexer/internal/database"
28+
"github.com/blinklabs-io/vpn-indexer/internal/jwt"
2829
"github.com/blinklabs-io/vpn-indexer/internal/wireguard"
2930
httpSwagger "github.com/swaggo/http-swagger"
3031
)
@@ -35,11 +36,12 @@ const (
3536

3637
// Api holds the dependencies for the API server.
3738
type Api struct {
38-
cfg *config.Config
39-
db *database.Database
40-
ca *ca.Ca
41-
wgClient *wireguard.Client
42-
s3Client *client.Client
39+
cfg *config.Config
40+
db *database.Database
41+
ca *ca.Ca
42+
wgClient *wireguard.Client
43+
s3Client *client.Client
44+
jwtIssuer *jwt.Issuer
4345
}
4446

4547
// @title vpn-indexer
@@ -58,16 +60,18 @@ func Start(
5860
ca *ca.Ca,
5961
wgClient *wireguard.Client,
6062
s3Client *client.Client,
63+
jwtIssuer *jwt.Issuer,
6164
) error {
6265
logger := slog.Default()
6366
logger.Info("initializing API server")
6467

6568
api := &Api{
66-
cfg: cfg,
67-
db: db,
68-
ca: ca,
69-
wgClient: wgClient,
70-
s3Client: s3Client,
69+
cfg: cfg,
70+
db: db,
71+
ca: ca,
72+
wgClient: wgClient,
73+
s3Client: s3Client,
74+
jwtIssuer: jwtIssuer,
7175
}
7276

7377
//
@@ -91,6 +95,15 @@ func Start(
9195
mainMux.HandleFunc("/api/tx/transfer", api.handleTxTransfer)
9296
mainMux.HandleFunc("/api/tx/submit", api.handleTxSubmit)
9397

98+
// Session auth route (only register when a JWT issuer is available)
99+
if api.jwtIssuer != nil {
100+
mainMux.HandleFunc("/api/auth/session", api.handleAuthSession)
101+
} else {
102+
logger.Warn(
103+
"session auth route not registered: jwtIssuer is nil",
104+
)
105+
}
106+
94107
// WireGuard API routes (only register when both wgClient and s3Client are available)
95108
if api.wgClient != nil && api.s3Client != nil {
96109
mainMux.HandleFunc("/api/client/wg-register", api.handleWGRegister)

0 commit comments

Comments
 (0)