github.com/lestrrat-go/jwx/v2@v2.0.21/jwt/openid/token_gen.go (about) 1 // Code generated by tools/cmd/genjwt/main.go. DO NOT EDIT. 2 3 package openid 4 5 import ( 6 "bytes" 7 "context" 8 "fmt" 9 "sort" 10 "sync" 11 "time" 12 13 "github.com/lestrrat-go/iter/mapiter" 14 "github.com/lestrrat-go/jwx/v2/internal/base64" 15 "github.com/lestrrat-go/jwx/v2/internal/iter" 16 "github.com/lestrrat-go/jwx/v2/internal/json" 17 "github.com/lestrrat-go/jwx/v2/internal/pool" 18 "github.com/lestrrat-go/jwx/v2/jwt" 19 "github.com/lestrrat-go/jwx/v2/jwt/internal/types" 20 ) 21 22 const ( 23 AddressKey = "address" 24 AudienceKey = "aud" 25 BirthdateKey = "birthdate" 26 EmailKey = "email" 27 EmailVerifiedKey = "email_verified" 28 ExpirationKey = "exp" 29 FamilyNameKey = "family_name" 30 GenderKey = "gender" 31 GivenNameKey = "given_name" 32 IssuedAtKey = "iat" 33 IssuerKey = "iss" 34 JwtIDKey = "jti" 35 LocaleKey = "locale" 36 MiddleNameKey = "middle_name" 37 NameKey = "name" 38 NicknameKey = "nickname" 39 NotBeforeKey = "nbf" 40 PhoneNumberKey = "phone_number" 41 PhoneNumberVerifiedKey = "phone_number_verified" 42 PictureKey = "picture" 43 PreferredUsernameKey = "preferred_username" 44 ProfileKey = "profile" 45 SubjectKey = "sub" 46 UpdatedAtKey = "updated_at" 47 WebsiteKey = "website" 48 ZoneinfoKey = "zoneinfo" 49 ) 50 51 type Token interface { 52 53 // Address returns the value for "address" field of the token 54 Address() *AddressClaim 55 56 // Audience returns the value for "aud" field of the token 57 Audience() []string 58 59 // Birthdate returns the value for "birthdate" field of the token 60 Birthdate() *BirthdateClaim 61 62 // Email returns the value for "email" field of the token 63 Email() string 64 65 // EmailVerified returns the value for "email_verified" field of the token 66 EmailVerified() bool 67 68 // Expiration returns the value for "exp" field of the token 69 Expiration() time.Time 70 71 // FamilyName returns the value for "family_name" field of the token 72 FamilyName() string 73 74 // Gender returns the value for "gender" field of the token 75 Gender() string 76 77 // GivenName returns the value for "given_name" field of the token 78 GivenName() string 79 80 // IssuedAt returns the value for "iat" field of the token 81 IssuedAt() time.Time 82 83 // Issuer returns the value for "iss" field of the token 84 Issuer() string 85 86 // JwtID returns the value for "jti" field of the token 87 JwtID() string 88 89 // Locale returns the value for "locale" field of the token 90 Locale() string 91 92 // MiddleName returns the value for "middle_name" field of the token 93 MiddleName() string 94 95 // Name returns the value for "name" field of the token 96 Name() string 97 98 // Nickname returns the value for "nickname" field of the token 99 Nickname() string 100 101 // NotBefore returns the value for "nbf" field of the token 102 NotBefore() time.Time 103 104 // PhoneNumber returns the value for "phone_number" field of the token 105 PhoneNumber() string 106 107 // PhoneNumberVerified returns the value for "phone_number_verified" field of the token 108 PhoneNumberVerified() bool 109 110 // Picture returns the value for "picture" field of the token 111 Picture() string 112 113 // PreferredUsername returns the value for "preferred_username" field of the token 114 PreferredUsername() string 115 116 // Profile returns the value for "profile" field of the token 117 Profile() string 118 119 // Subject returns the value for "sub" field of the token 120 Subject() string 121 122 // UpdatedAt returns the value for "updated_at" field of the token 123 UpdatedAt() time.Time 124 125 // Website returns the value for "website" field of the token 126 Website() string 127 128 // Zoneinfo returns the value for "zoneinfo" field of the token 129 Zoneinfo() string 130 131 // PrivateClaims return the entire set of fields (claims) in the token 132 // *other* than the pre-defined fields such as `iss`, `nbf`, `iat`, etc. 133 PrivateClaims() map[string]interface{} 134 135 // Get returns the value of the corresponding field in the token, such as 136 // `nbf`, `exp`, `iat`, and other user-defined fields. If the field does not 137 // exist in the token, the second return value will be `false` 138 // 139 // If you need to access fields like `alg`, `kid`, `jku`, etc, you need 140 // to access the corresponding fields in the JWS/JWE message. For this, 141 // you will need to access them by directly parsing the payload using 142 // `jws.Parse` and `jwe.Parse` 143 Get(string) (interface{}, bool) 144 145 // Set assigns a value to the corresponding field in the token. Some 146 // pre-defined fields such as `nbf`, `iat`, `iss` need their values to 147 // be of a specific type. See the other getter methods in this interface 148 // for the types of each of these fields 149 Set(string, interface{}) error 150 Remove(string) error 151 152 // Options returns the per-token options associated with this token. 153 // The options set value will be copied when the token is cloned via `Clone()` 154 // but it will not survive when the token goes through marshaling/unmarshaling 155 // such as `json.Marshal` and `json.Unmarshal` 156 Options() *jwt.TokenOptionSet 157 Clone() (jwt.Token, error) 158 Iterate(context.Context) Iterator 159 Walk(context.Context, Visitor) error 160 AsMap(context.Context) (map[string]interface{}, error) 161 } 162 type stdToken struct { 163 mu *sync.RWMutex 164 dc DecodeCtx // per-object context for decoding 165 options jwt.TokenOptionSet // per-object option 166 address *AddressClaim 167 audience types.StringList // https://tools.ietf.org/html/rfc7519#section-4.1.3 168 birthdate *BirthdateClaim 169 email *string 170 emailVerified *bool 171 expiration *types.NumericDate // https://tools.ietf.org/html/rfc7519#section-4.1.4 172 familyName *string 173 gender *string 174 givenName *string 175 issuedAt *types.NumericDate // https://tools.ietf.org/html/rfc7519#section-4.1.6 176 issuer *string // https://tools.ietf.org/html/rfc7519#section-4.1.1 177 jwtID *string // https://tools.ietf.org/html/rfc7519#section-4.1.7 178 locale *string 179 middleName *string 180 name *string 181 nickname *string 182 notBefore *types.NumericDate // https://tools.ietf.org/html/rfc7519#section-4.1.5 183 phoneNumber *string 184 phoneNumberVerified *bool 185 picture *string 186 preferredUsername *string 187 profile *string 188 subject *string // https://tools.ietf.org/html/rfc7519#section-4.1.2 189 updatedAt *types.NumericDate 190 website *string 191 zoneinfo *string 192 privateClaims map[string]interface{} 193 } 194 195 // New creates a standard token, with minimal knowledge of 196 // possible claims. Standard claims include"address", "aud", "birthdate", "email", "email_verified", "exp", "family_name", "gender", "given_name", "iat", "iss", "jti", "locale", "middle_name", "name", "nickname", "nbf", "phone_number", "phone_number_verified", "picture", "preferred_username", "profile", "sub", "updated_at", "website" and "zoneinfo". 197 // Convenience accessors are provided for these standard claims 198 func New() Token { 199 return &stdToken{ 200 mu: &sync.RWMutex{}, 201 privateClaims: make(map[string]interface{}), 202 options: jwt.DefaultOptionSet(), 203 } 204 } 205 206 func (t *stdToken) Options() *jwt.TokenOptionSet { 207 return &t.options 208 } 209 210 func (t *stdToken) Get(name string) (interface{}, bool) { 211 t.mu.RLock() 212 defer t.mu.RUnlock() 213 switch name { 214 case AddressKey: 215 if t.address == nil { 216 return nil, false 217 } 218 v := t.address 219 return v, true 220 case AudienceKey: 221 if t.audience == nil { 222 return nil, false 223 } 224 v := t.audience.Get() 225 return v, true 226 case BirthdateKey: 227 if t.birthdate == nil { 228 return nil, false 229 } 230 v := t.birthdate 231 return v, true 232 case EmailKey: 233 if t.email == nil { 234 return nil, false 235 } 236 v := *(t.email) 237 return v, true 238 case EmailVerifiedKey: 239 if t.emailVerified == nil { 240 return nil, false 241 } 242 v := *(t.emailVerified) 243 return v, true 244 case ExpirationKey: 245 if t.expiration == nil { 246 return nil, false 247 } 248 v := t.expiration.Get() 249 return v, true 250 case FamilyNameKey: 251 if t.familyName == nil { 252 return nil, false 253 } 254 v := *(t.familyName) 255 return v, true 256 case GenderKey: 257 if t.gender == nil { 258 return nil, false 259 } 260 v := *(t.gender) 261 return v, true 262 case GivenNameKey: 263 if t.givenName == nil { 264 return nil, false 265 } 266 v := *(t.givenName) 267 return v, true 268 case IssuedAtKey: 269 if t.issuedAt == nil { 270 return nil, false 271 } 272 v := t.issuedAt.Get() 273 return v, true 274 case IssuerKey: 275 if t.issuer == nil { 276 return nil, false 277 } 278 v := *(t.issuer) 279 return v, true 280 case JwtIDKey: 281 if t.jwtID == nil { 282 return nil, false 283 } 284 v := *(t.jwtID) 285 return v, true 286 case LocaleKey: 287 if t.locale == nil { 288 return nil, false 289 } 290 v := *(t.locale) 291 return v, true 292 case MiddleNameKey: 293 if t.middleName == nil { 294 return nil, false 295 } 296 v := *(t.middleName) 297 return v, true 298 case NameKey: 299 if t.name == nil { 300 return nil, false 301 } 302 v := *(t.name) 303 return v, true 304 case NicknameKey: 305 if t.nickname == nil { 306 return nil, false 307 } 308 v := *(t.nickname) 309 return v, true 310 case NotBeforeKey: 311 if t.notBefore == nil { 312 return nil, false 313 } 314 v := t.notBefore.Get() 315 return v, true 316 case PhoneNumberKey: 317 if t.phoneNumber == nil { 318 return nil, false 319 } 320 v := *(t.phoneNumber) 321 return v, true 322 case PhoneNumberVerifiedKey: 323 if t.phoneNumberVerified == nil { 324 return nil, false 325 } 326 v := *(t.phoneNumberVerified) 327 return v, true 328 case PictureKey: 329 if t.picture == nil { 330 return nil, false 331 } 332 v := *(t.picture) 333 return v, true 334 case PreferredUsernameKey: 335 if t.preferredUsername == nil { 336 return nil, false 337 } 338 v := *(t.preferredUsername) 339 return v, true 340 case ProfileKey: 341 if t.profile == nil { 342 return nil, false 343 } 344 v := *(t.profile) 345 return v, true 346 case SubjectKey: 347 if t.subject == nil { 348 return nil, false 349 } 350 v := *(t.subject) 351 return v, true 352 case UpdatedAtKey: 353 if t.updatedAt == nil { 354 return nil, false 355 } 356 v := t.updatedAt.Get() 357 return v, true 358 case WebsiteKey: 359 if t.website == nil { 360 return nil, false 361 } 362 v := *(t.website) 363 return v, true 364 case ZoneinfoKey: 365 if t.zoneinfo == nil { 366 return nil, false 367 } 368 v := *(t.zoneinfo) 369 return v, true 370 default: 371 v, ok := t.privateClaims[name] 372 return v, ok 373 } 374 } 375 376 func (t *stdToken) Remove(key string) error { 377 t.mu.Lock() 378 defer t.mu.Unlock() 379 switch key { 380 case AddressKey: 381 t.address = nil 382 case AudienceKey: 383 t.audience = nil 384 case BirthdateKey: 385 t.birthdate = nil 386 case EmailKey: 387 t.email = nil 388 case EmailVerifiedKey: 389 t.emailVerified = nil 390 case ExpirationKey: 391 t.expiration = nil 392 case FamilyNameKey: 393 t.familyName = nil 394 case GenderKey: 395 t.gender = nil 396 case GivenNameKey: 397 t.givenName = nil 398 case IssuedAtKey: 399 t.issuedAt = nil 400 case IssuerKey: 401 t.issuer = nil 402 case JwtIDKey: 403 t.jwtID = nil 404 case LocaleKey: 405 t.locale = nil 406 case MiddleNameKey: 407 t.middleName = nil 408 case NameKey: 409 t.name = nil 410 case NicknameKey: 411 t.nickname = nil 412 case NotBeforeKey: 413 t.notBefore = nil 414 case PhoneNumberKey: 415 t.phoneNumber = nil 416 case PhoneNumberVerifiedKey: 417 t.phoneNumberVerified = nil 418 case PictureKey: 419 t.picture = nil 420 case PreferredUsernameKey: 421 t.preferredUsername = nil 422 case ProfileKey: 423 t.profile = nil 424 case SubjectKey: 425 t.subject = nil 426 case UpdatedAtKey: 427 t.updatedAt = nil 428 case WebsiteKey: 429 t.website = nil 430 case ZoneinfoKey: 431 t.zoneinfo = nil 432 default: 433 delete(t.privateClaims, key) 434 } 435 return nil 436 } 437 438 func (t *stdToken) Set(name string, value interface{}) error { 439 t.mu.Lock() 440 defer t.mu.Unlock() 441 return t.setNoLock(name, value) 442 } 443 444 func (t *stdToken) DecodeCtx() DecodeCtx { 445 t.mu.RLock() 446 defer t.mu.RUnlock() 447 return t.dc 448 } 449 450 func (t *stdToken) SetDecodeCtx(v DecodeCtx) { 451 t.mu.Lock() 452 defer t.mu.Unlock() 453 t.dc = v 454 } 455 456 func (t *stdToken) setNoLock(name string, value interface{}) error { 457 switch name { 458 case AddressKey: 459 var acceptor AddressClaim 460 if err := acceptor.Accept(value); err != nil { 461 return fmt.Errorf(`invalid value for %s key: %w`, AddressKey, err) 462 } 463 t.address = &acceptor 464 return nil 465 case AudienceKey: 466 var acceptor types.StringList 467 if err := acceptor.Accept(value); err != nil { 468 return fmt.Errorf(`invalid value for %s key: %w`, AudienceKey, err) 469 } 470 t.audience = acceptor 471 return nil 472 case BirthdateKey: 473 var acceptor BirthdateClaim 474 if err := acceptor.Accept(value); err != nil { 475 return fmt.Errorf(`invalid value for %s key: %w`, BirthdateKey, err) 476 } 477 t.birthdate = &acceptor 478 return nil 479 case EmailKey: 480 if v, ok := value.(string); ok { 481 t.email = &v 482 return nil 483 } 484 return fmt.Errorf(`invalid value for %s key: %T`, EmailKey, value) 485 case EmailVerifiedKey: 486 if v, ok := value.(bool); ok { 487 t.emailVerified = &v 488 return nil 489 } 490 return fmt.Errorf(`invalid value for %s key: %T`, EmailVerifiedKey, value) 491 case ExpirationKey: 492 var acceptor types.NumericDate 493 if err := acceptor.Accept(value); err != nil { 494 return fmt.Errorf(`invalid value for %s key: %w`, ExpirationKey, err) 495 } 496 t.expiration = &acceptor 497 return nil 498 case FamilyNameKey: 499 if v, ok := value.(string); ok { 500 t.familyName = &v 501 return nil 502 } 503 return fmt.Errorf(`invalid value for %s key: %T`, FamilyNameKey, value) 504 case GenderKey: 505 if v, ok := value.(string); ok { 506 t.gender = &v 507 return nil 508 } 509 return fmt.Errorf(`invalid value for %s key: %T`, GenderKey, value) 510 case GivenNameKey: 511 if v, ok := value.(string); ok { 512 t.givenName = &v 513 return nil 514 } 515 return fmt.Errorf(`invalid value for %s key: %T`, GivenNameKey, value) 516 case IssuedAtKey: 517 var acceptor types.NumericDate 518 if err := acceptor.Accept(value); err != nil { 519 return fmt.Errorf(`invalid value for %s key: %w`, IssuedAtKey, err) 520 } 521 t.issuedAt = &acceptor 522 return nil 523 case IssuerKey: 524 if v, ok := value.(string); ok { 525 t.issuer = &v 526 return nil 527 } 528 return fmt.Errorf(`invalid value for %s key: %T`, IssuerKey, value) 529 case JwtIDKey: 530 if v, ok := value.(string); ok { 531 t.jwtID = &v 532 return nil 533 } 534 return fmt.Errorf(`invalid value for %s key: %T`, JwtIDKey, value) 535 case LocaleKey: 536 if v, ok := value.(string); ok { 537 t.locale = &v 538 return nil 539 } 540 return fmt.Errorf(`invalid value for %s key: %T`, LocaleKey, value) 541 case MiddleNameKey: 542 if v, ok := value.(string); ok { 543 t.middleName = &v 544 return nil 545 } 546 return fmt.Errorf(`invalid value for %s key: %T`, MiddleNameKey, value) 547 case NameKey: 548 if v, ok := value.(string); ok { 549 t.name = &v 550 return nil 551 } 552 return fmt.Errorf(`invalid value for %s key: %T`, NameKey, value) 553 case NicknameKey: 554 if v, ok := value.(string); ok { 555 t.nickname = &v 556 return nil 557 } 558 return fmt.Errorf(`invalid value for %s key: %T`, NicknameKey, value) 559 case NotBeforeKey: 560 var acceptor types.NumericDate 561 if err := acceptor.Accept(value); err != nil { 562 return fmt.Errorf(`invalid value for %s key: %w`, NotBeforeKey, err) 563 } 564 t.notBefore = &acceptor 565 return nil 566 case PhoneNumberKey: 567 if v, ok := value.(string); ok { 568 t.phoneNumber = &v 569 return nil 570 } 571 return fmt.Errorf(`invalid value for %s key: %T`, PhoneNumberKey, value) 572 case PhoneNumberVerifiedKey: 573 if v, ok := value.(bool); ok { 574 t.phoneNumberVerified = &v 575 return nil 576 } 577 return fmt.Errorf(`invalid value for %s key: %T`, PhoneNumberVerifiedKey, value) 578 case PictureKey: 579 if v, ok := value.(string); ok { 580 t.picture = &v 581 return nil 582 } 583 return fmt.Errorf(`invalid value for %s key: %T`, PictureKey, value) 584 case PreferredUsernameKey: 585 if v, ok := value.(string); ok { 586 t.preferredUsername = &v 587 return nil 588 } 589 return fmt.Errorf(`invalid value for %s key: %T`, PreferredUsernameKey, value) 590 case ProfileKey: 591 if v, ok := value.(string); ok { 592 t.profile = &v 593 return nil 594 } 595 return fmt.Errorf(`invalid value for %s key: %T`, ProfileKey, value) 596 case SubjectKey: 597 if v, ok := value.(string); ok { 598 t.subject = &v 599 return nil 600 } 601 return fmt.Errorf(`invalid value for %s key: %T`, SubjectKey, value) 602 case UpdatedAtKey: 603 var acceptor types.NumericDate 604 if err := acceptor.Accept(value); err != nil { 605 return fmt.Errorf(`invalid value for %s key: %w`, UpdatedAtKey, err) 606 } 607 t.updatedAt = &acceptor 608 return nil 609 case WebsiteKey: 610 if v, ok := value.(string); ok { 611 t.website = &v 612 return nil 613 } 614 return fmt.Errorf(`invalid value for %s key: %T`, WebsiteKey, value) 615 case ZoneinfoKey: 616 if v, ok := value.(string); ok { 617 t.zoneinfo = &v 618 return nil 619 } 620 return fmt.Errorf(`invalid value for %s key: %T`, ZoneinfoKey, value) 621 default: 622 if t.privateClaims == nil { 623 t.privateClaims = map[string]interface{}{} 624 } 625 t.privateClaims[name] = value 626 } 627 return nil 628 } 629 630 func (t *stdToken) Address() *AddressClaim { 631 t.mu.RLock() 632 defer t.mu.RUnlock() 633 return t.address 634 } 635 636 func (t *stdToken) Audience() []string { 637 t.mu.RLock() 638 defer t.mu.RUnlock() 639 if t.audience != nil { 640 return t.audience.Get() 641 } 642 return nil 643 } 644 645 func (t *stdToken) Birthdate() *BirthdateClaim { 646 t.mu.RLock() 647 defer t.mu.RUnlock() 648 return t.birthdate 649 } 650 651 func (t *stdToken) Email() string { 652 t.mu.RLock() 653 defer t.mu.RUnlock() 654 if t.email != nil { 655 return *(t.email) 656 } 657 return "" 658 } 659 660 func (t *stdToken) EmailVerified() bool { 661 t.mu.RLock() 662 defer t.mu.RUnlock() 663 if t.emailVerified != nil { 664 return *(t.emailVerified) 665 } 666 return false 667 } 668 669 func (t *stdToken) Expiration() time.Time { 670 t.mu.RLock() 671 defer t.mu.RUnlock() 672 if t.expiration != nil { 673 return t.expiration.Get() 674 } 675 return time.Time{} 676 } 677 678 func (t *stdToken) FamilyName() string { 679 t.mu.RLock() 680 defer t.mu.RUnlock() 681 if t.familyName != nil { 682 return *(t.familyName) 683 } 684 return "" 685 } 686 687 func (t *stdToken) Gender() string { 688 t.mu.RLock() 689 defer t.mu.RUnlock() 690 if t.gender != nil { 691 return *(t.gender) 692 } 693 return "" 694 } 695 696 func (t *stdToken) GivenName() string { 697 t.mu.RLock() 698 defer t.mu.RUnlock() 699 if t.givenName != nil { 700 return *(t.givenName) 701 } 702 return "" 703 } 704 705 func (t *stdToken) IssuedAt() time.Time { 706 t.mu.RLock() 707 defer t.mu.RUnlock() 708 if t.issuedAt != nil { 709 return t.issuedAt.Get() 710 } 711 return time.Time{} 712 } 713 714 func (t *stdToken) Issuer() string { 715 t.mu.RLock() 716 defer t.mu.RUnlock() 717 if t.issuer != nil { 718 return *(t.issuer) 719 } 720 return "" 721 } 722 723 func (t *stdToken) JwtID() string { 724 t.mu.RLock() 725 defer t.mu.RUnlock() 726 if t.jwtID != nil { 727 return *(t.jwtID) 728 } 729 return "" 730 } 731 732 func (t *stdToken) Locale() string { 733 t.mu.RLock() 734 defer t.mu.RUnlock() 735 if t.locale != nil { 736 return *(t.locale) 737 } 738 return "" 739 } 740 741 func (t *stdToken) MiddleName() string { 742 t.mu.RLock() 743 defer t.mu.RUnlock() 744 if t.middleName != nil { 745 return *(t.middleName) 746 } 747 return "" 748 } 749 750 func (t *stdToken) Name() string { 751 t.mu.RLock() 752 defer t.mu.RUnlock() 753 if t.name != nil { 754 return *(t.name) 755 } 756 return "" 757 } 758 759 func (t *stdToken) Nickname() string { 760 t.mu.RLock() 761 defer t.mu.RUnlock() 762 if t.nickname != nil { 763 return *(t.nickname) 764 } 765 return "" 766 } 767 768 func (t *stdToken) NotBefore() time.Time { 769 t.mu.RLock() 770 defer t.mu.RUnlock() 771 if t.notBefore != nil { 772 return t.notBefore.Get() 773 } 774 return time.Time{} 775 } 776 777 func (t *stdToken) PhoneNumber() string { 778 t.mu.RLock() 779 defer t.mu.RUnlock() 780 if t.phoneNumber != nil { 781 return *(t.phoneNumber) 782 } 783 return "" 784 } 785 786 func (t *stdToken) PhoneNumberVerified() bool { 787 t.mu.RLock() 788 defer t.mu.RUnlock() 789 if t.phoneNumberVerified != nil { 790 return *(t.phoneNumberVerified) 791 } 792 return false 793 } 794 795 func (t *stdToken) Picture() string { 796 t.mu.RLock() 797 defer t.mu.RUnlock() 798 if t.picture != nil { 799 return *(t.picture) 800 } 801 return "" 802 } 803 804 func (t *stdToken) PreferredUsername() string { 805 t.mu.RLock() 806 defer t.mu.RUnlock() 807 if t.preferredUsername != nil { 808 return *(t.preferredUsername) 809 } 810 return "" 811 } 812 813 func (t *stdToken) Profile() string { 814 t.mu.RLock() 815 defer t.mu.RUnlock() 816 if t.profile != nil { 817 return *(t.profile) 818 } 819 return "" 820 } 821 822 func (t *stdToken) Subject() string { 823 t.mu.RLock() 824 defer t.mu.RUnlock() 825 if t.subject != nil { 826 return *(t.subject) 827 } 828 return "" 829 } 830 831 func (t *stdToken) UpdatedAt() time.Time { 832 t.mu.RLock() 833 defer t.mu.RUnlock() 834 if t.updatedAt != nil { 835 return t.updatedAt.Get() 836 } 837 return time.Time{} 838 } 839 840 func (t *stdToken) Website() string { 841 t.mu.RLock() 842 defer t.mu.RUnlock() 843 if t.website != nil { 844 return *(t.website) 845 } 846 return "" 847 } 848 849 func (t *stdToken) Zoneinfo() string { 850 t.mu.RLock() 851 defer t.mu.RUnlock() 852 if t.zoneinfo != nil { 853 return *(t.zoneinfo) 854 } 855 return "" 856 } 857 858 func (t *stdToken) PrivateClaims() map[string]interface{} { 859 t.mu.RLock() 860 defer t.mu.RUnlock() 861 return t.privateClaims 862 } 863 864 func (t *stdToken) makePairs() []*ClaimPair { 865 t.mu.RLock() 866 defer t.mu.RUnlock() 867 868 pairs := make([]*ClaimPair, 0, 26) 869 if t.address != nil { 870 v := t.address 871 pairs = append(pairs, &ClaimPair{Key: AddressKey, Value: v}) 872 } 873 if t.audience != nil { 874 v := t.audience.Get() 875 pairs = append(pairs, &ClaimPair{Key: AudienceKey, Value: v}) 876 } 877 if t.birthdate != nil { 878 v := t.birthdate 879 pairs = append(pairs, &ClaimPair{Key: BirthdateKey, Value: v}) 880 } 881 if t.email != nil { 882 v := *(t.email) 883 pairs = append(pairs, &ClaimPair{Key: EmailKey, Value: v}) 884 } 885 if t.emailVerified != nil { 886 v := *(t.emailVerified) 887 pairs = append(pairs, &ClaimPair{Key: EmailVerifiedKey, Value: v}) 888 } 889 if t.expiration != nil { 890 v := t.expiration.Get() 891 pairs = append(pairs, &ClaimPair{Key: ExpirationKey, Value: v}) 892 } 893 if t.familyName != nil { 894 v := *(t.familyName) 895 pairs = append(pairs, &ClaimPair{Key: FamilyNameKey, Value: v}) 896 } 897 if t.gender != nil { 898 v := *(t.gender) 899 pairs = append(pairs, &ClaimPair{Key: GenderKey, Value: v}) 900 } 901 if t.givenName != nil { 902 v := *(t.givenName) 903 pairs = append(pairs, &ClaimPair{Key: GivenNameKey, Value: v}) 904 } 905 if t.issuedAt != nil { 906 v := t.issuedAt.Get() 907 pairs = append(pairs, &ClaimPair{Key: IssuedAtKey, Value: v}) 908 } 909 if t.issuer != nil { 910 v := *(t.issuer) 911 pairs = append(pairs, &ClaimPair{Key: IssuerKey, Value: v}) 912 } 913 if t.jwtID != nil { 914 v := *(t.jwtID) 915 pairs = append(pairs, &ClaimPair{Key: JwtIDKey, Value: v}) 916 } 917 if t.locale != nil { 918 v := *(t.locale) 919 pairs = append(pairs, &ClaimPair{Key: LocaleKey, Value: v}) 920 } 921 if t.middleName != nil { 922 v := *(t.middleName) 923 pairs = append(pairs, &ClaimPair{Key: MiddleNameKey, Value: v}) 924 } 925 if t.name != nil { 926 v := *(t.name) 927 pairs = append(pairs, &ClaimPair{Key: NameKey, Value: v}) 928 } 929 if t.nickname != nil { 930 v := *(t.nickname) 931 pairs = append(pairs, &ClaimPair{Key: NicknameKey, Value: v}) 932 } 933 if t.notBefore != nil { 934 v := t.notBefore.Get() 935 pairs = append(pairs, &ClaimPair{Key: NotBeforeKey, Value: v}) 936 } 937 if t.phoneNumber != nil { 938 v := *(t.phoneNumber) 939 pairs = append(pairs, &ClaimPair{Key: PhoneNumberKey, Value: v}) 940 } 941 if t.phoneNumberVerified != nil { 942 v := *(t.phoneNumberVerified) 943 pairs = append(pairs, &ClaimPair{Key: PhoneNumberVerifiedKey, Value: v}) 944 } 945 if t.picture != nil { 946 v := *(t.picture) 947 pairs = append(pairs, &ClaimPair{Key: PictureKey, Value: v}) 948 } 949 if t.preferredUsername != nil { 950 v := *(t.preferredUsername) 951 pairs = append(pairs, &ClaimPair{Key: PreferredUsernameKey, Value: v}) 952 } 953 if t.profile != nil { 954 v := *(t.profile) 955 pairs = append(pairs, &ClaimPair{Key: ProfileKey, Value: v}) 956 } 957 if t.subject != nil { 958 v := *(t.subject) 959 pairs = append(pairs, &ClaimPair{Key: SubjectKey, Value: v}) 960 } 961 if t.updatedAt != nil { 962 v := t.updatedAt.Get() 963 pairs = append(pairs, &ClaimPair{Key: UpdatedAtKey, Value: v}) 964 } 965 if t.website != nil { 966 v := *(t.website) 967 pairs = append(pairs, &ClaimPair{Key: WebsiteKey, Value: v}) 968 } 969 if t.zoneinfo != nil { 970 v := *(t.zoneinfo) 971 pairs = append(pairs, &ClaimPair{Key: ZoneinfoKey, Value: v}) 972 } 973 for k, v := range t.privateClaims { 974 pairs = append(pairs, &ClaimPair{Key: k, Value: v}) 975 } 976 sort.Slice(pairs, func(i, j int) bool { 977 return pairs[i].Key.(string) < pairs[j].Key.(string) 978 }) 979 return pairs 980 } 981 982 func (t *stdToken) UnmarshalJSON(buf []byte) error { 983 t.mu.Lock() 984 defer t.mu.Unlock() 985 t.address = nil 986 t.audience = nil 987 t.birthdate = nil 988 t.email = nil 989 t.emailVerified = nil 990 t.expiration = nil 991 t.familyName = nil 992 t.gender = nil 993 t.givenName = nil 994 t.issuedAt = nil 995 t.issuer = nil 996 t.jwtID = nil 997 t.locale = nil 998 t.middleName = nil 999 t.name = nil 1000 t.nickname = nil 1001 t.notBefore = nil 1002 t.phoneNumber = nil 1003 t.phoneNumberVerified = nil 1004 t.picture = nil 1005 t.preferredUsername = nil 1006 t.profile = nil 1007 t.subject = nil 1008 t.updatedAt = nil 1009 t.website = nil 1010 t.zoneinfo = nil 1011 dec := json.NewDecoder(bytes.NewReader(buf)) 1012 LOOP: 1013 for { 1014 tok, err := dec.Token() 1015 if err != nil { 1016 return fmt.Errorf(`error reading token: %w`, err) 1017 } 1018 switch tok := tok.(type) { 1019 case json.Delim: 1020 // Assuming we're doing everything correctly, we should ONLY 1021 // get either '{' or '}' here. 1022 if tok == '}' { // End of object 1023 break LOOP 1024 } else if tok != '{' { 1025 return fmt.Errorf(`expected '{', but got '%c'`, tok) 1026 } 1027 case string: // Objects can only have string keys 1028 switch tok { 1029 case AddressKey: 1030 var decoded AddressClaim 1031 if err := dec.Decode(&decoded); err != nil { 1032 return fmt.Errorf(`failed to decode value for key %s: %w`, AddressKey, err) 1033 } 1034 t.address = &decoded 1035 case AudienceKey: 1036 var decoded types.StringList 1037 if err := dec.Decode(&decoded); err != nil { 1038 return fmt.Errorf(`failed to decode value for key %s: %w`, AudienceKey, err) 1039 } 1040 t.audience = decoded 1041 case BirthdateKey: 1042 var decoded BirthdateClaim 1043 if err := dec.Decode(&decoded); err != nil { 1044 return fmt.Errorf(`failed to decode value for key %s: %w`, BirthdateKey, err) 1045 } 1046 t.birthdate = &decoded 1047 case EmailKey: 1048 if err := json.AssignNextStringToken(&t.email, dec); err != nil { 1049 return fmt.Errorf(`failed to decode value for key %s: %w`, EmailKey, err) 1050 } 1051 case EmailVerifiedKey: 1052 var decoded bool 1053 if err := dec.Decode(&decoded); err != nil { 1054 return fmt.Errorf(`failed to decode value for key %s: %w`, EmailVerifiedKey, err) 1055 } 1056 t.emailVerified = &decoded 1057 case ExpirationKey: 1058 var decoded types.NumericDate 1059 if err := dec.Decode(&decoded); err != nil { 1060 return fmt.Errorf(`failed to decode value for key %s: %w`, ExpirationKey, err) 1061 } 1062 t.expiration = &decoded 1063 case FamilyNameKey: 1064 if err := json.AssignNextStringToken(&t.familyName, dec); err != nil { 1065 return fmt.Errorf(`failed to decode value for key %s: %w`, FamilyNameKey, err) 1066 } 1067 case GenderKey: 1068 if err := json.AssignNextStringToken(&t.gender, dec); err != nil { 1069 return fmt.Errorf(`failed to decode value for key %s: %w`, GenderKey, err) 1070 } 1071 case GivenNameKey: 1072 if err := json.AssignNextStringToken(&t.givenName, dec); err != nil { 1073 return fmt.Errorf(`failed to decode value for key %s: %w`, GivenNameKey, err) 1074 } 1075 case IssuedAtKey: 1076 var decoded types.NumericDate 1077 if err := dec.Decode(&decoded); err != nil { 1078 return fmt.Errorf(`failed to decode value for key %s: %w`, IssuedAtKey, err) 1079 } 1080 t.issuedAt = &decoded 1081 case IssuerKey: 1082 if err := json.AssignNextStringToken(&t.issuer, dec); err != nil { 1083 return fmt.Errorf(`failed to decode value for key %s: %w`, IssuerKey, err) 1084 } 1085 case JwtIDKey: 1086 if err := json.AssignNextStringToken(&t.jwtID, dec); err != nil { 1087 return fmt.Errorf(`failed to decode value for key %s: %w`, JwtIDKey, err) 1088 } 1089 case LocaleKey: 1090 if err := json.AssignNextStringToken(&t.locale, dec); err != nil { 1091 return fmt.Errorf(`failed to decode value for key %s: %w`, LocaleKey, err) 1092 } 1093 case MiddleNameKey: 1094 if err := json.AssignNextStringToken(&t.middleName, dec); err != nil { 1095 return fmt.Errorf(`failed to decode value for key %s: %w`, MiddleNameKey, err) 1096 } 1097 case NameKey: 1098 if err := json.AssignNextStringToken(&t.name, dec); err != nil { 1099 return fmt.Errorf(`failed to decode value for key %s: %w`, NameKey, err) 1100 } 1101 case NicknameKey: 1102 if err := json.AssignNextStringToken(&t.nickname, dec); err != nil { 1103 return fmt.Errorf(`failed to decode value for key %s: %w`, NicknameKey, err) 1104 } 1105 case NotBeforeKey: 1106 var decoded types.NumericDate 1107 if err := dec.Decode(&decoded); err != nil { 1108 return fmt.Errorf(`failed to decode value for key %s: %w`, NotBeforeKey, err) 1109 } 1110 t.notBefore = &decoded 1111 case PhoneNumberKey: 1112 if err := json.AssignNextStringToken(&t.phoneNumber, dec); err != nil { 1113 return fmt.Errorf(`failed to decode value for key %s: %w`, PhoneNumberKey, err) 1114 } 1115 case PhoneNumberVerifiedKey: 1116 var decoded bool 1117 if err := dec.Decode(&decoded); err != nil { 1118 return fmt.Errorf(`failed to decode value for key %s: %w`, PhoneNumberVerifiedKey, err) 1119 } 1120 t.phoneNumberVerified = &decoded 1121 case PictureKey: 1122 if err := json.AssignNextStringToken(&t.picture, dec); err != nil { 1123 return fmt.Errorf(`failed to decode value for key %s: %w`, PictureKey, err) 1124 } 1125 case PreferredUsernameKey: 1126 if err := json.AssignNextStringToken(&t.preferredUsername, dec); err != nil { 1127 return fmt.Errorf(`failed to decode value for key %s: %w`, PreferredUsernameKey, err) 1128 } 1129 case ProfileKey: 1130 if err := json.AssignNextStringToken(&t.profile, dec); err != nil { 1131 return fmt.Errorf(`failed to decode value for key %s: %w`, ProfileKey, err) 1132 } 1133 case SubjectKey: 1134 if err := json.AssignNextStringToken(&t.subject, dec); err != nil { 1135 return fmt.Errorf(`failed to decode value for key %s: %w`, SubjectKey, err) 1136 } 1137 case UpdatedAtKey: 1138 var decoded types.NumericDate 1139 if err := dec.Decode(&decoded); err != nil { 1140 return fmt.Errorf(`failed to decode value for key %s: %w`, UpdatedAtKey, err) 1141 } 1142 t.updatedAt = &decoded 1143 case WebsiteKey: 1144 if err := json.AssignNextStringToken(&t.website, dec); err != nil { 1145 return fmt.Errorf(`failed to decode value for key %s: %w`, WebsiteKey, err) 1146 } 1147 case ZoneinfoKey: 1148 if err := json.AssignNextStringToken(&t.zoneinfo, dec); err != nil { 1149 return fmt.Errorf(`failed to decode value for key %s: %w`, ZoneinfoKey, err) 1150 } 1151 default: 1152 if dc := t.dc; dc != nil { 1153 if localReg := dc.Registry(); localReg != nil { 1154 decoded, err := localReg.Decode(dec, tok) 1155 if err == nil { 1156 t.setNoLock(tok, decoded) 1157 continue 1158 } 1159 } 1160 } 1161 decoded, err := registry.Decode(dec, tok) 1162 if err == nil { 1163 t.setNoLock(tok, decoded) 1164 continue 1165 } 1166 return fmt.Errorf(`could not decode field %s: %w`, tok, err) 1167 } 1168 default: 1169 return fmt.Errorf(`invalid token %T`, tok) 1170 } 1171 } 1172 return nil 1173 } 1174 1175 func (t stdToken) MarshalJSON() ([]byte, error) { 1176 buf := pool.GetBytesBuffer() 1177 defer pool.ReleaseBytesBuffer(buf) 1178 buf.WriteByte('{') 1179 enc := json.NewEncoder(buf) 1180 for i, pair := range t.makePairs() { 1181 f := pair.Key.(string) 1182 if i > 0 { 1183 buf.WriteByte(',') 1184 } 1185 buf.WriteRune('"') 1186 buf.WriteString(f) 1187 buf.WriteString(`":`) 1188 switch f { 1189 case AudienceKey: 1190 if err := json.EncodeAudience(enc, pair.Value.([]string), t.options.IsEnabled(jwt.FlattenAudience)); err != nil { 1191 return nil, fmt.Errorf(`failed to encode "aud": %w`, err) 1192 } 1193 continue 1194 case ExpirationKey, IssuedAtKey, NotBeforeKey, UpdatedAtKey: 1195 enc.Encode(pair.Value.(time.Time).Unix()) 1196 continue 1197 } 1198 switch v := pair.Value.(type) { 1199 case []byte: 1200 buf.WriteRune('"') 1201 buf.WriteString(base64.EncodeToString(v)) 1202 buf.WriteRune('"') 1203 default: 1204 if err := enc.Encode(v); err != nil { 1205 return nil, fmt.Errorf(`failed to marshal field %s: %w`, f, err) 1206 } 1207 buf.Truncate(buf.Len() - 1) 1208 } 1209 } 1210 buf.WriteByte('}') 1211 ret := make([]byte, buf.Len()) 1212 copy(ret, buf.Bytes()) 1213 return ret, nil 1214 } 1215 1216 func (t *stdToken) Iterate(ctx context.Context) Iterator { 1217 pairs := t.makePairs() 1218 ch := make(chan *ClaimPair, len(pairs)) 1219 go func(ctx context.Context, ch chan *ClaimPair, pairs []*ClaimPair) { 1220 defer close(ch) 1221 for _, pair := range pairs { 1222 select { 1223 case <-ctx.Done(): 1224 return 1225 case ch <- pair: 1226 } 1227 } 1228 }(ctx, ch, pairs) 1229 return mapiter.New(ch) 1230 } 1231 1232 func (t *stdToken) Walk(ctx context.Context, visitor Visitor) error { 1233 return iter.WalkMap(ctx, t, visitor) 1234 } 1235 1236 func (t *stdToken) AsMap(ctx context.Context) (map[string]interface{}, error) { 1237 return iter.AsMap(ctx, t) 1238 }