Authentication

Alpha Documentation

The documentation for the Enjin SDKs pertain to the Project and Player schemas which are currently in an Alpha release. The Project and Player schemas are not yet publicly available and therefore this documentation is limited only to those who already have access. For any queries, please contact Enjin Support.

Authenticating a Project Client

Creating the Client

The first step we take in setting up our ProjectClient is to instantiate it. During this we must specify which network we are using. Our choices are Mainnet, JumpNet, and Goerli. URLs for these networks can be programmatically acquired from the EnjinHosts class and passed to the clients. The chosen network must match the one we created our project on.
Java
C#
C++
1
import com.enjin.sdk.EnjinHosts;
2
import com.enjin.sdk.ProjectClient;
3
​
4
String mainnet = EnjinHosts.MAIN_NET;
5
String jumpnet = EnjinHosts.JUMP_NET;
6
String goerli = EnjinHosts.GOERLI;
7
​
8
ProjectClient client = new ProjectClient(/* Enjin host here */);
Copied!
1
using Enjin.SDK;
2
​
3
System.Uri mainnet = EnjinHosts.MAIN_NET;
4
System.Uri jumpnet = EnjinHosts.JUMP_NET;
5
System.Uri goerli = EnjinHosts.GOERLI;
6
​
7
ProjectClient client = new ProjectClient(/* Enjin host here */);
Copied!
1
#include "enjinsdk/EnjinHosts.hpp"
2
#include "enjinsdk/ProjectClient.hpp"
3
#include <memory>
4
#include <string>
5
​
6
using namespace enjin::sdk;
7
​
8
std::string mainnet = MAIN_NET;
9
std::string jumpnet = JUMP_NET;
10
std::string goerli = GOERLI;
11
​
12
std::unique_ptr<ProjectClient> client = ProjectClientBuilder()
13
.base_uri(/* Enjin host here */)
14
.build();
Copied!

Creating the Authentication Request

Before creating the AuthProject request, we must first gather our project's UUID and secret key. To do so, we may navigate to our project page on the Enjin platform and find these items under Settings ->API Credentials.
Once the UUID and secret have been acquired, we can pass them to the AuthProject request we have created.
You should make sure that you're only authenticating as a project in a secure environment. The project's secret enables a large degree of access over the project and is intended solely to be ran in an environment managed by the developer. It mustn't be exposed to others.
Java
C#
C++
1
import com.enjin.sdk.schemas.project.queries.AuthProject;
2
​
3
AuthProject req = new AuthProject()
4
.uuid("<the-project's-uuid>")
5
.secret("<the-project's-secret>");
Copied!
1
using Enjin.SDK.ProjectSchema;
2
​
3
AuthProject req = new AuthProject()
4
.Uuid("<the-project's-uuid>")
5
.Secret("<the-project's-secret>");
Copied!
1
#include "enjinsdk/project/AuthProject.hpp"
2
​
3
using namespace enjin::sdk::project;
4
​
5
AuthProject req = AuthProject()
6
.set_uuid("<the-project's-uuid>")
7
.set_secret("<the-project's-secret>");
Copied!

Sending the Authentication Request

