Skip to content

Instantly share code, notes, and snippets.

@derrick-branch
Created August 17, 2016 01:36
Show Gist options
  • Save derrick-branch/aa7696a7a7a0aed5956fef6c030bd2c6 to your computer and use it in GitHub Desktop.
Save derrick-branch/aa7696a7a7a0aed5956fef6c030bd2c6 to your computer and use it in GitHub Desktop.
var crypto = require('crypto');
module.exports = function(original_url, branch_base_url, branch_hmac_secret, three_p_url) {
if (!original_url) { return new Error('Missing original_url'); }
if (typeof original_url != 'string') { return new Error('Invalid original_url'); }
if (!branch_base_url) { return new Error('Missing branch_base_url, should be similar to https://bnc.lt/abcd/3p?%243p=xx'); }
if (typeof branch_base_url != 'string') { return new Error('Invalid branch_base_url'); }
if (!branch_hmac_secret) { return new Error('Missing branch_hmac_secret'); }
if (typeof branch_hmac_secret != 'string') { return new Error('Invalid branch_hmac_secret'); }
if (three_p_url && typeof three_p_url != 'string') { return new Error('Invalid three_p_url'); }
var pre_hmac_url = branch_base_url + (three_p_url ? '&%243p_url=' + encodeURIComponent(three_p_url) : '') + '&%24original_url=' + encodeURIComponent(original_url),
hmac = crypto.createHmac('sha256', branch_hmac_secret).update(pre_hmac_url).digest('hex');
return pre_hmac_url + '&%24hmac=' + hmac;
};
// Tests
// console.log('Test with 3P URL:', module.exports('http://originalurl.com/?utm=y', 'http://dev.bnc.lt/tjgc/3p?%243p=np', 'SECRET HERE', 'http://example.com/this-is-an-3p-url?foo=bar'));
// console.log('Test without 3P URL:', module.exports('http://originalurl.com/?utm=y', 'http://dev.bnc.lt/tjgc/3p?%243p=np', 'SECRET HERE', null));
import hmac
import hashlib
import urllib
safe_chars = '~()*!.\''
def getBranchLink(original_url, branch_base_url, branch_hmac_secret, three_p_url):
if original_url is None:
raise Exception('Missing original_url')
if type(original_url) is not str:
raise Exception('Invalid original_url')
if branch_base_url is None:
raise Exception('Missing branch_base_url, should be similar to https://bnc.lt/abcd/3p?3p=xx')
if type(branch_base_url) is not str:
raise Exception('Invalid branch_base_url')
if branch_hmac_secret is None:
raise Exception('Missing branch_hmac_secret')
if type(branch_hmac_secret) is not str:
raise Exception('Invalid branch_hmac_secret')
if three_p_url and type(three_p_url) is not str:
raise Exception('Invalid three_p_url')
pre_hmac_url = branch_base_url;
if three_p_url is not None:
pre_hmac_url += '&%243p_url=' + urllib.quote(three_p_url.encode("utf-8"), safe=safe_chars)
pre_hmac_url += '&%24original_url=' + urllib.quote(original_url.encode("utf-8"), safe=safe_chars)
hmac_for_url = hmac.new(branch_hmac_secret, pre_hmac_url, hashlib.sha256).hexdigest()
return pre_hmac_url + '&%24hmac=' + hmac_for_url
def main():
## Tests
## print 'Test with 3P URL: ' + getBranchLink('http://originalurl.com/?utm=y', 'http://dev.bnc.lt/tjgc/3p?%243p=np', 'SECRET HERE', 'http://example.com/this-is-an-3p-url?foo=bar');
## print 'Test without 3P URL: ' + getBranchLink('http://originalurl.com/?utm=y', 'http://dev.bnc.lt/tjgc/3p?%243p=np', 'SECRET HERE', None);
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment