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 applied. They are custom persistence, file persistence, and DB persistence (default).
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 interface object for the library to process it.
save_token(UserSignature user, Token token) - invoked after fetching access and refresh tokens from Zoho.
delete_token(UserSignature user, 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.
Table of Contents
Custom Persistence
To use Custom Persistence, the user must extend Store::TokenStore and include the methods.
Here is the code:
using System;
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
end
File Persistence
In case of default File Persistence, the user can persist tokens in the local drive, by providing the the absolute file path to the FileStore object.
The file contains:
- user_mail
- client_id
- refresh_token
- access_token
- grant_token
- expiry_time
Here is the code to create a FileStore object:
/*
#Parameter containing the absolute file path to store tokens
tokenstore = FileStore.new("/Users/user_name/Documents/ruby_sdk_token.txt")
Database Persistence
If you want to use database persistence, you can use MySQL. The DB persistence mechanism is the default method in Ruby SDK.
The MySQL should run in the same machine
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))
access_token(varchar(255))
grant_token(varchar(255))
expiry_time(varchar(20))
The default MySQL persistence requires MySQL2 to be installed. Use the below command to install MySQL2.
gem install mysql2 -v 0.5.2
The order of precedence for token persistence is custom, file, followed by DB persistence. That is, if you provide the details for custom persistence (in persistence_handler_class_path) and file persistence details (in token_persistence_path), then custom persistence is given priority over file.
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:
/*
#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 = Store::DBStore.new()
tokenstore = Store::DBStore.new("hostName", "dataBaseName", "userName", "password", "portNumber")