Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

now using more robust url parser

  • Loading branch information...
commit 8f6a7946d7ca5f7b9172ac80bbce0cc544695ef2 1 parent 80f1e46
authored

Showing 1 changed file with 41 additions and 44 deletions. Show diff stats Hide diff stats

  1. 85  jsoauth.js
85  jsoauth.js
... ...
@@ -1,38 +1,13 @@
1  
-// The standard js lib is here: http://oauth.googlecode.com/svn/code/javascript/oauth.js
2  
-// I put this script together as a learning exercise
  1
+// I've found this script easier to debug with than the netflix js lib (http://oauth.googlecode.com/svn/code/javascript/oauth.js), which wasn't working out of the box for me
3 2
 // License: anything not credited to someone else is licensed under Yahoo! BSD: http://gist.github.com/375593
  3
+// Source: http://github.com/erikeldridge/jsoauth
  4
+// Dependencies:
  5
+// - phpjs' parse_url http://phpjs.org/functions/parse_url:485
4 6
 
5 7
 var jsoauth = (function() {
6  
-    
7  
-    //@credit "JavaScript: The Good Stuff", Crockford
8  
-    var parseURL = function(inputURL) {
9  
-
10  
-        var pattern = "^(https|http):\/\/" //scheme
11  
-        + "([0-9a-z\-.]+)" //authority, aka host
12  
-        + "(?::(\\d+))?" //port, optional
13  
-        + "(?:\/([0-9a-z\-_\/]+))" //path
14  
-        + "(?:[?]([0-9a-z=]*))?" //query, optional. the ?:[?] is used because ?:\? causes problems in FF3,safari
15  
-        + "(?:#(.*))?",
16  
-            //anchor, optional
17  
-        regex = new RegExp(pattern),
18  
-            matches = regex.exec(inputURL.toLowerCase()),
19  
-            fields = ['url', 'scheme', 'authority', 'port', 'path', 'query', 'anchor'],
20  
-            parsedURL = {};
21  
-
22  
-        if (!matches) {
23  
-            throw ("parseURL error: incorrect formatting. Expected http[s]://host.tld[:port]/path[?q=query][#anchor], but got " + inputURL);
24  
-        }
25  
-
26  
-        //map matched items to field names
27  
-        for (var i = 0; i < fields.length; i++) {
28  
-            parsedURL[fields[i]] = matches[i];
29  
-        }
30  
-
31  
-        return parsedURL;
32  
-    },
33 8
 
34 9
     // @credit oauth js lib: http://code.google.com/p/oauth/source/browse/#svn/code/javascript
35  
-    percentEncode = function(str) {
  10
+    var percentEncode = function(str) {
36 11
 
37 12
         if (str == null) {
38 13
             return "";
@@ -59,12 +34,17 @@ var jsoauth = (function() {
59 34
 
60 35
         try {
61 36
 
62  
-            parsedURL = parseURL(inputURL);
63  
-            return parsedURL['scheme'] + '://' + parsedURL['authority'] + '/' + parsedURL['path'];
  37
+            // parse_url http://phpjs.org/functions/parse_url:485
  38
+            parsedURL = parse_url(inputURL);
  39
+
  40
+            return parsedURL['scheme'] + '://' + parsedURL['host'] + '/' + parsedURL['path'];
64 41
 
65 42
         } catch(e) {
  43
+
66 44
             //incorrectly formatted input url
67  
-            alert(e);
  45
+            if (alert) {
  46
+                alert(e);
  47
+            }
68 48
         }
69 49
     },
70 50
 
@@ -115,32 +95,46 @@ var jsoauth = (function() {
115 95
         return sig;
116 96
     };
117 97
 
  98
+
  99
+
  100
+    function toHeaderString(params) {
  101
+        var pairs = [];
  102
+        for (var i = 0; i < params.length; i++) {
  103
+            var pair = params[i].split('=');
  104
+            pairs.push(pair[0] + '="' + pair[1] + '"');
  105
+        }
  106
+        return pairs.join(',');
  107
+    }
  108
+
118 109
     return {
119 110
         consumerKey: '',
120 111
         consumerSecret: '',
121 112
         reqMethod: 'GET',
122 113
         tokenSecret: '',
123  
-        
  114
+
124 115
         //must be uppercase @ref http://oauth.net/core/1.0/#rfc.section.9.1.3
125 116
         sigMethod: 'HMAC-SHA1',
126  
-        
  117
+
127 118
         oauthVersion: '1.0',
128 119
         callbackURL: '',
129  
-        
  120
+
130 121
         //handy for debugging
131 122
         params: null,
132 123
         normalReqParams: null,
133 124
         baseStr: null,
134 125
         signature: null,
135  
-        
  126
+
  127
+        //utils
  128
+        toHeaderString: toHeaderString,
  129
+
136 130
         sign: function(args) {
137 131
 
138 132
             var timestamp = Math.floor(new Date().getTime() / 1000);
139  
-            
  133
+
140 134
             // flush so multiple calls to obj don't conflict
141 135
             this.normalReqParams = null;
142  
-            this.baseStr = null; 
143  
-            this.signature = null; 
  136
+            this.baseStr = null;
  137
+            this.signature = null;
144 138
             this.params = [];
145 139
 
146 140
             // basic oauth params formatted as strings in array so we can sort easily
@@ -159,7 +153,7 @@ var jsoauth = (function() {
159 153
 
160 154
             // elems for base str
161 155
             this.normalReqParams = normalizeReqParams(this.params);
162  
-            reqURL = constructReqURL(args.URL); 
  156
+            reqURL = constructReqURL(args.URL);
163 157
 
164 158
             // create base str
165 159
             this.baseStr = concatenateReqElems({
@@ -167,7 +161,7 @@ var jsoauth = (function() {
167 161
                 'reqURL': percentEncode(reqURL),
168 162
                 'params': this.normalReqParams
169 163
             });
170  
-            
  164
+
171 165
             this.signature = generateSig({
172 166
                 'baseStr': this.baseStr,
173 167
                 'secret': this.consumerSecret,
@@ -181,7 +175,10 @@ var jsoauth = (function() {
181 175
             //@ref http://oauth.net/core/1.0/#9.2.1
182 176
             var signedURL = args.URL + '?' + concatenateParams(this.params.sort());
183 177
 
184  
-            return signedURL;
  178
+            return {
  179
+                queryString: concatenateParams(this.params),
  180
+                headerString: toHeaderString(this.params)
  181
+            };
185 182
         }
186 183
     };
187  
-}());
  184
+} ());

0 notes on commit 8f6a794

Please sign in to comment.
Something went wrong with that request. Please try again.