-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Deploy full oauth functionality (#99)
* Fix encodeURI params and some clean up (#98) (#4) * v2.7.7 * fix encodeURI issue * version bump Co-authored-by: Ajaykumar <[email protected]> * Add OAUTH endpoint to constants * Add full oauth support * Add oauth endpoint to expected properties * Rename access_token -> appAccessToken * Remove oauth functions from utils * Add user token demo * Fix missing context var * Add missing function description * Add urldecode declaration to docstring * Remove trailling spaces * Migrate credential functions to credentials.js Co-authored-by: Ajaykumar <[email protected]>
- Loading branch information
1 parent
ffa4bf3
commit d389d49
Showing
8 changed files
with
232 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const readline = require('readline'); | ||
const Ebay = require('../src/index'); | ||
const { clientId, clientSecret, redirectUri } = require('./credentials/index'); | ||
|
||
let ebay = new Ebay({ | ||
clientID: clientId, | ||
clientSecret: clientSecret, | ||
redirectUri: redirectUri, | ||
body: { | ||
grant_type: 'authorization_code', | ||
scope: 'https://api.ebay.com/oauth/api_scope' | ||
} | ||
}); | ||
|
||
const rl = readline.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout, | ||
}); | ||
|
||
const authURL = ebay.getUserAuthorizationUrl(); | ||
console.log(`Please go here for auth code: ${authURL}`); | ||
rl.question("Enter the auth code recieved from the redirect url (should urldecode it first): ", code => { | ||
rl.close(); | ||
ebay.getUserTokenByCode(code).then(data => { | ||
console.log('User token by code response:-'); | ||
console.log(data); | ||
ebay.getUserTokenByRefresh().then(data => { | ||
console.log('User token by refresh token response:-'); | ||
console.log(data); | ||
}); | ||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
'use strict'; | ||
const qs = require('querystring'); | ||
const { base64Encode } = require('./common-utils'); | ||
const { makeRequest } = require('./request'); | ||
const DEFAULT_API_SCOPE = 'https://api.ebay.com/oauth/api_scope'; | ||
|
||
/** | ||
* Generates an application access token for client credentials grant flow | ||
* | ||
* @return appAccessToken object | ||
*/ | ||
const getAccessToken = function () { | ||
if (!this.options.clientID) throw new Error('Missing Client ID'); | ||
if (!this.options.clientSecret) throw new Error('Missing Client Secret or Cert Id'); | ||
if (!this.options.body) throw new Error('Missing Body, required Grant type'); | ||
let scopesParam = this.options.body.scopes | ||
? Array.isArray(this.options.body.scopes) | ||
? this.options.body.scopes.join('%20') | ||
: this.options.body.scopes | ||
: DEFAULT_API_SCOPE; | ||
this.options.data = qs.stringify({ | ||
grant_type: 'client_credentials', | ||
scope: scopesParam | ||
}); | ||
this.options.contentType = 'application/x-www-form-urlencoded'; | ||
const self = this; | ||
const encodedStr = base64Encode(this.options.clientID + ':' + this.options.clientSecret); | ||
const auth = 'Basic ' + encodedStr; | ||
return makeRequest(this.options, '/identity/v1/oauth2/token', 'POST', auth).then((result) => { | ||
const resultJSON = JSON.parse(result); | ||
if (!resultJSON.error) self.setAppAccessToken(resultJSON); | ||
return resultJSON; | ||
}); | ||
}; | ||
|
||
/** | ||
* Generates user consent authorization url | ||
* | ||
* @param state custom state value | ||
* @return userConsentUrl | ||
*/ | ||
const getUserAuthorizationUrl = function (state = null) { | ||
if (!this.options.clientID) throw new Error('Missing Client ID'); | ||
if (!this.options.clientSecret) throw new Error('Missing Client Secret or Cert Id'); | ||
if (!this.options.body) throw new Error('Missing Body, required Grant type'); | ||
if (!this.options.redirectUri) throw new Error('redirect_uri is required for redirection after sign in\nkindly check here https://developer.ebay.com/api-docs/static/oauth-redirect-uri.html'); | ||
let scopesParam = this.options.body.scopes | ||
? Array.isArray(this.options.body.scopes) | ||
? this.options.body.scopes.join('%20') | ||
: this.options.body.scopes | ||
: DEFAULT_API_SCOPE; | ||
let queryParam = `client_id=${this.options.clientID}`; | ||
queryParam += `&redirect_uri=${this.options.redirectUri}`; | ||
queryParam += `&response_type=code`; | ||
queryParam += `&scope=${scopesParam}`; | ||
queryParam += state ? `&state=${state}` : ''; | ||
return `${this.options.oauthEndpoint}?${queryParam}`; | ||
}; | ||
|
||
/** | ||
* Generates a User access token given auth code | ||
* | ||
* @param code code generated from browser using the method getUserAuthorizationUrl (should be urldecoded) | ||
* @return userAccessToken object (with refresh_token) | ||
*/ | ||
const getUserTokenByCode = function (code) { | ||
if (!code) throw new Error('Authorization code is required, to generate authorization code use getUserAuthorizationUrl method'); | ||
if (!this.options.clientID) throw new Error('Missing Client ID'); | ||
if (!this.options.clientSecret) throw new Error('Missing Client Secret or Cert Id'); | ||
if (!this.options.redirectUri) throw new Error('redirect_uri is required for redirection after sign in\nkindly check here https://developer.ebay.com/api-docs/static/oauth-redirect-uri.html'); | ||
this.options.data = qs.stringify({ | ||
code: code, | ||
grant_type: 'authorization_code', | ||
redirect_uri: this.options.redirectUri | ||
}); | ||
this.options.contentType = 'application/x-www-form-urlencoded'; | ||
const self = this; | ||
const encodedStr = base64Encode(`${this.options.clientID}:${this.options.clientSecret}`); | ||
const auth = `Basic ${encodedStr}`; | ||
return makeRequest(this.options, '/identity/v1/oauth2/token', 'POST', auth).then(result => { | ||
const resultJSON = JSON.parse(result); | ||
if (!resultJSON.error) self.setUserAccessToken(resultJSON); | ||
return resultJSON; | ||
}); | ||
}; | ||
|
||
/** | ||
* Use a refresh token to update a User access token (Updating the expired access token) | ||
* | ||
* @param refreshToken refresh token, defaults to pre-assigned refresh token | ||
* @param scopes array of scopes for the access token | ||
* @return userAccessToken object (without refresh_token) | ||
*/ | ||
const getUserTokenByRefresh = function (refreshToken = null) { | ||
if (!this.options.clientID) throw new Error('Missing Client ID'); | ||
if (!this.options.clientSecret) throw new Error('Missing Client Secret or Cert Id'); | ||
if (!this.options.body) throw new Error('Missing Body, required Grant type'); | ||
if (!refreshToken && !this.options.refreshToken) { | ||
throw new Error('Refresh token is required, to generate refresh token use getUserTokenByCode method'); // eslint-disable-line max-len | ||
} | ||
refreshToken = refreshToken ? refreshToken : this.options.refreshToken; | ||
let scopesParam = this.options.body.scopes | ||
? Array.isArray(this.options.body.scopes) | ||
? this.options.body.scopes.join('%20') | ||
: this.options.body.scopes | ||
: DEFAULT_API_SCOPE; | ||
this.options.data = qs.stringify({ | ||
refresh_token: refreshToken, | ||
grant_type: 'refresh_token', | ||
scope: scopesParam | ||
}); | ||
this.options.contentType = 'application/x-www-form-urlencoded'; | ||
const self = this; | ||
const encodedStr = base64Encode(`${this.options.clientID}:${this.options.clientSecret}`); | ||
const auth = `Basic ${encodedStr}`; | ||
return makeRequest(this.options, '/identity/v1/oauth2/token', 'POST', auth).then(result => { | ||
const resultJSON = JSON.parse(result); | ||
if (!resultJSON.error) self.setUserAccessToken(resultJSON); | ||
return resultJSON; | ||
}); | ||
}; | ||
|
||
/** | ||
* Assign user access token and refresh token returned from authorization grant workflow (i.e getUserTokenByRefresh) | ||
* | ||
* @param userAccessToken userAccessToken obj returned from getUserTokenByCode or getAccessTokenByRefresh | ||
*/ | ||
const setUserAccessToken = function (userAccessToken) { | ||
if (!userAccessToken.token_type === 'User Access Token') throw new Error('userAccessToken is either missing or invalid'); | ||
if (userAccessToken.refresh_token) this.options.refreshToken = userAccessToken.refresh_token; | ||
this.options.userAccessToken = userAccessToken.access_token; | ||
}; | ||
|
||
/** | ||
* Assign application access token returned from client credentials workflow (i.e getAccessToken) | ||
* | ||
* @param appAccessToken appAccessToken obj returned from getApplicationToken | ||
*/ | ||
const setAppAccessToken = function (appAccessToken) { | ||
if (!appAccessToken.token_type === 'Application Access Token') throw new Error('appAccessToken is either missing or invalid'); | ||
this.options.appAccessToken = appAccessToken.access_token; | ||
}; | ||
|
||
module.exports = { | ||
getAccessToken, | ||
getUserAuthorizationUrl, | ||
getUserTokenByCode, | ||
getUserTokenByRefresh, | ||
setUserAccessToken, | ||
setAppAccessToken | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.