Skip to main content

Token Base Authentication

There is a token-based authentication mechanism that allows a user who is logged in to your mobile application to securely activate the device they are logged in to in the Diyalog system. A user who has activated dialogue can now chat, make voice or video calls, and receive messages sent from your backoffice applications via that device.

In order to perform token-based authentication for Dialog activation from your mobile application, you must first configure the Diyalog SDK and create the Dialog Instance once your application is opened. You can find details for these processes in the How ​​to use Dialog, Configuration and Styling documents.

Domain Customer User#

The general flow of the token-based authentication mechanism of the Diyalog system for customers is tried to be shown in the following diagram.

As explained in the flow above, in order to initiate Diyalog activation on that device for the user who is logged in to your mobile application, you are expected to generate a random token specific to Diyalog in your backend server. Your mobile application should receive this token and give it to the methods detailed below to DiyalogSDK. DiyalogSDK receives this token and sends it to its own server, and the Diyalog server will validate this token by calling your checkauthtoken API that you will provide, and will activate the user on that device after verification.

You can activate Diyalog in your mobile application with two different methods. The first of these is to use the method we call the background method, and the other is to use the method we call the frontend method. The background method activates Diyalog in the background, gives you the result, and then closes the Diyalog server connections. The other method activates the user via Diyalog and keeps the connections to server open. This second method should be used if Diyalog screens will be opened directly after the user activation. If Dialog screens will not be opened directly to the user and the user will use Diyalog screens later when desired, the background method should be used.

Regardless of which method is used, after your user logs into your application on your system, you are expected to generate a token to authenticate your user on your system and give this token to the method you call so that Diyalog can authenticate your user via your servers. Diyalog will receive this token and send it to its own server, and Diyalog server will send this token to the API detailed in the checkauthtoken document below and expected to be implemented on your servers to perform the verification. After verification, the user is created in the Diyalog system and the user is bound with the device.

a. Background method

With this method, your user will be registered securely to the Diyalog system. After registration, a session will be created in the SDK then the SDK will close its connections with the server.

Method Definition :

public void startTokenAuthInBackground(
Activity context,
String dlgToken,
String deviceId,
String customerId,
String customerName,
Long customerPhone,
String customerEmail,
TokenAuthResponse tokenAuthResponse)

Usage :

DiyalogEngine.diyalogInstance().startTokenAuthInBackground(
yourActivity.this,
"<Token generated on your server>",
"<device id corralated with token>",
"<customerId>",
"<Full name of the customer>",
"<Optional - Customer phone number>",
"<Optional - Customer email address>",
new TokenAuthResponse() {
@Override
public void success() {
Log.d("Auth In Bakground", "==>> Success");
}
@Override
public void failure(TokenAuthError tokenAuthError) {
Log.e("Auth In Bakground", "==>> Error Message: " + tokenAuthError.getErrorMsg());
Log.e("Auth In Bakground", "==>> Error Code: "+ getString(im.diyalog.sdk.R.string.auth_error_technical_reason)+" "+ tokenAuthError.getErrorCode());
}
});

Parameters :

Parameter NameTypeDescriptionMandatory
contextActivityThe android activity object from which you call the methodYes
dlgTokenStringToken generated for logged in user for Diyalog authYes
deviceIDStringUnique device id corralated with tokenYes
customerIDStringCustomer Id of the userYes
customerNameStringName of the userYes
customerPhoneStringPhone number of the userNo
customerEmailStringEmail address of the userNo

The method runs asynchronously and its result is returned in the completion/callback function result in TokenAuthResponse.

b. Foreground method

This method is different from the background method, if Diyalog messaging screens will be opened and used immediately after Diyalog activation, you can use this method. This method was added to prevent delays during the establishment of the Diyalog server connection with the client.

If Diyalog screens will not be opened after activation, you should not use this method. Because a connection will remain open in the Diyalog system unnecessarily.

Method Definition :

public void startTokenAuthForOpenDlg(
Activity context,
String dlgToken,
String deviceId,
String customerId,
String customerName,
Long customerPhone,
String customerEmail,
TokenAuthResponse tokenAuthResponse)

Usage :

