mirror of
https://github.com/pomerium/pomerium.git
synced 2025-07-04 18:38:12 +02:00
internal/directory/onelogin: store directory information by user id (#992)
Same as #988
This commit is contained in:
parent
2501463dc9
commit
a042bb7b82
2 changed files with 35 additions and 33 deletions
|
@ -9,6 +9,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
@ -111,30 +112,31 @@ func (p *Provider) UserGroups(ctx context.Context) ([]*directory.User, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
userEmailToGroupIDs, err := p.getUserEmailToGroupIDs(ctx, token)
|
userIDToGroupIDs, err := p.getUserIDToGroupIDs(ctx, token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
userEmailToGroupNames := map[string][]string{}
|
userIDToGroupNames := map[int][]string{}
|
||||||
for email, groupIDs := range userEmailToGroupIDs {
|
for userID, groupIDs := range userIDToGroupIDs {
|
||||||
for _, groupID := range groupIDs {
|
for _, groupID := range groupIDs {
|
||||||
if groupName, ok := groupIDToName[groupID]; ok {
|
if groupName, ok := groupIDToName[groupID]; ok {
|
||||||
userEmailToGroupNames[email] = append(userEmailToGroupNames[email], groupName)
|
userIDToGroupNames[userID] = append(userIDToGroupNames[userID], groupName)
|
||||||
} else {
|
} else {
|
||||||
userEmailToGroupNames[email] = append(userEmailToGroupNames[email], "NOGROUP")
|
userIDToGroupNames[userID] = append(userIDToGroupNames[userID], "NOGROUP")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var users []*directory.User
|
var users []*directory.User
|
||||||
for userEmail, groups := range userEmailToGroupNames {
|
for userID, groups := range userIDToGroupNames {
|
||||||
sort.Strings(groups)
|
sort.Strings(groups)
|
||||||
users = append(users, &directory.User{
|
users = append(users, &directory.User{
|
||||||
Id: databroker.GetUserID(Name, userEmail),
|
Id: databroker.GetUserID(Name, strconv.Itoa(userID)),
|
||||||
Groups: groups,
|
Groups: groups,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
sort.Slice(users, func(i, j int) bool {
|
sort.Slice(users, func(i, j int) bool {
|
||||||
return users[i].Id < users[j].Id
|
return users[i].Id < users[j].Id
|
||||||
})
|
})
|
||||||
|
@ -168,8 +170,8 @@ func (p *Provider) getGroupIDToName(ctx context.Context, token *oauth2.Token) (m
|
||||||
return groupIDToName, nil
|
return groupIDToName, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Provider) getUserEmailToGroupIDs(ctx context.Context, token *oauth2.Token) (map[string][]int, error) {
|
func (p *Provider) getUserIDToGroupIDs(ctx context.Context, token *oauth2.Token) (map[int][]int, error) {
|
||||||
userEmailToGroupIDs := map[string][]int{}
|
userIDToGroupIDs := map[int][]int{}
|
||||||
|
|
||||||
apiURL := p.cfg.apiURL.ResolveReference(&url.URL{
|
apiURL := p.cfg.apiURL.ResolveReference(&url.URL{
|
||||||
Path: "/api/1/users",
|
Path: "/api/1/users",
|
||||||
|
@ -177,7 +179,7 @@ func (p *Provider) getUserEmailToGroupIDs(ctx context.Context, token *oauth2.Tok
|
||||||
}).String()
|
}).String()
|
||||||
for apiURL != "" {
|
for apiURL != "" {
|
||||||
var result []struct {
|
var result []struct {
|
||||||
Email string `json:"email"`
|
ID int `json:"id"`
|
||||||
GroupID *int `json:"group_id"`
|
GroupID *int `json:"group_id"`
|
||||||
}
|
}
|
||||||
nextLink, err := p.apiGet(ctx, token, apiURL, &result)
|
nextLink, err := p.apiGet(ctx, token, apiURL, &result)
|
||||||
|
@ -190,13 +192,13 @@ func (p *Provider) getUserEmailToGroupIDs(ctx context.Context, token *oauth2.Tok
|
||||||
if r.GroupID != nil {
|
if r.GroupID != nil {
|
||||||
groupID = *r.GroupID
|
groupID = *r.GroupID
|
||||||
}
|
}
|
||||||
userEmailToGroupIDs[r.Email] = append(userEmailToGroupIDs[r.Email], groupID)
|
userIDToGroupIDs[r.ID] = append(userIDToGroupIDs[r.ID], groupID)
|
||||||
}
|
}
|
||||||
|
|
||||||
apiURL = nextLink
|
apiURL = nextLink
|
||||||
}
|
}
|
||||||
|
|
||||||
return userEmailToGroupIDs, nil
|
return userIDToGroupIDs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Provider) apiGet(ctx context.Context, token *oauth2.Token, uri string, out interface{}) (nextLink string, err error) {
|
func (p *Provider) apiGet(ctx context.Context, token *oauth2.Token, uri string, out interface{}) (nextLink string, err error) {
|
||||||
|
|
|
@ -20,9 +20,9 @@ import (
|
||||||
|
|
||||||
type M = map[string]interface{}
|
type M = map[string]interface{}
|
||||||
|
|
||||||
func newMockAPI(srv *httptest.Server, userEmailToGroupName map[string]string) http.Handler {
|
func newMockAPI(srv *httptest.Server, userIDToGroupName map[int]string) http.Handler {
|
||||||
lookup := map[string]struct{}{}
|
lookup := map[string]struct{}{}
|
||||||
for _, group := range userEmailToGroupName {
|
for _, group := range userIDToGroupName {
|
||||||
lookup[group] = struct{}{}
|
lookup[group] = struct{}{}
|
||||||
}
|
}
|
||||||
var allGroups []string
|
var allGroups []string
|
||||||
|
@ -31,11 +31,11 @@ func newMockAPI(srv *httptest.Server, userEmailToGroupName map[string]string) ht
|
||||||
}
|
}
|
||||||
sort.Strings(allGroups)
|
sort.Strings(allGroups)
|
||||||
|
|
||||||
var allEmails []string
|
var allUserIDs []int
|
||||||
for email := range userEmailToGroupName {
|
for userID := range userIDToGroupName {
|
||||||
allEmails = append(allEmails, email)
|
allUserIDs = append(allUserIDs, userID)
|
||||||
}
|
}
|
||||||
sort.Strings(allEmails)
|
sort.Ints(allUserIDs)
|
||||||
|
|
||||||
r := chi.NewRouter()
|
r := chi.NewRouter()
|
||||||
r.Use(middleware.Logger)
|
r.Use(middleware.Logger)
|
||||||
|
@ -103,21 +103,21 @@ func newMockAPI(srv *httptest.Server, userEmailToGroupName map[string]string) ht
|
||||||
_ = json.NewEncoder(w).Encode(result)
|
_ = json.NewEncoder(w).Encode(result)
|
||||||
})
|
})
|
||||||
r.Get("/users", func(w http.ResponseWriter, r *http.Request) {
|
r.Get("/users", func(w http.ResponseWriter, r *http.Request) {
|
||||||
userEmailToGroupID := map[string]int{}
|
userIDToGroupID := map[int]int{}
|
||||||
for email, groupName := range userEmailToGroupName {
|
for userID, groupName := range userIDToGroupName {
|
||||||
for id, n := range allGroups {
|
for id, n := range allGroups {
|
||||||
if groupName == n {
|
if groupName == n {
|
||||||
userEmailToGroupID[email] = id
|
userIDToGroupID[userID] = id
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var result []M
|
var result []M
|
||||||
for i, email := range allEmails {
|
for _, userID := range allUserIDs {
|
||||||
result = append(result, M{
|
result = append(result, M{
|
||||||
"id": i,
|
"id": userID,
|
||||||
"email": email,
|
"email": userIDToGroupName[userID] + "@example.com",
|
||||||
"group_id": userEmailToGroupID[email],
|
"group_id": userIDToGroupID[userID],
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
_ = json.NewEncoder(w).Encode(M{
|
_ = json.NewEncoder(w).Encode(M{
|
||||||
|
@ -134,10 +134,10 @@ func TestProvider_UserGroups(t *testing.T) {
|
||||||
mockAPI.ServeHTTP(w, r)
|
mockAPI.ServeHTTP(w, r)
|
||||||
}))
|
}))
|
||||||
defer srv.Close()
|
defer srv.Close()
|
||||||
mockAPI = newMockAPI(srv, map[string]string{
|
mockAPI = newMockAPI(srv, map[int]string{
|
||||||
"a@example.com": "admin",
|
111: "admin",
|
||||||
"b@example.com": "test",
|
222: "test",
|
||||||
"c@example.com": "user",
|
333: "user",
|
||||||
})
|
})
|
||||||
|
|
||||||
p := New(
|
p := New(
|
||||||
|
@ -151,15 +151,15 @@ func TestProvider_UserGroups(t *testing.T) {
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, []*directory.User{
|
assert.Equal(t, []*directory.User{
|
||||||
{
|
{
|
||||||
Id: "onelogin/a@example.com",
|
Id: "onelogin/111",
|
||||||
Groups: []string{"admin"},
|
Groups: []string{"admin"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Id: "onelogin/b@example.com",
|
Id: "onelogin/222",
|
||||||
Groups: []string{"test"},
|
Groups: []string{"test"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Id: "onelogin/c@example.com",
|
Id: "onelogin/333",
|
||||||
Groups: []string{"user"},
|
Groups: []string{"user"},
|
||||||
},
|
},
|
||||||
}, users)
|
}, users)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue