pass client defined metadata from JWT to participant (#17)
This commit is contained in:
+17
-8
@@ -13,11 +13,12 @@ const (
|
||||
|
||||
// Signer that produces token signed with API key and secret
|
||||
type AccessToken struct {
|
||||
apiKey string
|
||||
secret string
|
||||
identity string
|
||||
grant *VideoGrant
|
||||
validFor time.Duration
|
||||
apiKey string
|
||||
secret string
|
||||
identity string
|
||||
videoGrant *VideoGrant
|
||||
metadata map[string]interface{}
|
||||
validFor time.Duration
|
||||
}
|
||||
|
||||
func NewAccessToken(key string, secret string) *AccessToken {
|
||||
@@ -38,7 +39,12 @@ func (t *AccessToken) SetValidFor(duration time.Duration) *AccessToken {
|
||||
}
|
||||
|
||||
func (t *AccessToken) AddGrant(grant *VideoGrant) *AccessToken {
|
||||
t.grant = grant
|
||||
t.videoGrant = grant
|
||||
return t
|
||||
}
|
||||
|
||||
func (t *AccessToken) SetMetadata(md map[string]interface{}) *AccessToken {
|
||||
t.metadata = md
|
||||
return t
|
||||
}
|
||||
|
||||
@@ -65,8 +71,11 @@ func (t *AccessToken) ToJWT() (string, error) {
|
||||
ID: t.identity,
|
||||
}
|
||||
grants := &ClaimGrants{}
|
||||
if t.grant != nil {
|
||||
grants.Video = t.grant
|
||||
if t.videoGrant != nil {
|
||||
grants.Video = t.videoGrant
|
||||
}
|
||||
if t.metadata != nil {
|
||||
grants.Metadata = t.metadata
|
||||
}
|
||||
return jwt.Signed(sig).Claims(cl).Claims(grants).CompactSerialize()
|
||||
}
|
||||
|
||||
@@ -12,7 +12,9 @@ import (
|
||||
"github.com/livekit/livekit-server/pkg/utils"
|
||||
)
|
||||
|
||||
func TestAPIIssuer(t *testing.T) {
|
||||
func TestAccessToken(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
t.Run("keys must be set", func(t *testing.T) {
|
||||
token := auth.NewAccessToken("", "")
|
||||
_, err := token.ToJWT()
|
||||
|
||||
+3
-2
@@ -12,6 +12,7 @@ type VideoGrant struct {
|
||||
}
|
||||
|
||||
type ClaimGrants struct {
|
||||
Identity string `json:"-"`
|
||||
Video *VideoGrant `json:"video,omitempty"`
|
||||
Identity string `json:"-"`
|
||||
Video *VideoGrant `json:"video,omitempty"`
|
||||
Metadata map[string]interface{} `json:"metadata,omitempty"`
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
"github.com/livekit/livekit-server/pkg/auth"
|
||||
)
|
||||
|
||||
func TestAPIVerifier(t *testing.T) {
|
||||
func TestVerifier(t *testing.T) {
|
||||
apiKey := "APID3B67uxk4Nj2GKiRPibAZ9"
|
||||
secret := "YHC-CUhbQhGeVCaYgn1BNA++"
|
||||
accessToken := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2MDg5MzAzMDgsImlzcyI6IkFQSUQzQjY3dXhrNE5qMkdLaVJQaWJBWjkiLCJuYmYiOjE2MDg5MjY3MDgsInJvb21fam9pbiI6dHJ1ZSwicm9vbV9zaWQiOiJteWlkIiwic3ViIjoiQVBJRDNCNjd1eGs0TmoyR0tpUlBpYkFaOSJ9.cmHEBq0MLyRqphmVLM2cLXg5ao5Sro7am8yXhcYKcwE"
|
||||
@@ -50,4 +50,28 @@ func TestAPIVerifier(t *testing.T) {
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, &claim, decoded.Video)
|
||||
})
|
||||
|
||||
t.Run("ensure metadata can be passed through", func(t *testing.T) {
|
||||
metadata := map[string]interface{}{
|
||||
"user": "value",
|
||||
"number": float64(3),
|
||||
}
|
||||
at := auth.NewAccessToken(apiKey, secret).
|
||||
AddGrant(&auth.VideoGrant{
|
||||
RoomAdmin: true,
|
||||
Room: "myroom",
|
||||
}).
|
||||
SetMetadata(metadata)
|
||||
|
||||
authToken, err := at.ToJWT()
|
||||
assert.NoError(t, err)
|
||||
|
||||
v, err := auth.ParseAPIToken(authToken)
|
||||
assert.NoError(t, err)
|
||||
|
||||
decoded, err := v.Verify(secret)
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.EqualValues(t, metadata, decoded.Metadata)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user