...

Source file src/github.com/lestrrat-go/jwx/jwt/builder_gen.go

Documentation: github.com/lestrrat-go/jwx/jwt

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

View as plain text