Comparing version 1.0.48 to 1.0.49
380
coaclient.js
@@ -27,73 +27,101 @@ const csvWriter = require('csv-write-stream'); | ||
function CoaclientAPI() {} | ||
class CoaclientAPI { | ||
/** | ||
* Get refresh, access tokens and expired time by client name from local cache token file | ||
* | ||
* @param clientName Name of client | ||
* @returns {Promise<any>} Refresh, access tokens and expired_in time | ||
*/ | ||
static getAuthTokens(clientName) { | ||
return new Promise(function (resolve, reject) { | ||
let inputStream = fs.createReadStream(CACHE_DIR_PATH + clientName + AUTH_FILE_SUFFIX, ENCODING_UTF8); | ||
inputStream | ||
.pipe(csvParser()) | ||
.on('data', function (row) { | ||
resolve(row) | ||
}) | ||
.on('end', function (data) { | ||
reject("Tokens not found in file: " + CACHE_DIR_PATH + clientName + AUTH_FILE_SUFFIX) | ||
}); | ||
inputStream.on('error', function(err) { | ||
reject("Error reading config file: " + err); | ||
}); | ||
}) | ||
}; | ||
/** | ||
* Get access token by client name from local cache token file. | ||
* If lifetime of access token is expired then it will refresh and save new tokens. | ||
* | ||
* @param clientName Name of client | ||
* @returns {Promise<any>} Access token | ||
*/ | ||
CoaclientAPI.prototype.getAccessToken = function(clientName) { | ||
return new Promise(function (resolve, reject) { | ||
let inputStream = fs.createReadStream(CACHE_DIR_PATH + clientName + AUTH_FILE_SUFFIX, ENCODING_UTF8); | ||
inputStream | ||
.pipe(csvParser()) | ||
.on('data', function (authTokens) { | ||
if (Number(authTokens.expiredIn) < new Date().getTime()) { | ||
refreshAuthTokens(authTokens, clientName).then(function (accessToken) { | ||
resolve(accessToken); | ||
}).catch(function (error) { | ||
console.log(error); | ||
}) | ||
} else { | ||
resolve(authTokens.accessToken) | ||
} | ||
}) | ||
.on('end', function (data) { | ||
/** | ||
* Get access token by client name from local cache token file. | ||
* If lifetime of access token is expired then it will refresh and save new tokens. | ||
* | ||
* @param clientName Name of client | ||
* @returns {Promise<any>} Access token | ||
*/ | ||
static getAccessToken(clientName) { | ||
return new Promise(function (resolve, reject) { | ||
let inputStream = fs.createReadStream(CACHE_DIR_PATH + clientName + AUTH_FILE_SUFFIX, ENCODING_UTF8); | ||
inputStream | ||
.pipe(csvParser()) | ||
.on('data', function (authTokens) { | ||
if (Number(authTokens.expiredIn) < new Date().getTime()) { | ||
refreshAuthTokens(authTokens, clientName).then(function (accessToken) { | ||
resolve(accessToken); | ||
}).catch(function (error) { | ||
console.log(error); | ||
}) | ||
} else { | ||
resolve(authTokens.accessToken) | ||
} | ||
}) | ||
.on('end', function (data) { | ||
}); | ||
inputStream.on('error', function(err) { | ||
reject("Error reading auth tokens file, try to generate new one: " + err); | ||
}); | ||
inputStream.on('error', function(err) { | ||
reject("Error reading auth tokens file, try to generate new one: " + err); | ||
}); | ||
}); | ||
}; | ||
}; | ||
/** | ||
* Get refresh, access tokens and expired time by client name from local cache token file | ||
* | ||
* @param clientName Name of client | ||
* @returns {Promise<any>} Refresh, access tokens and expired_in time | ||
*/ | ||
CoaclientAPI.prototype.getAuthTokens = function(clientName) { | ||
return new Promise(function (resolve, reject) { | ||
let inputStream = fs.createReadStream(CACHE_DIR_PATH + clientName + AUTH_FILE_SUFFIX, ENCODING_UTF8); | ||
inputStream | ||
.pipe(csvParser()) | ||
.on('data', function (row) { | ||
resolve(row) | ||
}) | ||
.on('end', function (data) { | ||
reject("Tokens not found in file: " + CACHE_DIR_PATH + clientName + AUTH_FILE_SUFFIX) | ||
/** | ||
* Get client config with full information by client name | ||
* or clientId from local cache config file. | ||
* | ||
* @param clientIdentifier Name of client or client ID | ||
* @returns {Promise<any>} Client config object | ||
*/ | ||
static getClientConfig(clientIdentifier) { | ||
return new Promise(function (resolve, reject) { | ||
let inputStream = fs.createReadStream(CACHE_DIR_PATH + CONFIG_FILE_NAME, ENCODING_UTF8); | ||
inputStream | ||
.pipe(csvParser()) | ||
.on('data', function (row) { | ||
if (row.name === clientIdentifier || row.clientId === clientIdentifier) { | ||
var clientConfig = { | ||
'name': row.name, | ||
'clientId': row.clientId, | ||
'secretKey': row.secretKey, | ||
'scope': row.scope | ||
}; | ||
resolve(clientConfig); | ||
} | ||
}).on('end', function (data) { | ||
reject("Client " + clientIdentifier + " not found.") | ||
}); | ||
inputStream.on('error', function(err) { | ||
reject("Error reading config file: " + err); | ||
}); | ||
}) | ||
}; | ||
inputStream.on('error', function(err) { | ||
reject("Error reading config file: " + err); | ||
}); | ||
}) | ||
}; | ||
/** | ||
* Get client config with full information by client name | ||
* or clientId from local cache config file. | ||
* | ||
* @param clientIdentifier Name of client or client ID | ||
* @returns {Promise<any>} Client config object | ||
*/ | ||
CoaclientAPI.prototype.getClientConfig = function(clientIdentifier) { | ||
return new Promise(function (resolve, reject) { | ||
let inputStream = fs.createReadStream(CACHE_DIR_PATH + CONFIG_FILE_NAME, ENCODING_UTF8); | ||
inputStream | ||
.pipe(csvParser()) | ||
.on('data', function (row) { | ||
if (row.name === clientIdentifier || row.clientId === clientIdentifier) { | ||
var clientConfig = { | ||
/** | ||
* Get list of client config from local cache config dir. | ||
* | ||
* @returns {Promise<any>} List of client config ojects | ||
*/ | ||
static getClientConfigs() { | ||
return new Promise(function (resolve, reject) { | ||
let listOfClientConfig = []; | ||
let inputStream = fs.createReadStream(CACHE_DIR_PATH + CONFIG_FILE_NAME, ENCODING_UTF8); | ||
inputStream | ||
.pipe(csvParser()) | ||
.on('data', function (row) { | ||
let clientConfig = { | ||
'name': row.name, | ||
@@ -104,136 +132,108 @@ 'clientId': row.clientId, | ||
}; | ||
resolve(clientConfig); | ||
} | ||
}).on('end', function (data) { | ||
reject("Client " + clientIdentifier + " not found.") | ||
listOfClientConfig.push(clientConfig); | ||
resolve(listOfClientConfig); | ||
}).on('end', function (data) { | ||
reject("Config file is empty: " + CACHE_DIR_PATH + CONFIG_FILE_NAME); | ||
}); | ||
inputStream.on('error', function(err) { | ||
reject("Error reading config file: " + err); | ||
}); | ||
}); | ||
inputStream.on('error', function(err) { | ||
reject("Error reading config file: " + err); | ||
}); | ||
}) | ||
}; | ||
}; | ||
/** | ||
* Get list of client config from local cache config dir. | ||
* | ||
* @returns {Promise<any>} List of client config ojects | ||
*/ | ||
CoaclientAPI.prototype.getClientConfigs = function() { | ||
return new Promise(function (resolve, reject) { | ||
let listOfClientConfig = []; | ||
let inputStream = fs.createReadStream(CACHE_DIR_PATH + CONFIG_FILE_NAME, ENCODING_UTF8); | ||
inputStream | ||
.pipe(csvParser()) | ||
.on('data', function (row) { | ||
let clientConfig = { | ||
'name': row.name, | ||
'clientId': row.clientId, | ||
'secretKey': row.secretKey, | ||
'scope': row.scope | ||
}; | ||
listOfClientConfig.push(clientConfig); | ||
resolve(listOfClientConfig); | ||
}).on('end', function (data) { | ||
reject("Config file is empty: " + CACHE_DIR_PATH + CONFIG_FILE_NAME); | ||
}); | ||
inputStream.on('error', function(err) { | ||
reject("Error reading config file: " + err); | ||
}); | ||
}); | ||
}; | ||
/** | ||
* Add client config to local cache config file | ||
* | ||
* @param clientName Name of client | ||
* @param clientId Client ID | ||
* @param clientSecret Client Secret Key | ||
* @param scope Scope of access (by default used 'view_profile') | ||
*/ | ||
static addClientConfig(clientName, clientId, clientSecret, scope) { | ||
var self = this; | ||
if (clientName === '' || clientId === '' || clientSecret === '') { | ||
console.log("Parameters can't be empty."); | ||
} else if (scope !== '' && | ||
scope !== SCOPE_VIEW_PROFILE && | ||
scope !== SCOPE_VIEW_PROFILE + "," + SCOPE_ACCESS_BUSINESS && | ||
scope !== SCOPE_ACCESS_BUSINESS) { | ||
console.log("Scope is invalid: " + scope + ". " + | ||
"Available scopes are 'view_profile' or 'access_business_api'") | ||
} else { | ||
self.getClientConfig(clientName).then(function (clientConfig) { | ||
console.log("Client with name: " + clientConfig.name + " already exist"); | ||
}).catch(function (error) { | ||
self.getClientConfig(clientId).then(function (clientConfig) { | ||
console.log("Client with id: " + clientConfig.clientId + " already exist"); | ||
}).catch(function (error) { | ||
saveClientToCSVFile(clientName, clientId, clientSecret, scope); | ||
}); | ||
}); | ||
} | ||
}; | ||
/** | ||
* Add client config to local cache config file | ||
* | ||
* @param clientName Name of client | ||
* @param clientId Client ID | ||
* @param clientSecret Client Secret Key | ||
* @param scope Scope of access (by default used 'view_profile') | ||
*/ | ||
CoaclientAPI.prototype.addClientConfig = function(clientName, clientId, clientSecret, scope) { | ||
var self = this; | ||
if (clientName === '' || clientId === '' || clientSecret === '') { | ||
console.log("Parameters can't be empty."); | ||
} else if (scope !== '' && | ||
scope !== SCOPE_VIEW_PROFILE && | ||
scope !== SCOPE_VIEW_PROFILE + "," + SCOPE_ACCESS_BUSINESS && | ||
scope !== SCOPE_ACCESS_BUSINESS) { | ||
console.log("Scope is invalid: " + scope + ". " + | ||
"Available scopes are 'view_profile' or 'access_business_api'") | ||
} else { | ||
/** | ||
* Start server callback listener, generate new tokens and save them to local cache token file. | ||
* | ||
* @param clientName Name of client | ||
*/ | ||
static generateAuthTokens(clientName) { | ||
var self = this; | ||
self.getClientConfig(clientName).then(function (clientConfig) { | ||
console.log("Client with name: " + clientConfig.name + " already exist"); | ||
const courseraCodeURI = util.format( | ||
COURSERA_CODE_URI, | ||
clientConfig.scope, | ||
COURSERA_CALLBACK_URI + clientConfig.clientId, | ||
clientConfig.clientId); | ||
startServerCallbackListener(); | ||
(async () => { | ||
await open(courseraCodeURI); | ||
})(); | ||
}).catch(function (error) { | ||
self.getClientConfig(clientId).then(function (clientConfig) { | ||
console.log("Client with id: " + clientConfig.clientId + " already exist"); | ||
}).catch(function (error) { | ||
saveClientToCSVFile(clientName, clientId, clientSecret, scope); | ||
}); | ||
console.log(error); | ||
}); | ||
} | ||
}; | ||
}; | ||
/** | ||
* Start server callback listener, generate new tokens and save them to local cache token file. | ||
* | ||
* @param clientName Name of client | ||
*/ | ||
CoaclientAPI.prototype.generateAuthTokens = function(clientName) { | ||
var self = this; | ||
self.getClientConfig(clientName).then(function (clientConfig) { | ||
const courseraCodeURI = util.format( | ||
COURSERA_CODE_URI, | ||
clientConfig.scope, | ||
COURSERA_CALLBACK_URI + clientConfig.clientId, | ||
clientConfig.clientId); | ||
startServerCallbackListener(); | ||
(async () => { | ||
await open(courseraCodeURI); | ||
})(); | ||
}).catch(function (error) { | ||
console.log(error); | ||
}); | ||
}; | ||
/** | ||
* Delete client config by client name from local cache config file | ||
* | ||
* @param clientName Name of client | ||
*/ | ||
CoaclientAPI.prototype.deleteClientConfig = function(clientName) { | ||
var self = this; | ||
self.getClientConfig(clientName).then(function (result) { | ||
self.getClientConfigs().then(function (clients) { | ||
const arr = clients.filter(client => { | ||
return client.name !== clientName; | ||
}); | ||
let writer; | ||
if (!fs.existsSync(CACHE_DIR_PATH)) { | ||
fs.mkdirSync(CACHE_DIR_PATH); | ||
} | ||
if (fs.existsSync(CACHE_DIR_PATH + CONFIG_FILE_NAME)) { | ||
fs.unlinkSync(CACHE_DIR_PATH + CONFIG_FILE_NAME) | ||
} | ||
writer = csvWriter({headers: ["name", "clientId", "secretKey", "scope"]}); | ||
writer.pipe(fs.createWriteStream(CACHE_DIR_PATH + CONFIG_FILE_NAME, {flags: 'a'})); | ||
arr.forEach(config => { | ||
writer.write({ | ||
name: config.name, | ||
clientId: config.clientId, | ||
secretKey: config.secretKey, | ||
scope: config.scope | ||
/** | ||
* Delete client config by client name from local cache config file | ||
* | ||
* @param clientName Name of client | ||
*/ | ||
static deleteClientConfig(clientName) { | ||
var self = this; | ||
self.getClientConfig(clientName).then(function (result) { | ||
self.getClientConfigs().then(function (clients) { | ||
const arr = clients.filter(client => { | ||
return client.name !== clientName; | ||
}); | ||
}); | ||
writer.end(); | ||
console.log("Client " + clientName + " successfully deleted"); | ||
let writer; | ||
if (!fs.existsSync(CACHE_DIR_PATH)) { | ||
fs.mkdirSync(CACHE_DIR_PATH); | ||
} | ||
if (fs.existsSync(CACHE_DIR_PATH + CONFIG_FILE_NAME)) { | ||
fs.unlinkSync(CACHE_DIR_PATH + CONFIG_FILE_NAME) | ||
} | ||
writer = csvWriter({headers: ["name", "clientId", "secretKey", "scope"]}); | ||
writer.pipe(fs.createWriteStream(CACHE_DIR_PATH + CONFIG_FILE_NAME, {flags: 'a'})); | ||
arr.forEach(config => { | ||
writer.write({ | ||
name: config.name, | ||
clientId: config.clientId, | ||
secretKey: config.secretKey, | ||
scope: config.scope | ||
}); | ||
}); | ||
writer.end(); | ||
console.log("Client " + clientName + " successfully deleted"); | ||
}).catch(function (error) { | ||
console.log(error) | ||
}) | ||
}).catch(function (error) { | ||
console.log(error) | ||
console.log(error); | ||
}) | ||
}).catch(function (error) { | ||
console.log(error); | ||
}) | ||
}; | ||
}; | ||
} | ||
function sendAuthTokensRequest(clientId, courseraCode) { | ||
CoaclientAPI.prototype.getClientConfig(clientId).then(function (config) { | ||
CoaclientAPI.getClientConfig(clientId).then(function (config) { | ||
const form = { | ||
@@ -247,5 +247,2 @@ 'client_id': config.clientId, | ||
}; | ||
console.log(config) | ||
console.log(courseraCode) | ||
console.log(COURSERA_AUTH_TOKEN_URI) | ||
const headers = { | ||
@@ -257,3 +254,2 @@ 'Content-Type': CONTENT_TYPE_FORM | ||
const params = JSON.parse(body); | ||
console.log(params) | ||
if (params.refresh_token === undefined) { | ||
@@ -337,3 +333,3 @@ console.log("Error send authentication tokens request: " + params.msg); | ||
return new Promise(function (resolve, reject) { | ||
CoaclientAPI.prototype.getClientConfig(clientName).then(function (config) { | ||
CoaclientAPI.getClientConfig(clientName).then(function (config) { | ||
const form = { | ||
@@ -340,0 +336,0 @@ 'client_id': config.clientId, |
{ | ||
"name": "coaclient", | ||
"version": "1.0.48", | ||
"version": "1.0.49", | ||
"description": "Execute Node.js library for manage CourseraOAuth2 API tokens", | ||
@@ -5,0 +5,0 @@ "main": "coaclient.js", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
28430
339