auth: add Validate() and deprecate Validator{} (#272)

This commit is contained in:
Alessandro Ros
2023-05-07 19:34:20 +02:00
committed by GitHub
parent 2170ef4b00
commit 7c67221494
11 changed files with 280 additions and 106 deletions

View File

@@ -1,7 +1,10 @@
package auth
import (
"crypto/md5"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"fmt"
"strings"
@@ -11,7 +14,20 @@ import (
"github.com/bluenviron/gortsplib/v3/pkg/url"
)
func md5Hex(in string) string {
h := md5.New()
h.Write([]byte(in))
return hex.EncodeToString(h.Sum(nil))
}
func sha256Base64(in string) string {
h := sha256.New()
h.Write([]byte(in))
return base64.StdEncoding.EncodeToString(h.Sum(nil))
}
// Validator allows to validate credentials generated by a Sender.
// Deprecated: Validator{} has been replaced by Validate()
type Validator struct {
user string
userHashed bool
@@ -24,6 +40,7 @@ type Validator struct {
// NewValidator allocates a Validator.
// If methods is nil, the Basic and Digest methods are used.
// Deprecated: Validator{} has been replaced by Validate()
func NewValidator(user string, pass string, methods []headers.AuthMethod) *Validator {
if methods == nil {
methods = []headers.AuthMethod{headers.AuthBasic, headers.AuthDigest}
@@ -63,6 +80,7 @@ func NewValidator(user string, pass string, methods []headers.AuthMethod) *Valid
// Header generates the WWW-Authenticate header needed by a client to
// authenticate.
// Deprecated: Validator{} has been replaced by Validate()
func (va *Validator) Header() base.HeaderValue {
var ret base.HeaderValue
for _, m := range va.methods {
@@ -84,8 +102,9 @@ func (va *Validator) Header() base.HeaderValue {
return ret
}
// ValidateRequest validates a request sent by a client.
func (va *Validator) ValidateRequest(req *base.Request, baseURL *url.URL) error {
// Validate validates a request sent by a client.
// Deprecated: Validator{} has been replaced by Validate()
func (va *Validator) Validate(req *base.Request, baseURL *url.URL) error {
var auth headers.Authorization
err := auth.Unmarshal(req.Header["Authorization"])
if err != nil {