Adding new user to ejabberd through Smack android api - java

I have installed ejabberd on my local server. This was then tested in spark for its functionality and it worked fine. Now I want to add a new user through the android app.
I tried adding a new user through spark and it worked fine. The fields I have given are uesrname, password, confirm password, server. But when I tried to do it using the smack api in the android app it gave the following error:
org.jivesoftware.smack.XMPPException$XMPPErrorException: XMPPError: forbidden - auth
I was using createAccount(), seen in the code I was using below, to create the new account in smack.
AccountManager accountManager = AccountManager.getInstance(conn1);
try {
accountManager.createAccount("tryuser", "qwerty");
Log.i("log", "created user successfully");
} catch (SmackException.NoResponseException e) {
e.printStackTrace();
} catch (XMPPException.XMPPErrorException e) {
e.printStackTrace();
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
}
I have checked if it supports new account creation by supportsAccountCreation() and it returned true.
I have changed my register rule to allow all in ejabberd server. and i don't think it has any problem because i can create account from spark, but getting error in smack.
I have looked into the following SO questions related to this topic but no luck.
Ejabberd can't register new user
How to register a new user on XMPP using (a)Smack library
Does anyone have any suggestions on how to solve this?

Please give a try with below -
AccountManager accountManager = AccountManager.getInstance(connection);
try {
if (accountManager.supportsAccountCreation()) {
accountManager.sensitiveOperationOverInsecureConnection(true);
accountManager.createAccount("userName", "password");
}
} catch (SmackException.NoResponseException e) {
e.printStackTrace();
} catch (XMPPException.XMPPErrorException e) {
e.printStackTrace();
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
}
And you also need to set below in ejabberd.cfg (config file)
{access, register, [{allow, all}]}.
which means - In-band registration allows registration of any possible username. To disable in-band registration, replace 'allow' with 'deny'.
And in mod_register (module in same config file) please set below -
{access_from, register}
& before that please check you are connected to XMPP server.
Probably this will resolve your issue.

Related

Firebase FCM XMPP server error: connection denied due to policy

