pass client defined metadata from JWT to participant (#17)

This commit is contained in:
David Zhao
2021-02-10 23:13:36 -08:00
committed by GitHub
parent 38140debdf
commit 538461d5dc
23 changed files with 286 additions and 81 deletions
+17 -8
View File
@@ -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()
}
+3 -1
View File
@@ -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
View File
@@ -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"`
}
+25 -1
View File
@@ -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)
})
}