func TestMultipleAudienceFix_AfterMarshal(t *testing.T) { // Create JWS claims claims := jws.Claims{} claims.SetAudience("example.com", "api.example.com") token := jws.NewJWT(claims, crypto.SigningMethodHS256) serializedToken, _ := token.Serialize([]byte("abcdef")) // Unmarshal JSON newToken, _ := jws.ParseJWT(serializedToken) c := newToken.Claims() // Get Audience aud, ok := c.Audience() if !ok { // Fails t.Fail() } t.Logf("aud len(): %d", len(aud)) t.Logf("aud Value: %s", aud) t.Logf("aud Type : %T", aud) }
// createSignedCustomAuthTokenForUser creates a custom auth token for a given user. func createSignedCustomAuthTokenForUser(uid string, developerClaims *Claims, issuer string, privateKey *rsa.PrivateKey) (string, error) { if uid == "" { return "", errors.New("Uid must be provided.") } if issuer == "" { return "", errors.New("Must provide an issuer.") } if len(uid) > 128 { return "", errors.New("Uid must be shorter than 128 characters") } method := crypto.SigningMethodRS256 claims := jws.Claims{} claims.Set("uid", uid) claims.SetIssuer(issuer) claims.SetSubject(issuer) claims.SetAudience(firebaseAudience) now := clock.Now() claims.SetIssuedAt(now) claims.SetExpiration(now.Add(time.Hour)) if developerClaims != nil { for claim := range *developerClaims { if isReserved(claim) { return "", fmt.Errorf("developer_claims cannot contain a reserved key: %s", claim) } } claims.Set("claims", developerClaims) } jwt := jws.NewJWT(claims, method) bytes, err := jwt.Serialize(privateKey) if err != nil { return "", err } return string(bytes), nil }