Return *url.Userinfo from identity.GetServiceBasicAuth()

This commit is contained in:
Ingo Oppermann
2023-08-08 17:19:57 +03:00
parent 8caab33ba8
commit 3d7230444f
5 changed files with 29 additions and 21 deletions

View File

@@ -979,7 +979,7 @@ func (a *api) start(ctx context.Context) error {
} }
if identity != nil { if identity != nil {
u.User = url.UserPassword(identity.Name(), identity.GetServiceBasicAuth()) u.User = identity.GetServiceBasicAuth()
} else { } else {
u.User = url.User(config.Owner) u.User = url.User(config.Owner)
} }
@@ -1007,7 +1007,7 @@ func (a *api) start(ctx context.Context) error {
} }
if identity != nil { if identity != nil {
u.User = url.UserPassword(identity.Name(), identity.GetServiceBasicAuth()) u.User = identity.GetServiceBasicAuth()
} else { } else {
u.User = url.User(config.Owner) u.User = url.User(config.Owner)
} }
@@ -1037,7 +1037,9 @@ func (a *api) start(ctx context.Context) error {
} }
if identity != nil { if identity != nil {
u.User = url.UserPassword(identity.Name(), identity.GetServiceBasicAuth()) u.User = identity.GetServiceBasicAuth()
} else {
u.User = url.User(config.Owner)
} }
if len(config.Domain) != 0 { if len(config.Domain) != 0 {

View File

@@ -2,6 +2,7 @@ package identity
import ( import (
"fmt" "fmt"
"net/url"
"strings" "strings"
"sync" "sync"
"time" "time"
@@ -105,7 +106,7 @@ type Verifier interface {
VerifyServiceToken(token string) (bool, error) VerifyServiceToken(token string) (bool, error)
VerifyServiceSession(jwt string) (bool, interface{}, error) VerifyServiceSession(jwt string) (bool, interface{}, error)
GetServiceBasicAuth() string GetServiceBasicAuth() *url.Userinfo
GetServiceToken() string GetServiceToken() string
GetServiceSession(interface{}, time.Duration) string GetServiceSession(interface{}, time.Duration) string
@@ -319,12 +320,17 @@ func (i *identity) VerifyServiceBasicAuth(password string) (bool, error) {
return false, nil return false, nil
} }
func (i *identity) GetServiceBasicAuth() string { func (i *identity) GetServiceBasicAuth() *url.Userinfo {
i.lock.RLock() i.lock.RLock()
defer i.lock.RUnlock() defer i.lock.RUnlock()
if !i.isValid() { if !i.isValid() {
return "" return nil
}
name := i.Alias()
if len(name) == 0 {
name = i.Name()
} }
for _, password := range i.user.Auth.Services.Basic { for _, password := range i.user.Auth.Services.Basic {
@@ -332,10 +338,10 @@ func (i *identity) GetServiceBasicAuth() string {
continue continue
} }
return password return url.UserPassword(name, password)
} }
return "" return url.User(name)
} }
func (i *identity) VerifyServiceToken(token string) (bool, error) { func (i *identity) VerifyServiceToken(token string) (bool, error) {
@@ -368,7 +374,12 @@ func (i *identity) GetServiceToken() string {
continue continue
} }
return enctoken.Marshal(i.Name(), token) name := i.Alias()
if len(name) == 0 {
name = i.Name()
}
return enctoken.Marshal(name, token)
} }
return "" return ""

View File

@@ -182,7 +182,8 @@ func TestIdentityServiceBasicAuth(t *testing.T) {
require.False(t, ok) require.False(t, ok)
require.NoError(t, err) require.NoError(t, err)
password := identity.GetServiceBasicAuth() userinfo := identity.GetServiceBasicAuth()
password, _ := userinfo.Password()
require.Equal(t, "terces", password) require.Equal(t, "terces", password)
} }

View File

@@ -115,13 +115,7 @@ func (g *rewrite) isLocal(u *url.URL) bool {
} }
func (g *rewrite) httpURL(u *url.URL, mode Access, identity iamidentity.Verifier) string { func (g *rewrite) httpURL(u *url.URL, mode Access, identity iamidentity.Verifier) string {
password := identity.GetServiceBasicAuth() u.User = identity.GetServiceBasicAuth()
if len(password) == 0 {
u.User = nil
} else {
u.User = url.UserPassword(identity.Name(), password)
}
return u.String() return u.String()
} }

View File

@@ -74,10 +74,10 @@ func TestRewriteHTTP(t *testing.T) {
{"http://example.com/live/stream.m3u8", "write", "http://example.com/live/stream.m3u8"}, {"http://example.com/live/stream.m3u8", "write", "http://example.com/live/stream.m3u8"},
{"http://localhost:8181/live/stream.m3u8", "read", "http://localhost:8181/live/stream.m3u8"}, {"http://localhost:8181/live/stream.m3u8", "read", "http://localhost:8181/live/stream.m3u8"},
{"http://localhost:8181/live/stream.m3u8", "write", "http://localhost:8181/live/stream.m3u8"}, {"http://localhost:8181/live/stream.m3u8", "write", "http://localhost:8181/live/stream.m3u8"},
{"http://localhost:8080/live/stream.m3u8", "read", "http://localhost:8080/live/stream.m3u8"}, {"http://localhost:8080/live/stream.m3u8", "read", "http://foobar@localhost:8080/live/stream.m3u8"},
{"http://localhost:8080/live/stream.m3u8", "write", "http://localhost:8080/live/stream.m3u8"}, {"http://localhost:8080/live/stream.m3u8", "write", "http://foobar@localhost:8080/live/stream.m3u8"},
{"http://admin:pass@localhost:8080/live/stream.m3u8", "read", "http://localhost:8080/live/stream.m3u8"}, {"http://admin:pass@localhost:8080/live/stream.m3u8", "read", "http://foobar@localhost:8080/live/stream.m3u8"},
{"http://admin:pass@localhost:8080/live/stream.m3u8", "write", "http://localhost:8080/live/stream.m3u8"}, {"http://admin:pass@localhost:8080/live/stream.m3u8", "write", "http://foobar@localhost:8080/live/stream.m3u8"},
} }
for _, e := range samples { for _, e := range samples {