Preserve original expiry when refreshing token (#4580)
To avoid shortening the token expiration time during refreshing cause client reconnect failed after network down for a long time (>5min).
This commit is contained in:
+22
-5
@@ -19,6 +19,7 @@ import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/twitchtv/twirp"
|
||||
|
||||
@@ -35,8 +36,9 @@ const (
|
||||
type grantsKey struct{}
|
||||
|
||||
type grantsValue struct {
|
||||
claims *auth.ClaimGrants
|
||||
apiKey string
|
||||
claims *auth.ClaimGrants
|
||||
apiKey string
|
||||
expiresAt time.Time
|
||||
}
|
||||
|
||||
var (
|
||||
@@ -90,17 +92,23 @@ func (m *APIKeyAuthMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request,
|
||||
return
|
||||
}
|
||||
|
||||
_, grants, err := v.Verify(secret)
|
||||
claims, grants, err := v.Verify(secret)
|
||||
if err != nil {
|
||||
HandleError(w, r, http.StatusUnauthorized, errors.New("invalid token: "+authToken+", error: "+err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
var expiresAt time.Time
|
||||
if claims != nil && claims.ExpiresAt != nil {
|
||||
expiresAt = claims.ExpiresAt.Time
|
||||
}
|
||||
|
||||
// set grants in context
|
||||
ctx := r.Context()
|
||||
r = r.WithContext(context.WithValue(ctx, grantsKey{}, &grantsValue{
|
||||
claims: grants,
|
||||
apiKey: v.APIKey(),
|
||||
claims: grants,
|
||||
apiKey: v.APIKey(),
|
||||
expiresAt: expiresAt,
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -123,6 +131,15 @@ func GetGrants(ctx context.Context) *auth.ClaimGrants {
|
||||
return v.claims
|
||||
}
|
||||
|
||||
func GetTokenExpiresAt(ctx context.Context) time.Time {
|
||||
val := ctx.Value(grantsKey{})
|
||||
v, ok := val.(*grantsValue)
|
||||
if !ok {
|
||||
return time.Time{}
|
||||
}
|
||||
return v.expiresAt
|
||||
}
|
||||
|
||||
func GetAPIKey(ctx context.Context) string {
|
||||
val := ctx.Value(grantsKey{})
|
||||
v, ok := val.(*grantsValue)
|
||||
|
||||
@@ -489,6 +489,7 @@ func (r *RoomManager) StartSession(
|
||||
PublishEnabledCodecs: enabledCodecs,
|
||||
SubscribeEnabledCodecs: enabledCodecs,
|
||||
Grants: pi.Grants,
|
||||
TokenExpiresAt: pi.TokenExpiresAt,
|
||||
Reconnect: pi.Reconnect,
|
||||
Logger: pLogger,
|
||||
Reporter: roomobs.NewNoopParticipantSessionReporter(),
|
||||
@@ -1124,11 +1125,20 @@ func (r *RoomManager) refreshToken(participant types.LocalParticipant) error {
|
||||
}
|
||||
|
||||
grants := participant.ClaimGrants()
|
||||
|
||||
// Preserve the original token's expiry
|
||||
validFor := tokenDefaultTTL
|
||||
if expiresAt := participant.TokenExpiresAt(); !expiresAt.IsZero() {
|
||||
if remaining := time.Until(expiresAt); remaining > validFor {
|
||||
validFor = remaining
|
||||
}
|
||||
}
|
||||
|
||||
token := auth.NewAccessToken(key, secret)
|
||||
token.SetName(grants.Name).
|
||||
SetIdentity(string(participant.Identity())).
|
||||
SetKind(grants.GetParticipantKind()).
|
||||
SetValidFor(tokenDefaultTTL).
|
||||
SetValidFor(validFor).
|
||||
SetMetadata(grants.Metadata).
|
||||
SetAttributes(grants.Attributes).
|
||||
SetVideoGrant(grants.Video).
|
||||
|
||||
@@ -218,6 +218,7 @@ func (s *RTCService) validateInternal(
|
||||
Identity: livekit.ParticipantIdentity(res.grants.Identity),
|
||||
Name: livekit.ParticipantName(res.grants.Name),
|
||||
Grants: res.grants,
|
||||
TokenExpiresAt: res.tokenExpiresAt,
|
||||
Region: res.region,
|
||||
CreateRoom: res.createRoomRequest,
|
||||
UseSinglePeerConnection: useSinglePeerConnection,
|
||||
|
||||
@@ -28,6 +28,7 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/ua-parser/uap-go/uaparser"
|
||||
"gopkg.in/yaml.v3"
|
||||
@@ -289,6 +290,7 @@ type ValidateConnectRequestParams struct {
|
||||
type ValidateConnectRequestResult struct {
|
||||
roomName livekit.RoomName
|
||||
grants *auth.ClaimGrants
|
||||
tokenExpiresAt time.Time
|
||||
region string
|
||||
createRoomRequest *livekit.CreateRoomRequest
|
||||
}
|
||||
@@ -402,6 +404,7 @@ func ValidateConnectRequest(
|
||||
}
|
||||
|
||||
res.grants = claims
|
||||
res.tokenExpiresAt = GetTokenExpiresAt(r.Context())
|
||||
return res, http.StatusOK, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user