-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #578 from alemayhu/fix/hash-tokens
fix: repair the broken down migration
- Loading branch information
Showing
11 changed files
with
126 additions
and
18 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,8 @@ | ||
import CryptoJS from "crypto-js"; | ||
|
||
export default function hashToken(token: string): string { | ||
return CryptoJS.AES.encrypt( | ||
token, | ||
process.env.THE_HASHING_SECRET! | ||
).toString(); | ||
} |
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,7 @@ | ||
import CryptoJS from "crypto-js"; | ||
|
||
export default function unHashToken(hashed: string): string { | ||
return CryptoJS.AES.decrypt(hashed, process.env.THE_HASHING_SECRET!).toString( | ||
CryptoJS.enc.Utf8 | ||
); | ||
} |
11 changes: 11 additions & 0 deletions
11
server/migrations/20220410100114_add-encrypted-field-notion-tokens.js
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,11 @@ | ||
module.exports.up = (knex) => { | ||
return knex.schema.table("notion_tokens", (table) => { | ||
table.boolean("encrypted").defaultTo(false); | ||
}); | ||
}; | ||
|
||
module.exports.down = (knex) => { | ||
return knex.schema.table("notion_tokens", (table) => { | ||
table.dropColumn("encrypted"); | ||
}); | ||
}; |
57 changes: 57 additions & 0 deletions
57
server/migrations/20220410101019_hash-all-notion-tokens.js
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,57 @@ | ||
const CryptoJS = require("crypto-js"); | ||
|
||
function unHashToken(token) { | ||
return CryptoJS.AES.decrypt(token, process.env.THE_HASHING_SECRET).toString( | ||
CryptoJS.enc.Utf8 | ||
); | ||
} | ||
|
||
function hashToken(token) { | ||
return CryptoJS.AES.encrypt(token, process.env.THE_HASHING_SECRET).toString(); | ||
} | ||
|
||
module.exports.up = (knex) => { | ||
return knex | ||
.select() | ||
.from("notion_tokens") | ||
.then((users) => { | ||
return knex.transaction((trx) => { | ||
return knex.schema | ||
.table("notion_tokens", () => | ||
Promise.all( | ||
users.map((row) => { | ||
return knex("notion_tokens") | ||
.update({ token: hashToken(row.token), encrypted: true }) | ||
.where({ owner: row.owner, encrypted: false }) | ||
.transacting(trx); | ||
}) | ||
) | ||
) | ||
.then(trx.commit) | ||
.catch(trx.rollback); | ||
}); | ||
}); | ||
}; | ||
|
||
module.exports.down = (knex) => { | ||
return knex | ||
.select() | ||
.from("notion_tokens") | ||
.then((users) => { | ||
return knex.transaction((trx) => { | ||
return knex.schema | ||
.table("notion_tokens", () => | ||
Promise.all( | ||
users.map((row) => { | ||
return knex("notion_tokens") | ||
.update({ token: unHashToken(row.token), encrypted: false }) | ||
.where({ owner: row.owner, encrypted: true }) | ||
.transacting(trx); | ||
}) | ||
) | ||
) | ||
.then(trx.commit) | ||
.catch(trx.rollback); | ||
}); | ||
}); | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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