mirror of
https://github.com/livekit/livekit.git
synced 2026-04-26 15:17:35 +00:00
73 lines
1.4 KiB
Go
73 lines
1.4 KiB
Go
package auth
|
|
|
|
import (
|
|
"time"
|
|
|
|
"gopkg.in/square/go-jose.v2"
|
|
"gopkg.in/square/go-jose.v2/jwt"
|
|
)
|
|
|
|
const (
|
|
defaultValidDuration = 6 * time.Hour
|
|
)
|
|
|
|
// Signer that produces token signed with API key and secret
|
|
type AccessToken struct {
|
|
apiKey string
|
|
secret string
|
|
identity string
|
|
grant *VideoGrant
|
|
validFor time.Duration
|
|
}
|
|
|
|
func NewAccessToken(key string, secret string) *AccessToken {
|
|
return &AccessToken{
|
|
apiKey: key,
|
|
secret: secret,
|
|
}
|
|
}
|
|
|
|
func (t *AccessToken) SetIdentity(identity string) *AccessToken {
|
|
t.identity = identity
|
|
return t
|
|
}
|
|
|
|
func (t *AccessToken) SetValidFor(duration time.Duration) *AccessToken {
|
|
t.validFor = duration
|
|
return t
|
|
}
|
|
|
|
func (t *AccessToken) AddGrant(grant *VideoGrant) *AccessToken {
|
|
t.grant = grant
|
|
return t
|
|
}
|
|
|
|
func (t *AccessToken) ToJWT() (string, error) {
|
|
if t.apiKey == "" || t.secret == "" {
|
|
return "", ErrKeysMissing
|
|
}
|
|
|
|
sig, err := jose.NewSigner(jose.SigningKey{Algorithm: jose.HS256, Key: []byte(t.secret)},
|
|
(&jose.SignerOptions{}).WithType("JWT"))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
validFor := defaultValidDuration
|
|
if t.validFor > 0 {
|
|
validFor = t.validFor
|
|
}
|
|
|
|
cl := jwt.Claims{
|
|
Issuer: t.apiKey,
|
|
NotBefore: jwt.NewNumericDate(time.Now()),
|
|
Expiry: jwt.NewNumericDate(time.Now().Add(validFor)),
|
|
ID: t.identity,
|
|
}
|
|
grants := &ClaimGrants{}
|
|
if t.grant != nil {
|
|
grants.Video = t.grant
|
|
}
|
|
return jwt.Signed(sig).Claims(cl).Claims(grants).CompactSerialize()
|
|
}
|