Token Persistence

Token persistence refers to storing and utilizing the authentication tokens that are provided by Zoho. There are three ways provided by the SDK in which persistence can be utilized. They are DataBase Persistence, File Persistence and Custom Persistence.

Implementing OAuth Persistence

Once the application is authorized, OAuth access and refresh tokens can be used for subsequent user data requests to Zoho CRM. Hence, they need to be persisted by the client app.

The persistence is achieved by writing an implementation of the inbuilt TokenStore interface, which has the following callback methods.

  • getToken($user, $token) - invoked before firing a request to fetch the saved tokens. This method should return implementation Token interface object for the library to process it.

  • saveToken($user, $token)- invoked after fetching access and refresh tokens from Zoho.

  • deleteToken( $token) - invoked before saving the latest tokens.

  • getTokens() - The method to retrieve all the stored tokens.

  • deleteTokens() - The method to delete all the stored tokens.

  • getTokenById($id, $token) - The method to retrieve the user's token details based on unique ID.

Note
  • $id is a string.

  • $user instance of UserSignature class

  • $token instance of Token interface implementing class.

There are three ways provided by the SDK in which you can achieve persistence. They are:

Database Persistence

If you want to use database persistence, you can use MySQL. The DB persistence mechanism is the default method.

  • The database name should be zohooauth.

  • There must be a table oauthtoken with columns

    • id(int(11))

    • user_mail (varchar(255))

    • client_id (varchar(255))

    • client_secret varchar(255)/p>

    • refresh_token (varchar(255))

    • grant_token (varchar(255))

    • access_token (varchar(255))

    • expiry_time(varchar(20))

    • redirect_url(varchar(255)

Note

Custom database name and table name can be set in DBStore instance.

MySQL Query


CREATE TABLE oauthtoken (
  id varchar(255) NOT NULL,
  user_mail varchar(255) NOT NULL,
  client_id varchar(255),
  client_secret varchar(255),
  refresh_token varchar(255),
  access_token varchar(255),
  grant_token varchar(255),
  expiry_time varchar(20),
  redirect_url varchar(255),
  primary key (id)
);

Here is the code to create a DBStore object:

/*
/*
* hostName -> DataBase host name. Default value "localhost"
* databaseName -> DataBase name. Default  value "zohooauth"
* userName -> DataBase user name. Default value "root"
* password -> DataBase password. Default value ""
* portNumber -> DataBase port number. Default value "3306"
* tableName -> Table Name. Default value "oauthtoken"
*/
// $tokenstore = (new DBBuilder())->build();
$tokenstore = (new DBBuilder())
->host("hostName")
->databaseName("databaseName")
->userName("userName")
->portNumber("portNumber")
->tableName("tableName")
->password("password")
->build();

File Persistence

In case of file persistence, you can set up persistence the tokens in the local drive, and provide the absolute file path in the FileStore object. This file must contain the following:

  • user_mail

  • client_id

  • client_secret

  • refresh_token

  • access_token

  • grant_token

  • expiry_time

  • redirect_url

Here is the code to create a FileStore object:


//Parameter containing the absolute file path to store tokens
$tokenstore = new FileStore("/Users/username/Documents/php_sdk_token.txt");

Custom Persistence

To use Custom Persistence, you must implement TokenStore interface(com\zoho\api\authenticator\store\TokenStore) and override the methods.

Here is the code:


namespace store;
use com\zoho\api\authenticator\Token;
use com\zoho\crm\api\exception\SDKException;
use com\zoho\crm\api\UserSignature;
use com\zoho\api\authenticator\store\TokenStore;
class CustomStore implements TokenStore
{
    /**
      * @param user A UserSignature class instance.
      * @param token A Token (com\zoho\api\authenticator\OAuthToken) class instance.
      * @return A Token class instance representing the user token details.
      * @throws SDKException if any problem occurs.
    */
    public function getToken($user, $token)
    {
      // Add code to get the token
      return null;
    }

    /**
      * @param user A UserSignature class instance.
      * @param token A Token (com\zoho\api\authenticator\OAuthToken) class instance.
      * @throws SDKException if any problem occurs.
    */
    public function saveToken($user, $token)
    {
      // Add code to save the token
    }

    /**
      * @param token A Token (com\zoho\api\authenticator\OAuthToken) class instance.
      * @throws SDKException if any problem occurs.
    */
    public function deleteToken($token)
    {
      // Add code to delete the token
    }

    /**
      * @return array  An array of Token (com\zoho\api\authenticator\OAuthToken) class instances
    */
    public function getTokens()
    {
      //Add code to retrieve all the stored tokens
    }

    public function deleteTokens()
    {
      //Add code to delete all the stored tokens.
    }

    /**
      * @param id A string.
      * @param token A Token (com\zoho\api\authenticator\OAuthToken) class instance.
      * @return A Token class instance representing the user token details.
      * @throws SDKException if any problem occurs.
    */
    public function getTokenById($id, $token)
    {
      // Add code to get the token using unique id
      return null;
    }
}