I am trying to implement an XMPP server in my Google Cloud module (endpoints) for my Android app using Android Studio in order to send downstream messages to devices.
I have found an example project which uses Smark here. The following is the code for the connection:
CcsClient ccs = CcsClient.prepareClient(Utils.FCM_ID,Utils.FCM_KEY,false);
try {
ccs.connect();
} catch (XMPPException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (SmackException e) {
e.printStackTrace();
}
But I am getting the following SmackException:
The following addresses failed: 'fcm-xmpp.googleapis.com:5236' failed because
java.net.SocketException: Permission denied: connection to
(10, [2607:f8b0:4001:c06::bc]:5236,6) denied due to policy
How can I fix this?

Smack Openfire Android Unable to send message to Group/Room Error 403 forbidden

MultiUserChatManager manager = MultiUserChatManager.getInstanceFor(xmppconnection.getConnection());
try {
MultiUserChat muc = manager.getMultiUserChat("test2#conference.cca");
muc.join("test2#conference.cca");
Message msg = new Message("test2#conference.cca", Message.Type.groupchat);
msg.setBody("Hi Testing..Group chat..");
muc.sendMessage(msg);
// muc.join("test", "1234");
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
} catch (SmackException e) {
e.printStackTrace();
} catch (XMPPException.XMPPErrorException e) {
e.printStackTrace();
} catch (XMPPException e) {
e.printStackTrace();
}
Error is:
error code="403" type="auth" forbidden xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>**
There are several errors, logical and procedurals.
With this invocation:
MultiUserChat muc = manager.getMultiUserChat("test2#conference.cca");
you have in muc object your groupchat.
So you need to check if you already joined this groupchat or double join will raise an exception.
so
if (!muc.isJoined())
muc.join("My nickname");
more, when you join, you MUST provide an unique nickname per User to join, or you'll obtain an exception with the second user. Set as nickname the same name of the groupchat it's 99% a logical error.
Finally, to send a message, just send it through MUC object or you'll risk, like in this case, to miss some information.
So just send it with
muc.send("Hi Testing..Group chat..");
Last but not least: of course multiuserchat must exists or inititilized before properly, it's a prerequisite to do all this. As first step, just create it in Openfire with http-admin-panel (make it persistant)

Smack XMPP error while creating new user: forbidden - auth

I'm trying to create new user via admin account. But I receive auth error.
I tried answers like this, but it doesn't help.
My code for creating new user:
AccountManager accountManager = AccountManager.getInstance(mConnection);
if (accountManager.supportsAccountCreation()) {
try {
accountManager.createAccount(username, password);
}
catch (XMPPException ex) {
LOG.info(ex.getMessage(), ex);
}
}
else{
LOG.error("Server doesn't support creating new accounts");
}
And I allow registration for ALL in config:
{access, register, [{allow, all}]}.
{access, register_from, [{allow, all}]}.
{mod_register, [
...
{ip_access, [{allow, "127.0.0.0/8"},
{deny, "0.0.0.0/0"}]},
{access_from, register_from},
{access, register}
]},
And even with this config i have error:
org.jivesoftware.smack.XMPPException$XMPPErrorException: XMPPError: forbidden - auth
at org.jivesoftware.smack.XMPPException$XMPPErrorException.ifHasErrorThenThrow(XMPPException.java:135)
at org.jivesoftware.smack.PacketCollector.nextResultOrThrow(PacketCollector.java:232)
at org.jivesoftware.smack.PacketCollector.nextResultOrThrow(PacketCollector.java:213)
at org.jivesoftware.smackx.iqregister.AccountManager.createAccount(AccountManager.java:272)
at org.jivesoftware.smackx.iqregister.AccountManager.createAccount(AccountManager.java:244)
P.S. Smack 4.1.0
Solution:
step1:
step2:
step3:
after doing all these three steps run your program u will be able to register a new user on ejabberd server.

error: cannot access SaslException class file for javax.security.sasl.SaslException not found

I want to use the latest release of smack 4.0.0 in my project for xmpp with android studio 0.6.0,the code as below:
try{
ConnectionConfiguration config = new ConnectionConfiguration("wooxonline.com",4000);
XMPPConnection connection = new XMPPTCPConnection(config);
connection.connect();
connection.login("cliff","cliff123");
}
catch (SmackException e) {
e.printStackTrace();
}
catch (java.io.IOException e)
{
e.printStackTrace();
}
catch (XMPPException e) {
e.printStackTrace();
Log.i(TAG, "login failed!");
}
this almost like sample with the smack sample code,but I have a compile issue that can't be solved as below trace info:
Error:(58, 29) error: cannot access SaslException
class file for javax.security.sasl.SaslException not found
I'm stuck here and anyone can help to have a look?
I ran into the same problem. This is the appropriate import sentence:
import org.apache.harmony.javax.security.sasl.SaslException;
According to the official forum "Smack does not work on Android. You need aSmack." So you should try to change the library to ASmack.
You can use maven to get the jar or download it from here
Reference:https://community.igniterealtime.org/thread/52833

asmack android XMPP - get all users

I'm using the asmack XMPP with Android, but I have a problem. I would like a player to connect to XMPP server (only if username not already taken), receive all currently connected users and choose a user from the list to play against.
Currently I'm logging into the XMPP server like this:
/* then connect with a newly created username */
try {
if (connection != null && connection.isConnected()) {
connection.login(username, password);
}
} catch (XMPPException e) {
Log.w("[xmpp_login] Cannot connect to XMPP server with username: " + username, "0");
e.printStackTrace();
}
And for the creation of users I'm using the following code:
/* required attributes for creation of a new account */
HashMap<String, String> attr = new HashMap<String, String>();
attr.put("username", username);
attr.put("password", password);
manager.createAccount(username, password, attr);
} catch (XMPPException e) {
Log.w("[create_user] Cannot create new user: XMPP Exception.", "0");
Log.w(e.getMessage(), "0");
e.printStackTrace();
} catch (IllegalStateException e) {
Log.w("[create_user] Cannot create new user: not logged in.", "0");
e.printStackTrace();
}
I have two questions:
How can I check if the username is already taken?
How can I return a list of all currently connected (online) users to each player? Since I've googled and found nothing useful, I guess I'll have to provide that function at serverside. This is where I'm using openfire XMPP server. Is there a plugin for it that I can use that does that - then I could call some function from a client, which would call that specific plugin, which will in turn respond with all the online players.
I guess any tip is appreciated. Thank you
I found a solution. To find an answer you can google "XMPP shared group". Basically I've done the following:
Create a new group in openfire XMPP server.
Under users, make sure that you check "Enable automatically adding of new users to a group."
Of course you'll have to have the right modules enabled for those preferences to even be present.
Then we can just write the following function that will return all users from XMPP server:
/*
* Return a list of #num players currently connected. (if #num == 0: return
* all players)
*/
public ArrayList<String> xmpp_playerlist(int num) {
try {
if (!connection.isConnected())
connection.connect();
if (!connection.isAuthenticated())
connection.login(user, pass);
} catch (XMPPException e) {
Log.w("Cannot connect to XMPP server with default admin username and password.", "0");
e.printStackTrace();
}
ArrayList<String> players = new ArrayList<String>();
roster = connection.getRoster();
Collection<RosterEntry> entries = roster.getEntries();
for (RosterEntry entry : entries) {
players.add(entry.getName());
}
Log.w("**Number Users: " + roster.getEntryCount(), "0");
return players;
}

Categories