cli to support token based auth
This commit is contained in:
+10
-1
@@ -7,6 +7,10 @@ import (
|
||||
"gopkg.in/square/go-jose.v2/jwt"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultValidDuration = 10 * time.Minute
|
||||
)
|
||||
|
||||
// Signer that produces token signed with API key and secret
|
||||
type AccessToken struct {
|
||||
apiKey string
|
||||
@@ -49,10 +53,15 @@ func (t *AccessToken) ToJWT() (string, error) {
|
||||
return "", err
|
||||
}
|
||||
|
||||
validFor := defaultValidDuration
|
||||
if t.validFor > 0 {
|
||||
t.validFor = validFor
|
||||
}
|
||||
|
||||
cl := jwt.Claims{
|
||||
Issuer: t.apiKey,
|
||||
NotBefore: jwt.NewNumericDate(time.Now()),
|
||||
Expiry: jwt.NewNumericDate(time.Now().Add(t.validFor)),
|
||||
Expiry: jwt.NewNumericDate(time.Now().Add(validFor)),
|
||||
ID: t.identity,
|
||||
}
|
||||
grants := &ClaimGrants{}
|
||||
|
||||
@@ -42,6 +42,24 @@ func TestAPIIssuer(t *testing.T) {
|
||||
|
||||
assert.EqualValues(t, videoGrant, decodedGrant.Video)
|
||||
})
|
||||
|
||||
t.Run("default validity should be more than a minute", func(t *testing.T) {
|
||||
apiKey, secret := apiKeypair()
|
||||
videoGrant := &auth.VideoGrant{RoomJoin: true, Room: "myroom"}
|
||||
at := auth.NewAccessToken(apiKey, secret).
|
||||
AddGrant(videoGrant)
|
||||
value, err := at.ToJWT()
|
||||
token, err := jwt.ParseSigned(value)
|
||||
|
||||
claim := jwt.Claims{}
|
||||
decodedGrant := auth.ClaimGrants{}
|
||||
err = token.UnsafeClaimsWithoutVerification(&claim, &decodedGrant)
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, videoGrant, decodedGrant.Video)
|
||||
|
||||
// default validity
|
||||
assert.True(t, claim.Expiry.Time().Sub(claim.IssuedAt.Time()) > time.Minute)
|
||||
})
|
||||
}
|
||||
|
||||
func apiKeypair() (string, string) {
|
||||
|
||||
@@ -18,17 +18,17 @@ type FakeTokenVerifier struct {
|
||||
identityReturnsOnCall map[int]struct {
|
||||
result1 string
|
||||
}
|
||||
VerifyStub func(interface{}) (*auth.VideoGrant, error)
|
||||
VerifyStub func(interface{}) (*auth.ClaimGrants, error)
|
||||
verifyMutex sync.RWMutex
|
||||
verifyArgsForCall []struct {
|
||||
arg1 interface{}
|
||||
}
|
||||
verifyReturns struct {
|
||||
result1 *auth.VideoGrant
|
||||
result1 *auth.ClaimGrants
|
||||
result2 error
|
||||
}
|
||||
verifyReturnsOnCall map[int]struct {
|
||||
result1 *auth.VideoGrant
|
||||
result1 *auth.ClaimGrants
|
||||
result2 error
|
||||
}
|
||||
invocations map[string][][]interface{}
|
||||
@@ -88,7 +88,7 @@ func (fake *FakeTokenVerifier) IdentityReturnsOnCall(i int, result1 string) {
|
||||
}{result1}
|
||||
}
|
||||
|
||||
func (fake *FakeTokenVerifier) Verify(arg1 interface{}) (*auth.VideoGrant, error) {
|
||||
func (fake *FakeTokenVerifier) Verify(arg1 interface{}) (*auth.ClaimGrants, error) {
|
||||
fake.verifyMutex.Lock()
|
||||
ret, specificReturn := fake.verifyReturnsOnCall[len(fake.verifyArgsForCall)]
|
||||
fake.verifyArgsForCall = append(fake.verifyArgsForCall, struct {
|
||||
@@ -113,7 +113,7 @@ func (fake *FakeTokenVerifier) VerifyCallCount() int {
|
||||
return len(fake.verifyArgsForCall)
|
||||
}
|
||||
|
||||
func (fake *FakeTokenVerifier) VerifyCalls(stub func(interface{}) (*auth.VideoGrant, error)) {
|
||||
func (fake *FakeTokenVerifier) VerifyCalls(stub func(interface{}) (*auth.ClaimGrants, error)) {
|
||||
fake.verifyMutex.Lock()
|
||||
defer fake.verifyMutex.Unlock()
|
||||
fake.VerifyStub = stub
|
||||
@@ -126,28 +126,28 @@ func (fake *FakeTokenVerifier) VerifyArgsForCall(i int) interface{} {
|
||||
return argsForCall.arg1
|
||||
}
|
||||
|
||||
func (fake *FakeTokenVerifier) VerifyReturns(result1 *auth.VideoGrant, result2 error) {
|
||||
func (fake *FakeTokenVerifier) VerifyReturns(result1 *auth.ClaimGrants, result2 error) {
|
||||
fake.verifyMutex.Lock()
|
||||
defer fake.verifyMutex.Unlock()
|
||||
fake.VerifyStub = nil
|
||||
fake.verifyReturns = struct {
|
||||
result1 *auth.VideoGrant
|
||||
result1 *auth.ClaimGrants
|
||||
result2 error
|
||||
}{result1, result2}
|
||||
}
|
||||
|
||||
func (fake *FakeTokenVerifier) VerifyReturnsOnCall(i int, result1 *auth.VideoGrant, result2 error) {
|
||||
func (fake *FakeTokenVerifier) VerifyReturnsOnCall(i int, result1 *auth.ClaimGrants, result2 error) {
|
||||
fake.verifyMutex.Lock()
|
||||
defer fake.verifyMutex.Unlock()
|
||||
fake.VerifyStub = nil
|
||||
if fake.verifyReturnsOnCall == nil {
|
||||
fake.verifyReturnsOnCall = make(map[int]struct {
|
||||
result1 *auth.VideoGrant
|
||||
result1 *auth.ClaimGrants
|
||||
result2 error
|
||||
})
|
||||
}
|
||||
fake.verifyReturnsOnCall[i] = struct {
|
||||
result1 *auth.VideoGrant
|
||||
result1 *auth.ClaimGrants
|
||||
result2 error
|
||||
}{result1, result2}
|
||||
}
|
||||
|
||||
+2
-1
@@ -7,5 +7,6 @@ type VideoGrant struct {
|
||||
}
|
||||
|
||||
type ClaimGrants struct {
|
||||
Video *VideoGrant `json:"video,omitempty"`
|
||||
Identity string `json:"-"`
|
||||
Video *VideoGrant `json:"video,omitempty"`
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ var (
|
||||
//counterfeiter:generate . TokenVerifier
|
||||
type TokenVerifier interface {
|
||||
Identity() string
|
||||
Verify(key interface{}) (*VideoGrant, error)
|
||||
Verify(key interface{}) (*ClaimGrants, error)
|
||||
}
|
||||
|
||||
//counterfeiter:generate . KeyProvider
|
||||
|
||||
@@ -39,7 +39,7 @@ func (v *APIKeyTokenVerifier) Identity() string {
|
||||
return v.identity
|
||||
}
|
||||
|
||||
func (v *APIKeyTokenVerifier) Verify(key interface{}) (*VideoGrant, error) {
|
||||
func (v *APIKeyTokenVerifier) Verify(key interface{}) (*ClaimGrants, error) {
|
||||
if key == nil || key == "" {
|
||||
return nil, ErrKeysMissing
|
||||
}
|
||||
@@ -54,5 +54,8 @@ func (v *APIKeyTokenVerifier) Verify(key interface{}) (*VideoGrant, error) {
|
||||
if err := out.Validate(jwt.Expected{Issuer: v.apiKey, Time: time.Now()}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return claims.Video, nil
|
||||
|
||||
// copy over identity
|
||||
claims.Identity = out.ID
|
||||
return &claims, nil
|
||||
}
|
||||
|
||||
@@ -48,6 +48,6 @@ func TestAPIVerifier(t *testing.T) {
|
||||
|
||||
decoded, err := v.Verify(secret)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, &claim, decoded)
|
||||
assert.Equal(t, &claim, decoded.Video)
|
||||
})
|
||||
}
|
||||
|
||||
+6
-6
@@ -73,8 +73,8 @@ func (m *APIKeyAuthMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request,
|
||||
next.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
func GetGrants(ctx context.Context) *auth.VideoGrant {
|
||||
claims, ok := ctx.Value(grantsKey).(*auth.VideoGrant)
|
||||
func GetGrants(ctx context.Context) *auth.ClaimGrants {
|
||||
claims, ok := ctx.Value(grantsKey).(*auth.ClaimGrants)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
@@ -90,13 +90,13 @@ func EnsureJoinPermission(ctx context.Context) (name string, err error) {
|
||||
return "", nil
|
||||
}
|
||||
claims := GetGrants(ctx)
|
||||
if claims == nil {
|
||||
if claims == nil || claims.Video == nil {
|
||||
err = ErrPermissionDenied
|
||||
return
|
||||
}
|
||||
|
||||
if claims.RoomJoin {
|
||||
name = claims.Room
|
||||
if claims.Video.RoomJoin {
|
||||
name = claims.Video.Room
|
||||
} else {
|
||||
err = ErrPermissionDenied
|
||||
}
|
||||
@@ -112,7 +112,7 @@ func EnsureCreatePermission(ctx context.Context) error {
|
||||
return ErrPermissionDenied
|
||||
}
|
||||
|
||||
if claims.RoomCreate {
|
||||
if claims.Video.RoomCreate {
|
||||
return nil
|
||||
}
|
||||
return ErrPermissionDenied
|
||||
|
||||
@@ -19,7 +19,7 @@ func TestAuthMiddleware(t *testing.T) {
|
||||
provider.GetSecretReturns(secret)
|
||||
|
||||
m := service.NewAPIKeyAuthMiddleware(provider)
|
||||
var grants *auth.VideoGrant
|
||||
var grants *auth.ClaimGrants
|
||||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
grants = service.GetGrants(r.Context())
|
||||
w.WriteHeader(http.StatusOK)
|
||||
@@ -38,7 +38,7 @@ func TestAuthMiddleware(t *testing.T) {
|
||||
m.ServeHTTP(w, r, handler)
|
||||
|
||||
assert.NotNil(t, grants)
|
||||
assert.EqualValues(t, orig, grants)
|
||||
assert.EqualValues(t, orig, grants.Video)
|
||||
|
||||
// no authorization == no claims
|
||||
grants = nil
|
||||
|
||||
+14
-2
@@ -17,15 +17,17 @@ import (
|
||||
type RTCService struct {
|
||||
manager *rtc.RoomManager
|
||||
upgrader websocket.Upgrader
|
||||
isDev bool
|
||||
}
|
||||
|
||||
func NewRTCService(conf *config.Config, manager *rtc.RoomManager) *RTCService {
|
||||
s := &RTCService{
|
||||
manager: manager,
|
||||
upgrader: websocket.Upgrader{},
|
||||
isDev: conf.Development,
|
||||
}
|
||||
|
||||
if conf.Development {
|
||||
if s.isDev {
|
||||
s.upgrader.CheckOrigin = func(r *http.Request) bool {
|
||||
// allow all in dev
|
||||
return true
|
||||
@@ -37,7 +39,17 @@ func NewRTCService(conf *config.Config, manager *rtc.RoomManager) *RTCService {
|
||||
|
||||
func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
roomId := r.FormValue("room_id")
|
||||
pName := r.FormValue("name")
|
||||
var pName string
|
||||
if s.isDev {
|
||||
r.FormValue("name")
|
||||
} else {
|
||||
claims := GetGrants(r.Context())
|
||||
// require a claim
|
||||
if claims == nil || claims.Video == nil {
|
||||
writeJSONError(w, http.StatusUnauthorized, rtc.ErrPermissionDenied.Error())
|
||||
}
|
||||
pName = claims.Identity
|
||||
}
|
||||
log := logger.GetLogger()
|
||||
|
||||
onlyName, err := EnsureJoinPermission(r.Context())
|
||||
|
||||
Reference in New Issue
Block a user