Skip to content

Commit abf21d9

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

File tree

6 files changed

+44
-44
lines changed

6 files changed

+44
-44
lines changed

modify.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -161,17 +161,10 @@ 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-
}
164+
if referral, ok := getReferral(err, packet); ok {
165+
result.Referral = referral
166+
167+
break
175168
}
176169

177170
return result, err

passwdmodify.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,8 @@ 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-
}
100+
if referral, ok := getReferral(err, packet); ok {
101+
result.Referral = referral
111102
}
112103

113104
return result, err

request.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,19 @@ 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, ok bool) {
74+
if !IsErrorWithCode(err, LDAPResultReferral) || len(packet.Children) < 2 {
75+
return "", false
76+
}
77+
78+
for _, child := range packet.Children[1].Children {
79+
if child.Tag == ber.TagBitString && len(child.Children) >= 1 {
80+
if referral, ok = child.Children[0].Value.(string); ok {
81+
return referral, ok
82+
}
83+
}
84+
}
85+
86+
return "", false
87+
}

v3/modify.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -161,17 +161,10 @@ 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-
}
164+
if referral, ok := getReferral(err, packet); ok {
165+
result.Referral = referral
166+
167+
break
175168
}
176169

177170
return result, err

v3/passwdmodify.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,8 @@ 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-
}
100+
if referral, ok := getReferral(err, packet); ok {
101+
result.Referral = referral
111102
}
112103

113104
return result, err

v3/request.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,19 @@ 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, ok bool) {
74+
if !IsErrorWithCode(err, LDAPResultReferral) || len(packet.Children) < 2 {
75+
return "", false
76+
}
77+
78+
for _, child := range packet.Children[1].Children {
79+
if child.Tag == ber.TagBitString && len(child.Children) >= 1 {
80+
if referral, ok = child.Children[0].Value.(string); ok {
81+
return referral, ok
82+
}
83+
}
84+
}
85+
86+
return "", false
87+
}

0 commit comments

Comments
 (0)