feat(connector/authproxy): support multiple groups (#2643)

Signed-off-by: Marcelo Clavel <mclavel00@gmail.com>
This commit is contained in:
Marcelo Clavel 2022-09-01 07:46:24 -04:00 committed by GitHub
parent d33e994805
commit 29b3523e07
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 140 additions and 1 deletions

View File

@ -7,6 +7,7 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"net/url" "net/url"
"strings"
"github.com/dexidp/dex/connector" "github.com/dexidp/dex/connector"
"github.com/dexidp/dex/pkg/log" "github.com/dexidp/dex/pkg/log"
@ -69,7 +70,11 @@ func (m *callback) HandleCallback(s connector.Scopes, r *http.Request) (connecto
groups := m.groups groups := m.groups
headerGroup := r.Header.Get(m.groupHeader) headerGroup := r.Header.Get(m.groupHeader)
if headerGroup != "" { if headerGroup != "" {
groups = append(groups, headerGroup) splitheaderGroup := strings.Split(headerGroup, ",")
for i, v := range splitheaderGroup {
splitheaderGroup[i] = strings.TrimSpace(v)
}
groups = append(splitheaderGroup, groups...)
} }
return connector.Identity{ return connector.Identity{
UserID: remoteUser, // TODO: figure out if this is a bad ID value. UserID: remoteUser, // TODO: figure out if this is a bad ID value.

View File

@ -0,0 +1,134 @@
package authproxy
import (
"io"
"net/http"
"reflect"
"testing"
"github.com/sirupsen/logrus"
"github.com/dexidp/dex/connector"
)
const (
testEmail = "testuser@example.com"
testGroup1 = "group1"
testGroup2 = "group2"
testGroup3 = "group 3"
testGroup4 = "group 4"
testStaticGroup1 = "static1"
testStaticGroup2 = "static 2"
)
var logger = &logrus.Logger{Out: io.Discard, Formatter: &logrus.TextFormatter{}}
func TestUser(t *testing.T) {
config := Config{
UserHeader: "X-Remote-User",
}
conn := callback{userHeader: config.UserHeader, logger: logger, pathSuffix: "/test"}
req, err := http.NewRequest("GET", "/", nil)
expectNil(t, err)
req.Header = map[string][]string{
"X-Remote-User": {testEmail},
}
ident, err := conn.HandleCallback(connector.Scopes{OfflineAccess: true, Groups: true}, req)
expectNil(t, err)
expectEquals(t, ident.UserID, testEmail)
expectEquals(t, ident.Email, testEmail)
expectEquals(t, len(ident.Groups), 0)
}
func TestSingleGroup(t *testing.T) {
config := Config{
UserHeader: "X-Remote-User",
GroupHeader: "X-Remote-Group",
}
conn := callback{userHeader: config.UserHeader, groupHeader: config.GroupHeader, logger: logger, pathSuffix: "/test"}
req, err := http.NewRequest("GET", "/", nil)
expectNil(t, err)
req.Header = map[string][]string{
"X-Remote-User": {testEmail},
"X-Remote-Group": {testGroup1},
}
ident, err := conn.HandleCallback(connector.Scopes{OfflineAccess: true, Groups: true}, req)
expectNil(t, err)
expectEquals(t, ident.UserID, testEmail)
expectEquals(t, len(ident.Groups), 1)
expectEquals(t, ident.Groups[0], testGroup1)
}
func TestMultipleGroup(t *testing.T) {
config := Config{
UserHeader: "X-Remote-User",
GroupHeader: "X-Remote-Group",
}
conn := callback{userHeader: config.UserHeader, groupHeader: config.GroupHeader, logger: logger, pathSuffix: "/test"}
req, err := http.NewRequest("GET", "/", nil)
expectNil(t, err)
req.Header = map[string][]string{
"X-Remote-User": {testEmail},
"X-Remote-Group": {testGroup1 + ", " + testGroup2 + ", " + testGroup3 + ", " + testGroup4},
}
ident, err := conn.HandleCallback(connector.Scopes{OfflineAccess: true, Groups: true}, req)
expectNil(t, err)
expectEquals(t, ident.UserID, testEmail)
expectEquals(t, len(ident.Groups), 4)
expectEquals(t, ident.Groups[0], testGroup1)
expectEquals(t, ident.Groups[1], testGroup2)
expectEquals(t, ident.Groups[2], testGroup3)
expectEquals(t, ident.Groups[3], testGroup4)
}
func TestStaticGroup(t *testing.T) {
config := Config{
UserHeader: "X-Remote-User",
GroupHeader: "X-Remote-Group",
Groups: []string{"static1", "static 2"},
}
conn := callback{userHeader: config.UserHeader, groupHeader: config.GroupHeader, groups: config.Groups, logger: logger, pathSuffix: "/test"}
req, err := http.NewRequest("GET", "/", nil)
expectNil(t, err)
req.Header = map[string][]string{
"X-Remote-User": {testEmail},
"X-Remote-Group": {testGroup1 + ", " + testGroup2 + ", " + testGroup3 + ", " + testGroup4},
}
ident, err := conn.HandleCallback(connector.Scopes{OfflineAccess: true, Groups: true}, req)
expectNil(t, err)
expectEquals(t, ident.UserID, testEmail)
expectEquals(t, len(ident.Groups), 6)
expectEquals(t, ident.Groups[0], testGroup1)
expectEquals(t, ident.Groups[1], testGroup2)
expectEquals(t, ident.Groups[2], testGroup3)
expectEquals(t, ident.Groups[3], testGroup4)
expectEquals(t, ident.Groups[4], testStaticGroup1)
expectEquals(t, ident.Groups[5], testStaticGroup2)
}
func expectNil(t *testing.T, a interface{}) {
if a != nil {
t.Errorf("Expected %+v to equal nil", a)
}
}
func expectEquals(t *testing.T, a interface{}, b interface{}) {
if !reflect.DeepEqual(a, b) {
t.Errorf("Expected %+v to equal %+v", a, b)
}
}