|
| 1 | +var querystring= require('querystring'), |
| 2 | + https= require('https'), |
| 3 | + http= require('http'), |
| 4 | + URL= require('url'); |
| 5 | + |
| 6 | +exports.OAuth2= function(clientId, clientSecret, baseSite, authorizePath, accessTokenPath, callbackURL, customHeaders) { |
| 7 | + this._clientId= clientId; |
| 8 | + this._clientSecret= clientSecret; |
| 9 | + this._baseSite= baseSite; |
| 10 | + this._authorizeUrl= authorizePath || "/oauth/authorize"; |
| 11 | + this._accessTokenUrl= accessTokenPath || "/oauth/access_token"; |
| 12 | + this._callbackURL = callbackURL; |
| 13 | + this._accessTokenName= "access_token"; |
| 14 | + this._authMethod= "Basic"; |
| 15 | + this._customHeaders = customHeaders || {}; |
| 16 | +} |
| 17 | + |
| 18 | +// This 'hack' method is required for sites that don't use |
| 19 | +// 'access_token' as the name of the access token (for requests). |
| 20 | +// ( http://tools.ietf.org/html/draft-ietf-oauth-v2-16#section-7 ) |
| 21 | +// it isn't clear what the correct value should be atm, so allowing |
| 22 | +// for specific (temporary?) override for now. |
| 23 | +exports.OAuth2.prototype.setAccessTokenName= function ( name ) { |
| 24 | + this._accessTokenName= name; |
| 25 | +} |
| 26 | + |
| 27 | +exports.OAuth2.prototype._getAccessTokenUrl= function() { |
| 28 | + return this._baseSite + this._accessTokenUrl; |
| 29 | +} |
| 30 | + |
| 31 | +// Build the authorization header. In particular, build the part after the colon. |
| 32 | +// e.g. Authorization: Bearer <token> # Build "Bearer <token>" |
| 33 | +exports.OAuth2.prototype.buildAuthHeader= function() { |
| 34 | + var key = this._clientId + ':' + this._clientSecret; |
| 35 | + var base64 = (new Buffer(key)).toString('base64'); |
| 36 | + return this._authMethod + ' ' + base64; |
| 37 | +}; |
| 38 | + |
| 39 | +exports.OAuth2.prototype._request= function(method, url, headers, post_body, access_token, callback) { |
| 40 | + |
| 41 | + var http_library= https; |
| 42 | + var parsedUrl= URL.parse( url, true ); |
| 43 | + if( parsedUrl.protocol == "https:" && !parsedUrl.port ) { |
| 44 | + parsedUrl.port= 443; |
| 45 | + } |
| 46 | + |
| 47 | + // As this is OAUth2, we *assume* https unless told explicitly otherwise. |
| 48 | + if( parsedUrl.protocol != "https:" ) { |
| 49 | + http_library= http; |
| 50 | + } |
| 51 | + |
| 52 | + var realHeaders= {}; |
| 53 | + for( var key in this._customHeaders ) { |
| 54 | + realHeaders[key]= this._customHeaders[key]; |
| 55 | + } |
| 56 | + if( headers ) { |
| 57 | + for(var key in headers) { |
| 58 | + realHeaders[key] = headers[key]; |
| 59 | + } |
| 60 | + } |
| 61 | + realHeaders['Host']= parsedUrl.host; |
| 62 | + |
| 63 | + //realHeaders['Content-Length']= post_body ? Buffer.byteLength(post_body) : 0; |
| 64 | + if( access_token && !('Authorization' in realHeaders)) { |
| 65 | + if( ! parsedUrl.query ) parsedUrl.query= {}; |
| 66 | + parsedUrl.query[this._accessTokenName]= access_token; |
| 67 | + } |
| 68 | + |
| 69 | + var queryStr= querystring.stringify(parsedUrl.query); |
| 70 | + if( queryStr ) queryStr= "?" + queryStr; |
| 71 | + var options = { |
| 72 | + host:parsedUrl.hostname, |
| 73 | + port: parsedUrl.port, |
| 74 | + path: parsedUrl.pathname + queryStr, |
| 75 | + method: method, |
| 76 | + headers: realHeaders |
| 77 | + }; |
| 78 | + |
| 79 | + this._executeRequest( http_library, options, post_body, callback ); |
| 80 | +} |
| 81 | + |
| 82 | +exports.OAuth2.prototype._executeRequest= function( http_library, options, post_body, callback ) { |
| 83 | + // Some hosts *cough* google appear to close the connection early / send no content-length header |
| 84 | + // allow this behaviour. |
| 85 | + var allowEarlyClose= options.host && options.host.match(".*google(apis)?.com$"); |
| 86 | + var callbackCalled= false; |
| 87 | + function passBackControl( response, result ) { |
| 88 | + if(!callbackCalled) { |
| 89 | + callbackCalled=true; |
| 90 | + if( response.statusCode != 200 && (response.statusCode != 301) && (response.statusCode != 302) ) { |
| 91 | + callback({ statusCode: response.statusCode, data: result }); |
| 92 | + } else { |
| 93 | + callback(null, result, response); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + var result= ""; |
| 99 | + |
| 100 | + var request = http_library.request(options, function (response) { |
| 101 | + response.on("data", function (chunk) { |
| 102 | + result+= chunk |
| 103 | + }); |
| 104 | + response.on("close", function (err) { |
| 105 | + if( allowEarlyClose ) { |
| 106 | + passBackControl( response, result ); |
| 107 | + } |
| 108 | + }); |
| 109 | + response.addListener("end", function () { |
| 110 | + passBackControl( response, result ); |
| 111 | + }); |
| 112 | + }); |
| 113 | + request.on('error', function(e) { |
| 114 | + callbackCalled= true; |
| 115 | + callback(e); |
| 116 | + }); |
| 117 | + |
| 118 | + if(options.method == 'POST' && post_body) { |
| 119 | + request.write(post_body); |
| 120 | + } |
| 121 | + request.end(); |
| 122 | +} |
| 123 | + |
| 124 | +exports.OAuth2.prototype.getAuthorizeUrl= function() { |
| 125 | + return this._baseSite + this._authorizeUrl + '?response_type=code&client_id=' + this._clientId + '&state=xyz&redirect_uri=' + this._callbackURL; |
| 126 | +} |
| 127 | + |
| 128 | +exports.OAuth2.prototype.getOAuthAccessToken= function(code, callback) { |
| 129 | + |
| 130 | + var post_data = 'grant_type=authorization_code&code=' + code + '&redirect_uri=' + this._callbackURL; |
| 131 | + |
| 132 | + var post_headers= { |
| 133 | + 'Authorization': this.buildAuthHeader(), |
| 134 | + 'Content-Type': 'application/x-www-form-urlencoded' |
| 135 | + }; |
| 136 | + |
| 137 | + this._request("POST", this._getAccessTokenUrl(), post_headers, post_data, null, function(error, data, response) { |
| 138 | + if( error ) callback(error); |
| 139 | + else { |
| 140 | + var results; |
| 141 | + try { |
| 142 | + // As of http://tools.ietf.org/html/draft-ietf-oauth-v2-07 |
| 143 | + // responses should be in JSON |
| 144 | + results= JSON.parse(data); |
| 145 | + } |
| 146 | + catch(e) { |
| 147 | + // .... However both Facebook + Github currently use rev05 of the spec |
| 148 | + // and neither seem to specify a content-type correctly in their response headers :( |
| 149 | + // clients of these services will suffer a *minor* performance cost of the exception |
| 150 | + // being thrown |
| 151 | + results= querystring.parse(data); |
| 152 | + } |
| 153 | + callback(null, results); |
| 154 | + } |
| 155 | + }); |
| 156 | +} |
| 157 | + |
| 158 | +exports.OAuth2.prototype.get= function(url, access_token, callback) { |
| 159 | + this._request("GET", url, {}, "", access_token, callback); |
| 160 | +} |
0 commit comments