Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Work around baton-sdk's detection of repetitive pagetokens. #64

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions pkg/ldap/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,29 @@ func (c *Client) getConnection(ctx context.Context, isModify bool, f func(client
return nil
}

func parsePageToken(pageToken string) (string, []byte, error) {
if pageToken == "" {
return "", nil, nil
}
parts := strings.SplitN(pageToken, ":", 2)
if len(parts) != 2 {
return "", nil, fmt.Errorf("invalid page token")
}
decodedToken, err := base64.StdEncoding.DecodeString(parts[1])
return parts[0], decodedToken, err
}

var requestId = 0

func encodePageToken(cookie []byte) string {
if len(cookie) == 0 {
return ""
}
requestId++
requestId %= 100
return fmt.Sprintf("%v:%v", requestId, base64.StdEncoding.EncodeToString(cookie))
}

func (c *Client) LdapSearch(ctx context.Context, filter string, attrNames []string, pageToken string, pageSize uint32, baseDNOverride string) ([]*ldap.Entry, string, error) {
l := ctxzap.Extract(ctx)

Expand All @@ -112,7 +135,7 @@ func (c *Client) LdapSearch(ctx context.Context, filter string, attrNames []stri

pagingControl := ldap.NewControlPaging(pageSize)
if pageToken != "" {
decodedToken, err := base64.StdEncoding.DecodeString(pageToken)
_, decodedToken, err := parsePageToken(pageToken)
if err != nil {
return err
}
Expand Down Expand Up @@ -154,7 +177,7 @@ func (c *Client) LdapSearch(ctx context.Context, filter string, attrNames []stri

resultPc := ldap.FindControl(resp.Controls, ldap.ControlTypePaging)
if pc, ok := resultPc.(*ldap.ControlPaging); ok {
nextPageToken = base64.StdEncoding.EncodeToString(pc.Cookie)
nextPageToken = encodePageToken(pc.Cookie)
}
return nil
})
Expand Down
Loading