DiyalogEngine.diyalogInstance().startTokenAuthForOpenDlg(
dlgToken: "<Token generated on your server>",
deviceID: "<device id corralated with token>",
customerID: "<customerId>",
customerName: "<Full name of the customer>",
customerPhone: "<Optional - Customer phone number>",
customerEmail: "<Optional - Customer email address>") { (success, errorMessage) in
if(success)
{
print("Success")
}else{
//print("there is an error")
print("there is an error \(String(describing: errorMessage?.errorCode))")
}
}
DiyalogEngine.diyalogInstance().startTokenAuthForOpenDlg(
yourActivity.this,
"<Token generated on your server>",
"<device id corralated with token>",
"<customerId>",
"<Full name of the customer>",
"<Optional - Customer phone number>",
"<Optional - Customer email address>",
new TokenAuthResponse() {
@Override
public void success() {
Log.d("Auth In Bakground", "==>> Success");
}
@Override
public void failure(TokenAuthError tokenAuthError) {
Log.e("Auth In Bakground", "==>> Error Message: " + tokenAuthError.getErrorMsg());
Log.e("Auth In Bakground", "==>> Error Code: "+ getString(im.diyalog.sdk.R.string.auth_error_technical_reason)+" "+ tokenAuthError.getErrorCode());
}
});

Parameters :

Parameter NameTypeDescriptionMandatory
contextActivityThe android activity object from which you call the methodYes
dlgTokenStringToken generated for logged in user for Diyalog authYes
deviceIDStringUnique device id corralated with tokenYes
customerIDStringCustomer Id of the userYes
customerNameStringName of the userYes
customerPhoneStringPhone number of the userNo
customerEmailStringEmail address of the userNo

The method runs asynchronously and its result is returned in the completion/callback function result.

result :

Parameter NameTypeDescriptionValues
successBooleanIt returns activation success or nor.true : Success false: failed
errorTokenAuthErrorFailure detail if activation is failed.Instance of TokenAuthError

Domain Staff User#

a. Foreground method

For staff user registration client need to call below method with necessary information. After successfully register on server it will create session in Diyalog SDK and can start messaging. Using below method if user already created session in Diyalog SDK, then it will directly open that session and user can start messaging.

DiyalogEngine.diyalogInstance().startDomainUserTokenAuth( context, dlgToken, sessionId,
userId,
userName,
userPhone,
userEmail);

context - Activity context

dlgToken - Main application server will return Diyalog Token when user will login into main application

sessionId – Staff user's sessionId

userId - Staff user's UserID

UserName - Staff user's UserName

userPhone - Staff user's PhoneNumber(Optional)

userEmail - Staff user's EmailID (Optional)

b. Background method When staff user will call background method then it will be register on server and after successfully register, it will create session into sdk. And when user will click on Diyalog chat icon from main application to open sdk, it will directly open that session. user does not wait for register. And In background method if there is any failure then it will pass error message into failure callback.

DiyalogEngine.diyalogInstance().startDomainUserTokenAuthInBackground(context, dlgToken, sessionId, userId,
userName, userPhone, userEmail, new TokenAuthResponse() {
@Override
public void success() {
Log.e("Auth In Bakground","==>> Success ");
}
@Override
public void failure(TokenAuthError tokenAuthError) {
Log.e("Auth In Bakground", "==>> Error Message: " + tokenAuthError.getErrorMsg());
Log.e("Auth In Bakground", "==>> Error Code: "+ tokenAuthError.getErrorCode());
}
}) ;

Get Active sessions#

User can get all sessions list those already registered in diyalog SDK. This method will be use for both Domain customer user and staff user.

ArrayList<AccountVo> sessionList = DiyalogEngine.diyalogInstance().getActiveSessions();

This method will return list of saved session from sdk. It will contain userInfo as below:

  • ApplicationUserID
  • AuthID
  • UserName
  • User Image.
  • User is active or not
  • Unread message count

Messaging from specific User#

User can start messaging from specific user. If session already saved into Diyalog SDK then user can start messaging with thier UserID.

DiyalogEngine.diyalogInstance().startMessagingWithUserId(context,customerId);

context - Activity context

customerId - domain customer id

This method will be use for both Domain customer user and staff user.

Delete session of user#

Client can delete session of diyalog sdk for any user.

boolean isDelted = DiyalogEngine.diyalogInstance().deleteSessionOfUser(customerId);

Get total unread count into main application.#

Client can get total no of unread message count into main application, so they can set unread badge count into main application. Client need to subscribe unread count event as below:

