Skip to content

Commit

Permalink
Merge pull request borismus#10 from neocotic/adapters
Browse files Browse the repository at this point in the history
Bug fixes and API improvements (3 of 4)
  • Loading branch information
Boris Smus committed Jun 9, 2012
2 parents 1c91c9b + 6920340 commit d27c828
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 12 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ Here's a table that will come in handy:
<td>https://github.com/robots.txt</td>
<td>https://github.com/login/oauth/access_token</td>
</tr>
<tr>
<td>bitly</td>
<td>http://bitly.com/robots.txt</td>
<td>https://api-ssl.bitly.com/oauth/access_token</td>
</tr>
</table>

#### Step 1: Copy library
Expand Down
47 changes: 47 additions & 0 deletions lib/adapters/bitly.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
OAuth2.adapter('bitly', {

authorizationCodeURL: function(config) {
return ('https://bitly.com/oauth/authorize?' +
'client_id={{CLIENT_ID}}&' +
'redirect_uri={{REDIRECT_URI}}')
.replace('{{CLIENT_ID}}', config.clientId)
.replace('{{REDIRECT_URI}}', this.redirectURL(config));
},

redirectURL: function(config) {
return 'http://bitly.com/robots.txt';
},

parseAuthorizationCode: function(url) {
// TODO: Error handling (URL may have ?error=foo_bar&error_code=43 etc).
return url.match(/[&\?]code=([^&]+)/)[1];
},

accessTokenURL: function() {
return 'https://api-ssl.bitly.com/oauth/access_token';
},

accessTokenMethod: function() {
return 'POST';
},

accessTokenParams: function(authorizationCode, config) {
return {
code: authorizationCode,
client_id: config.clientId,
client_secret: config.clientSecret,
redirect_uri: this.redirectURL(config),
grant_type: 'authorization_code'
};
},

parseAccessToken: function(response) {
return {
accessToken: response.match(/access_token=([^&]*)/)[1],
apiKey: response.match(/apiKey=([^&]*)/)[1],
expiresIn: Number.MAX_VALUE,
login: response.match(/login=([^&]*)/)[1]
};
}

});
6 changes: 4 additions & 2 deletions lib/adapters/facebook.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
OAuth2.adapter('facebook', {
authorizationCodeURL: function(config) {
return 'https://www.facebook.com/dialog/oauth? \
client_id={{CLIENT_ID}}&redirect_uri={{REDIRECT_URI}}&scope={{API_SCOPE}}'
return ('https://www.facebook.com/dialog/oauth?' +
'client_id={{CLIENT_ID}}&' +
'redirect_uri={{REDIRECT_URI}}&' +
'scope={{API_SCOPE}}')
.replace('{{CLIENT_ID}}', config.clientId)
.replace('{{REDIRECT_URI}}', this.redirectURL(config))
.replace('{{API_SCOPE}}', config.apiScope);
Expand Down
5 changes: 3 additions & 2 deletions lib/adapters/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ OAuth2.adapter('github', {
* @return {URL} URL to the page that returns the authorization code
*/
authorizationCodeURL: function(config) {
return 'https://github.com/login/oauth/authorize?\
client_id={{CLIENT_ID}}&redirect_uri={{REDIRECT_URI}}'
return ('https://github.com/login/oauth/authorize?' +
'client_id={{CLIENT_ID}}&' +
'redirect_uri={{REDIRECT_URI}}')
.replace('{{CLIENT_ID}}', config.clientId)
.replace('{{REDIRECT_URI}}', this.redirectURL(config));
},
Expand Down
11 changes: 6 additions & 5 deletions lib/adapters/google.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
OAuth2.adapter('google', {
authorizationCodeURL: function(config) {
return 'https://accounts.google.com/o/oauth2/auth?\
client_id={{CLIENT_ID}}&\
redirect_uri={{REDIRECT_URI}}&\
scope={{API_SCOPE}}&\
response_type=code'
return ('https://accounts.google.com/o/oauth2/auth?' +
'client_id={{CLIENT_ID}}&' +
'redirect_uri={{REDIRECT_URI}}&' +
'scope={{API_SCOPE}}&' +
'access_type=offline&' +
'response_type=code')
.replace('{{CLIENT_ID}}', config.clientId)
.replace('{{REDIRECT_URI}}', this.redirectURL(config))
.replace('{{API_SCOPE}}', config.apiScope);
Expand Down
2 changes: 1 addition & 1 deletion lib/adapters/sample.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ OAuth2.adapter('sample', {
return {
accessToken: '',
refreshToken: '',
expiresIn: 0
expiresIn: Number.MAX_VALUE
};
}
});
4 changes: 2 additions & 2 deletions lib/oauth2.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,8 @@ OAuth2.loadAdapter = function(adapterName, callback) {
* @throws {String} If the specified adapter is invalid
*/
OAuth2.adapter = function(name, impl) {
var implementing = 'authorizationCodeURL redirectURL \
accessTokenURL accessTokenMethod accessTokenParams accessToken';
var implementing = 'authorizationCodeURL redirectURL accessTokenURL ' +
'accessTokenMethod accessTokenParams accessToken';

// Check for missing methods
implementing.split(' ').forEach(function(method, index) {
Expand Down

0 comments on commit d27c828

Please sign in to comment.