To send any request to the platform, we must call the method in our client whose name matches the request. The result of these methods in a synchronous operation will be a GraphQL response that wraps the data we want. An example of what this looks like for the AuthProject request is shown below:
Java
C#
C++
1
import com.enjin.sdk.graphql.GraphQLResponse;
2
​
3
GraphQLResponse<AccessToken> res = client.authProject(req);
Copied!
1
using Enjin.SDK.Graphql;
2
​
3
GraphqlResponse<AccessToken> res = client.AuthProject(req).Result;
Copied!
1
#include "enjinsdk/GraphqlResponse.hpp"
2
​
3
using namespace enjin::sdk::graphql;
4
​
5
GraphqlResponse<AccessToken> res = client->auth_project(req).get();
Copied!
With the GraphQL response from the platform we can get the data contained within it. For authentication requests this will be a AccessToken class representing the platform's Auth type. Before retrieving the data however, we may want to consider checking if the response was successful. To do so, we can use the method shown below and get its result:
Java
C#
C++
1
boolean result = res.isSuccess();
Copied!
1
bool result = res.IsSuccess;
Copied!
1
bool result = res.is_successful();
Copied!
Now that we have determined that the response was successful we can retrieve the AccessToken model from the response. This may be done with the code shown below:
Java
C#
C++
1
import com.enjin.sdk.models.AccessToken;
2
​
3
AccessToken accessToken = res.getData();
Copied!
1
using Enjin.SDK.Models;
2
​
3
AccessToken accessToken = res.Result;
Copied!
1
#include "enjinsdk/models/AccessToken.hpp"
2
​
3
using namespace enjin::sdk::models;
4
​
5
AccessToken access_token = res.get_result().value();
Copied!

Authenticating

With the AccessToken we can now pass the string token contained within to the client's authentication method.
Java
C#
C++
1
client.auth(accessToken.getToken());
Copied!
1
client.Auth(accessToken.Token);
Copied!
1
client->auth(access_token.get_token().value());
Copied!
After authenticating, we may check to see if the authentication was successful. We can do so with the method shown below and getting the Boolean value returned.
Java
C#
C++
1
boolean result = client.isAuthenticated();
Copied!
1
bool result = client.IsAuthenticated;
Copied!
1
bool result = client->is_authenticated();
Copied!
With our client successfully authenticated we can now use it to make further requests to the platform. One point of note though is that our client's authentication with the platform will eventually expire. The time in seconds until the authentication expires can retrieved from the AccessToken as shown below:
Java
C#
C++
1
long time = accessToken.getExpiresIn();
Copied!
1
long time = accessToken.ExpiresIn.Value;
Copied!
1
long time = access_token.get_expires_in().value();
Copied!

Authenticating a Player Client

To authenticate a player client we must first instantiate and authenticate a project client, which has access to the schemas we need. Read over the Authenticate a Project Client section for steps on setting up a project client ready to send requests to the platform.

Getting the Access Token

Currently there are two ways to get the access token for authenticating a player client. One such way is through the CreatePlayer request, which is for new players that do not yet exist for our project. The other way to acquire a player's access token is via the AuthPlayer request, which is used for existing players.

Creating a New Player

When using the CreatePlayer request, we provide the ID of the new player we wish to add to our project as shown below:
Java
C#
C++
1
import com.enjin.sdk.schemas.project.mutations.CreatePlayer;
2
​
3
CreatePlayer req = new CreatePlayer()
4
.id("<the-player's-id>");
Copied!
1
using Enjin.SDK.ProjectSchema;
2
​
3
CreatePlayer req = new CreatePlayer()
4
.Id("<the-player's-id>");
5
​
6
// Using a authenticated ProjectClient
7
GraphqlResponse<AccessToken> res = client.CreatePlayer(req).Result;
Copied!
1
#include "enjinsdk/project/CreatePlayer.hpp"
2
​
3
using namespace enjin::sdk::project;
4
​
5
CreatePlayer req = CreatePlayer()
6
.set_id("<the-player's-id>");
Copied!
With the request in hand, we can now send the CreatePlayer request to the platform and if successful we will get the player's access token in the response as shown below:
Java
C#
C++
1
import com.enjin.sdk.graphql.GraphQLResponse;
2
import com.enjin.sdk.models.AccessToken;
3
​
4
// Using a authenticated ProjectClient
5
GraphQLResponse<AccessToken> res = client.createPlayer(req);
6
​
7
AccessToken accessToken = res.getData();
Copied!
1
using Enjin.SDK.Graphql;
2
using Enjin.SDK.Models;
3
​
4
// Using a authenticated ProjectClient
5
GraphqlResponse<AccessToken> res = client.CreatePlayer(req).Result;
6
​
7
AccessToken accessToken = res.Result;
Copied!
1
#include "enjinsdk/GraphqlResponse.hpp"
2
#include "enjinsdk/models/AccessToken.hpp"
3
​
4
using namespace enjin::sdk::graphql;
5
using namespace enjin::sdk::models;
6
​
7
// Using a authenticated ProjectClient
8
GraphqlResponse<AccessToken> res = client->create_player(req).get();
9
​
10
AccessToken access_token = res.get_result().value();
Copied!

