mirror of
https://github.com/aler9/gortsplib
synced 2025-10-05 15:16:51 +08:00
headers: rename Auth into Authenticate
This commit is contained in:
@@ -24,7 +24,7 @@ func TestAuth(t *testing.T) {
|
||||
},
|
||||
{
|
||||
"both",
|
||||
[]headers.AuthMethod{headers.AuthBasic, headers.AuthDigest},
|
||||
nil,
|
||||
},
|
||||
} {
|
||||
for _, conf := range []string{
|
||||
|
@@ -21,26 +21,26 @@ type Sender struct {
|
||||
// a Validator and a set of credentials.
|
||||
func NewSender(v base.HeaderValue, user string, pass string) (*Sender, error) {
|
||||
// prefer digest
|
||||
if headerAuthDigest := func() string {
|
||||
if v0 := func() string {
|
||||
for _, vi := range v {
|
||||
if strings.HasPrefix(vi, "Digest ") {
|
||||
if strings.HasPrefix(vi, "Digest") {
|
||||
return vi
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}(); headerAuthDigest != "" {
|
||||
var auth headers.Auth
|
||||
err := auth.Read(base.HeaderValue{headerAuthDigest})
|
||||
}(); v0 != "" {
|
||||
var auth headers.Authenticate
|
||||
err := auth.Read(base.HeaderValue{v0})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if auth.Realm == nil {
|
||||
return nil, fmt.Errorf("realm not provided")
|
||||
return nil, fmt.Errorf("realm is missing")
|
||||
}
|
||||
|
||||
if auth.Nonce == nil {
|
||||
return nil, fmt.Errorf("nonce not provided")
|
||||
return nil, fmt.Errorf("nonce is missing")
|
||||
}
|
||||
|
||||
return &Sender{
|
||||
@@ -52,22 +52,22 @@ func NewSender(v base.HeaderValue, user string, pass string) (*Sender, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
if headerAuthBasic := func() string {
|
||||
if v0 := func() string {
|
||||
for _, vi := range v {
|
||||
if strings.HasPrefix(vi, "Basic ") {
|
||||
if strings.HasPrefix(vi, "Basic") {
|
||||
return vi
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}(); headerAuthBasic != "" {
|
||||
var auth headers.Auth
|
||||
err := auth.Read(base.HeaderValue{headerAuthBasic})
|
||||
}(); v0 != "" {
|
||||
var auth headers.Authenticate
|
||||
err := auth.Read(base.HeaderValue{v0})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if auth.Realm == nil {
|
||||
return nil, fmt.Errorf("realm not provided")
|
||||
return nil, fmt.Errorf("realm is missing")
|
||||
}
|
||||
|
||||
return &Sender{
|
||||
@@ -78,7 +78,7 @@ func NewSender(v base.HeaderValue, user string, pass string) (*Sender, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("there are no authentication methods available")
|
||||
return nil, fmt.Errorf("no authentication methods available")
|
||||
}
|
||||
|
||||
// GenerateHeader generates an Authorization Header that allows to authenticate a request with
|
||||
@@ -99,7 +99,7 @@ func (se *Sender) GenerateHeader(method base.Method, ur *base.URL) base.HeaderVa
|
||||
response := md5Hex(md5Hex(se.user+":"+se.realm+":"+se.pass) + ":" +
|
||||
se.nonce + ":" + md5Hex(string(method)+":"+urStr))
|
||||
|
||||
h.DigestValues = headers.Auth{
|
||||
h.DigestValues = headers.Authenticate{
|
||||
Method: headers.AuthDigest,
|
||||
Username: &se.user,
|
||||
Realm: &se.realm,
|
||||
|
@@ -29,17 +29,13 @@ func NewValidator(user string, pass string, methods []headers.AuthMethod) *Valid
|
||||
}
|
||||
|
||||
userHashed := false
|
||||
if strings.HasPrefix(user, "plain:") {
|
||||
user = strings.TrimPrefix(user, "plain:")
|
||||
} else if strings.HasPrefix(user, "sha256:") {
|
||||
if strings.HasPrefix(user, "sha256:") {
|
||||
user = strings.TrimPrefix(user, "sha256:")
|
||||
userHashed = true
|
||||
}
|
||||
|
||||
passHashed := false
|
||||
if strings.HasPrefix(pass, "plain:") {
|
||||
pass = strings.TrimPrefix(pass, "plain:")
|
||||
} else if strings.HasPrefix(pass, "sha256:") {
|
||||
if strings.HasPrefix(pass, "sha256:") {
|
||||
pass = strings.TrimPrefix(pass, "sha256:")
|
||||
passHashed = true
|
||||
}
|
||||
@@ -71,13 +67,13 @@ func (va *Validator) GenerateHeader() base.HeaderValue {
|
||||
for _, m := range va.methods {
|
||||
switch m {
|
||||
case headers.AuthBasic:
|
||||
ret = append(ret, (&headers.Auth{
|
||||
ret = append(ret, (&headers.Authenticate{
|
||||
Method: headers.AuthBasic,
|
||||
Realm: &va.realm,
|
||||
}).Write()...)
|
||||
|
||||
case headers.AuthDigest:
|
||||
ret = append(ret, headers.Auth{
|
||||
ret = append(ret, headers.Authenticate{
|
||||
Method: headers.AuthDigest,
|
||||
Realm: &va.realm,
|
||||
Nonce: &va.nonce,
|
||||
@@ -125,23 +121,23 @@ func (va *Validator) ValidateHeader(
|
||||
|
||||
default: // headers.AuthDigest
|
||||
if auth.DigestValues.Realm == nil {
|
||||
return fmt.Errorf("realm not provided")
|
||||
return fmt.Errorf("realm is missing")
|
||||
}
|
||||
|
||||
if auth.DigestValues.Nonce == nil {
|
||||
return fmt.Errorf("nonce not provided")
|
||||
return fmt.Errorf("nonce is missing")
|
||||
}
|
||||
|
||||
if auth.DigestValues.Username == nil {
|
||||
return fmt.Errorf("username not provided")
|
||||
return fmt.Errorf("username is missing")
|
||||
}
|
||||
|
||||
if auth.DigestValues.URI == nil {
|
||||
return fmt.Errorf("uri not provided")
|
||||
return fmt.Errorf("uri is missing")
|
||||
}
|
||||
|
||||
if auth.DigestValues.Response == nil {
|
||||
return fmt.Errorf("response not provided")
|
||||
return fmt.Errorf("response is missing")
|
||||
}
|
||||
|
||||
if *auth.DigestValues.Nonce != va.nonce {
|
||||
|
@@ -19,8 +19,8 @@ const (
|
||||
AuthDigest
|
||||
)
|
||||
|
||||
// Auth is an Authenticate or a WWW-Authenticate header.
|
||||
type Auth struct {
|
||||
// Authenticate is an Authenticate or a WWW-Authenticate header.
|
||||
type Authenticate struct {
|
||||
// authentication method
|
||||
Method AuthMethod
|
||||
|
||||
@@ -50,7 +50,7 @@ type Auth struct {
|
||||
}
|
||||
|
||||
// Read decodes an Authenticate or a WWW-Authenticate header.
|
||||
func (h *Auth) Read(v base.HeaderValue) error {
|
||||
func (h *Authenticate) Read(v base.HeaderValue) error {
|
||||
if len(v) == 0 {
|
||||
return fmt.Errorf("value not provided")
|
||||
}
|
||||
@@ -120,7 +120,7 @@ func (h *Auth) Read(v base.HeaderValue) error {
|
||||
}
|
||||
|
||||
// Write encodes an Authenticate or a WWW-Authenticate header.
|
||||
func (h Auth) Write() base.HeaderValue {
|
||||
func (h Authenticate) Write() base.HeaderValue {
|
||||
ret := ""
|
||||
|
||||
switch h.Method {
|
||||
|
@@ -8,17 +8,17 @@ import (
|
||||
"github.com/aler9/gortsplib/pkg/base"
|
||||
)
|
||||
|
||||
var casesAuth = []struct {
|
||||
var casesAuthenticate = []struct {
|
||||
name string
|
||||
vin base.HeaderValue
|
||||
vout base.HeaderValue
|
||||
h Auth
|
||||
h Authenticate
|
||||
}{
|
||||
{
|
||||
"basic",
|
||||
base.HeaderValue{`Basic realm="4419b63f5e51"`},
|
||||
base.HeaderValue{`Basic realm="4419b63f5e51"`},
|
||||
Auth{
|
||||
Authenticate{
|
||||
Method: AuthBasic,
|
||||
Realm: func() *string {
|
||||
v := "4419b63f5e51"
|
||||
@@ -30,7 +30,7 @@ var casesAuth = []struct {
|
||||
"digest request 1",
|
||||
base.HeaderValue{`Digest realm="4419b63f5e51", nonce="8b84a3b789283a8bea8da7fa7d41f08b", stale="FALSE"`},
|
||||
base.HeaderValue{`Digest realm="4419b63f5e51", nonce="8b84a3b789283a8bea8da7fa7d41f08b", stale="FALSE"`},
|
||||
Auth{
|
||||
Authenticate{
|
||||
Method: AuthDigest,
|
||||
Realm: func() *string {
|
||||
v := "4419b63f5e51"
|
||||
@@ -50,7 +50,7 @@ var casesAuth = []struct {
|
||||
"digest request 2",
|
||||
base.HeaderValue{`Digest realm="4419b63f5e51", nonce="8b84a3b789283a8bea8da7fa7d41f08b", stale=FALSE`},
|
||||
base.HeaderValue{`Digest realm="4419b63f5e51", nonce="8b84a3b789283a8bea8da7fa7d41f08b", stale="FALSE"`},
|
||||
Auth{
|
||||
Authenticate{
|
||||
Method: AuthDigest,
|
||||
Realm: func() *string {
|
||||
v := "4419b63f5e51"
|
||||
@@ -70,7 +70,7 @@ var casesAuth = []struct {
|
||||
"digest request 3",
|
||||
base.HeaderValue{`Digest realm="4419b63f5e51",nonce="133767111917411116111311118211673010032", stale="FALSE"`},
|
||||
base.HeaderValue{`Digest realm="4419b63f5e51", nonce="133767111917411116111311118211673010032", stale="FALSE"`},
|
||||
Auth{
|
||||
Authenticate{
|
||||
Method: AuthDigest,
|
||||
Realm: func() *string {
|
||||
v := "4419b63f5e51"
|
||||
@@ -90,7 +90,7 @@ var casesAuth = []struct {
|
||||
"digest response generic",
|
||||
base.HeaderValue{`Digest username="aa", realm="bb", nonce="cc", uri="dd", response="ee"`},
|
||||
base.HeaderValue{`Digest username="aa", realm="bb", nonce="cc", uri="dd", response="ee"`},
|
||||
Auth{
|
||||
Authenticate{
|
||||
Method: AuthDigest,
|
||||
Username: func() *string {
|
||||
v := "aa"
|
||||
@@ -118,7 +118,7 @@ var casesAuth = []struct {
|
||||
"digest response with empty field",
|
||||
base.HeaderValue{`Digest username="", realm="IPCAM", nonce="5d17cd12b9fa8a85ac5ceef0926ea5a6", uri="rtsp://localhost:8554/mystream", response="c072ae90eb4a27f4cdcb90d62266b2a1"`},
|
||||
base.HeaderValue{`Digest username="", realm="IPCAM", nonce="5d17cd12b9fa8a85ac5ceef0926ea5a6", uri="rtsp://localhost:8554/mystream", response="c072ae90eb4a27f4cdcb90d62266b2a1"`},
|
||||
Auth{
|
||||
Authenticate{
|
||||
Method: AuthDigest,
|
||||
Username: func() *string {
|
||||
v := ""
|
||||
@@ -146,7 +146,7 @@ var casesAuth = []struct {
|
||||
"digest response with no spaces and additional fields",
|
||||
base.HeaderValue{`Digest realm="Please log in with a valid username",nonce="752a62306daf32b401a41004555c7663",opaque="",stale=FALSE,algorithm=MD5`},
|
||||
base.HeaderValue{`Digest realm="Please log in with a valid username", nonce="752a62306daf32b401a41004555c7663", opaque="", stale="FALSE", algorithm="MD5"`},
|
||||
Auth{
|
||||
Authenticate{
|
||||
Method: AuthDigest,
|
||||
Realm: func() *string {
|
||||
v := "Please log in with a valid username"
|
||||
@@ -172,10 +172,10 @@ var casesAuth = []struct {
|
||||
},
|
||||
}
|
||||
|
||||
func TestAuthRead(t *testing.T) {
|
||||
for _, ca := range casesAuth {
|
||||
func TestAuthenticateRead(t *testing.T) {
|
||||
for _, ca := range casesAuthenticate {
|
||||
t.Run(ca.name, func(t *testing.T) {
|
||||
var h Auth
|
||||
var h Authenticate
|
||||
err := h.Read(ca.vin)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, ca.h, h)
|
||||
@@ -183,8 +183,8 @@ func TestAuthRead(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthWrite(t *testing.T) {
|
||||
for _, ca := range casesAuth {
|
||||
func TestAuthenticateWrite(t *testing.T) {
|
||||
for _, ca := range casesAuthenticate {
|
||||
t.Run(ca.name, func(t *testing.T) {
|
||||
vout := ca.h.Write()
|
||||
require.Equal(t, ca.vout, vout)
|
||||
@@ -192,7 +192,7 @@ func TestAuthWrite(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthReadError(t *testing.T) {
|
||||
func TestAutenticatehReadError(t *testing.T) {
|
||||
for _, ca := range []struct {
|
||||
name string
|
||||
hv base.HeaderValue
|
||||
@@ -220,7 +220,7 @@ func TestAuthReadError(t *testing.T) {
|
||||
},
|
||||
} {
|
||||
t.Run(ca.name, func(t *testing.T) {
|
||||
var h Auth
|
||||
var h Authenticate
|
||||
err := h.Read(ca.hv)
|
||||
require.Equal(t, ca.err, err.Error())
|
||||
})
|
||||
|
@@ -20,7 +20,7 @@ type Authorization struct {
|
||||
BasicPass string
|
||||
|
||||
// digest values
|
||||
DigestValues Auth
|
||||
DigestValues Authenticate
|
||||
}
|
||||
|
||||
// Read decodes an Authorization header.
|
||||
@@ -56,7 +56,7 @@ func (h *Authorization) Read(v base.HeaderValue) error {
|
||||
case strings.HasPrefix(v0, "Digest "):
|
||||
h.Method = AuthDigest
|
||||
|
||||
var vals Auth
|
||||
var vals Authenticate
|
||||
err := vals.Read(base.HeaderValue{v0})
|
||||
if err != nil {
|
||||
return err
|
||||
|
@@ -30,7 +30,7 @@ var casesAuthorization = []struct {
|
||||
base.HeaderValue{"Digest realm=\"4419b63f5e51\", nonce=\"8b84a3b789283a8bea8da7fa7d41f08b\", stale=\"FALSE\""},
|
||||
Authorization{
|
||||
Method: AuthDigest,
|
||||
DigestValues: Auth{
|
||||
DigestValues: Authenticate{
|
||||
Method: AuthDigest,
|
||||
Realm: func() *string {
|
||||
v := "4419b63f5e51"
|
||||
|
Reference in New Issue
Block a user