-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: export model endpoint (#213)
* Adds export model endpoint This commit adds the necessary structure to export model Resolves #75 * Adds import model endpoint This commit adds import model endpoint for tests Resolves #76 * Adds crypto unit test Refs #75 #76 * Adds export model unit test Resolves #75
- Loading branch information
Showing
9 changed files
with
217 additions
and
6 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
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,34 @@ | ||
const crypto = require("crypto"); | ||
|
||
const ALGORITHM = "aes-256-ctr"; | ||
const ENCRYPTION_KEY = process.env.SECRET_TOKEN; | ||
const IV_LENGTH = 16; | ||
|
||
const encrypt = (data) => { | ||
const key = crypto | ||
.createHash("sha256") | ||
.update(String(ENCRYPTION_KEY)) | ||
.digest("base64") | ||
.substr(0, 32); | ||
const iv = crypto.randomBytes(IV_LENGTH); | ||
const cipher = crypto.createCipheriv(ALGORITHM, Buffer.from(key), iv); | ||
return Buffer.concat([iv, cipher.update(data), cipher.final()]); | ||
}; | ||
|
||
const decrypt = (hash) => { | ||
const key = crypto | ||
.createHash("sha256") | ||
.update(String(ENCRYPTION_KEY)) | ||
.digest("base64") | ||
.substr(0, 32); | ||
const iv = hash.slice(0, IV_LENGTH); | ||
const decipher = crypto.createDecipheriv(ALGORITHM, Buffer.from(key), iv); | ||
const data = hash.slice(IV_LENGTH); | ||
const decrypted = Buffer.concat([decipher.update(data), decipher.final()]); | ||
return decrypted.toString(); | ||
}; | ||
|
||
module.exports = { | ||
encrypt, | ||
decrypt, | ||
}; |
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,35 @@ | ||
const { encrypt, decrypt } = require("./crypto"); | ||
|
||
describe("crypto test", () => { | ||
const OLD_ENV = process.env; | ||
|
||
beforeEach(() => { | ||
jest.resetModules(); | ||
process.env = { ...OLD_ENV }; | ||
}); | ||
|
||
afterAll(() => { | ||
process.env = OLD_ENV; | ||
}); | ||
|
||
test("Encrypt data", async () => { | ||
process.env.SECRET_TOKEN = "some_token"; | ||
const data = { name: "some_name" }; | ||
const json = JSON.stringify(data); | ||
|
||
const encrypted = encrypt(json); | ||
|
||
expect(encrypted).not.toBeNull(); | ||
}); | ||
|
||
test("Decrypt data", async () => { | ||
process.env.SECRET_TOKEN = "some_token"; | ||
const data = { name: "some_name" }; | ||
const json = JSON.stringify(data); | ||
|
||
const decrypted = decrypt(encrypt(json)); | ||
|
||
expect(decrypted).not.toBeNull(); | ||
expect(JSON.parse(decrypted)).toEqual(data); | ||
}); | ||
}); |
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,22 @@ | ||
const getRawBody = require("raw-body"); | ||
|
||
const uploadMiddleware = (req, res, next) => { | ||
if (req.headers["content-type"] === "application/octet-stream") { | ||
const options = { | ||
length: req.headers["content-length"], | ||
encoding: req.charset, | ||
}; | ||
const callback = (err, string) => { | ||
if (err) return next(err); | ||
req.body = string; | ||
return next(); | ||
}; | ||
getRawBody(req, options, callback); | ||
} else { | ||
next(); | ||
} | ||
}; | ||
|
||
module.exports = { | ||
uploadMiddleware, | ||
}; |
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