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, the 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 Class, which has the following callback methods.
getToken(user, token) - invoked before firing a request to fetch the saved tokens. This method should return an implementation of Token Class 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.
Note
- user is an instance of the UserSignature Class.
- token is an instance of the Token Class.
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 oauthtokens with columns
id(int(11))
user_mail (varchar(255))
client_id (varchar(255))
refresh_token (varchar(255))
grant_token (varchar(255))
access_token (varchar(255))
expiry_time(varchar(20))
MySQL Query
create table oauthtoken(id int(11) not null auto_increment, user_mail varchar(255) not null, client_id varchar(255), refresh_token varchar(255), access_token varchar(255), grant_token varchar(255), expiry_time varchar(20), primary key (id));
alter table oauthtoken auto_increment = 1;
Here is the code to create a DBStore object:
const DBStore = require( "@zohocrm/nodejs-sdk-2.0/models/authenticator/store/db_store").DBStore;
/*
* DBStore takes the following parameters
* 1 -> DataBase host name. Default value "localhost"
* 2 -> DataBase name. Default value "zohooauth"
* 3 -> DataBase user name. Default value "root"
* 4 -> DataBase password. Default value ""
* 5 -> DataBase port number. Default value "3306"
*/
let tokenstore = new DBStore();
let tokenstore = new DBStore("hostName", "dataBaseName", "userName", "password", "portNumber");
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
refresh_token
access_token
grant_token
expiry_time
Here is the code to create a FileStore object:
const FileStore = require( "@zohocrm/nodejs-sdk-2.0/models/authenticator/store/file_store").FileStore;
/*
* FileStore takes the following parameter
* 1 -> Absolute file path of the file to persist tokens
*/
let tokenstore = new FileStore("/Users/username/Documents/nodejs_sdk_tokens.txt");
Custom Persistence
To use Custom Persistence, you must extend the TokenStore class (@zohocrm/nodejs-sdk-2.0/models/authenticator/store/token_store) and override the methods.
Here is the code:
const TokenStore = require('@zohocrm/nodejs-sdk-2.0/models/authenticator/store/token_store').TokenStore;
class CustomStore extends TokenStore {
constructor(){
super();
}
/**
*
* @param {UserSignature} user A UserSignature class instance.
* @param {Token} token A Token (@zohocrm/nodejs-sdk-2.0/models/authenticator/oauth_token) class instance.
* @returns A Token class instance representing the user token details.
* @throws {SDKException} if any error occurs.
*/
getToken(user, token) {
// Add code to get the token
return null;
}
/**
*
* @param {UserSignature} user A UserSignature class instance.
* @param {Token} token A Token (@zohocrm/nodejs-sdk-2.0/models/authenticator/oauth_token) class instance.
* @throws {SDKException} if any error occurs.
*/
saveToken(user, token) {
// Add code to save the token
}
/**
*
* @param {Token} token A Token (@zohocrm/nodejs-sdk-2.0/models/authenticator/oauth_token) class instance.
* @throws {SDKException} if any error occurs.
*/
deleteToken(user, token) {
// Add code to delete the token
}
/**
* @returns {Array} - An array of Token class instances
* @throws {SDKException}
*/
getTokens() {
//Add code to retrieve all the stored tokens.
}
/**
* @throws {SDKException}
*/
deleteTokens() {
//Add code to delete all the stored tokens.
}
}
module.exports = {CustomStore}