Compare commits

..

No commits in common. "a85f4766008a3b86956089cbd5dee9fb7c08076f" and "c146c8dae2d735a243edd54679558b5fbea6447b" have entirely different histories.

3 changed files with 31 additions and 61 deletions

24
main.go
View File

@ -18,7 +18,6 @@ import (
const ( const (
Version = "v0.5-dev" Version = "v0.5-dev"
configurationFilePath = "http-auth/httpAuth.conf" configurationFilePath = "http-auth/httpAuth.conf"
langFilePath = "http-auth/messages.lang"
PathConfEnvVariable = "XDG_CONFIG_DIRS" PathConfEnvVariable = "XDG_CONFIG_DIRS"
DefaultXdgConfigDirs = "/etc/xdg" DefaultXdgConfigDirs = "/etc/xdg"
) )
@ -33,7 +32,6 @@ func init() {
if !loadConfigFile() { if !loadConfigFile() {
log.Fatal("Failed to load configuration file.") log.Fatal("Failed to load configuration file.")
} }
loadLangFile()
// HTTP config // HTTP config
httpTimeout, err := strconv.Atoi(mapConfig["http_timeout_sec"]) httpTimeout, err := strconv.Atoi(mapConfig["http_timeout_sec"])
@ -100,28 +98,6 @@ func loadConfigFile() bool {
return ret return ret
} }
func loadLangFile() bool {
ret := false
envVariable := os.Getenv(PathConfEnvVariable)
if envVariable == "" {
envVariable = DefaultXdgConfigDirs
}
for _, path := range strings.Split(envVariable, ":") {
log.Println("Try to find messages lang file into " + path)
langFile := path + "/" + langFilePath
if _, err := os.Stat(langFile); err == nil {
// The config file exist
if cfg.Load(langFile, xmpp.MapLangs) == nil {
// And has been loaded succesfully
log.Println("Find messages lang file at " + langFile)
ret = true
break
}
}
}
return ret
}
func main() { func main() {
go http.Run() go http.Run()

View File

@ -3,27 +3,20 @@ package xmpp
import ( import (
"git.kingpenguin.tk/chteufleur/go-xmpp.git/src/xmpp" "git.kingpenguin.tk/chteufleur/go-xmpp.git/src/xmpp"
"crypto/rand"
"log" "log"
"strconv" "strconv"
"strings"
) )
const ( const (
dictionary = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
REPLY_UNREACHABLE = "reply_unreachable" REPLY_UNREACHABLE = "reply_unreachable"
REPLY_DENY = "reply_deny" REPLY_DENY = "reply_deny"
REPLY_OK = "reply_ok" REPLY_OK = "reply_ok"
TYPE_SEND_MESSAGE = "type_send_message" TYPE_SEND_MESSAGE = "type_send_message"
TYPE_SEND_IQ = "type_send_iq" TYPE_SEND_IQ = "type_send_iq"
TEMPLATE_DOMAIN = "_DOMAIN_"
TEMPLATE_METHOD = "_METHOD_"
TEMPLATE_VALIDATION_CODE = "_VALIDE_CODE_"
DEFAULT_MESSAGE = "_DOMAIN_ (with method _METHOD_) need to validate your identity, do you agree ?\nValidation code : _VALIDE_CODE_\nPlease check that this code is the same as on _DOMAIN_.\n\nIf your client doesn't support that functionnality, please send back the validation code to confirm the request."
)
var (
MapLangs = make(map[string]string)
) )
type Confirmation struct { type Confirmation struct {
@ -40,9 +33,15 @@ type Confirmation struct {
func (confirmation *Confirmation) SendConfirmation() { func (confirmation *Confirmation) SendConfirmation() {
log.Printf("%sQuery JID %s", LogInfo, confirmation.JID) log.Printf("%sQuery JID %s", LogInfo, confirmation.JID)
isAutoGeneratedTranctionID := false
if confirmation.Transaction == "" {
// Random transaction ID generation
confirmation.Transaction = TransactionID()
isAutoGeneratedTranctionID = true
}
clientJID, _ := xmpp.ParseJID(confirmation.JID) clientJID, _ := xmpp.ParseJID(confirmation.JID)
if clientJID.Resource == "" { if clientJID.Resource == "" {
confirmation.askViaMessage() confirmation.askViaMessage(isAutoGeneratedTranctionID)
} else { } else {
confirmation.askViaIQ() confirmation.askViaIQ()
} }
@ -61,10 +60,17 @@ func (confirmation *Confirmation) askViaIQ() {
confirmation.IdMap = stanzaIDstr confirmation.IdMap = stanzaIDstr
} }
func (confirmation *Confirmation) askViaMessage() { func (confirmation *Confirmation) askViaMessage(isAutoGeneratedTranctionID bool) {
m := xmpp.Message{From: jid.Full(), To: confirmation.JID, Type: xmpp.MessageTypeNormal} m := xmpp.Message{From: jid.Full(), To: confirmation.JID, Type: xmpp.MessageTypeNormal}
m.Thread = xmpp.SessionID() m.Thread = xmpp.SessionID()
confirmation.setBodies(&m) m.Body = confirmation.Domain + " (with method " + confirmation.Method + ") need to validate your identity, do you agree ?"
m.Body += "\nValidation code : " + confirmation.Transaction
if !isAutoGeneratedTranctionID {
// Send only if the transaction ID is not autogenerated
m.Body += "\nPlease check that this code is the same as on " + confirmation.Domain
}
m.Body += "\n\nIf your client doesn't support that functionnality, please send back the validation code to confirm the request."
m.Confir = &xmpp.Confirm{Id: confirmation.Transaction, Method: confirmation.Method, URL: confirmation.Domain} m.Confir = &xmpp.Confirm{Id: confirmation.Transaction, Method: confirmation.Method, URL: confirmation.Domain}
log.Printf("%sSend message %v", LogInfo, m) log.Printf("%sSend message %v", LogInfo, m)
@ -75,23 +81,13 @@ func (confirmation *Confirmation) askViaMessage() {
confirmation.IdMap = confirmation.Transaction confirmation.IdMap = confirmation.Transaction
} }
func (confirmation *Confirmation) setBodies(message *xmpp.Message) { func TransactionID() string {
msg := DEFAULT_MESSAGE var bytes = make([]byte, 8)
if len(MapLangs) == 0 { if _, err := rand.Read(bytes); err != nil {
msg = strings.Replace(msg, TEMPLATE_DOMAIN, confirmation.Domain, -1) panic(err)
msg = strings.Replace(msg, TEMPLATE_METHOD, confirmation.Method, -1)
msg = strings.Replace(msg, TEMPLATE_VALIDATION_CODE, confirmation.Transaction, -1)
message.Body = append(message.Body, xmpp.MessageBody{Lang: "en", Value: msg})
} else {
for key, val := range MapLangs {
msg = val
msg = strings.Replace(msg, TEMPLATE_DOMAIN, confirmation.Domain, -1)
msg = strings.Replace(msg, TEMPLATE_METHOD, confirmation.Method, -1)
msg = strings.Replace(msg, TEMPLATE_VALIDATION_CODE, confirmation.Transaction, -1)
msg = strings.Replace(msg, "\\n", "\n", -1)
msg = strings.Replace(msg, "\\r", "\r", -1)
msg = strings.Replace(msg, "\\t", "\t", -1)
message.Body = append(message.Body, xmpp.MessageBody{Lang: key, Value: msg})
} }
for k, v := range bytes {
bytes[k] = dictionary[v%byte(len(dictionary))]
} }
return string(bytes)
} }

View File

@ -100,14 +100,12 @@ func mainXMPP() {
} else { } else {
// If body is the confirmation id, it will be considerated as accepted. // If body is the confirmation id, it will be considerated as accepted.
// In order to be compatible with all confirmations. // In order to be compatible with all confirmations.
if len(v.Body) > 0 { confirmation := WaitMessageAnswers[v.Body]
confirmation := WaitMessageAnswers[v.Body[0].Value]
jidFrom, _ := xmpp.ParseJID(v.From) jidFrom, _ := xmpp.ParseJID(v.From)
if confirmation != nil && confirmation.JID == jidFrom.Bare() { if confirmation != nil && confirmation.JID == jidFrom.Bare() {
processConfirm(v, confirmation) processConfirm(v, confirmation)
} }
} }
}
case *xmpp.Iq: case *xmpp.Iq:
switch v.PayloadName().Space { switch v.PayloadName().Space {