JavaJava V4 Token Management System API Reference for Realtime Apps

The Token Management System (TMS) is a part of the Java SDK which allows you to store access tokens obtained via grantToken(). If a particular PubNub method requires authentication, the TMS will perform a lookup in order to select an apropriate access token, and attach it to the request in question - as the authKey query parameter.

 
  • Currently, the TMS selects tokens only for requests from the Objects API. For authorizing other requests, please use the classic grant() method.
  • The TMS is automatically disabled if the PubNub instance it runs on has a valid secret key, meaning altough you can set tokens into the TMS, it will not authorize requests automatically.

    Otherwise, the TMS is enabled by default, but can also be disabled excplicitly via pnConfiguration.setDisableTokenManager(true);.

Stores a single token in the Token Management System for use in API calls.
  1. ParameterTypeRequiredDescription
    tokenStringYesThe token to add to the Token Management System.
/*
* A token is usually not hardcoded.
*
* Tokens are returned as a result to calling 'grantToken()' on a PubNub
* server instance.
*
* Use that token as the parameter to 'pubnub.setToken()' on a PubNub
* client instance.
*/

pubnub.setToken("p0F2AkF0Gl2AX-JDdHRsCkNyZXOkRGNoYW6cKBDdXNyoWl1LTMzNTIwNTUPQ3NwY6...");

The setToken() method does not return anything.

Stores multiple tokens in the Token Management System for use in API calls.
  1. ParameterTypeRequiredDescription
    tokensList<String>YesThe list of tokens to add to the Token Management System.
mPubNub.setToken("p0F2AkF0Gl2AX-JDdHRsCkNyZXOkRGNoYW6gQ2dycKBDNTIwNTUPQ3NwY6...");

The setTokens() method does not return anything.

Perfoms a lookup and returns a token for the specified resource type and ID. If no token is found for the supplied resource type and ID, the TMS checks for a pattern token at the resource type level. Returns null if no token is found.
  1. ParameterTypeRequiredDescription
    resourceIdStringYesThe resource ID for which the token is to be retrieved.
    resourceTypePNResourceTypeYesThe resource type of the resource for which the token is to be retrieved.
String token = pubnub.getToken("user_1", PNResourceType.USER);

The getToken() method returns a String which is the token itself, or null if no token is found.

Returns a map of all tokens stored by pubnub.setToken() or pubnub.setTokens().

HashMap<String, HashMap<String, HashMap<String, String>>> tokenMap = pubNub.getTokens();
// the 'tokenMap' contains all available tokens

for (String resourceType : tokenMap.keySet()) {
    System.out.println("Type: " + resourceType);
    // 'resourceType' can be either "user" or "space"
    for (String tokenType : tokenMap.get(resourceType).keySet()) {
        // 'tokenType' can be either "resource" or "pattern"
        for (String resource : tokenMap.get(resourceType).get(tokenType).keySet()) {
            // 'resource' is either an actual resource ID or a regex pattern
            String token = tokenMap.get(resourceType).get(tokenType).get(resource);
            System.out.println("\t"  + tokenType + ": " + resource + ", token: " + token);
        }
    }
}

/*
* Output:
* 
* ------------------------------------------------
* Type: user
* 	 resource: user_1, token: p0F2AkF0Gl2VnplDd==
* 	 resource: user_2, token: p0F2AkF0zo_Bg9iqQ==
* Type: space
* 	 pattern: emp-.*, token: p0F2AkF0Gl
* ------------------------------------------------
* 
* Please note that the actual tokens have been trimmed
* for the sake of brevity.
*/

The getTokens() method returns a HashMap, where the key is either "user" or "space". The value assigned to each of these keys is another HashMap, which key is either "resource" or "pattern". The value is yet another HashMap which key then is a concrete resource id or a pattern (depends on the second-level key), and the value being the actual access token.

Returns the token(s) for the specified resource type.
  1. ParameterTypeRequiredDescription
    resourceTypePNResourceTypeYesThe resource type of the resource for which the token is to be retrieved.

Get Tokens By Resource Type

HashMap<String, HashMap<String, String>> userTokenMap = pubNub.getTokensByResource(PNResourceType.USER);
// the 'userTokenMap' contains tokens for 'Users' only

for (String tokenType: userTokenMap.keySet()) {
    System.out.println("Token Type: " + tokenType);
    // 'tokenType' can be either "resource" or "pattern"
    for (String resource: userTokenMap.get(tokenType).keySet()) {
        // 'resource' is either an actual resource ID or a regex pattern
        String token = userTokenMap.get(tokenType).get(resource);
        System.out.print("\t");
        System.out.print(" Resource: " + resource);
        System.out.println(" - Token: " + token);
    }
}

/*
* Output:
* 
* ------------------------------------------------
* Token Type: resource
* 	 Resource: user_2 - Token: p0F2AkF0Gl2VnplDd==
* 	 Resource: user_1 - Token: p0F2AkF0zo_Bg9iqQ==
* Token Type: pattern
* 	 Resource: emp-.* - Token: p0FVUK40zo_Bg9iqQ==
* ------------------------------------------------
* 
* Please note that the actual tokens have been trimmed
* for the sake of brevity.
*/

The getTokensByResource() method returns a HashMap, where the key is either "resource" or "pattern", and the value is another HashMap which key then is a concrete resource id or a pattern (depends on the top-level key), and the value being the actual access token.