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..
Table of Contents
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.
get_token(UserSignature user, Token token) - invoked before firing a request to fetch the saved tokens. This method should return an implementation of the Token object for the library to process it.
save_token(UserSignature user, Token token) - invoked after fetching access and refresh tokens from Zoho.
delete_token(Token token) - invoked before saving the latest tokens.
get_tokens() - The method to retrieve all the stored tokens.
delete_tokens() - The method to delete all the stored tokens.
get_token_by_id() - The method to retrieve the user's token details based on unique ID.
Custom Persistence
To use Custom Persistence, the user must extend ZOHOCRMSDK::Store::TokenStore and include the methods.
Here is the code:
require 'ZOHOCRMSDK2_0'
# This class stores the user token details to the file.
class TokenStore
# This method is used to get the user token details.
# @param user A UserSignature class instance.
# @param token A Token class instance.
# @return A Token class instance representing the user token details.
# @raise SDKException
def get_token(user, token); end
def get_tokens; end
# This method is used to store the user token details.
# @param user A UserSignature class instance.
# @param token A Token class instance.
# @raise SDKException
def save_token(user, token); end
# This method is used to delete the user token details.
# @param user A User class instance.
# @param token A Token class instance.
# @raise SDKException
def delete_token(token); end
def delete_tokens; end
# This method is used to get the user token details.
# @param user A UserSignature class instance.
# @param id A String containing id
# @return A Token class instance representing the user token details.
# @raise SDKException
def get_token_by_id(id, token); end
end
File Persistence
In case of default File Persistence, the user can persist tokens in the local drive, by providing the absolute file path to the FileStore object.
The file contains:
- id
- 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 = ZOHOCRMSDK::Store::FileStore.new("/Users/user_name/Documents/ruby_sdk_token.txt")
Database Persistence
If you want to use database persistence, you can use MySQL.
The MySQL should run in the same machine
The database name should be zohooauth.
There must be a table oauthtoken with the following columns
id(int(11))
user_mail(varchar(255))
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))
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)
)
alter table oauthtoken auto_increment = 1
Here is the code to create a DBStore object:
/*
# 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"
tokenstore = ZOHOCRMSDK::Store::DBStore.new()
tokenstore = ZOHOCRMSDK::Store::DBStore.new(host: "host_name", database_name: "database_name" ,table_name: "table_name",user_name: "user_name",password: "password",port_number:"port_number")