Skip to content

Commit

Permalink
Merge pull request #3 from zoho/beta
Browse files Browse the repository at this point in the history
3.0.0
  • Loading branch information
tharunpargi committed Oct 17, 2023
2 parents 04dc71d + 08b581e commit e0c4d91
Show file tree
Hide file tree
Showing 1,484 changed files with 166,312 additions and 92 deletions.
8 changes: 6 additions & 2 deletions README.md
Expand Up @@ -25,6 +25,10 @@ License

## Latest Version

- [3.0.0](/versions/3.0.0/README.md)

- Added the new key api_domain in FileStore and DBStore.

- [2.0.0](/versions//2.0.0/README.md)

- Email related records API pagination Issue fixed.
Expand Down Expand Up @@ -54,7 +58,7 @@ For older versions, please [refer](https://github.com/zoho/zohocrm-nodejs-sdk-5.
## Including the SDK in your project
You can include the SDK to your project using:

For including the latest [version](https://github.com/zoho/zohocrm-nodejs-sdk-5.0/releases/tag/1.0.0) using npm
For including the latest [version](https://github.com/zoho/zohocrm-nodejs-sdk-5.0/releases/tag/3.0.0) using npm

- Install **Node** from [nodejs.org](https://nodejs.org/en/download/) (if not installed).

Expand All @@ -67,4 +71,4 @@ For including the latest [version](https://github.com/zoho/zohocrm-nodejs-sdk-5.
```sh
npm install @zohocrm/nodejs-sdk-5.0
```
For more details, kindly refer [here](/versions/2.0.0/README.md).
For more details, kindly refer [here](/versions/3.0.0/README.md).
2 changes: 1 addition & 1 deletion lib/config/json_details.json

Large diffs are not rendered by default.

57 changes: 54 additions & 3 deletions lib/models/authenticator/oauth_token.js
Expand Up @@ -6,6 +6,7 @@ import pkg from "winston";
let Logger = pkg;
import { SDKException } from '../../core/com/zoho/crm/api/exception/sdk_exception.js';
import got from 'got';
import {DataCenter} from "../../routes/dc/data_center.js";


/**
Expand All @@ -30,6 +31,8 @@ class OAuthToken extends Token {

id;

apiDomain;

/**
* This is a setter method to set OAuth client id.
* @param {string} clientID - A String containing the client Id.
Expand Down Expand Up @@ -174,14 +177,29 @@ class OAuthToken extends Token {
this.id = id;
}

/**
* This is a getter method to get the apiDomain
* @returns the apiDomain
*/
getAPIDomain() {
return this.apiDomain;
}

/**
* This is a setter method to set the apiDomain
* @param {String} apiDomain A String containing the apiDomain
*/
setAPIDomain(apiDomain) {
this.apiDomain = apiDomain;
}

async generateToken() {
await this.getToken().catch((err) => { throw err });
}

async getToken() {
let initializer = await Initializer.getInitializer().catch((err) => { throw err });
let store = initializer.getStore();
let url = initializer.getEnvironment().getAccountsUrl();
let oauthToken = await Reflect.construct(OAuthToken, []);
oauthToken = null;
if (this.getId() != null) {
Expand All @@ -197,6 +215,34 @@ class OAuthToken extends Token {
}
oauthToken = this;
}

if (oauthToken.getAPIDomain() == null || oauthToken.getAPIDomain().length <= 0)
{
if(initializer.getEnvironment() == null)
{
throw new SDKException(Constants.ENVIRONMENT_ERROR_1, Constants.ENVIRONMENT_ERROR_MESSAGE.concat(" : "));
}
oauthToken.setAPIDomain(initializer.getEnvironment().getUrl());
}
let environment = await DataCenter.get(oauthToken.getAPIDomain());
if (environment !== null) {
try{
let initialzer = await Initializer.getInitializer();
initializer["_environment"] = environment;
}
catch (error)
{
throw new SDKException(Constants.ENVIRONMENT_ERROR_MESSAGE.concat(" : "), null, null, error);
}
}
else {
if (initializer.getEnvironment() == null)
{
throw new SDKException(Constants.ENVIRONMENT_ERROR_1, Constants.ENVIRONMENT_ERROR_MESSAGE.concat(" : "));
}
}

let url = initializer.getEnvironment().getAccountsUrl();
if (oauthToken.getAccessToken() == null || oauthToken.getAccessToken().length == 0) {
if (oauthToken.getRefreshToken() != null && oauthToken.getRefreshToken().length > 0) {
Logger.info(Constants.ACCESS_TOKEN_USING_REFRESH_TOKEN_MESSAGE);
Expand All @@ -218,7 +264,7 @@ class OAuthToken extends Token {
return oauthToken.getAccessToken();
}
getTokenExpiry(oauthToken) {
let num = (parseInt(oauthToken.getExpiresIn()) - parseInt(new Date().getTime()));
let num = (parseInt(oauthToken.getExpiresIn()) - new Date().getTime());
var check = num < 5000;
return check;
}
Expand Down Expand Up @@ -339,6 +385,11 @@ class OAuthToken extends Token {
if (responseJSON.hasOwnProperty(Constants.REFRESH_TOKEN)) {
oauthToken.setRefreshToken(responseJSON[Constants.REFRESH_TOKEN]);
}
if (responseJSON.hasOwnProperty(Constants.API_DOMAIN))
{
oauthToken.setAPIDomain(responseJSON[Constants.API_DOMAIN]);
}

return oauthToken;
}

Expand All @@ -352,7 +403,7 @@ class OAuthToken extends Token {
try {
let initializer = await Initializer.getInitializer();

await initializer.store.deleteToken(this).catch((err) => {
await initializer.getStore().deleteToken(this.id).catch((err) => {
throw err;
});
return true;
Expand Down
52 changes: 17 additions & 35 deletions lib/models/authenticator/store/db_store.js
Expand Up @@ -179,9 +179,9 @@ class DBStore extends TokenStore {
oauthToken.setId(newId);
}
var sqlQuery =
"INSERT INTO " +
that.tableName +
"(id,user_name,client_id,client_secret,refresh_token,access_token,grant_token,expiry_time,redirect_url) VALUES ?";
"INSERT INTO " +
that.tableName +
"(id,user_name,client_id,client_secret,refresh_token,access_token,grant_token,expiry_time,redirect_url,api_domain) VALUES ?";

var values = [
[
Expand All @@ -194,6 +194,7 @@ class DBStore extends TokenStore {
token.getGrantToken(),
token.getExpiresIn(),
token.getRedirectURL(),
token.getAPIDomain(),
],
];
connection.query(sqlQuery, [values], function (err, result) {
Expand Down Expand Up @@ -223,8 +224,8 @@ class DBStore extends TokenStore {
async deleteToken(id) {
try {
var connection = await this.getConnection();
let query = "delete from ".this.tableName + " where id='" + id + "';";
return new promise(function (resolve, reject) {
let query = "delete from " + this.tableName + " where id='" + id + "';";
return new Promise(function (resolve, reject) {
connection.connect(function (err) {
if (err) {
return reject(err);
Expand All @@ -251,6 +252,7 @@ class DBStore extends TokenStore {

async getTokens() {
var tokens = [];
let _this = this;
try {
var connection = await this.getConnection();
var sqlQuery = "select * from " + this.tableName + ";";
Expand All @@ -266,37 +268,10 @@ class DBStore extends TokenStore {
}
if (result.length > 0) {
for (let row of result) {
let grantToken =
row.grant_token !== null &&
row.grant_token !== Constants.NULL_VALUE &&
row.grant_token.length > 0
? row.grant_token
: null;
let token = new OAuthBuilder()
.clientId(row.client_id)
.clientSecret(row.client_secret)
.refreshToken(row.refresh_token)
.build();

token.setId(row.id);

if (grantToken != null) {
token.setGrantToken(grantToken);
}

let name = result[0].user_name;

if (name != null) {
token.setUserSignature(new UserSignature(name));
}

token.setAccessToken(row.access_token);

token.setExpiresIn(row.expiry_time);

token.setRedirectURL(row.redirect_url);

tokens.push(token);
let oauthToken = Reflect.construct(OAuthToken, []);
_this.setMergeData(oauthToken, [row])
tokens.push(oauthToken);
}
return resolve(tokens);
}
Expand Down Expand Up @@ -433,6 +408,10 @@ class DBStore extends TokenStore {
if (oauthToken.getRedirectURL() == null) {
oauthToken.setRedirectURL(result[0].redirect_url);
}
if (oauthToken.getAPIDomain() == null)
{
oauthToken.setAPIDomain(result[0].api_domain)
}
}
}

Expand Down Expand Up @@ -466,6 +445,9 @@ class DBStore extends TokenStore {
if (oauthToken.getRedirectURL() != null) {
query.push(this.setTokenInfo(Constants.REDIRECT_URL, oauthToken.getRedirectURL()));
}
if (oauthToken.getAPIDomain() != null) {
query.push(this.setTokenInfo(Constants.API_DOMAIN, oauthToken.getAPIDomain()));
}
return query.join(",");
}
setTokenInfo(key, value) {
Expand Down
58 changes: 35 additions & 23 deletions lib/models/authenticator/store/file_store.js
Expand Up @@ -19,10 +19,31 @@ class FileStore extends TokenStore {
constructor(filePath) {
super();
this.filePath = filePath;
this.headers = [Constants.ID, Constants.USER_NAME, Constants.CLIENT_ID, Constants.CLIENT_SECRET, Constants.REFRESH_TOKEN, Constants.ACCESS_TOKEN, Constants.GRANT_TOKEN, Constants.EXPIRY_TIME, Constants.REDIRECT_URL];
let isCreate = false;
this.headers = [Constants.ID, Constants.USER_NAME, Constants.CLIENT_ID, Constants.CLIENT_SECRET, Constants.REFRESH_TOKEN, Constants.ACCESS_TOKEN, Constants.GRANT_TOKEN, Constants.EXPIRY_TIME, Constants.REDIRECT_URL, Constants.API_DOMAIN];
if (!fs.existsSync(this.filePath) || (fs.existsSync(this.filePath) && fs.readFileSync(this.filePath, 'utf-8') === "")) {
isCreate = true;
fs.writeFileSync(filePath, this.headers.join(), 'utf-8');
}
if (!isCreate){
try{
var allContents = fs.readFileSync(this.filePath, 'utf-8').toString().split("\n");
let headers = allContents[0].split(",");
if (headers.length === 9) {
allContents = allContents.map(content => {
const arr = content.split(",");
const newArr = arr.slice();
newArr.push('');
return newArr;
});
allContents[0] = this.headers.join();
fs.writeFileSync(this.filePath, allContents.join("\n"));
}
}
catch (error){
throw error;
}
}
}

async findToken(token) {
Expand Down Expand Up @@ -139,12 +160,12 @@ class FileStore extends TokenStore {

if (recordId == id) {
isRowPresent = true;
csvReader.slice(index, csvReader.length + 1);
csvReader.splice(index, 1)
}
}
}
if (isRowPresent) {
fs.appendFileSync(this.filePath, csvReader.join());
fs.writeFileSync(this.filePath, csvReader.join("\n"));
}
else {
throw new SDKException(Constants.TOKEN_STORE, Constants.GET_TOKEN_BY_ID_FILE_ERROR);
Expand All @@ -161,31 +182,16 @@ class FileStore extends TokenStore {
}

async getTokens() {
let _this = this;
try {
var tokens = [];
var csvReader = fs.readFileSync(this.filePath, 'utf-8').toString().split("\n");
for (var i = 1; i < csvReader.length; i++) {
let allContents = csvReader[i];
let nextRecord = allContents.split(",");
let grantToken = this.getData(nextRecord[6]);
let token = new OAuthBuilder().clientId(nextRecord[2]).clientSecret(nextRecord[3]).refreshToken(nextRecord[4]).build();
token.setId(nextRecord[0]);
if (grantToken != null) {
token.setGrantToken(grantToken);
}
let name = await this.getData(nextRecord[1]);
if (name != null) {
token.setUserSignature(new UserSignature(name));
}
else if (name == null) {
token.setUserSignature(null);
}
token.setAccessToken(nextRecord[5]);
token.setExpiresIn(nextRecord[7]);
if (nextRecord[8] != null) {
token.setRedirectURL(nextRecord[8]);
}
tokens.push(token);
let oauthToken = Reflect.construct(OAuthToken, []);
await _this.setMergeData(oauthToken, nextRecord)
tokens.push(oauthToken);
}
return tokens;
}
Expand Down Expand Up @@ -288,7 +294,7 @@ class FileStore extends TokenStore {
if (oauthToken.getAccessToken() == null) {
oauthToken.setAccessToken(await this.getData(nextRecord[5]));
}
if (oauthToken.setGrantToken() == null) {
if (oauthToken.getGrantToken() == null) {
oauthToken.setGrantToken(await this.getData(nextRecord[6]));
}
if (oauthToken.getExpiresIn() == null) {
Expand All @@ -300,6 +306,10 @@ class FileStore extends TokenStore {
if (oauthToken.getRedirectURL() == null) {
oauthToken.setRedirectURL(await this.getData(nextRecord[8]));
}
if (oauthToken.getAPIDomain() == null)
{
oauthToken.setAPIDomain(await this.getData(nextRecord[9]));
}
}

async setToken(oauthToken) {
Expand All @@ -323,6 +333,8 @@ class FileStore extends TokenStore {

data[8] = oauthToken.getRedirectURL();

data[9] = oauthToken.getAPIDomain();

return data;
}
async generateID(allcontents) {
Expand Down

0 comments on commit e0c4d91

Please sign in to comment.