Skip to content

Commit 6ce3b95

Browse files
committed
Exclude actions when using JWT authentication
This is implemented with reference to the following. bluenviron#3431
1 parent b66efd6 commit 6ce3b95

File tree

6 files changed

+52
-0
lines changed

6 files changed

+52
-0
lines changed

internal/auth/manager.go

+5
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ type Manager struct {
106106
HTTPAddress string
107107
HTTPExclude []conf.AuthInternalUserPermission
108108
JWTJWKS string
109+
JWTExclude []conf.AuthInternalUserPermission
109110
JWTClaimKey string
110111
ReadTimeout time.Duration
111112
RTSPAuthMethods []auth.ValidateMethod
@@ -250,6 +251,10 @@ func (m *Manager) authenticateHTTP(req *Request) error {
250251
}
251252

252253
func (m *Manager) authenticateJWT(req *Request) error {
254+
if matchesPermission(m.JWTExclude, req) {
255+
return nil
256+
}
257+
253258
keyfunc, err := m.pullJWTJWKS()
254259
if err != nil {
255260
return err

internal/auth/manager_test.go

+21
Original file line numberDiff line numberDiff line change
@@ -431,3 +431,24 @@ func TestAuthJWT(t *testing.T) {
431431
})
432432
}
433433
}
434+
435+
func TestAuthJWTExclude(t *testing.T) {
436+
m := Manager{
437+
Method: conf.AuthMethodJWT,
438+
JWTExclude: []conf.AuthInternalUserPermission{{
439+
Action: conf.AuthActionPublish,
440+
}},
441+
RTSPAuthMethods: nil,
442+
}
443+
444+
err := m.Authenticate(&Request{
445+
User: "",
446+
Pass: "",
447+
IP: net.ParseIP("127.0.0.1"),
448+
Action: conf.AuthActionPublish,
449+
Path: "teststream",
450+
Protocol: ProtocolRTSP,
451+
Query: "param=value",
452+
})
453+
require.NoError(t, err)
454+
}

internal/conf/conf.go

+12
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ type Conf struct {
178178
AuthHTTPExclude AuthInternalUserPermissions `json:"authHTTPExclude"`
179179
AuthJWTJWKS string `json:"authJWTJWKS"`
180180
AuthJWTClaimKey string `json:"authJWTClaimKey"`
181+
AuthJWTExclude AuthInternalUserPermissions `json:"authJWTExclude"`
181182

182183
// Control API
183184
API bool `json:"api"`
@@ -330,6 +331,17 @@ func (conf *Conf) setDefaults() {
330331
},
331332
}
332333
conf.AuthJWTClaimKey = "mediamtx_permissions"
334+
conf.AuthJWTExclude = []AuthInternalUserPermission{
335+
{
336+
Action: AuthActionAPI,
337+
},
338+
{
339+
Action: AuthActionMetrics,
340+
},
341+
{
342+
Action: AuthActionPprof,
343+
},
344+
}
333345

334346
// Control API
335347
conf.APIAddress = ":9997"

internal/conf/conf_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,8 @@ func TestConfOverrideDefaultSlices(t *testing.T) {
425425
" - user: user1\n" +
426426
" - user: user2\n" +
427427
"authHTTPExclude:\n" +
428+
" - path: ''\n" +
429+
"authJWTExclude:\n" +
428430
" - path: ''\n"))
429431
require.NoError(t, err)
430432
defer os.Remove(tmpf)
@@ -444,4 +446,8 @@ func TestConfOverrideDefaultSlices(t *testing.T) {
444446
require.Equal(t, AuthInternalUserPermissions{
445447
{},
446448
}, conf.AuthHTTPExclude)
449+
450+
require.Equal(t, AuthInternalUserPermissions{
451+
{},
452+
}, conf.AuthJWTExclude)
447453
}

internal/core/core.go

+2
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ func (p *Core) createResources(initial bool) error {
269269
HTTPAddress: p.conf.AuthHTTPAddress,
270270
HTTPExclude: p.conf.AuthHTTPExclude,
271271
JWTJWKS: p.conf.AuthJWTJWKS,
272+
JWTExclude: p.conf.AuthJWTExclude,
272273
JWTClaimKey: p.conf.AuthJWTClaimKey,
273274
ReadTimeout: time.Duration(p.conf.ReadTimeout),
274275
RTSPAuthMethods: p.conf.RTSPAuthMethods,
@@ -652,6 +653,7 @@ func (p *Core) closeResources(newConf *conf.Conf, calledByAPI bool) {
652653
newConf.AuthHTTPAddress != p.conf.AuthHTTPAddress ||
653654
!reflect.DeepEqual(newConf.AuthHTTPExclude, p.conf.AuthHTTPExclude) ||
654655
newConf.AuthJWTJWKS != p.conf.AuthJWTJWKS ||
656+
!reflect.DeepEqual(newConf.AuthJWTExclude, p.conf.AuthJWTExclude) ||
655657
newConf.AuthJWTClaimKey != p.conf.AuthJWTClaimKey ||
656658
newConf.ReadTimeout != p.conf.ReadTimeout ||
657659
!reflect.DeepEqual(newConf.RTSPAuthMethods, p.conf.RTSPAuthMethods)

mediamtx.yml

+6
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ authHTTPExclude:
123123
authJWTJWKS:
124124
# name of the claim that contains permissions.
125125
authJWTClaimKey: mediamtx_permissions
126+
# Actions to exclude from JWT-based authentication.
127+
# Format is the same as the one of user permissions.
128+
authJWTExclude:
129+
- action: api
130+
- action: metrics
131+
- action: pprof
126132

127133
###############################################
128134
# Global settings -> Control API

0 commit comments

Comments
 (0)