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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
|
package appcheck
import (
"context"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"errors"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
"firebase.google.com/go/v4/internal"
"github.com/golang-jwt/jwt/v4"
"github.com/google/go-cmp/cmp"
)
func TestVerifyTokenHasValidClaims(t *testing.T) {
ts, err := setupFakeJWKS()
if err != nil {
t.Fatalf("Error setting up fake JWKS server: %v", err)
}
defer ts.Close()
privateKey, err := loadPrivateKey()
if err != nil {
t.Fatalf("Error loading private key: %v", err)
}
JWKSUrl = ts.URL
conf := &internal.AppCheckConfig{
ProjectID: "project_id",
}
client, err := NewClient(context.Background(), conf)
if err != nil {
t.Errorf("Error creating NewClient: %v", err)
}
type appCheckClaims struct {
Aud []string `json:"aud"`
jwt.RegisteredClaims
}
mockTime := time.Date(2020, time.January, 1, 0, 0, 0, 0, time.UTC)
jwt.TimeFunc = func() time.Time {
return mockTime
}
tokenTests := []struct {
claims *appCheckClaims
wantErr error
wantToken *DecodedAppCheckToken
}{
{
&appCheckClaims{
[]string{"projects/12345678", "projects/project_id"},
jwt.RegisteredClaims{
Issuer: "https://firebaseappcheck.googleapis.com/12345678",
Subject: "12345678:app:ID",
ExpiresAt: jwt.NewNumericDate(mockTime.Add(time.Hour)),
IssuedAt: jwt.NewNumericDate(mockTime),
}},
nil,
&DecodedAppCheckToken{
Issuer: "https://firebaseappcheck.googleapis.com/12345678",
Subject: "12345678:app:ID",
Audience: []string{"projects/12345678", "projects/project_id"},
ExpiresAt: mockTime.Add(time.Hour),
IssuedAt: mockTime,
AppID: "12345678:app:ID",
Claims: map[string]interface{}{},
},
}, {
&appCheckClaims{
[]string{"projects/12345678", "projects/project_id"},
jwt.RegisteredClaims{
Issuer: "https://firebaseappcheck.googleapis.com/12345678",
Subject: "12345678:app:ID",
ExpiresAt: jwt.NewNumericDate(mockTime.Add(time.Hour)),
IssuedAt: jwt.NewNumericDate(mockTime),
// A field our AppCheckToken does not use.
NotBefore: jwt.NewNumericDate(mockTime.Add(-1 * time.Hour)),
}},
nil,
&DecodedAppCheckToken{
Issuer: "https://firebaseappcheck.googleapis.com/12345678",
Subject: "12345678:app:ID",
Audience: []string{"projects/12345678", "projects/project_id"},
ExpiresAt: mockTime.Add(time.Hour),
IssuedAt: mockTime,
AppID: "12345678:app:ID",
Claims: map[string]interface{}{
"nbf": float64(mockTime.Add(-1 * time.Hour).Unix()),
},
},
}, {
&appCheckClaims{
[]string{"projects/0000000", "projects/another_project_id"},
jwt.RegisteredClaims{
Issuer: "https://firebaseappcheck.googleapis.com/12345678",
Subject: "12345678:app:ID",
ExpiresAt: jwt.NewNumericDate(mockTime.Add(time.Hour)),
IssuedAt: jwt.NewNumericDate(mockTime),
}},
ErrTokenAudience,
nil,
}, {
&appCheckClaims{
[]string{"projects/12345678", "projects/project_id"},
jwt.RegisteredClaims{
Issuer: "https://not-firebaseappcheck.googleapis.com/12345678",
Subject: "12345678:app:ID",
ExpiresAt: jwt.NewNumericDate(mockTime.Add(time.Hour)),
IssuedAt: jwt.NewNumericDate(mockTime),
}},
ErrTokenIssuer,
nil,
}, {
&appCheckClaims{
[]string{"projects/12345678", "projects/project_id"},
jwt.RegisteredClaims{
Issuer: "https://firebaseappcheck.googleapis.com/12345678",
Subject: "",
ExpiresAt: jwt.NewNumericDate(mockTime.Add(time.Hour)),
IssuedAt: jwt.NewNumericDate(mockTime),
}},
ErrTokenSubject,
nil,
}, {
&appCheckClaims{
[]string{"projects/12345678", "projects/project_id"},
jwt.RegisteredClaims{
Issuer: "https://firebaseappcheck.googleapis.com/12345678",
ExpiresAt: jwt.NewNumericDate(mockTime.Add(time.Hour)),
IssuedAt: jwt.NewNumericDate(mockTime),
}},
ErrTokenSubject,
nil,
},
}
for _, tc := range tokenTests {
// Create an App Check-style token.
jwtToken := jwt.NewWithClaims(jwt.SigningMethodRS256, tc.claims)
// kid matches the key ID in testdata/mock.jwks.json,
// which is the public key matching to the private key
// in testdata/appcheck_pk.pem.
jwtToken.Header["kid"] = "FGQdnRlzAmKyKr6-Hg_kMQrBkj_H6i6ADnBQz4OI6BU"
token, err := jwtToken.SignedString(privateKey)
if err != nil {
t.Fatalf("error generating JWT: %v", err)
}
// Verify the token.
gotToken, gotErr := client.VerifyToken(token)
if !errors.Is(gotErr, tc.wantErr) {
t.Errorf("Expected error %v, got %v", tc.wantErr, gotErr)
continue
}
if diff := cmp.Diff(tc.wantToken, gotToken); diff != "" {
t.Errorf("VerifyToken mismatch (-want +got):\n%s", diff)
}
}
}
func TestVerifyTokenMustExist(t *testing.T) {
ts, err := setupFakeJWKS()
if err != nil {
t.Fatalf("Error setting up fake JWK server: %v", err)
}
defer ts.Close()
JWKSUrl = ts.URL
conf := &internal.AppCheckConfig{
ProjectID: "project_id",
}
client, err := NewClient(context.Background(), conf)
if err != nil {
t.Errorf("Error creating NewClient: %v", err)
}
for _, token := range []string{"", "-", "."} {
gotToken, gotErr := client.VerifyToken(token)
if gotErr == nil {
t.Errorf("VerifyToken(%s) expected error, got nil", token)
}
if gotToken != nil {
t.Errorf("Expected nil, got token %v", gotToken)
}
}
}
func TestVerifyTokenNotExpired(t *testing.T) {
ts, err := setupFakeJWKS()
if err != nil {
t.Fatalf("Error setting up fake JWKS server: %v", err)
}
defer ts.Close()
privateKey, err := loadPrivateKey()
if err != nil {
t.Fatalf("Error loading private key: %v", err)
}
JWKSUrl = ts.URL
conf := &internal.AppCheckConfig{
ProjectID: "project_id",
}
client, err := NewClient(context.Background(), conf)
if err != nil {
t.Errorf("Error creating NewClient: %v", err)
}
mockTime := time.Date(2020, time.January, 1, 0, 0, 0, 0, time.UTC)
jwt.TimeFunc = func() time.Time {
return mockTime
}
tokenTests := []struct {
expiresAt time.Time
wantErr bool
}{
// Expire in the future is OK.
{mockTime.Add(time.Hour), false},
// Expire in the past is not OK.
{mockTime.Add(-1 * time.Hour), true},
}
for _, tc := range tokenTests {
claims := struct {
Aud []string `json:"aud"`
jwt.RegisteredClaims
}{
[]string{"projects/12345678", "projects/project_id"},
jwt.RegisteredClaims{
Issuer: "https://firebaseappcheck.googleapis.com/12345678",
Subject: "12345678:app:ID",
ExpiresAt: jwt.NewNumericDate(tc.expiresAt),
IssuedAt: jwt.NewNumericDate(mockTime),
},
}
jwtToken := jwt.NewWithClaims(jwt.SigningMethodRS256, claims)
jwtToken.Header["kid"] = "FGQdnRlzAmKyKr6-Hg_kMQrBkj_H6i6ADnBQz4OI6BU"
token, err := jwtToken.SignedString(privateKey)
if err != nil {
t.Fatalf("error generating JWT: %v", err)
}
_, gotErr := client.VerifyToken(token)
if tc.wantErr && gotErr == nil {
t.Errorf("Expected an error, got none")
} else if !tc.wantErr && gotErr != nil {
t.Errorf("Expected no error, got %v", gotErr)
}
}
}
func setupFakeJWKS() (*httptest.Server, error) {
jwks, err := os.ReadFile("../testdata/mock.jwks.json")
if err != nil {
return nil, err
}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write(jwks)
}))
return ts, nil
}
func loadPrivateKey() (*rsa.PrivateKey, error) {
pk, err := os.ReadFile("../testdata/appcheck_pk.pem")
if err != nil {
return nil, err
}
block, _ := pem.Decode(pk)
privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
return privateKey, nil
}
|