Skip to content

Commit 8da03e1

Browse files
refactor: factorize referral retrieval
1 parent 6e90b19 commit 8da03e1

File tree

6 files changed

+40
-48
lines changed

6 files changed

+40
-48
lines changed

modify.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -161,18 +161,7 @@ func (l *Conn) ModifyWithResult(modifyRequest *ModifyRequest) (*ModifyResult, er
161161
case ApplicationModifyResponse:
162162
err := GetLDAPError(packet)
163163
if err != nil {
164-
if IsErrorWithCode(err, LDAPResultReferral) && len(packet.Children) >= 2 {
165-
for _, child := range packet.Children[1].Children {
166-
if child.Tag == ber.TagBitString && len(child.Children) >= 1 {
167-
referral, ok := child.Children[0].Value.(string)
168-
if ok {
169-
result.Referral = referral
170-
171-
break
172-
}
173-
}
174-
}
175-
}
164+
result.Referral = getReferral(err, packet)
176165

177166
return result, err
178167
}

passwdmodify.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -97,18 +97,7 @@ func (l *Conn) PasswordModify(passwordModifyRequest *PasswordModifyRequest) (*Pa
9797
if packet.Children[1].Tag == ApplicationExtendedResponse {
9898
err := GetLDAPError(packet)
9999
if err != nil {
100-
if IsErrorWithCode(err, LDAPResultReferral) && len(packet.Children) >= 2 {
101-
for _, child := range packet.Children[1].Children {
102-
if child.Tag == ber.TagBitString && len(child.Children) >= 1 {
103-
referral, ok := child.Children[0].Value.(string)
104-
if ok {
105-
result.Referral = referral
106-
107-
break
108-
}
109-
}
110-
}
111-
}
100+
result.Referral = getReferral(err, packet)
112101

113102
return result, err
114103
}

request.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,21 @@ func (l *Conn) readPacket(msgCtx *messageContext) (*ber.Packet, error) {
6969
}
7070
return packet, nil
7171
}
72+
73+
func getReferral(err error, packet *ber.Packet) (referral string) {
74+
var ok bool
75+
76+
if !IsErrorWithCode(err, LDAPResultReferral) || len(packet.Children) < 2 {
77+
return ""
78+
}
79+
80+
for _, child := range packet.Children[1].Children {
81+
if child.Tag == ber.TagBitString && len(child.Children) >= 1 {
82+
if referral, ok = child.Children[0].Value.(string); ok {
83+
return referral
84+
}
85+
}
86+
}
87+
88+
return ""
89+
}

v3/modify.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -161,18 +161,7 @@ func (l *Conn) ModifyWithResult(modifyRequest *ModifyRequest) (*ModifyResult, er
161161
case ApplicationModifyResponse:
162162
err := GetLDAPError(packet)
163163
if err != nil {
164-
if IsErrorWithCode(err, LDAPResultReferral) && len(packet.Children) >= 2 {
165-
for _, child := range packet.Children[1].Children {
166-
if child.Tag == ber.TagBitString && len(child.Children) >= 1 {
167-
referral, ok := child.Children[0].Value.(string)
168-
if ok {
169-
result.Referral = referral
170-
171-
break
172-
}
173-
}
174-
}
175-
}
164+
result.Referral = getReferral(err, packet)
176165

177166
return result, err
178167
}

v3/passwdmodify.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -97,18 +97,7 @@ func (l *Conn) PasswordModify(passwordModifyRequest *PasswordModifyRequest) (*Pa
9797
if packet.Children[1].Tag == ApplicationExtendedResponse {
9898
err := GetLDAPError(packet)
9999
if err != nil {
100-
if IsErrorWithCode(err, LDAPResultReferral) && len(packet.Children) >= 2 {
101-
for _, child := range packet.Children[1].Children {
102-
if child.Tag == ber.TagBitString && len(child.Children) >= 1 {
103-
referral, ok := child.Children[0].Value.(string)
104-
if ok {
105-
result.Referral = referral
106-
107-
break
108-
}
109-
}
110-
}
111-
}
100+
result.Referral = getReferral(err, packet)
112101

113102
return result, err
114103
}

v3/request.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,21 @@ func (l *Conn) readPacket(msgCtx *messageContext) (*ber.Packet, error) {
6969
}
7070
return packet, nil
7171
}
72+
73+
func getReferral(err error, packet *ber.Packet) (referral string) {
74+
var ok bool
75+
76+
if !IsErrorWithCode(err, LDAPResultReferral) || len(packet.Children) < 2 {
77+
return ""
78+
}
79+
80+
for _, child := range packet.Children[1].Children {
81+
if child.Tag == ber.TagBitString && len(child.Children) >= 1 {
82+
if referral, ok = child.Children[0].Value.(string); ok {
83+
return referral
84+
}
85+
}
86+
}
87+
88+
return ""
89+
}

0 commit comments

Comments
 (0)