Ejemplo n.º 1
0
func TestWriteStanza(t *testing.T) {
	t.Parallel()

	var want, got []byte

	to, from := jid.New("foo@bar"), jid.New("baz@quux")
	iq := stanza.NewIQResult(to, from, "test", stanza.IQResult)
	want = iq.TransformElement().WriteBytes()
	got = make([]byte, len(want))

	read, write := net.Pipe()
	tcpTsp := NewTCP(write, stream.Receiving, nil, false)

	go func() {
		_, err := read.Read(got)
		if err != nil {
			t.Errorf("Received error while reading from connection: %s", err)
		}
	}()

	err := tcpTsp.WriteStanza(iq.TransformStanza())
	if err != nil {
		t.Errorf("Unexpected error from WriteElement: %s", err)
	}

	if !reflect.DeepEqual(want, got) {
		t.Error("Should be able to write element to TCP stream.")
		t.Errorf("\nWant:%v\nGot :%v", want, got)
	}
}
Ejemplo n.º 2
0
Archivo: iq.go Proyecto: skriptble/nine
func NewIQError(iq IQ, err element.Element) IQ {
	to := jid.New(iq.From)
	from := jid.New(iq.To)

	s := NewStanza(to, from, iq.ID, string(IQError))
	s.Children = append(s.Children, err)

	return IQ{}.LoadStanza(s)
}
Ejemplo n.º 3
0
func (sh SessionHandler) HandleIQ(iq stanza.IQ, props stream.Properties) (
	[]stanza.Stanza, stream.Properties) {
	var sts []stanza.Stanza
	to := jid.New(iq.From)
	from := jid.New(iq.To)
	res := stanza.NewIQResult(to, from, iq.ID, stanza.IQResult)
	sts = append(sts, res.TransformStanza())
	return sts, props
}
Ejemplo n.º 4
0
func NewBindResult(iq IQ, j jid.JID) BindResult {
	to := jid.New(iq.To)
	from := jid.New(iq.From)

	s := NewStanza(to, from, iq.ID, string(IQResult))
	s.Namespaces = map[string]string{"": namespace.Client}

	result := element.Bind.AddChild(
		element.JID.SetText(j.String()),
	)
	s = s.AddChild(result)
	iq = IQ{}.LoadStanza(s)

	return BindResult{IQ: iq}
}
Ejemplo n.º 5
0
// Authenticate implements the Mechanism interface for PlainMech
func (pm plainMech) Authenticate(data string, props stream.Properties) ([]element.Element, stream.Properties, bool) {
	decoded, err := base64.StdEncoding.DecodeString(data)
	if err != nil {
		return []element.Element{element.SASLFailure.MalformedRequest}, props, false
	}

	res := strings.Split(string(decoded), "\000")
	if len(res) != 3 {
		return []element.Element{element.SASLFailure.MalformedRequest}, props, false
	}
	identity, user, password := res[0], res[1], res[2]
	err = pm.auth.Authenticate(identity, user, password)
	// TODO: Handle different types of errors
	if err != nil {
		return []element.Element{element.SASLFailure.NotAuthorized}, props, false
	}
	if identity != "" {
		user = identity
	} else {
		// TODO: Add a way to determine the address of the server for the domain
		// part of the jid (do it better than this.)
		user += "@" + props.Domain
	}

	j := jid.New(user)
	props.Header.To = j.String()
	props.Status = props.Status | stream.Restart | stream.Auth
	return []element.Element{element.SASLSuccess}, props, false
}
Ejemplo n.º 6
0
func (h Handler) HandleIQ(iq stanza.IQ, props stream.Properties) ([]stanza.Stanza, stream.Properties) {
	var sts []stanza.Stanza
	// ensure we have a bind request
	req, err := stanza.TransformBindRequest(iq)
	if err != nil {
		// TODO: Should this return an error?
		return sts, props
	}
	if req.Resource == "" {
		// TODO: Create a random resource generator
		req.Resource = genResourceID()
	}

	// Should do some resource validation here.
	// TODO: Need to use proper jids here.
	props.Header.To += "/" + req.Resource

	j := jid.New(props.Header.To)
	res := stanza.NewBindResult(iq, j)
	sts = append(sts, res.TransformStanza())

	props.Status = props.Status | stream.Bind
	return sts, props
}