"Fossies" - the Fresh Open Source Software Archive

Member "mattermost-server-6.0.1/vendor/github.com/mattermost/ldap/add.go" (18 Oct 2021, 2806 Bytes) of package /linux/www/mattermost-server-6.0.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Go source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 //
    2 // https://tools.ietf.org/html/rfc4511
    3 //
    4 // AddRequest ::= [APPLICATION 8] SEQUENCE {
    5 //      entry           LDAPDN,
    6 //      attributes      AttributeList }
    7 //
    8 // AttributeList ::= SEQUENCE OF attribute Attribute
    9 
   10 package ldap
   11 
   12 import (
   13     "log"
   14 
   15     ber "github.com/go-asn1-ber/asn1-ber"
   16 )
   17 
   18 // Attribute represents an LDAP attribute
   19 type Attribute struct {
   20     // Type is the name of the LDAP attribute
   21     Type string
   22     // Vals are the LDAP attribute values
   23     Vals []string
   24 }
   25 
   26 func (a *Attribute) encode() *ber.Packet {
   27     seq := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "Attribute")
   28     seq.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, a.Type, "Type"))
   29     set := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSet, nil, "AttributeValue")
   30     for _, value := range a.Vals {
   31         set.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, value, "Vals"))
   32     }
   33     seq.AppendChild(set)
   34     return seq
   35 }
   36 
   37 // AddRequest represents an LDAP AddRequest operation
   38 type AddRequest struct {
   39     // DN identifies the entry being added
   40     DN string
   41     // Attributes list the attributes of the new entry
   42     Attributes []Attribute
   43     // Controls hold optional controls to send with the request
   44     Controls []Control
   45 }
   46 
   47 func (req *AddRequest) appendTo(envelope *ber.Packet) error {
   48     pkt := ber.Encode(ber.ClassApplication, ber.TypeConstructed, ApplicationAddRequest, nil, "Add Request")
   49     pkt.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, req.DN, "DN"))
   50     attributes := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "Attributes")
   51     for _, attribute := range req.Attributes {
   52         attributes.AppendChild(attribute.encode())
   53     }
   54     pkt.AppendChild(attributes)
   55 
   56     envelope.AppendChild(pkt)
   57     if len(req.Controls) > 0 {
   58         envelope.AppendChild(encodeControls(req.Controls))
   59     }
   60 
   61     return nil
   62 }
   63 
   64 // Attribute adds an attribute with the given type and values
   65 func (req *AddRequest) Attribute(attrType string, attrVals []string) {
   66     req.Attributes = append(req.Attributes, Attribute{Type: attrType, Vals: attrVals})
   67 }
   68 
   69 // NewAddRequest returns an AddRequest for the given DN, with no attributes
   70 func NewAddRequest(dn string, controls []Control) *AddRequest {
   71     return &AddRequest{
   72         DN:       dn,
   73         Controls: controls,
   74     }
   75 
   76 }
   77 
   78 // Add performs the given AddRequest
   79 func (l *Conn) Add(addRequest *AddRequest) error {
   80     msgCtx, err := l.doRequest(addRequest)
   81     if err != nil {
   82         return err
   83     }
   84     defer l.finishMessage(msgCtx)
   85 
   86     packet, err := l.readPacket(msgCtx)
   87     if err != nil {
   88         return err
   89     }
   90 
   91     if packet.Children[1].Tag == ApplicationAddResponse {
   92         err := GetLDAPError(packet)
   93         if err != nil {
   94             return err
   95         }
   96     } else {
   97         log.Printf("Unexpected Response: %d", packet.Children[1].Tag)
   98     }
   99     return nil
  100 }