ordercloud-api / ordercloud-seed

CLI and library to Import and export serialized representations of full marketplaces
MIT License
9 stars 9 forks source link

Add refresh token support #16

Closed fpodshivadlov closed 2 years ago

fpodshivadlov commented 2 years ago

The token issued by OrderCloud portal is valid only for 20 minutes, it's not enough for bigger marketplaces. So Refresh Token support is crucial for real world solutions.

The console output:

PS> & ./exe/seeding-win.exe download ./output.yml -i=<hidden> -u='<hidden>' -p='<hidden>'
βœ”οΈ  SUCCESS - Found your Marketplace "<hidden>" . Beginning download.

πŸ• PROGRESS - Found 4 SecurityProfiles
πŸ• PROGRESS - Found 1 ImpersonationConfigs
πŸ• PROGRESS - Found 3 AdminUsers
πŸ• PROGRESS - Found 3 MessageSenders
πŸ• PROGRESS - Found 5 ApiClients
πŸ• PROGRESS - Found 1 Locales
πŸ• PROGRESS - Found 6 Incrementors
πŸ• PROGRESS - Found 1 Webhooks
πŸ• PROGRESS - Found 2 IntegrationEvents
πŸ• PROGRESS - Found 18 XpIndices
πŸ• PROGRESS - Found 284 Users
πŸ• PROGRESS - Found 1 UserGroups
πŸ• PROGRESS - Found 2 Addresses
πŸ• PROGRESS - Found 161 UserGroupAssignments
πŸ• PROGRESS - Found 1 Buyers
πŸ• PROGRESS - Found 81 Categories
πŸ• PROGRESS - Found 81 CategoryAssignments
πŸ• PROGRESS - Found 1344 CategoryProductAssignments
πŸ• PROGRESS - Found 1 Catalogs
πŸ• PROGRESS - Found 1344 SupplierUsers
πŸ• PROGRESS - Found 1344 SupplierUserGroups
πŸ• PROGRESS - Found 1344 SupplierAddresses
πŸ• PROGRESS - Found 1344 SupplierUserGroupsAssignments
πŸ• PROGRESS - Found 1344 SupplierBuyerAssignment      
@ordercloud/seeding download [filePath]

Create a local seed file from an existing marketplace.

Positionals:
  filePath, f  File path               [string] [default: "ordercloud-seed.yml"]

Options:
  --version       Show version number                                  [boolean]
  --help          Show help                                            [boolean]
  --id, -i        Marketplace ID                                        [string]
  --username, -u  Portal username                                       [string]
  --password, -p  Portal password                                       [string]

Error [OrderCloudError]
    at new OrderCloudError (C:\snapshot\ordercloud-seed\node_modules\ordercloud-javascript-sdk\dist\index.js:132:24)
    at C:\snapshot\ordercloud-seed\node_modules\ordercloud-javascript-sdk\dist\index.js:10769:43
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (internal/process/task_queues.js:95:5) {
  isOrderCloudError: true,
  errors: { Errors: [ [Object] ] },
  errorCode: undefined,
  status: 401,
  statusText: 'Unauthorized',
  response: {
    status: 401,
    statusText: 'Unauthorized',
    headers: {
      date: 'Fri, 29 Apr 2022 08:53:22 GMT',
      'content-type': 'application/json',
      'transfer-encoding': 'chunked',
      connection: 'close',
      server: 'Microsoft-IIS/10.0',
      'x-oc-logid': '7e2e2954-c93b-4d6b-a951-8393fbd9f158',
      'x-powered-by': 'ASP.NET'
    },
    config: {
      url: 'https://sandboxapi.ordercloud.io/v1/products/6042603/variants/56042605/inventoryrecords',
      method: 'get',
      headers: [Object],
      params: [Object],
      transformRequest: [Array],
      transformResponse: [Array],
      paramsSerializer: [Function: ParamSerializer],
      timeout: 60000,
      adapter: [Function: httpAdapter],
      xsrfCookieName: 'XSRF-TOKEN',
      xsrfHeaderName: 'X-XSRF-TOKEN',
      maxContentLength: -1,
      maxBodyLength: -1,
      validateStatus: [Function: validateStatus],
      data: undefined
    },
    request: ClientRequest {
      // removed
      _header: 'GET /v1/products/6042603/variants/56042605/inventoryrecords?page=1&pageSize=100&depth=all HTTP/1.1\r\n' +
        'Accept: application/json, text/plain, */*\r\n' +
        'Content-Type: application/json\r\n' +
        'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoiRnVsbEFjY2VzcyIsInVzcnR5cGUiOiJzZWxsZXIiLCJjb2lkIjoiNTQxMjQwIiwidXNyIjoiRTIzMDNFRDYtODNEMC00QkE3LTg1NUMtNzg4NkUyMEI1MThFIiwidWlkIjoiNjA4NTQxMCIsImNvaWlkIjoidEtjWHFLSHNXdEw1d0drcyIsIm1waWQiOiI5MTQiLCJzaWlkIjoidEtjWHFLSHNXdEw1d0drcyIsInNpZCI6IjU0MTI0MCIsIm5iZiI6MTY1MTIyMTIwMiwiZXhwIjoxNjUxMjIyNDAyLCJpYXQiOjE2NTEyMjEyMDIsImlzcyI6Imh0dHBzOi8vc2FuZGJveGFwaS5vcmRlcmNsb3VkLmlvIiwiYXVkIjoiaHR0cHM6Ly9zYW5kYm94YXBpLm9yZGVyY2xvdWQuaW8ifQ.C5hkgaIpJPMqqjRPB8Meit9jA2yclmYeidTgS6mcRko\r\n' +
        'User-Agent: axios/0.21.1\r\n' +
        'Host: sandboxapi.ordercloud.io\r\n' +
        'Connection: close\r\n' +
        '\r\n',
        // removed
    },
    data: { Errors: [Array] }
  },
  request: <ref *1> ClientRequest {
    // removed
  }
}
fpodshivadlov commented 2 years ago

The OrderCloud API returnes refresh_token for portal auth and doesn't return it for organization auth. As I can see the current portal-javascript-sdk doesn't support custom refresh token logic.

Hopefully it can be done with the following workaround (changes for download.ts operation):

oliverheywood451 commented 2 years ago

Hi @fpodshivadlov. Thanks for creating the issue!

I apologize for the slow reply. If you can make this PR I will confirm it works and publish it!

Otherwise I can make the PR myself but it might be a little while.

oliverheywood451 commented 2 years ago

Should be fixed in version 1.0.30