mirror of
https://github.com/FluuxIO/go-xmpp.git
synced 2025-11-21 09:03:44 -08:00
Introduce Credential structure to define auth type
For now we are planning to support Password and OAuthToken. In the future, we would like to add certificate-based authentication.
This commit is contained in:
committed by
Mickaël Rémond
parent
3b66e31888
commit
9c8353d081
36
auth.go
36
auth.go
@@ -10,8 +10,34 @@ import (
|
||||
"gosrc.io/xmpp/stanza"
|
||||
)
|
||||
|
||||
func authSASL(socket io.ReadWriter, decoder *xml.Decoder, f stanza.StreamFeatures, user string, password string) (err error) {
|
||||
// TODO: Implement other type of SASL Authentication
|
||||
// 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) {
|
||||
// TODO: Implement other type of SASL mechanisms
|
||||
havePlain := false
|
||||
for _, m := range f.Mechanisms.Mechanism {
|
||||
if m == "PLAIN" {
|
||||
@@ -24,12 +50,12 @@ func authSASL(socket io.ReadWriter, decoder *xml.Decoder, f stanza.StreamFeature
|
||||
return NewConnError(err, true)
|
||||
}
|
||||
|
||||
return authPlain(socket, decoder, user, password)
|
||||
return authPlain(socket, decoder, user, credential)
|
||||
}
|
||||
|
||||
// Plain authentication: send base64-encoded \x00 user \x00 password
|
||||
func authPlain(socket io.ReadWriter, decoder *xml.Decoder, user string, password string) error {
|
||||
raw := "\x00" + user + "\x00" + password
|
||||
func authPlain(socket io.ReadWriter, decoder *xml.Decoder, user string, credential Credential) error {
|
||||
raw := "\x00" + user + "\x00" + credential.secret
|
||||
enc := make([]byte, base64.StdEncoding.EncodedLen(len(raw)))
|
||||
base64.StdEncoding.Encode(enc, []byte(raw))
|
||||
fmt.Fprintf(socket, "<auth xmlns='%s' mechanism='PLAIN'>%s</auth>", stanza.NSSASL, enc)
|
||||
|
||||
Reference in New Issue
Block a user