About custom authentication - How to do it in PHP

Options
Hello all,

First post here :)
I am still checking the PhotonCloud docs, so far so good. But I can't find more information about this custom authentication process.
Already checked here: http://doc.exitgames.com/photon-cloud/C ... ntication/
And here: https://github.com/exitgames/photon.cus ... entication

The only example of working with this is with Asp.Net...
I want to use custom auth for login attempt for users, in a testing environment, but I don't really understand about the "Key/Value" pairs... and it says I need to return a Json right? Can I just use it with a PHP script and return the information needed? Or anyone did anything related?

Thanks in advance.

Best,
Alexandre

Comments

  • xandeck
    xandeck
    edited October 2015
    Options
    Hello all again,

    Ok, just got it working. But I think the manual should clarify a bit more about this (it is not hard, though), with a real example.
    I will post here for anyone trying to do the same (I did in PHP at the end). For tests purposes, I will not explain about security, hashs (SHA, MD5, and stuff) I only did in my game test here. Anyway...

    In Unity:
    - I created like 3 variables (strings) for "username", "password" and "gameVersion": login_userName , login_password , gameVersion
    - Commands to send the variables:
    PhotonNetwork.AuthValues.AuthType = CustomAuthenticationType.Custom; //I dont know if this is mandatory, need to test without it
    PhotonNetwork.AuthValues.AuthParameters = "username=" + login_userName + "&password=" + login_password + "&gameVersion=" + gameVersion + ""; //this will save the values of those variables to send via PHP
    PhotonNetwork.ConnectUsingSettings("1.0");
    

    The variables were normally filled with a GUI.TextField in Unity.

    In PhotonCloud dashboard:
    - In Custom Authentication, uncheck the "Allow anonymous clients..." just for you to make sure your variables are working, cause if not the client could log in anyway;
    - In Custom Provider, click EDIT;
    - In "Authentication URL" put your main URL for the PHP script, example: "http://auth.mymastergame.com/unity_login.php";
    - Create 3 new "Optional Key/Value Pairs", the key should be the variable name, in our case, is "username", "password" and "gameVersion". The values you can put whatever you like (these will be send in Unity);
    - Also, I checked the "Reject Clients if service is unavailable" for myself, you do whatever you like.

    In PHP script:
    - I will not put here all the PHP code, but willl tell you how to return the data PhotonCloud need, it should be in JSON format.
    - I am getting my variables like this (remember to make security checks and everything else for a production environment):
    $username = $_GET["username"];
    $password = $_GET["password"]; //the password is comming in plain text, it is trully NOT RECOMMENDED to keep it that way
    $gameVersion = $_GET["gameVersion"];
    

    - After getting those variables, you can check in your own database if the user and password exists or not, also check if it is the same game version.
    - For return answer, I am doing this way:
    //For success return use this variable array
    $login_info = array(
        "ResultCode" => 1,
        "Message" => "You are connected!",
    );
    
    //For failure return use this variable array
    $login_info = array(
        "ResultCode" => 2,
        "Message" => "Wrong username or password",
    );
    
    //Or this one
    $login_info = array(
        "ResultCode" => 3,
        "Message" => "Wrong data",
    );
    

    Note: you need to choose one of the three above to send, obviouslly, keeping that way will only return the last array.
    Also, you can put your own php variable in the "Message" to see what is inside it.
    You can check more about here: https://github.com/exitgames/photon.cus ... entication

    In this same PHP script, put this in the end:
    $json = json_encode($login_info);
    echo $json;
    

    Remember this is just and example and you can do in any other way.

    C ya :)
  • janla
    janla
    edited October 2015
    Options
    Hi there,

    thanks for the usefull tutorial!
    I've successfully implemented it in my app, but I'm struggling to get the json message upon successful authentification.
    Is there any way in Unity to catch the "message" or any other json keys apart from
    onCustomAuthentificationFailed(debugMessage)
    
    ?

    Thanks,
    jan
  • Tobias
    Options
    I don't know which values you are looking for at the moment.
    You could check PUN for the place where we call onCustomAuthentificationFailed and log out the operationResponse with .ToStringFull().
    This will show you if any other useful info is in the response.
  • As a follow up, is it possible to receive information back from the custom authentication if the user was logged in successfully? I'd like to be able to get some information about the user after they have logged in. Is this possible through Photon Cloud or will we have to devise a way to use our own servers mixed with Photon Cloud's authentication?
  • Tobias
    Options
    Afaik, the Op Authenticate will fail if the authentication itself failed. I don't think we are handing over any information about that user's account yet in the response.

    In "Turnbased" games you can already do custom "WebRPCs", which means: You can all an operation on the Photon server to call your own script on any web service. With this, it's quite easy to build a "fetch user data" method.
    This will become available for all types of games eventually but you could create a turnbased app to try it out and get access right away. It will be able to run "realtime games", despite the naming.

    Apply here (and allow us some time to accept the join request):
    https://groups.google.com/a/exitgames.c ... ased-group
  • akauper
    Options
    Hey everyone.

    PhotonNetwork.AuthValues.AuthParameters no longer exists. It seems it has become PhotonNetwork.AuthValues.AuthGetParameters. But this change is undocumented.

    My authentication is no longer working with this new change to AuthGetParamters. Anyone know whats changed?
  • Tobias
    Options
    Yes, we changed things for the AuthValues. The cleanup was necessary to better support a UserId.
    The change itself should be listed in the changelog.txt but the API documentation only describes the current state of course.

    AuthGetParameters should not be different from what you did before though. It's still a string value which you can fill any way you like. Alternatively, use AddAuthParameter(key, value) to add get parameters.

    Did you check what you get in your custom auth server's logs? You could also make your server's error debug message return the values it got and more info why it failed to auth.
  • r_projects
    edited October 2015
    Options
    Hmm, so no simple way to catch a success message??

    How to stop it doing the authentication failed error, that will maked the editor jump into pause state?


  • JohnTube
    JohnTube ✭✭✭✭✭
    Options
    @r_projects what are you trying to accomplish exactly? what custom authentication related questions do you have?
  • r_projects
    edited October 2015
    Options
    I can see the idea of this is to check for a user before connecting. After that point I will use my own system.

    On authentication fail, error message in editor:

    Custom Authentication failed (either due to user-input or configuration or AuthParameter string format). Calling: OnCustomAuthenticationFailed()

    Why is it throwing an error message, befor OnCustomAuthenticationFailed ?
  • vadim
    Options
    That's the way PUN works.
    It logs an error, then calls OnCustomAuthenticationFailed
  • r_projects
    edited October 2015
    Options
    NetworkingPeer.cs

    line 1082 : Debug.LogError(string.Format("Custom Authentication failed......

    That is what I was looking for.