|
| 1 | +'use strict'; |
| 2 | +const qs = require('querystring'); |
| 3 | +const { base64Encode } = require('./common-utils'); |
| 4 | +const { makeRequest } = require('./request'); |
| 5 | +const DEFAULT_API_SCOPE = 'https://api.ebay.com/oauth/api_scope'; |
| 6 | + |
| 7 | +/** |
| 8 | +* Generates an application access token for client credentials grant flow |
| 9 | +* |
| 10 | +* @return appAccessToken object |
| 11 | +*/ |
| 12 | +const getAccessToken = function () { |
| 13 | + if (!this.options.clientID) throw new Error('Missing Client ID'); |
| 14 | + if (!this.options.clientSecret) throw new Error('Missing Client Secret or Cert Id'); |
| 15 | + if (!this.options.body) throw new Error('Missing Body, required Grant type'); |
| 16 | + let scopesParam = this.options.body.scopes |
| 17 | + ? Array.isArray(this.options.body.scopes) |
| 18 | + ? this.options.body.scopes.join('%20') |
| 19 | + : this.options.body.scopes |
| 20 | + : DEFAULT_API_SCOPE; |
| 21 | + this.options.data = qs.stringify({ |
| 22 | + grant_type: 'client_credentials', |
| 23 | + scope: scopesParam |
| 24 | + }); |
| 25 | + this.options.contentType = 'application/x-www-form-urlencoded'; |
| 26 | + const self = this; |
| 27 | + const encodedStr = base64Encode(this.options.clientID + ':' + this.options.clientSecret); |
| 28 | + const auth = 'Basic ' + encodedStr; |
| 29 | + return makeRequest(this.options, '/identity/v1/oauth2/token', 'POST', auth).then((result) => { |
| 30 | + const resultJSON = JSON.parse(result); |
| 31 | + if (!resultJSON.error) self.setAppAccessToken(resultJSON); |
| 32 | + return resultJSON; |
| 33 | + }); |
| 34 | +}; |
| 35 | + |
| 36 | +/** |
| 37 | + * Generates user consent authorization url |
| 38 | + * |
| 39 | + * @param state custom state value |
| 40 | + * @return userConsentUrl |
| 41 | +*/ |
| 42 | +const getUserAuthorizationUrl = function (state = null) { |
| 43 | + if (!this.options.clientID) throw new Error('Missing Client ID'); |
| 44 | + if (!this.options.clientSecret) throw new Error('Missing Client Secret or Cert Id'); |
| 45 | + if (!this.options.body) throw new Error('Missing Body, required Grant type'); |
| 46 | + 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'); |
| 47 | + let scopesParam = this.options.body.scopes |
| 48 | + ? Array.isArray(this.options.body.scopes) |
| 49 | + ? this.options.body.scopes.join('%20') |
| 50 | + : this.options.body.scopes |
| 51 | + : DEFAULT_API_SCOPE; |
| 52 | + let queryParam = `client_id=${this.options.clientID}`; |
| 53 | + queryParam += `&redirect_uri=${this.options.redirectUri}`; |
| 54 | + queryParam += `&response_type=code`; |
| 55 | + queryParam += `&scope=${scopesParam}`; |
| 56 | + queryParam += state ? `&state=${state}` : ''; |
| 57 | + return `${this.options.oauthEndpoint}?${queryParam}`; |
| 58 | +}; |
| 59 | + |
| 60 | +/** |
| 61 | + * Generates a User access token given auth code |
| 62 | + * |
| 63 | + * @param code code generated from browser using the method getUserAuthorizationUrl (should be urldecoded) |
| 64 | + * @return userAccessToken object (with refresh_token) |
| 65 | +*/ |
| 66 | +const getUserTokenByCode = function (code) { |
| 67 | + if (!code) throw new Error('Authorization code is required, to generate authorization code use getUserAuthorizationUrl method'); |
| 68 | + if (!this.options.clientID) throw new Error('Missing Client ID'); |
| 69 | + if (!this.options.clientSecret) throw new Error('Missing Client Secret or Cert Id'); |
| 70 | + 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'); |
| 71 | + this.options.data = qs.stringify({ |
| 72 | + code: code, |
| 73 | + grant_type: 'authorization_code', |
| 74 | + redirect_uri: this.options.redirectUri |
| 75 | + }); |
| 76 | + this.options.contentType = 'application/x-www-form-urlencoded'; |
| 77 | + const self = this; |
| 78 | + const encodedStr = base64Encode(`${this.options.clientID}:${this.options.clientSecret}`); |
| 79 | + const auth = `Basic ${encodedStr}`; |
| 80 | + return makeRequest(this.options, '/identity/v1/oauth2/token', 'POST', auth).then(result => { |
| 81 | + const resultJSON = JSON.parse(result); |
| 82 | + if (!resultJSON.error) self.setUserAccessToken(resultJSON); |
| 83 | + return resultJSON; |
| 84 | + }); |
| 85 | +}; |
| 86 | + |
| 87 | +/** |
| 88 | + * Use a refresh token to update a User access token (Updating the expired access token) |
| 89 | + * |
| 90 | + * @param refreshToken refresh token, defaults to pre-assigned refresh token |
| 91 | + * @param scopes array of scopes for the access token |
| 92 | + * @return userAccessToken object (without refresh_token) |
| 93 | +*/ |
| 94 | +const getUserTokenByRefresh = function (refreshToken = null) { |
| 95 | + if (!this.options.clientID) throw new Error('Missing Client ID'); |
| 96 | + if (!this.options.clientSecret) throw new Error('Missing Client Secret or Cert Id'); |
| 97 | + if (!this.options.body) throw new Error('Missing Body, required Grant type'); |
| 98 | + if (!refreshToken && !this.options.refreshToken) { |
| 99 | + throw new Error('Refresh token is required, to generate refresh token use getUserTokenByCode method'); // eslint-disable-line max-len |
| 100 | + } |
| 101 | + refreshToken = refreshToken ? refreshToken : this.options.refreshToken; |
| 102 | + let scopesParam = this.options.body.scopes |
| 103 | + ? Array.isArray(this.options.body.scopes) |
| 104 | + ? this.options.body.scopes.join('%20') |
| 105 | + : this.options.body.scopes |
| 106 | + : DEFAULT_API_SCOPE; |
| 107 | + this.options.data = qs.stringify({ |
| 108 | + refresh_token: refreshToken, |
| 109 | + grant_type: 'refresh_token', |
| 110 | + scope: scopesParam |
| 111 | + }); |
| 112 | + this.options.contentType = 'application/x-www-form-urlencoded'; |
| 113 | + const self = this; |
| 114 | + const encodedStr = base64Encode(`${this.options.clientID}:${this.options.clientSecret}`); |
| 115 | + const auth = `Basic ${encodedStr}`; |
| 116 | + return makeRequest(this.options, '/identity/v1/oauth2/token', 'POST', auth).then(result => { |
| 117 | + const resultJSON = JSON.parse(result); |
| 118 | + if (!resultJSON.error) self.setUserAccessToken(resultJSON); |
| 119 | + return resultJSON; |
| 120 | + }); |
| 121 | +}; |
| 122 | + |
| 123 | +/** |
| 124 | + * Assign user access token and refresh token returned from authorization grant workflow (i.e getUserTokenByRefresh) |
| 125 | + * |
| 126 | + * @param userAccessToken userAccessToken obj returned from getUserTokenByCode or getAccessTokenByRefresh |
| 127 | +*/ |
| 128 | +const setUserAccessToken = function (userAccessToken) { |
| 129 | + if (!userAccessToken.token_type === 'User Access Token') throw new Error('userAccessToken is either missing or invalid'); |
| 130 | + if (userAccessToken.refresh_token) this.options.refreshToken = userAccessToken.refresh_token; |
| 131 | + this.options.userAccessToken = userAccessToken.access_token; |
| 132 | +}; |
| 133 | + |
| 134 | +/** |
| 135 | + * Assign application access token returned from client credentials workflow (i.e getAccessToken) |
| 136 | + * |
| 137 | + * @param appAccessToken appAccessToken obj returned from getApplicationToken |
| 138 | +*/ |
| 139 | +const setAppAccessToken = function (appAccessToken) { |
| 140 | + if (!appAccessToken.token_type === 'Application Access Token') throw new Error('appAccessToken is either missing or invalid'); |
| 141 | + this.options.appAccessToken = appAccessToken.access_token; |
| 142 | +}; |
| 143 | + |
| 144 | +module.exports = { |
| 145 | + getAccessToken, |
| 146 | + getUserAuthorizationUrl, |
| 147 | + getUserTokenByCode, |
| 148 | + getUserTokenByRefresh, |
| 149 | + setUserAccessToken, |
| 150 | + setAppAccessToken |
| 151 | +}; |
0 commit comments