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 8da03e1
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 48 deletions.
13 changes: 1 addition & 12 deletions modify.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,18 +161,7 @@ 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
}
}
}
}
result.Referral = getReferral(err, packet)

return result, err
}
Expand Down
13 changes: 1 addition & 12 deletions passwdmodify.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,7 @@ 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
}
}
}
}
result.Referral = getReferral(err, packet)

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

func getReferral(err error, packet *ber.Packet) (referral string) {
var ok bool

if !IsErrorWithCode(err, LDAPResultReferral) || len(packet.Children) < 2 {
return ""
}

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
}
}
}

return ""
}
13 changes: 1 addition & 12 deletions v3/modify.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,18 +161,7 @@ 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
}
}
}
}
result.Referral = getReferral(err, packet)

return result, err
}
Expand Down
13 changes: 1 addition & 12 deletions v3/passwdmodify.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,7 @@ 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
}
}
}
}
result.Referral = getReferral(err, packet)

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

func getReferral(err error, packet *ber.Packet) (referral string) {
var ok bool

if !IsErrorWithCode(err, LDAPResultReferral) || len(packet.Children) < 2 {
return ""
}

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
}
}
}

return ""
}

0 comments on commit 8da03e1

Please sign in to comment.