-
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
8 changed files
with
1,192 additions
and
95 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,23 @@ | ||
const { ECToken, encrypt, decrypt } = require('./index.js'); | ||
// Alternatively: | ||
// const { V3 } = require('./index.js') | ||
|
||
(async () => { | ||
// Create the token. | ||
const ec_token = new ECToken() | ||
ec_token.addValue('ec_country_allow', 'US') | ||
ec_token.addValue('ec_country_allow', 'CA') | ||
ec_token.addValue('ec_expire', (Date.now() / 1000) + (60 * 60 * 24)) | ||
|
||
// Encrypt and encode it. | ||
const ec_token_str = await encrypt('my-secret-key', ec_token) | ||
console.log(`Encoded: ${ec_token_str}`) | ||
|
||
console.log(' ') | ||
|
||
// Now decrypt it back to plaintext. | ||
const plaintext = await decrypt('my-secret-key', ec_token_str) | ||
console.log(`Plaintext: ${plaintext}`) | ||
|
||
process.exit() | ||
})(); |
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 |
---|---|---|
@@ -1,21 +1,10 @@ | ||
const { ECToken, encrypt, decrypt } = require('./ECToken.js'); | ||
'use strict'; | ||
const { V3, encrypt, decrypt } = require('./lib/crypto') | ||
const { ECToken } = require('./lib/ECToken') | ||
|
||
(async () => { | ||
// Create the token. | ||
const ec_token = new ECToken() | ||
ec_token.addValue('ec_country_allow', 'US') | ||
ec_token.addValue('ec_country_allow', 'CA') | ||
ec_token.addValue('ec_expire', (Date.now() / 1000) + (60 * 60 * 24)) | ||
|
||
// Encrypt and encode it. | ||
const ec_token_str = await encrypt('my-secret-key', ec_token) | ||
console.log(`Encoded: ${ec_token_str}`) | ||
|
||
console.log(' ') | ||
|
||
// Now decrypt it back to plaintext. | ||
const plaintext = await decrypt('my-secret-key', ec_token_str) | ||
console.log(`Plaintext: ${plaintext}`) | ||
|
||
process.exit() | ||
})() | ||
module.exports = { | ||
V3, | ||
ECToken, | ||
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,73 @@ | ||
/** | ||
* A class to manage Edgecast Token creation. | ||
*/ | ||
class ECToken { | ||
constructor() { | ||
// Set default values for all valid token fields. | ||
this.values = { | ||
ec_expire: 0, | ||
ec_country_allow: [], | ||
ec_country_deny: [], | ||
ec_url_allow: [], | ||
ec_host_allow: [], | ||
ec_host_deny: [], | ||
ec_ref_allow: [], | ||
ec_ref_deny: [], | ||
ec_clientip: [], | ||
ec_proto_allow: [], | ||
ec_proto_deny: [] | ||
} | ||
} | ||
|
||
/** | ||
* Set or append a value to the token. | ||
* @param {String} key The key to set or append. | ||
* @param {String} value The value to add. | ||
* @returns None | ||
*/ | ||
addValue(key, value) { | ||
if (!(key in this.values)) { | ||
throw new Error(`Invalid key: ${key}`) | ||
} | ||
|
||
if (key === 'ec_expire') { | ||
this.values.ec_expire = parseInt(value) | ||
return | ||
} | ||
|
||
// All other keys can be multivalue. | ||
if (!this.values[key].includes(value)) { | ||
this.values[key].push(value) | ||
} | ||
} | ||
|
||
/** | ||
* @deprecated since v2.0.0; use toString() instead. | ||
*/ | ||
serialize() { | ||
return this.toString() | ||
} | ||
|
||
/** | ||
* Serialize the token object as a valid EdgeCast Token. | ||
* @returns {String} | ||
*/ | ||
toString() { | ||
let token = Object.keys(this.values).reduce((acc, curr) => { | ||
if (curr === 'ec_expire' && this.values.ec_expire && this.values.ec_expire > 0) { | ||
acc.push(`ec_expire=${this.values.ec_expire}`) | ||
} else { | ||
if (this.values[curr].length) { | ||
acc.push(`${curr}=${this.values[curr].join(',')}`) | ||
} | ||
} | ||
return acc | ||
}, []).join('&') | ||
return token | ||
} | ||
} | ||
|
||
// Export the ECToken helper class. | ||
module.exports = { | ||
ECToken | ||
} |
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.