github.com/livekit/protocol@v1.16.1-0.20240517185851-47e4c6bba773/auth/accesstoken.go (about)

     1  // Copyright 2023 LiveKit, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package auth
    16  
    17  import (
    18  	"time"
    19  
    20  	"github.com/go-jose/go-jose/v3"
    21  	"github.com/go-jose/go-jose/v3/jwt"
    22  
    23  	"github.com/livekit/protocol/livekit"
    24  )
    25  
    26  const (
    27  	defaultValidDuration = 6 * time.Hour
    28  )
    29  
    30  // AccessToken produces token signed with API key and secret
    31  type AccessToken struct {
    32  	apiKey   string
    33  	secret   string
    34  	grant    ClaimGrants
    35  	validFor time.Duration
    36  }
    37  
    38  func NewAccessToken(key string, secret string) *AccessToken {
    39  	return &AccessToken{
    40  		apiKey: key,
    41  		secret: secret,
    42  	}
    43  }
    44  
    45  func (t *AccessToken) SetIdentity(identity string) *AccessToken {
    46  	t.grant.Identity = identity
    47  	return t
    48  }
    49  
    50  func (t *AccessToken) SetValidFor(duration time.Duration) *AccessToken {
    51  	t.validFor = duration
    52  	return t
    53  }
    54  
    55  func (t *AccessToken) SetName(name string) *AccessToken {
    56  	t.grant.Name = name
    57  	return t
    58  }
    59  
    60  func (t *AccessToken) SetKind(kind livekit.ParticipantInfo_Kind) *AccessToken {
    61  	t.grant.SetParticipantKind(kind)
    62  	return t
    63  }
    64  
    65  func (t *AccessToken) AddGrant(grant *VideoGrant) *AccessToken {
    66  	t.grant.Video = grant
    67  	return t
    68  }
    69  
    70  func (t *AccessToken) SetMetadata(md string) *AccessToken {
    71  	t.grant.Metadata = md
    72  	return t
    73  }
    74  
    75  func (t *AccessToken) SetSha256(sha string) *AccessToken {
    76  	t.grant.Sha256 = sha
    77  	return t
    78  }
    79  
    80  func (t *AccessToken) ToJWT() (string, error) {
    81  	if t.apiKey == "" || t.secret == "" {
    82  		return "", ErrKeysMissing
    83  	}
    84  
    85  	sig, err := jose.NewSigner(jose.SigningKey{Algorithm: jose.HS256, Key: []byte(t.secret)},
    86  		(&jose.SignerOptions{}).WithType("JWT"))
    87  	if err != nil {
    88  		return "", err
    89  	}
    90  
    91  	validFor := defaultValidDuration
    92  	if t.validFor > 0 {
    93  		validFor = t.validFor
    94  	}
    95  
    96  	cl := jwt.Claims{
    97  		Issuer:    t.apiKey,
    98  		NotBefore: jwt.NewNumericDate(time.Now()),
    99  		Expiry:    jwt.NewNumericDate(time.Now().Add(validFor)),
   100  		Subject:   t.grant.Identity,
   101  	}
   102  	return jwt.Signed(sig).Claims(cl).Claims(&t.grant).CompactSerialize()
   103  }