-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathmailbox_auth_rpc.go
More file actions
116 lines (92 loc) · 2.79 KB
/
Copy pathmailbox_auth_rpc.go
File metadata and controls
116 lines (92 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package serverconn
import (
"context"
"fmt"
mailboxpb "github.com/lightninglabs/wavelength/mailbox/pb"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)
// MailboxAuthSigner returns the hex-encoded x-mailbox-auth-sig value for a
// recipient mailbox ID.
type MailboxAuthSigner func(context.Context, string) (string, error)
// authenticatedMailboxClient adds mailbox auth metadata before forwarding to
// the configured mailbox transport.
type authenticatedMailboxClient struct {
next mailboxpb.MailboxServiceClient
sign MailboxAuthSigner
}
// NewAuthenticatedMailboxClient wraps a mailbox edge with per-mailbox auth
// metadata. Nil signers return next unchanged for unauthenticated test
// transports.
func NewAuthenticatedMailboxClient(next mailboxpb.MailboxServiceClient,
sign MailboxAuthSigner) mailboxpb.MailboxServiceClient {
if sign == nil {
return next
}
return &authenticatedMailboxClient{
next: next,
sign: sign,
}
}
// Send authenticates the sender for the envelope's recipient mailbox.
func (c *authenticatedMailboxClient) Send(ctx context.Context,
req *mailboxpb.SendRequest, opts ...grpc.CallOption) (
*mailboxpb.SendResponse, error) {
recipient := ""
if req != nil && req.Envelope != nil {
recipient = req.Envelope.Recipient
}
ctx, err := c.authContext(ctx, recipient)
if err != nil {
return nil, err
}
return c.next.Send(ctx, req, opts...)
}
// Pull authenticates access to the requested mailbox.
func (c *authenticatedMailboxClient) Pull(ctx context.Context,
req *mailboxpb.PullRequest, opts ...grpc.CallOption) (
*mailboxpb.PullResponse, error) {
recipient := ""
if req != nil {
recipient = req.MailboxId
}
ctx, err := c.authContext(ctx, recipient)
if err != nil {
return nil, err
}
return c.next.Pull(ctx, req, opts...)
}
// AckUpTo authenticates access to the requested mailbox cursor.
func (c *authenticatedMailboxClient) AckUpTo(ctx context.Context,
req *mailboxpb.AckUpToRequest, opts ...grpc.CallOption) (
*mailboxpb.AckUpToResponse, error) {
recipient := ""
if req != nil {
recipient = req.MailboxId
}
ctx, err := c.authContext(ctx, recipient)
if err != nil {
return nil, err
}
return c.next.AckUpTo(ctx, req, opts...)
}
// authContext adds the mailbox auth metadata understood by both gRPC and the
// REST client's outgoing metadata bridge.
func (c *authenticatedMailboxClient) authContext(ctx context.Context,
recipient string) (context.Context, error) {
if recipient == "" {
return nil, fmt.Errorf("mailbox recipient is required")
}
sig, err := c.sign(ctx, recipient)
if err != nil {
return nil, fmt.Errorf("sign mailbox auth: %w", err)
}
md, ok := metadata.FromOutgoingContext(ctx)
if ok {
md = md.Copy()
} else {
md = metadata.New(nil)
}
md.Set(AuthHeaderKey, sig)
return metadata.NewOutgoingContext(ctx, md), nil
}