Skip to content

Commit ce711f9

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

8 files changed

Lines changed: 788 additions & 220 deletions

File tree

cmd/vpn-indexer/main.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ func main() {
157157
var crlInstance *crl.Crl
158158
var wgClient *wireguard.Client
159159
var s3Client *client.Client
160+
var jwtIssuer *jwt.Issuer
160161

161162
switch cfg.Vpn.Protocol {
162163
case "openvpn":
@@ -182,7 +183,8 @@ func main() {
182183
slog.Info("initializing WireGuard components")
183184

184185
// Initialize JWT issuer for WG container authentication
185-
jwtIssuer, err := jwt.NewIssuer(cfg.Vpn.WGJWTKeyFile)
186+
// and browser session tokens
187+
jwtIssuer, err = jwt.NewIssuer(cfg.Vpn.WGJWTKeyFile)
186188
if err != nil {
187189
slog.Error(
188190
fmt.Sprintf("failed to initialize JWT issuer: %s", err),
@@ -252,7 +254,7 @@ func main() {
252254
}
253255

254256
// Start API listener
255-
if err := api.Start(cfg, db, caInstance, wgClient, s3Client); err != nil {
257+
if err := api.Start(cfg, db, caInstance, wgClient, s3Client, jwtIssuer); err != nil {
256258
slog.Error(
257259
"failed to start API:",
258260
"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)