@@ -14,8 +14,6 @@ import (
1414 "time"
1515
1616 "github.com/MicahParks/keyfunc/v3"
17- "github.com/bluenviron/gortsplib/v4/pkg/auth"
18- "github.com/bluenviron/gortsplib/v4/pkg/headers"
1917 "github.com/bluenviron/mediamtx/internal/conf"
2018 "github.com/bluenviron/mediamtx/internal/conf/jsonwrapper"
2119 "github.com/golang-jwt/jwt/v5"
@@ -26,19 +24,19 @@ const (
2624 // PauseAfterError is the pause to apply after an authentication failure.
2725 PauseAfterError = 2 * time .Second
2826
29- rtspAuthRealm = "IPCAM"
3027 jwtRefreshPeriod = 60 * 60 * time .Second
3128)
3229
3330// Error is a authentication error.
3431type Error struct {
32+ Wrapped error
3533 Message string
3634 AskCredentials bool
3735}
3836
3937// Error implements the error interface.
40- func (e * Error ) Error () string {
41- return "authentication failed: " + e .Message
38+ func (e Error ) Error () string {
39+ return "authentication failed: " + e .Wrapped . Error ()
4240}
4341
4442func matchesPermission (perms []conf.AuthInternalUserPermission , req * Request ) bool {
@@ -102,14 +100,13 @@ func (c *customClaims) UnmarshalJSON(b []byte) error {
102100
103101// Manager is the authentication manager.
104102type Manager struct {
105- Method conf.AuthMethod
106- InternalUsers []conf.AuthInternalUser
107- HTTPAddress string
108- HTTPExclude []conf.AuthInternalUserPermission
109- JWTJWKS string
110- JWTClaimKey string
111- ReadTimeout time.Duration
112- RTSPAuthMethods []auth.VerifyMethod
103+ Method conf.AuthMethod
104+ InternalUsers []conf.AuthInternalUser
105+ HTTPAddress string
106+ HTTPExclude []conf.AuthInternalUserPermission
107+ JWTJWKS string
108+ JWTClaimKey string
109+ ReadTimeout time.Duration
113110
114111 mutex sync.RWMutex
115112 jwtHTTPClient * http.Client
@@ -140,8 +137,8 @@ func (m *Manager) Authenticate(req *Request) error {
140137 }
141138
142139 if err != nil {
143- return & Error {
144- Message : err . Error () ,
140+ return Error {
141+ Wrapped : err ,
145142 AskCredentials : (req .User == "" && req .Pass == "" ),
146143 }
147144 }
@@ -150,20 +147,11 @@ func (m *Manager) Authenticate(req *Request) error {
150147}
151148
152149func (m * Manager ) authenticateInternal (req * Request ) error {
153- var rtspAuthHeader * headers.Authorization
154- if req .RTSPRequest != nil {
155- var tmp headers.Authorization
156- err := tmp .Unmarshal (req .RTSPRequest .Header ["Authorization" ])
157- if err == nil {
158- rtspAuthHeader = & tmp
159- }
160- }
161-
162150 m .mutex .RLock ()
163151 defer m .mutex .RUnlock ()
164152
165153 for _ , u := range m .InternalUsers {
166- if err := m .authenticateWithUser (req , rtspAuthHeader , & u ); err == nil {
154+ if ok := m .authenticateWithUser (req , & u ); ok {
167155 return nil
168156 }
169157 }
@@ -173,39 +161,29 @@ func (m *Manager) authenticateInternal(req *Request) error {
173161
174162func (m * Manager ) authenticateWithUser (
175163 req * Request ,
176- rtspAuthHeader * headers.Authorization ,
177164 u * conf.AuthInternalUser ,
178- ) error {
179- if u .User != "any" && ! u .User .Check (req .User ) {
180- return fmt .Errorf ("wrong user" )
181- }
182-
165+ ) bool {
183166 if len (u .IPs ) != 0 && ! u .IPs .Contains (req .IP ) {
184- return fmt . Errorf ( "IP not allowed" )
167+ return false
185168 }
186169
187170 if ! matchesPermission (u .Permissions , req ) {
188- return fmt . Errorf ( "user doesn't have permission to perform action" )
171+ return false
189172 }
190173
191174 if u .User != "any" {
192- if req .RTSPRequest != nil && rtspAuthHeader != nil && rtspAuthHeader .Method == headers .AuthMethodDigest {
193- err := auth .Verify (
194- req .RTSPRequest ,
195- string (u .User ),
196- string (u .Pass ),
197- m .RTSPAuthMethods ,
198- rtspAuthRealm ,
199- req .RTSPNonce )
200- if err != nil {
201- return err
175+ if req .CustomVerifyFunc != nil {
176+ if ok := req .CustomVerifyFunc (string (u .User ), string (u .Pass )); ! ok {
177+ return false
178+ }
179+ } else {
180+ if ! u .User .Check (req .User ) || ! u .Pass .Check (req .Pass ) {
181+ return false
202182 }
203- } else if ! u .Pass .Check (req .Pass ) {
204- return fmt .Errorf ("invalid credentials" )
205183 }
206184 }
207185
208- return nil
186+ return true
209187}
210188
211189func (m * Manager ) authenticateHTTP (req * Request ) error {
0 commit comments