Skip to content

Commit

Permalink
refactor: factorize referral retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
james-d-elliott committed Apr 30, 2022
1 parent 6e90b19 commit 76b0db3
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 44 deletions.
13 changes: 2 additions & 11 deletions modify.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,17 +161,8 @@ func (l *Conn) ModifyWithResult(modifyRequest *ModifyRequest) (*ModifyResult, er
case ApplicationModifyResponse:
err := GetLDAPError(packet)
if err != nil {
if IsErrorWithCode(err, LDAPResultReferral) && len(packet.Children) >= 2 {
for _, child := range packet.Children[1].Children {
if child.Tag == ber.TagBitString && len(child.Children) >= 1 {
referral, ok := child.Children[0].Value.(string)
if ok {
result.Referral = referral

break
}
}
}
if referral, ok := getReferral(err, packet); ok {
result.Referral = referral
}

return result, err
Expand Down
13 changes: 2 additions & 11 deletions passwdmodify.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,8 @@ func (l *Conn) PasswordModify(passwordModifyRequest *PasswordModifyRequest) (*Pa
if packet.Children[1].Tag == ApplicationExtendedResponse {
err := GetLDAPError(packet)
if err != nil {
if IsErrorWithCode(err, LDAPResultReferral) && len(packet.Children) >= 2 {
for _, child := range packet.Children[1].Children {
if child.Tag == ber.TagBitString && len(child.Children) >= 1 {
referral, ok := child.Children[0].Value.(string)
if ok {
result.Referral = referral

break
}
}
}
if referral, ok := getReferral(err, packet); ok {
result.Referral = referral
}

return result, err
Expand Down
16 changes: 16 additions & 0 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,19 @@ func (l *Conn) readPacket(msgCtx *messageContext) (*ber.Packet, error) {
}
return packet, nil
}

func getReferral(err error, packet *ber.Packet) (referral string, ok bool) {
if !IsErrorWithCode(err, LDAPResultReferral) || len(packet.Children) < 2 {
return "", false
}

for _, child := range packet.Children[1].Children {
if child.Tag == ber.TagBitString && len(child.Children) >= 1 {
if referral, ok = child.Children[0].Value.(string); ok {
return referral, ok
}
}
}

return "", false
}
13 changes: 2 additions & 11 deletions v3/modify.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,17 +161,8 @@ func (l *Conn) ModifyWithResult(modifyRequest *ModifyRequest) (*ModifyResult, er
case ApplicationModifyResponse:
err := GetLDAPError(packet)
if err != nil {
if IsErrorWithCode(err, LDAPResultReferral) && len(packet.Children) >= 2 {
for _, child := range packet.Children[1].Children {
if child.Tag == ber.TagBitString && len(child.Children) >= 1 {
referral, ok := child.Children[0].Value.(string)
if ok {
result.Referral = referral

break
}
}
}
if referral, ok := getReferral(err, packet); ok {
result.Referral = referral
}

return result, err
Expand Down
13 changes: 2 additions & 11 deletions v3/passwdmodify.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,8 @@ func (l *Conn) PasswordModify(passwordModifyRequest *PasswordModifyRequest) (*Pa
if packet.Children[1].Tag == ApplicationExtendedResponse {
err := GetLDAPError(packet)
if err != nil {
if IsErrorWithCode(err, LDAPResultReferral) && len(packet.Children) >= 2 {
for _, child := range packet.Children[1].Children {
if child.Tag == ber.TagBitString && len(child.Children) >= 1 {
referral, ok := child.Children[0].Value.(string)
if ok {
result.Referral = referral

break
}
}
}
if referral, ok := getReferral(err, packet); ok {
result.Referral = referral
}

return result, err
Expand Down
16 changes: 16 additions & 0 deletions v3/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,19 @@ func (l *Conn) readPacket(msgCtx *messageContext) (*ber.Packet, error) {
}
return packet, nil
}

func getReferral(err error, packet *ber.Packet) (referral string, ok bool) {
if !IsErrorWithCode(err, LDAPResultReferral) || len(packet.Children) < 2 {
return "", false
}

for _, child := range packet.Children[1].Children {
if child.Tag == ber.TagBitString && len(child.Children) >= 1 {
if referral, ok = child.Children[0].Value.(string); ok {
return referral, ok
}
}
}

return "", false
}

0 comments on commit 76b0db3

Please sign in to comment.