This repository has been archived on 2023-08-14. You can view files and clone it, but cannot push or open issues or pull requests.
dex/vendor/gopkg.in/ldap.v2/add.go

114 lines
3.3 KiB
Go
Raw Normal View History

2016-07-25 20:00:28 +00:00
//
// https://tools.ietf.org/html/rfc4511
//
// AddRequest ::= [APPLICATION 8] SEQUENCE {
// entry LDAPDN,
// attributes AttributeList }
//
// AttributeList ::= SEQUENCE OF attribute Attribute
package ldap
import (
"errors"
"log"
"gopkg.in/asn1-ber.v1"
)
// Attribute represents an LDAP attribute
2016-07-25 20:00:28 +00:00
type Attribute struct {
// Type is the name of the LDAP attribute
Type string
// Vals are the LDAP attribute values
Vals []string
2016-07-25 20:00:28 +00:00
}
func (a *Attribute) encode() *ber.Packet {
seq := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "Attribute")
seq.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, a.Type, "Type"))
2016-07-25 20:00:28 +00:00
set := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSet, nil, "AttributeValue")
for _, value := range a.Vals {
2016-07-25 20:00:28 +00:00
set.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, value, "Vals"))
}
seq.AppendChild(set)
return seq
}
// AddRequest represents an LDAP AddRequest operation
2016-07-25 20:00:28 +00:00
type AddRequest struct {
// DN identifies the entry being added
DN string
// Attributes list the attributes of the new entry
Attributes []Attribute
2016-07-25 20:00:28 +00:00
}
func (a AddRequest) encode() *ber.Packet {
request := ber.Encode(ber.ClassApplication, ber.TypeConstructed, ApplicationAddRequest, nil, "Add Request")
request.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, a.DN, "DN"))
2016-07-25 20:00:28 +00:00
attributes := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "Attributes")
for _, attribute := range a.Attributes {
2016-07-25 20:00:28 +00:00
attributes.AppendChild(attribute.encode())
}
request.AppendChild(attributes)
return request
}
// Attribute adds an attribute with the given type and values
2016-07-25 20:00:28 +00:00
func (a *AddRequest) Attribute(attrType string, attrVals []string) {
a.Attributes = append(a.Attributes, Attribute{Type: attrType, Vals: attrVals})
2016-07-25 20:00:28 +00:00
}
// NewAddRequest returns an AddRequest for the given DN, with no attributes
2016-07-25 20:00:28 +00:00
func NewAddRequest(dn string) *AddRequest {
return &AddRequest{
DN: dn,
2016-07-25 20:00:28 +00:00
}
}
// Add performs the given AddRequest
2016-07-25 20:00:28 +00:00
func (l *Conn) Add(addRequest *AddRequest) error {
packet := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "LDAP Request")
packet.AppendChild(ber.NewInteger(ber.ClassUniversal, ber.TypePrimitive, ber.TagInteger, l.nextMessageID(), "MessageID"))
2016-07-25 20:00:28 +00:00
packet.AppendChild(addRequest.encode())
l.Debug.PrintPacket(packet)
msgCtx, err := l.sendMessage(packet)
2016-07-25 20:00:28 +00:00
if err != nil {
return err
}
defer l.finishMessage(msgCtx)
2016-07-25 20:00:28 +00:00
l.Debug.Printf("%d: waiting for response", msgCtx.id)
packetResponse, ok := <-msgCtx.responses
2016-07-25 20:00:28 +00:00
if !ok {
return NewError(ErrorNetwork, errors.New("ldap: response channel closed"))
2016-07-25 20:00:28 +00:00
}
packet, err = packetResponse.ReadPacket()
l.Debug.Printf("%d: got response %p", msgCtx.id, packet)
2016-07-25 20:00:28 +00:00
if err != nil {
return err
}
if l.Debug {
if err := addLDAPDescriptions(packet); err != nil {
return err
}
ber.PrintPacket(packet)
}
if packet.Children[1].Tag == ApplicationAddResponse {
resultCode, resultDescription := getLDAPResultCode(packet)
if resultCode != 0 {
return NewError(resultCode, errors.New(resultDescription))
}
} else {
log.Printf("Unexpected Response: %d", packet.Children[1].Tag)
}
l.Debug.Printf("%d: returning", msgCtx.id)
2016-07-25 20:00:28 +00:00
return nil
}