Repository navigation
Expand file tree
/
Copy pathpush.go
More file actions
116 lines (104 loc) · 3.98 KB
/
Copy pathpush.go
File metadata and controls
116 lines (104 loc) · 3.98 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 gmailapi
import (
"context"
"crypto/subtle"
"errors"
"fmt"
"net/http"
"strings"
"google.golang.org/api/idtoken"
)
// PushConfig configures how the bridge authenticates a Gmail Pub/Sub push
// (GMAIL_API_PLAN §6.4, FR-S.14). Exactly one mode is active: when PlainSecret
// is non-empty, a shared secret is checked instead of an OIDC token — the
// loopback/fixture escape hatch; otherwise the request must carry a valid
// Google-signed OIDC token for Audience whose `email` claim equals
// ServiceAccount.
type PushConfig struct {
// Audience is the OIDC audience the Pub/Sub push subscription mints
// for (typically the bridge's public base_url).
Audience string
// ServiceAccount is the `email` claim the token must carry.
ServiceAccount string
// PlainSecret, when set, switches to shared-secret mode (loopback
// rigs); the account's client token is used for this in production.
PlainSecret string
}
// PushVerifier authenticates the `Authorization: Bearer` token on a
// `POST /gmail/push/{account}` request. It is the seam the HTTP layer and
// tests depend on; the concrete type is built by NewPushVerifier.
type PushVerifier struct {
cfg PushConfig
validate idTokenValidator
}
// idTokenValidator validates a Google OIDC token and returns its claims. It is
// a function value so tests substitute a fake without minting real tokens;
// production wraps idtoken.Validate.
type idTokenValidator func(ctx context.Context, token, audience string) (map[string]any, error)
// NewPushVerifier builds the production verifier (idtoken.Validate).
func NewPushVerifier(cfg PushConfig) *PushVerifier {
return newPushVerifier(cfg, validateGoogleIDToken)
}
// newPushVerifier builds a verifier with an injected token validator.
func newPushVerifier(cfg PushConfig, validate idTokenValidator) *PushVerifier {
return &PushVerifier{cfg: cfg, validate: validate}
}
// validateGoogleIDToken is the production idtoken.Validate wrapper; the
// official package checks the signature, issuer, expiry and audience, and its
// claims carry the `email` of the signing service account.
func validateGoogleIDToken(ctx context.Context, token, audience string) (map[string]any, error) {
payload, err := idtoken.Validate(ctx, token, audience)
if err != nil {
return nil, err
}
return payload.Claims, nil
}
// Verify authenticates one push request. It fails closed: any missing,
// malformed or mismatched credential is an error and the caller answers
// non-2xx.
func (v *PushVerifier) Verify(ctx context.Context, r *http.Request) error {
if v.cfg.PlainSecret != "" {
return v.verifyPlain(r)
}
if v.cfg.Audience == "" || v.cfg.ServiceAccount == "" {
return errors.New("gmailapi: push verifier misconfigured")
}
return v.verifyOIDC(ctx, r)
}
func (v *PushVerifier) verifyOIDC(ctx context.Context, r *http.Request) error {
token := bearerToken(r)
if token == "" {
return errors.New("gmailapi: push missing bearer token")
}
claims, err := v.validate(ctx, token, v.cfg.Audience)
if err != nil {
return fmt.Errorf("gmailapi: push token invalid: %w", err)
}
email, _ := claims["email"].(string)
if !strings.EqualFold(email, v.cfg.ServiceAccount) {
return fmt.Errorf("gmailapi: push token email %q is not the configured service account", email)
}
if verified, ok := claims["email_verified"].(bool); ok && !verified {
return errors.New("gmailapi: push token email is not verified")
}
return nil
}
func (v *PushVerifier) verifyPlain(r *http.Request) error {
got := r.Header.Get("X-Push-Token")
if got == "" {
got = r.URL.Query().Get("token")
}
if subtle.ConstantTimeCompare([]byte(got), []byte(v.cfg.PlainSecret)) != 1 {
return errors.New("gmailapi: push plain secret mismatch")
}
return nil
}
// bearerToken extracts the token from an `Authorization: Bearer …` header.
func bearerToken(r *http.Request) string {
h := r.Header.Get("Authorization")
const prefix = "Bearer "
if len(h) < len(prefix) || !strings.EqualFold(h[:len(prefix)], prefix) {
return ""
}
return strings.TrimSpace(h[len(prefix):])
}