oauth.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. var assert = require("assert");
  2. var oauth = require("../oauth");
  3. var querystring = require("../util/querystring");
  4. exports.testGeneratingSignatureBaseString = function(){
  5. // base string described in <http://oauth.net/core/1.0/#sig_base_example>
  6. var client = new oauth.Client;
  7. var result = client._createSignatureBase("GET", "http://photos.example.net/photos",
  8. "file=vacation.jpg&oauth_consumer_key=dpf43f3p2l4k3l03&oauth_nonce=kllo9940pd9333jh&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1191242096&oauth_token=nnch734d00sl2jdk&oauth_version=1.0&size=original");
  9. assert.equal(result, "GET&http%3A%2F%2Fphotos.example.net%2Fphotos&file%3Dvacation.jpg%26oauth_consumer_key%3Ddpf43f3p2l4k3l03%26oauth_nonce%3Dkllo9940pd9333jh%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1191242096%26oauth_token%3Dnnch734d00sl2jdk%26oauth_version%3D1.0%26size%3Doriginal");
  10. };
  11. exports.testNormalizingUrl = function(){
  12. var client = new oauth.Client;
  13. // default ports should be stripped
  14. assert.equal(client._normalizeUrl({ protocol: "https:", hostname: "somehost.com", port: "443", pathname: "/foo/bar" }), "https://somehost.com/foo/bar");
  15. assert.equal(client._normalizeUrl({ protocol: "http:", hostname: "somehost.com", port: "80", pathname: "/foo/bar" }), "http://somehost.com/foo/bar");
  16. // should leave non-default ports from URLs for use in signature generation
  17. assert.equal(client._normalizeUrl({ protocol: "https:", hostname: "somehost.com", port: "446", pathname: "/foo/bar" }), "https://somehost.com:446/foo/bar");
  18. assert.equal(client._normalizeUrl({ protocol: "http:", hostname: "somehost.com", port: "81", pathname: "/foo/bar" }), "http://somehost.com:81/foo/bar");
  19. };
  20. exports.testNormalizingRequestParams = function(){
  21. var client = new oauth.Client;
  22. // ordered by name
  23. assert.equal(client._normalizeParams(["z", "a", "a", "b", "1", "c"]), "1=c&a=b&z=a");
  24. // if two parameter names are the same then order by the value
  25. assert.equal(client._normalizeParams(["z", "b", "z", "a", "1", "c"]), "1=c&z=a&z=b");
  26. // resulting parameters should be encoded and ordered as per <http://tools.ietf.org/html/rfc5849#section-3.1> (3.4.1.3.2)
  27. var requestParams = [];
  28. querystring.parseToArray(requestParams, querystring.stringify({
  29. "b5" : "=%3D",
  30. "c@": "",
  31. "a2": "r b",
  32. "oauth_consumer_key": "9djdj82h48djs9d2",
  33. "oauth_token":"kkk9d7dh3k39sjv7",
  34. "oauth_signature_method": "HMAC-SHA1",
  35. "oauth_timestamp": "137131201",
  36. "oauth_nonce": "7d8f3e4a",
  37. "c2" : ""
  38. }));
  39. querystring.addToArray(requestParams, "a3", "a");
  40. querystring.addToArray(requestParams, "a3", "2 q");
  41. assert.equal(client._normalizeParams(requestParams), "a2=r%20b&a3=2%20q&a3=a&b5=%3D%253D&c%40=&c2=&oauth_consumer_key=9djdj82h48djs9d2&oauth_nonce=7d8f3e4a&oauth_signature_method=HMAC-SHA1&oauth_timestamp=137131201&oauth_token=kkk9d7dh3k39sjv7");
  42. };
  43. function generateClient() {
  44. var client = new oauth.Client("consumerkey", "consumersecret", null, null, null, "1.0", null, function(){ return "ybHPeOEkAUJ3k2wJT9Xb43MjtSgTvKqp"; });
  45. oauth.Client.getTimestamp = function(){ return "1272399856"; };
  46. return client;
  47. }
  48. exports.testSigningUrl = {
  49. "test without token": function(){
  50. var client = generateClient();
  51. // provide a valid signature when no token is present
  52. var requestParams = ["bar", "foo"];
  53. var oauthParams = client._collectOAuthParams({}, requestParams);
  54. var params = client._normalizeParams(requestParams);
  55. var baseString = client._createSignatureBase("GET", "http://somehost.com:3323/foo/poop", params);
  56. var signature = client._createSignature(baseString);
  57. assert.equal(signature, "7ytO8vPSLut2GzHjU9pn1SV9xjc=");
  58. },
  59. "test with token": function(){
  60. var client = generateClient();
  61. // provide a valid signature when a token is present
  62. var bound = client.bind("token", "");
  63. var requestParams = ["bar", "foo"];
  64. var oauthParams = bound._collectOAuthParams({}, requestParams);
  65. var params = bound._normalizeParams(requestParams);
  66. var baseString = bound._createSignatureBase("GET", "http://somehost.com:3323/foo/poop", params);
  67. var signature = bound._createSignature(baseString);
  68. assert.equal(oauthParams.oauth_token, "token");
  69. assert.equal(signature, "9LwCuCWw5sURtpMroIolU3YwsdI=");
  70. },
  71. "test with token and secret": function(){
  72. var client = generateClient();
  73. // provide a valid signature when a token and a token secret are present
  74. var bound = client.bind("token", "tokensecret");
  75. var requestParams = ["bar", "foo"];
  76. var oauthParams = bound._collectOAuthParams({}, requestParams);
  77. var params = bound._normalizeParams(requestParams);
  78. var baseString = bound._createSignatureBase("GET", "http://somehost.com:3323/foo/poop", params);
  79. var signature = bound._createSignature(baseString);
  80. assert.equal(signature, "zeOR0Wsm6EG6XSg0Vw/sbpoSib8=");
  81. }
  82. };
  83. exports.testBuildingAuthHeader = function(){
  84. var client = generateClient();
  85. // all provided OAuth arguments should be concatenated correctly
  86. var request = client._signRequest({
  87. method: "GET",
  88. protocol: "http:",
  89. hostname: "somehost.com",
  90. port: "3323",
  91. pathname: "/foo/poop",
  92. headers: {}
  93. }, ["bar", "foo"]);
  94. assert.equal(request.headers.authorization, 'OAuth oauth_consumer_key="consumerkey",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1272399856",oauth_nonce="ybHPeOEkAUJ3k2wJT9Xb43MjtSgTvKqp",oauth_version="1.0",oauth_signature="7ytO8vPSLut2GzHjU9pn1SV9xjc%3D"');
  95. };
  96. if (require.main === module)
  97. require("patr/runner").run(exports);