Authenticating an Existing Player

When using the AuthPlayer request, we must specify the ID of the player whom we wish to authenticate for as shown below:
Java
C#
C++
1
import com.enjin.sdk.schemas.project.queries.AuthPlayer;
2
​
3
AuthPlayer req = new AuthPlayer()
4
.id("<the-player's-id>");
Copied!
1
using Enjin.SDK.ProjectSchema;
2
​
3
AuthPlayer req = new AuthPlayer()
4
.Id("<the-player's-id>");
Copied!
1
#include "enjinsdk/project/AuthPlayer.hpp"
2
​
3
using namespace enjin::sdk::project;
4
​
5
AuthPlayer req = AuthPlayer()
6
.set_id("<the-player's-id>");
Copied!
With the request in hand, we can now send the AuthPlayer request to the platform and get the player's access token.
Java
C#
C++
1
import com.enjin.sdk.graphql.GraphQLResponse;
2
import com.enjin.sdk.models.AccessToken;
3
​
4
// Using a authenticated ProjectClient
5
GraphQLResponse<AccessToken> res = client.authPlayer(req);
6
​
7
AccessToken accessToken = res.getData();
Copied!
1
using Enjin.SDK.Graphql;
2
using Enjin.SDK.Models;
3
​
4
// Using a authenticated ProjectClient
5
GraphqlResponse<AccessToken> res = client.AuthPlayer(req).Result;
6
​
7
AccessToken accessToken = res.Result;
Copied!
1
#include "enjinsdk/GraphqlResponse.hpp"
2
#include "enjinsdk/models/AccessToken.hpp"
3
​
4
using namespace enjin::sdk::graphql;
5
using namespace enjin::sdk::models;
6
​
7
// Using a authenticated ProjectClient
8
GraphqlResponse<AccessToken> res = client->auth_player(req).get();
9
​
10
AccessToken access_token = res.get_result().value();
Copied!

Authenticating the Client

Step 1: Create the Client

Creating a player client is similar to creating a project client, but instead of using the ProjectClient and its related classes we will be using the PlayerClient class.
Java
C#
C++
1
import com.enjin.sdk.PlayerClient;
2
​
3
PlayerClient playerClient = new PlayerClient("<enjin-host-url>");
Copied!
1
using Enjin.SDK;
2
​
3
PlayerClient playerClient = new PlayerClient("<enjin-host-url>");
Copied!
1
#include "enjinsdk/PlayerClient.hpp"
2
#include <memory>
3
​
4
using namespace enjin::sdk;
5
​
6
std::unique_ptr<PlayerClient> player_client = PlayerClientBuilder()
7
.base_uri("<enjin-host-url>")
8
.build();
Copied!

Step 2: Pass the String Token

Once the player client has been created, it may be authenticated with the access token we received from the platform using either the CreatePlayer or AuthPlayer requests. To do so, we call the respective authentication method and provide the string token contained within.
Java
C#
C++
1
playerClient.auth(accessToken.getToken());
Copied!
1
playerClient.Auth(accessToken.Token);
Copied!
1
player_client->auth(access_token.get_token().value());
Copied!
As with the project client, we may check if the token was valid by using the method seen below and getting its Boolean value.
Java
C#
C++
1
boolean result = playerClient.isAuthenticated();
Copied!
1
bool result = playerClient.IsAuthenticated;
Copied!
1
bool result = player_client->is_authenticated();
Copied!
Last modified 1d ago