mirror of
https://github.com/aler9/gortsplib
synced 2025-10-16 12:10:48 +08:00
move auth utils into dedicated folder
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
package gortsplib
|
||||
package auth
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
@@ -10,9 +10,9 @@ import (
|
||||
"github.com/aler9/gortsplib/headers"
|
||||
)
|
||||
|
||||
// authClient is an object that helps a client to send its credentials to a
|
||||
// Client is an object that helps a client to send its credentials to a
|
||||
// server.
|
||||
type authClient struct {
|
||||
type Client struct {
|
||||
user string
|
||||
pass string
|
||||
method headers.AuthMethod
|
||||
@@ -20,9 +20,9 @@ type authClient struct {
|
||||
nonce string
|
||||
}
|
||||
|
||||
// newAuthClient allocates an authClient.
|
||||
// NewClient allocates an Client.
|
||||
// header is the WWW-Authenticate header provided by the server.
|
||||
func newAuthClient(v base.HeaderValue, user string, pass string) (*authClient, error) {
|
||||
func NewClient(v base.HeaderValue, user string, pass string) (*Client, error) {
|
||||
// prefer digest
|
||||
if headerAuthDigest := func() string {
|
||||
for _, vi := range v {
|
||||
@@ -45,7 +45,7 @@ func newAuthClient(v base.HeaderValue, user string, pass string) (*authClient, e
|
||||
return nil, fmt.Errorf("nonce not provided")
|
||||
}
|
||||
|
||||
return &authClient{
|
||||
return &Client{
|
||||
user: user,
|
||||
pass: pass,
|
||||
method: headers.AuthDigest,
|
||||
@@ -71,7 +71,7 @@ func newAuthClient(v base.HeaderValue, user string, pass string) (*authClient, e
|
||||
return nil, fmt.Errorf("realm not provided")
|
||||
}
|
||||
|
||||
return &authClient{
|
||||
return &Client{
|
||||
user: user,
|
||||
pass: pass,
|
||||
method: headers.AuthBasic,
|
||||
@@ -84,7 +84,7 @@ func newAuthClient(v base.HeaderValue, user string, pass string) (*authClient, e
|
||||
|
||||
// GenerateHeader generates an Authorization Header that allows to authenticate a request with
|
||||
// the given method and url.
|
||||
func (ac *authClient) GenerateHeader(method base.Method, ur *url.URL) base.HeaderValue {
|
||||
func (ac *Client) GenerateHeader(method base.Method, ur *url.URL) base.HeaderValue {
|
||||
switch ac.method {
|
||||
case headers.AuthBasic:
|
||||
response := base64.StdEncoding.EncodeToString([]byte(ac.user + ":" + ac.pass))
|
2
auth/package.go
Normal file
2
auth/package.go
Normal file
@@ -0,0 +1,2 @@
|
||||
// Package auth contains utilities to perform authentication.
|
||||
package auth
|
@@ -1,4 +1,4 @@
|
||||
package gortsplib
|
||||
package auth
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
@@ -31,10 +31,10 @@ var casesAuth = []struct {
|
||||
func TestAuthMethods(t *testing.T) {
|
||||
for _, c := range casesAuth {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
authServer := NewAuthServer("testuser", "testpass", c.methods)
|
||||
authServer := NewServer("testuser", "testpass", c.methods)
|
||||
wwwAuthenticate := authServer.GenerateHeader()
|
||||
|
||||
ac, err := newAuthClient(wwwAuthenticate, "testuser", "testpass")
|
||||
ac, err := NewClient(wwwAuthenticate, "testuser", "testpass")
|
||||
require.NoError(t, err)
|
||||
authorization := ac.GenerateHeader(base.ANNOUNCE,
|
||||
&url.URL{Scheme: "rtsp", Host: "myhost", Path: "mypath"})
|
||||
@@ -47,11 +47,11 @@ func TestAuthMethods(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestAuthBasePath(t *testing.T) {
|
||||
authServer := NewAuthServer("testuser", "testpass",
|
||||
authServer := NewServer("testuser", "testpass",
|
||||
[]headers.AuthMethod{headers.AuthBasic, headers.AuthDigest})
|
||||
wwwAuthenticate := authServer.GenerateHeader()
|
||||
|
||||
ac, err := newAuthClient(wwwAuthenticate, "testuser", "testpass")
|
||||
ac, err := NewClient(wwwAuthenticate, "testuser", "testpass")
|
||||
require.NoError(t, err)
|
||||
authorization := ac.GenerateHeader(base.ANNOUNCE,
|
||||
&url.URL{Scheme: "rtsp", Host: "myhost", Path: "mypath/"})
|
@@ -1,4 +1,4 @@
|
||||
package gortsplib
|
||||
package auth
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
@@ -12,9 +12,9 @@ import (
|
||||
"github.com/aler9/gortsplib/headers"
|
||||
)
|
||||
|
||||
// AuthServer is an object that helps a server to validate the credentials of
|
||||
// Server is an object that helps a server to validate the credentials of
|
||||
// a client.
|
||||
type AuthServer struct {
|
||||
type Server struct {
|
||||
user string
|
||||
pass string
|
||||
methods []headers.AuthMethod
|
||||
@@ -22,9 +22,9 @@ type AuthServer struct {
|
||||
nonce string
|
||||
}
|
||||
|
||||
// NewAuthServer allocates an AuthServer.
|
||||
// NewServer allocates an Server.
|
||||
// If methods is nil, the Basic and Digest methods are used.
|
||||
func NewAuthServer(user string, pass string, methods []headers.AuthMethod) *AuthServer {
|
||||
func NewServer(user string, pass string, methods []headers.AuthMethod) *Server {
|
||||
if methods == nil {
|
||||
methods = []headers.AuthMethod{headers.AuthBasic, headers.AuthDigest}
|
||||
}
|
||||
@@ -33,7 +33,7 @@ func NewAuthServer(user string, pass string, methods []headers.AuthMethod) *Auth
|
||||
rand.Read(nonceByts)
|
||||
nonce := hex.EncodeToString(nonceByts)
|
||||
|
||||
return &AuthServer{
|
||||
return &Server{
|
||||
user: user,
|
||||
pass: pass,
|
||||
methods: methods,
|
||||
@@ -43,7 +43,7 @@ func NewAuthServer(user string, pass string, methods []headers.AuthMethod) *Auth
|
||||
}
|
||||
|
||||
// GenerateHeader generates the WWW-Authenticate header needed by a client to log in.
|
||||
func (as *AuthServer) GenerateHeader() base.HeaderValue {
|
||||
func (as *Server) GenerateHeader() base.HeaderValue {
|
||||
var ret base.HeaderValue
|
||||
for _, m := range as.methods {
|
||||
switch m {
|
||||
@@ -66,7 +66,7 @@ func (as *AuthServer) GenerateHeader() base.HeaderValue {
|
||||
|
||||
// ValidateHeader validates the Authorization header sent by a client after receiving the
|
||||
// WWW-Authenticate header.
|
||||
func (as *AuthServer) ValidateHeader(v base.HeaderValue, method base.Method, ur *url.URL) error {
|
||||
func (as *Server) ValidateHeader(v base.HeaderValue, method base.Method, ur *url.URL) error {
|
||||
if len(v) == 0 {
|
||||
return fmt.Errorf("authorization header not provided")
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
package gortsplib
|
||||
package auth
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
@@ -18,6 +18,7 @@ import (
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/aler9/gortsplib/auth"
|
||||
"github.com/aler9/gortsplib/base"
|
||||
"github.com/aler9/gortsplib/headers"
|
||||
"github.com/aler9/gortsplib/rtcpreceiver"
|
||||
@@ -77,7 +78,7 @@ type ConnClient struct {
|
||||
bw *bufio.Writer
|
||||
session string
|
||||
cseq int
|
||||
auth *authClient
|
||||
auth *auth.Client
|
||||
state connClientState
|
||||
streamUrl *url.URL
|
||||
streamProtocol *StreamProtocol
|
||||
@@ -308,7 +309,7 @@ func (c *ConnClient) Do(req *base.Request) (*base.Response, error) {
|
||||
// setup authentication
|
||||
if res.StatusCode == base.StatusUnauthorized && req.Url.User != nil && c.auth == nil {
|
||||
pass, _ := req.Url.User.Password()
|
||||
auth, err := newAuthClient(res.Header["WWW-Authenticate"], req.Url.User.Username(), pass)
|
||||
auth, err := auth.NewClient(res.Header["WWW-Authenticate"], req.Url.User.Username(), pass)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to setup authentication: %s", err)
|
||||
}
|
||||
|
Reference in New Issue
Block a user