From 0db9c082e7bb488f5aea17db33b8f0a7cd7cfd11 Mon Sep 17 00:00:00 2001 From: aler9 <46489434+aler9@users.noreply.github.com> Date: Sun, 4 Oct 2020 18:15:24 +0200 Subject: [PATCH] move auth utils into dedicated folder --- authclient.go => auth/client.go | 16 ++++++++-------- auth/package.go | 2 ++ auth_test.go => auth/package_test.go | 10 +++++----- authserver.go => auth/server.go | 16 ++++++++-------- auth.go => auth/utils.go | 2 +- connclient.go | 5 +++-- 6 files changed, 27 insertions(+), 24 deletions(-) rename authclient.go => auth/client.go (84%) create mode 100644 auth/package.go rename auth_test.go => auth/package_test.go (83%) rename authserver.go => auth/server.go (87%) rename auth.go => auth/utils.go (89%) diff --git a/authclient.go b/auth/client.go similarity index 84% rename from authclient.go rename to auth/client.go index fb2a2d9c..7f40efe4 100644 --- a/authclient.go +++ b/auth/client.go @@ -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)) diff --git a/auth/package.go b/auth/package.go new file mode 100644 index 00000000..7a995fa7 --- /dev/null +++ b/auth/package.go @@ -0,0 +1,2 @@ +// Package auth contains utilities to perform authentication. +package auth diff --git a/auth_test.go b/auth/package_test.go similarity index 83% rename from auth_test.go rename to auth/package_test.go index 42888056..6de1d0db 100644 --- a/auth_test.go +++ b/auth/package_test.go @@ -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/"}) diff --git a/authserver.go b/auth/server.go similarity index 87% rename from authserver.go rename to auth/server.go index b72d616c..bd6f22ac 100644 --- a/authserver.go +++ b/auth/server.go @@ -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") } diff --git a/auth.go b/auth/utils.go similarity index 89% rename from auth.go rename to auth/utils.go index 6959ab20..238ab753 100644 --- a/auth.go +++ b/auth/utils.go @@ -1,4 +1,4 @@ -package gortsplib +package auth import ( "crypto/md5" diff --git a/connclient.go b/connclient.go index e1417390..61123427 100644 --- a/connclient.go +++ b/connclient.go @@ -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) }