-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
176 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import httpClient from 'request-promise'; | ||
import { } from 'dotenv/config'; | ||
|
||
/** | ||
* This class contains all the methods responsible for copyleaks authentication | ||
*/ | ||
class Auth { | ||
constructor(email, apiKey) { | ||
this.options = { | ||
method: 'POST', | ||
uri: 'https://api.copyleaks.com/v1/account/login-api', | ||
body: { | ||
Email: email, | ||
ApiKey: apiKey, | ||
}, | ||
json: true, | ||
}; | ||
this.accessToken = {}; | ||
this.login(); | ||
} | ||
|
||
/** | ||
* returns the access token gotten after login | ||
*/ | ||
get accesstoken() { | ||
return this.accessToken.access_token; | ||
} | ||
|
||
/** | ||
* logs into the copyleaks api to get an access token | ||
*/ | ||
login() { | ||
httpClient(this.options) | ||
.then((response) => { | ||
this.accessToken = response; | ||
}) | ||
.catch(error => error); | ||
} | ||
} | ||
|
||
export default new Auth(process.env.COPYLEAKS_EMAIL, process.env.COPYLEAKS_KEY); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import httpClient from 'request-promise'; | ||
import { } from 'dotenv/config'; | ||
|
||
class Scan { | ||
constructor(accessToken) { | ||
this.accessToken = accessToken; | ||
} | ||
|
||
/** | ||
* @description - This method uploads a text of about 6,000 characters to | ||
* copyleaks api for plagiarism checks | ||
* @param {String} scanText - The text to be scanned | ||
*/ | ||
uploadScan(scanText) { | ||
return new Promise((resolve, reject) => { | ||
httpClient({ | ||
uri: 'https://api.copyleaks.com/v1/businesses/create-by-text', | ||
method: 'POST', | ||
headers: { | ||
'content-type': 'application/x-www-form-urlencoded', | ||
authorization: `Bearer ${this.accessToken}`, | ||
}, | ||
body: scanText, | ||
json: true, | ||
}) | ||
.then((response) => { | ||
setTimeout(() => { | ||
resolve(response.ProcessId); | ||
}, 20000); | ||
}) | ||
.catch((error) => { | ||
reject(error); | ||
}); | ||
}); | ||
} | ||
|
||
/** | ||
* @description - Checks the result of a scanning process | ||
* @param {Number} processId - The process id of a scan whose result is to be retrieved | ||
*/ | ||
checkResult(processId) { | ||
return new Promise((resolve, reject) => { | ||
httpClient({ | ||
uri: `https://api.copyleaks.com/v2/businesses/${processId}/result`, | ||
method: 'GET', | ||
headers: { | ||
'content-type': 'application/x-www-form-urlencoded', | ||
authorization: `Bearer ${this.accessToken}`, | ||
}, | ||
json: true, | ||
}) | ||
.then((response) => { | ||
resolve(response); | ||
}) | ||
.catch((error) => { | ||
reject(error); | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
export default Scan; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { } from 'dotenv/config'; | ||
import Auth from '../helpers/copyleaks/Auth'; | ||
import Scanner from '../helpers/copyleaks/Scan'; | ||
import Mailer from '../helpers/Mailer'; | ||
import emails from '../helpers/emailMessages'; | ||
|
||
/** | ||
* Run a plagiarism check on an article if unattributed, | ||
* checking for a >= 50% match. | ||
* @param {*} req the request object | ||
* @param {*} res the response object | ||
* @param {*} next calls the next middleware | ||
*/ | ||
const plagiarismCheck = async (req, res, next) => { | ||
if (process.env.NODE_ENV === 'test') return next(); | ||
if (req.body.isAttributed !== 'true') { | ||
try { | ||
const scanner = new Scanner(Auth.accesstoken); | ||
const { body } = req.body; | ||
const processId = await scanner.uploadScan(body); | ||
const response = await scanner.checkResult(processId); | ||
const foundIn = response.results.filter(occurrence => occurrence.totalMatchedPercents >= 50); | ||
if (foundIn.length >= 1) { | ||
return res.status(451).send({ | ||
status: 'fail', | ||
message: `The article cannot be created because more than 50% of the content was found elsewhere. | ||
Please attribute your sources and check the 'I have attributed all relevant sources' button to continue.` | ||
}); | ||
} | ||
next(); | ||
} catch (err) { | ||
const msg = emails.copyleaksCreditExhaustion(process.env.SITE_ADMIN_EMAIL); | ||
Mailer.sendMail(msg); | ||
return res.status(599).send({ | ||
status: 'error', | ||
message: 'The article cannot be created at the moment, please try again later.', | ||
}); | ||
} | ||
} | ||
next(); | ||
}; | ||
|
||
export default plagiarismCheck; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters