mirror of
https://github.com/aler9/rtsp-simple-server
synced 2025-10-05 23:56:54 +08:00

This is safer than passing JWTs through query parameters, unfortunately support is limited.
27 lines
604 B
Go
27 lines
604 B
Go
package httpp
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/bluenviron/mediamtx/internal/auth"
|
|
)
|
|
|
|
// Credentials extracts credentials from a HTTP request.
|
|
func Credentials(h *http.Request) *auth.Credentials {
|
|
c := &auth.Credentials{}
|
|
|
|
c.User, c.Pass, _ = h.BasicAuth()
|
|
|
|
if auth := h.Header.Get("Authorization"); strings.HasPrefix(auth, "Bearer ") {
|
|
if parts := strings.Split(auth[len("Bearer "):], ":"); len(parts) == 2 { // user:pass in Authorization Bearer
|
|
c.User = parts[0]
|
|
c.Pass = parts[1]
|
|
} else { // JWT in Authorization Bearer
|
|
c.Token = auth[len("Bearer "):]
|
|
}
|
|
}
|
|
|
|
return c
|
|
}
|