-
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
5 changed files
with
227 additions
and
197 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
const crypto = require('crypto'); | ||
|
||
async function _translate(word, systemLang) { | ||
return new Promise(async (resolve) => { | ||
const translations = require(`../admin/i18n/${systemLang ? systemLang : 'en'}/translations.json`); | ||
|
||
if (translations[word]) { | ||
resolve(translations[word]); | ||
} else { | ||
console.warn(`Please translate in translations.json: ${word}`); | ||
resolve(word); | ||
} | ||
}); | ||
} | ||
|
||
async function generateRandomToken(length) { | ||
return new Promise(async (resolve) => { | ||
const randomToken = crypto.randomBytes(length).toString('base64url'); | ||
resolve(randomToken); | ||
}); | ||
} | ||
|
||
async function computeCodeChallenge(token) { | ||
return new Promise(async (resolve) => { | ||
const hash = crypto.createHash('sha256'); | ||
hash.update(token); | ||
const digest = hash.digest(); | ||
|
||
const base64Url = digest.toString('base64') | ||
.replace(/\+/g, '-') | ||
.replace(/\//g, '_') | ||
.replace(/=+$/, ''); | ||
|
||
resolve(base64Url); | ||
}); | ||
} | ||
|
||
async function randomBytes(length) { | ||
return new Promise(async (resolve) => { | ||
const _randomBytes = crypto.randomBytes(length).toString('base64url'); | ||
resolve(_randomBytes); | ||
}); | ||
} | ||
|
||
module.exports = { | ||
_translate, | ||
generateRandomToken, | ||
computeCodeChallenge, | ||
randomBytes, | ||
}; |
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
Oops, something went wrong.