github.com/lestrrat-go/jwx/v2@v2.0.21/jwt/builder_gen.go (about) 1 // Code generated by tools/cmd/genjwt/main.go. DO NOT EDIT. 2 3 package jwt 4 5 import ( 6 "fmt" 7 "time" 8 ) 9 10 // Builder is a convenience wrapper around the New() constructor 11 // and the Set() methods to assign values to Token claims. 12 // Users can successively call Claim() on the Builder, and have it 13 // construct the Token when Build() is called. This alleviates the 14 // need for the user to check for the return value of every single 15 // Set() method call. 16 // Note that each call to Claim() overwrites the value set from the 17 // previous call. 18 type Builder struct { 19 claims []*ClaimPair 20 } 21 22 func NewBuilder() *Builder { 23 return &Builder{} 24 } 25 26 func (b *Builder) Claim(name string, value interface{}) *Builder { 27 b.claims = append(b.claims, &ClaimPair{Key: name, Value: value}) 28 return b 29 } 30 31 func (b *Builder) Audience(v []string) *Builder { 32 return b.Claim(AudienceKey, v) 33 } 34 35 func (b *Builder) Expiration(v time.Time) *Builder { 36 return b.Claim(ExpirationKey, v) 37 } 38 39 func (b *Builder) IssuedAt(v time.Time) *Builder { 40 return b.Claim(IssuedAtKey, v) 41 } 42 43 func (b *Builder) Issuer(v string) *Builder { 44 return b.Claim(IssuerKey, v) 45 } 46 47 func (b *Builder) JwtID(v string) *Builder { 48 return b.Claim(JwtIDKey, v) 49 } 50 51 func (b *Builder) NotBefore(v time.Time) *Builder { 52 return b.Claim(NotBeforeKey, v) 53 } 54 55 func (b *Builder) Subject(v string) *Builder { 56 return b.Claim(SubjectKey, v) 57 } 58 59 // Build creates a new token based on the claims that the builder has received 60 // so far. If a claim cannot be set, then the method returns a nil Token with 61 // a en error as a second return value 62 func (b *Builder) Build() (Token, error) { 63 tok := New() 64 for _, claim := range b.claims { 65 if err := tok.Set(claim.Key.(string), claim.Value); err != nil { 66 return nil, fmt.Errorf(`failed to set claim %q: %w`, claim.Key.(string), err) 67 } 68 } 69 return tok, nil 70 }