2019-06-18 07:28:30 -07:00
|
|
|
package xmpp
|
2016-01-06 07:51:12 -08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/xml"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2019-06-26 08:14:52 -07:00
|
|
|
|
|
|
|
"gosrc.io/xmpp/stanza"
|
2016-01-06 07:51:12 -08:00
|
|
|
)
|
|
|
|
|
2019-10-01 01:59:55 -07:00
|
|
|
// Credential is used to pass the type of secret that will be used to connect to XMPP server.
|
|
|
|
// It can be either a password or an OAuth 2 bearer token.
|
|
|
|
type Credential struct {
|
|
|
|
secret string
|
|
|
|
mechanisms []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func Password(pwd string) Credential {
|
|
|
|
credential := Credential{
|
|
|
|
secret: pwd,
|
|
|
|
mechanisms: []string{"PLAIN"},
|
|
|
|
}
|
|
|
|
return credential
|
|
|
|
}
|
|
|
|
|
|
|
|
func OAuthToken(token string) Credential {
|
|
|
|
credential := Credential{
|
|
|
|
secret: token,
|
|
|
|
mechanisms: []string{"X-OAUTH2"},
|
|
|
|
}
|
|
|
|
return credential
|
|
|
|
}
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
// Authentication flow for SASL mechanisms
|
|
|
|
|
|
|
|
func authSASL(socket io.ReadWriter, decoder *xml.Decoder, f stanza.StreamFeatures, user string, credential Credential) (err error) {
|
2019-10-01 02:30:23 -07:00
|
|
|
var matchingMech string
|
|
|
|
for _, mech := range credential.mechanisms {
|
|
|
|
if isSupportedMech(mech, f.Mechanisms.Mechanism) {
|
|
|
|
matchingMech = mech
|
2016-01-06 07:51:12 -08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2019-10-01 02:30:23 -07:00
|
|
|
|
|
|
|
switch matchingMech {
|
|
|
|
case "PLAIN", "X-OAUTH2":
|
|
|
|
// TODO: Implement other type of SASL mechanisms
|
|
|
|
return authPlain(socket, decoder, matchingMech, user, credential.secret)
|
|
|
|
default:
|
|
|
|
err := fmt.Errorf("no matching authentication (%v) supported by server: %v", credential.mechanisms, f.Mechanisms.Mechanism)
|
2019-06-07 06:23:23 -07:00
|
|
|
return NewConnError(err, true)
|
2016-01-06 07:51:12 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Plain authentication: send base64-encoded \x00 user \x00 password
|
2019-10-01 02:30:23 -07:00
|
|
|
func authPlain(socket io.ReadWriter, decoder *xml.Decoder, mech string, user string, secret string) error {
|
|
|
|
raw := "\x00" + user + "\x00" + secret
|
2016-01-06 07:51:12 -08:00
|
|
|
enc := make([]byte, base64.StdEncoding.EncodedLen(len(raw)))
|
|
|
|
base64.StdEncoding.Encode(enc, []byte(raw))
|
2019-11-28 08:15:15 -08:00
|
|
|
_, err := fmt.Fprintf(socket, "<auth xmlns='%s' mechanism='%s'>%s</auth>", stanza.NSSASL, mech, enc)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-06 07:51:12 -08:00
|
|
|
|
|
|
|
// Next message should be either success or failure.
|
2019-06-26 08:14:52 -07:00
|
|
|
val, err := stanza.NextPacket(decoder)
|
2016-01-06 07:51:12 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch v := val.(type) {
|
2019-06-26 08:14:52 -07:00
|
|
|
case stanza.SASLSuccess:
|
|
|
|
case stanza.SASLFailure:
|
2016-01-06 07:51:12 -08:00
|
|
|
// v.Any is type of sub-element in failure, which gives a description of what failed.
|
2019-06-07 06:23:23 -07:00
|
|
|
err := errors.New("auth failure: " + v.Any.Local)
|
|
|
|
return NewConnError(err, true)
|
2016-01-06 07:51:12 -08:00
|
|
|
default:
|
2018-01-13 09:50:17 -08:00
|
|
|
return errors.New("expected SASL success or failure, got " + v.Name())
|
2016-01-06 07:51:12 -08:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2019-10-01 02:30:23 -07:00
|
|
|
|
|
|
|
// isSupportedMech returns true if the mechanism is supported in the provided list.
|
|
|
|
func isSupportedMech(mech string, mechanisms []string) bool {
|
|
|
|
for _, m := range mechanisms {
|
|
|
|
if mech == m {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|