Skip to content

Commit

Permalink
Merge pull request q191201771#134 from joestarzxh/master
Browse files Browse the repository at this point in the history
[fix] rtsp: basic auth验证base64
  • Loading branch information
q191201771 authored Mar 16, 2022
2 parents 92c0c72 + b775a63 commit f1f4435
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
5 changes: 3 additions & 2 deletions pkg/rtsp/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package rtsp

import (
"encoding/base64"
"fmt"
"strings"

Expand Down Expand Up @@ -81,8 +82,8 @@ func (a *Auth) MakeAuthorization(method, uri string) string {
}
switch a.Typ {
case AuthTypeBasic:
ha1 := nazamd5.Md5([]byte(fmt.Sprintf(`%s:%s`, a.Username, a.Password)))
return fmt.Sprintf(`%s %s`, a.Typ, ha1)
base1 := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf(`%s:%s`, a.Username, a.Password)))
return fmt.Sprintf(`%s %s`, a.Typ, base1)
case AuthTypeDigest:
ha1 := nazamd5.Md5([]byte(fmt.Sprintf("%s:%s:%s", a.Username, a.Realm, a.Password)))
ha2 := nazamd5.Md5([]byte(fmt.Sprintf("%s:%s", method, uri)))
Expand Down
32 changes: 32 additions & 0 deletions pkg/rtsp/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,35 @@ func TestGetRtspFirstAuth(t *testing.T) {
assert.Equal(t, "admin", rtspAuth.Username)
assert.Equal(t, "admin123", rtspAuth.Password)
}
/*
OPTIONS rtsp://35.13.202.5:554/cam/realmonitor?channel=1&subtype=0 RTSP/1.0
CSeq: 1
User-Agent: lal/0.20.3
RTSP/1.0 401 Unauthorized
CSeq: 1
WWW-Authenticate: Basic realm="MediaServer3.0"
OPTIONS rtsp://35.13.202.5:554/cam/realmonitor?channel=1&subtype=0 RTSP/1.0
CSeq: 2
User-Agent: lal/0.20.3
Authorization: Basic YWRtaW46YWRtaW4=
RTSP/1.0 200 OK
CSeq: 2
Server: Rtsp Server/3.0
Public: OPTIONS, DESCRIBE, SETUP, PLAY, PAUSE, TEARDOWN, SET_PARAMETER, GET_PARAMETER, ANNOUNCE
*/

func TestRtspBasicAuth(t *testing.T) {
var rtspAuth rtsp.Auth
auths := make([]string, 1)
auths[0] = `Basic realm="MediaServer3.0"`
username := "admin"
password := "admin"
rtspAuth.FeedWwwAuthenticate(auths, username, password)
basicAuthStr:=rtspAuth.MakeAuthorization("OPTIONS","rtsp://35.13.202.5:554/cam/realmonitor?channel=1&subtype=0")

assert.Equal(t, rtsp.AuthTypeBasic, rtspAuth.Typ)
assert.Equal(t, "Basic YWRtaW46YWRtaW4=", basicAuthStr)
}

0 comments on commit f1f4435

Please sign in to comment.