DiyalogEngine.diyalogInstance().subscribeToTotalUnreadCountEvent(new ValueChangedListener<Integer>() {
@Override
public void onChanged(Integer val, Value<Integer> valueModel) {
unreadCount.setText(String.valueOf(val)); // unreadCount is textview for display unread message count.
}
});

TokenAuthResponse#

public interface TokenAuthResponse{
void success();
void failure(TokenAuthError tokenAuthError);
}

result :

Parameter NameTypeDescriptionValues
successBooleanIt returns activation success or nor.true : Success false: failed
errorTokenAuthErrorFailure detail if activation is failed.Instance of TokenAuthError

TokenAuthError#

public class TokenAuthError {
public String errorCode;
public String errorMsg;
public String getErrorCode() {
return errorCode;
}
public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}
public String getErrorMsg() {
return errorMsg;
}
public void setErrorMsg(String errorMsg) {
this.errorMsg = errorMsg;
}
}

In cases where activation fails, the following errorCode and errorMsg values ​​are returned.

errorCodeerrorMsg (English)errorMsg (Turkish)Meaning
DLGEXCPINP_DVCIDYou must need to pass deviceId.deviceId parametresi dolu olmalıdır.You did not provide the deviceId parameter to the method correctly.
DLGEXCPINP_CSTIDYou must need to pass customer Id.customerId parametresi dolu olmalıdır.You did not provide the customerId parameter to the method correctly.
DLGEXCPINP_CSTNMYou must need to pass customer name.customerName parametresi dolu olmalıdır.You did not provide the customerName parameter to the method correctly.
DLGEXCPINP_TKNYou must need to pass diyalog token.diyalogToken parametresi dolu olmalıdır.You did not provide the diyalogToken parameter to the method correctly.
DLGEXCPSRV_INVLDNMCustomer id is invalid.Müşteri no hatalıCustomer Id is not found in the system.
DLGEXCPSRV_FRBDNCSTForbidden customer.Yasaklı müşteri.Provided customer is forbidden to use Diyalog.
DLGEXCPSRV_TXHSHEXPtransactionHash has expired.transactionHash bilgisinin süresi geçmiş.This error occurs if the internal transaction hash information created in the Diyalog system has been used before. This error may occur when the same activation request is called more than once. It is not expected to occur.
DLGEXCPSRV_TKNNOTVLDdlgToken is not validated by auth server.dlgToken otorizasyon sunucusu tarafından doğrulanmadı.This error occurs when your token is not verified by the token verification service.
DLGEXCPSRV_AUTHSRVERRInternal failure in Auth server.Otorizasyon sunucusu internal hata.This error indicates that an internal server error response returned from your token verification service.
DLGEXCPSRV_BADREQThe request rejected by auth server.Otorizasyon sunucusu isteği kabul etmedi.This error indicates that an bad request response was returned from your token verification service.
DLGEXCPSRV_TKNEXPdlgToken is expired.dlgToken süresi dolmuşThis error is the error code returned by your token verification service when the token has expired.
DLGEXCPSRV_TKNINVLDdlgToken is invalid.Geçersiz dlgToken.Token is not validated by your token verification service.
DLGEXCPSRV_DVCIDINVLDInvalid device id.Geçersiz cihaz id.Device id is not found by your token verification service.
DLGEXCPSRV_SIDINVLDInvalid session id.Geçersiz session id.Session Id is not found by your token verification service.
DLGEXCPSRV_AUTHSRVINACCToken authorisation server is not accessible.Token otorizasyon sunucusuna erişim yok.Your token verification service is not responding.
DLGEXCPSRV_INTERRmessage in RpcExceptionmessage in RpcExceptionThis is rpc exception of the the Diyalog Server.
DLGEXCPINP_SESIDYou must need to pass session Id.Oturum kimliğini geçmeniz gerekiyor.You did not provide the sessionId parameter to the method correctly.
DLGEXCPINP_USRIDYou must need to pass user Id.Kullanıcı kimliğini geçmeniz gerekiyor.You did not provide the userId parameter to the method correctly.
DLGEXCPINP_USRNMYou must need to pass user name.Kullanıcı adını geçmeniz gerekiyor.You did not provide the userName parameter to the method correctly.
DLGEXCPSRV_CONNERRUnable to connect.Unable to connect. Please check that you are connected to the Internet.This error occurs when Diyalog SDK is not connected to the Diyalog server due to network or endpoint configuration error.