auth0 5.4.0 → 5.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (233) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +2 -1
  3. data/.gitignore +0 -1
  4. data/.shiprc +7 -0
  5. data/CHANGELOG.md +106 -30
  6. data/Gemfile +1 -0
  7. data/Gemfile.lock +233 -0
  8. data/README.md +10 -8
  9. data/auth0.gemspec +2 -1
  10. data/lib/auth0/api/authentication_endpoints.rb +40 -16
  11. data/lib/auth0/api/v2/actions.rb +210 -0
  12. data/lib/auth0/api/v2/attack_protection.rb +79 -0
  13. data/lib/auth0/api/v2/grants.rb +49 -0
  14. data/lib/auth0/api/v2/organizations.rb +1 -1
  15. data/lib/auth0/api/v2.rb +6 -0
  16. data/lib/auth0/exception.rb +12 -0
  17. data/lib/auth0/mixins/api_token_struct.rb +0 -1
  18. data/lib/auth0/mixins/httpproxy.rb +76 -40
  19. data/lib/auth0/mixins/initializer.rb +2 -3
  20. data/lib/auth0/mixins/token_management.rb +32 -0
  21. data/lib/auth0/mixins/validation.rb +1 -1
  22. data/lib/auth0/mixins.rb +2 -0
  23. data/lib/auth0/version.rb +1 -1
  24. data/spec/fixtures/vcr_cassettes/Auth0_Api_AuthenticationEndpoints/_change_password/should_trigger_a_password_reset.yml +53 -23
  25. data/spec/fixtures/vcr_cassettes/Auth0_Api_AuthenticationEndpoints/_login_with_resource_owner/should_fail_with_an_incorrect_email.yml +49 -18
  26. data/spec/fixtures/vcr_cassettes/Auth0_Api_AuthenticationEndpoints/_login_with_resource_owner/should_fail_with_an_incorrect_password.yml +49 -18
  27. data/spec/fixtures/vcr_cassettes/Auth0_Api_AuthenticationEndpoints/_login_with_resource_owner/should_fail_with_an_invalid_audience.yml +46 -15
  28. data/spec/fixtures/vcr_cassettes/Auth0_Api_AuthenticationEndpoints/_login_with_resource_owner/should_login_successfully_with_a_custom_audience.yml +49 -79
  29. data/spec/fixtures/vcr_cassettes/Auth0_Api_AuthenticationEndpoints/_login_with_resource_owner/should_login_successfully_with_a_default_scope.yml +49 -80
  30. data/spec/fixtures/vcr_cassettes/Auth0_Api_AuthenticationEndpoints/_saml_metadata/should_retrieve_SAML_metadata.yml +74 -21
  31. data/spec/fixtures/vcr_cassettes/Auth0_Api_AuthenticationEndpoints/_userinfo/should_fail_as_not_authorized.yml +53 -14
  32. data/spec/fixtures/vcr_cassettes/Auth0_Api_AuthenticationEndpoints/_userinfo/should_return_the_userinfo.yml +106 -48
  33. data/spec/fixtures/vcr_cassettes/Auth0_Api_AuthenticationEndpoints/_wsfed_metadata/should_retrieve_WSFED_metadata.yml +62 -19
  34. data/spec/fixtures/vcr_cassettes/Auth0_Api_AuthenticationEndpoints/create_test_user.yml +57 -22
  35. data/spec/fixtures/vcr_cassettes/Auth0_Api_AuthenticationEndpoints/delete_test_user.yml +38 -20
  36. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Blacklists/_add_token_to_blacklist/should_add_a_token_to_the_blacklist.yml +37 -19
  37. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Blacklists/_blacklisted_tokens/should_get_the_added_token_from_the_blacklist.yml +38 -23
  38. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_ClientGrants/_client_grants/should_return_at_least_1_result.yml +42 -26
  39. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_ClientGrants/_client_grants/should_return_the_test_client_grant.yml +42 -26
  40. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_ClientGrants/_delete_client_grant/should_delete_the_test_client_grant.yml +38 -20
  41. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_ClientGrants/_patch_client_grant/should_update_the_test_client_grant.yml +39 -27
  42. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_ClientGrants/create_test_client.yml +44 -82
  43. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_ClientGrants/create_test_client_grant.yml +39 -27
  44. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_ClientGrants/delete_test_client.yml +38 -20
  45. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_ClientGrants/delete_test_client_grant.yml +38 -20
  46. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Clients/_client/_filters/should_exclude_and_include_fields_properly.yml +43 -56
  47. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Clients/_client/_filters/should_include_the_specified_fields.yml +39 -28
  48. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Clients/_client/should_get_the_test_client.yml +43 -57
  49. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Clients/_clients/_filters/should_exclude_fields_not_specified.yml +43 -24
  50. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Clients/_clients/_filters/should_exclude_the_specified_fields.yml +236 -96
  51. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Clients/_clients/_filters/should_include_the_specified_fields.yml +52 -27
  52. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Clients/_clients/_filters/should_paginate_results.yml +39 -25
  53. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Clients/_clients/should_get_at_least_one_client.yml +235 -96
  54. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Clients/_delete_client/should_delete_the_test_client_without_an_error.yml +38 -20
  55. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Clients/_patch_client/should_update_the_client_with_the_correct_attributes.yml +43 -57
  56. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Clients/create_test_client.yml +45 -82
  57. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Connections/_connection/_filters/should_exclude_the_fields_indicated.yml +15 -16
  58. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Connections/_connection/_filters/should_include_the_fields_indicated.yml +15 -16
  59. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Connections/_connection/should_find_the_correct_connection.yml +15 -16
  60. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Connections/_connections/_filters/should_include_previously-created_connection_when_filtered.yml +14 -15
  61. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Connections/_connections/_filters/should_should_exclude_the_fields_indicated_from_filtered_results.yml +14 -15
  62. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Connections/_connections/_filters/should_should_include_the_fields_indicated_from_filtered_results.yml +14 -15
  63. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Connections/_connections/should_include_the_previously_created_connection.yml +15 -15
  64. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Connections/_connections/should_not_be_empty.yml +15 -15
  65. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Connections/_delete_connection/should_delete_the_connection.yml +15 -16
  66. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Connections/_delete_connection_user/should_delete_the_user_created.yml +29 -27
  67. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Connections/_update_connection/should_update_the_connection.yml +15 -16
  68. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Connections/create_test_connection.yml +16 -17
  69. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Connections/create_test_user.yml +40 -32
  70. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_DeviceCredentials/create_test_credential.yml +40 -30
  71. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_DeviceCredentials/create_test_user.yml +40 -32
  72. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_DeviceCredentials/delete_test_user.yml +38 -20
  73. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Emails/_configure_provider/should_configure_a_new_email_provider.yml +37 -28
  74. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Emails/_delete_provider/should_delete_the_existing_email_provider_without_an_error.yml +37 -19
  75. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Emails/_delete_provider/should_throw_an_error_trying_to_get_the_email_provider.yml +34 -16
  76. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Emails/_get_provider/_filters/should_get_the_existing_email_provider_with_specific_fields.yml +36 -27
  77. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Emails/_get_provider/_filters/should_get_the_existing_email_provider_without_specific_fields.yml +36 -28
  78. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Emails/_get_provider/should_get_the_existing_email_provider.yml +36 -28
  79. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Emails/_update_provider/should_update_the_existing_email_provider.yml +36 -28
  80. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Emails/delete_existing_provider.yml +37 -19
  81. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Jobs/_export_users_and_get_job/should_create_an_export_users_job_successfully.yml +38 -23
  82. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Jobs/_export_users_and_get_job/should_get_the_export_users_job.yml +76 -46
  83. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Jobs/_import_users_and_get_job/should_create_an_import_users_job_successfully.yml +46 -25
  84. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Jobs/_import_users_and_get_job/should_get_the_import_users_job.yml +84 -48
  85. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Jobs/_send_verification_email_and_get_job/should_create_a_new_verification_email_job.yml +77 -47
  86. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Jobs/_send_verification_email_and_get_job/should_get_the_completed_verification_email.yml +116 -71
  87. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Jobs/_send_verification_email_and_get_job/should_reject_an_invalid_client_id.yml +76 -39
  88. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Jobs/delete_imported_user.yml +76 -43
  89. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Jobs/search_for_connection_id.yml +43 -23
  90. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Logs/_log/should_match_the_created_log_entry.yml +224 -196
  91. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Logs/_log/should_not_be_empty.yml +224 -196
  92. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Logs/_logs/_filters/should_exclude_fields_not_specified.yml +38 -25
  93. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Logs/_logs/_filters/should_exclude_the_specified_fields.yml +39 -39
  94. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Logs/_logs/_filters/should_have_one_log_entry.yml +41 -40
  95. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Logs/_logs/_filters/should_include_the_specified_fields.yml +38 -26
  96. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Logs/_logs/_from/should_take_one_log_entry.yml +229 -189
  97. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Logs/create_test_user.yml +40 -32
  98. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Logs/delete_test_disabled_rule.yml +38 -20
  99. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Logs/delete_test_enabled_rule.yml +38 -20
  100. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Logs/delete_test_user.yml +38 -20
  101. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_ResourceServers/_delete_resource_server/should_delete_the_test_server_without_an_error.yml +38 -20
  102. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_ResourceServers/_patch_resource_server/should_update_the_resource_server_with_the_correct_attributes.yml +39 -24
  103. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_ResourceServers/_resource_server/should_get_the_test_server.yml +39 -24
  104. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_ResourceServers/_resource_servers/should_get_the_test_server.yml +146 -23
  105. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_ResourceServers/_resource_servers/should_return_at_least_1_result.yml +146 -23
  106. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_ResourceServers/_resource_servers/should_return_the_first_page_of_one_result.yml +131 -24
  107. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_ResourceServers/create_test_server.yml +40 -25
  108. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_ResourceServers/delete_test_server.yml +38 -20
  109. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Roles/_add_role_permissions/should_add_a_Permission_to_the_Role_successfully.yml +39 -32
  110. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Roles/_add_role_users/should_add_a_User_to_the_Role_successfully.yml +38 -31
  111. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Roles/_delete_role/should_delete_the_Role_successfully.yml +42 -30
  112. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Roles/_get_role/should_get_the_Role_successfully.yml +38 -30
  113. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Roles/_get_role_permissions/should_get_exactly_1_Permission.yml +37 -30
  114. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Roles/_get_role_permissions/should_get_the_added_Permission_from_the_Role_successfully.yml +37 -30
  115. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Roles/_get_role_users/should_get_exactly_1_User.yml +37 -30
  116. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Roles/_get_role_users/should_get_the_added_User_from_the_Role_successfully.yml +37 -30
  117. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Roles/_get_roles/should_get_the_Role_successfully.yml +37 -29
  118. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Roles/_remove_role_permissions/should_remove_a_Permission_from_the_Role_successfully.yml +39 -29
  119. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Roles/_update_role/should_update_the_Role_successfully.yml +37 -30
  120. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Roles/create_test_api.yml +38 -31
  121. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Roles/create_test_role.yml +39 -31
  122. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Roles/create_test_user.yml +38 -31
  123. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Roles/delete_test_api.yml +36 -26
  124. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Roles/delete_test_user.yml +36 -26
  125. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Rules/_delete_rule/should_delete_the_test_disabled_rule_without_an_error.yml +38 -20
  126. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Rules/_delete_rule/should_delete_the_test_enabled_rule_without_an_error.yml +38 -20
  127. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Rules/_rule/_filters/should_exclude_the_fields_not_specified.yml +40 -27
  128. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Rules/_rule/_filters/should_exclude_the_specified_fields.yml +40 -27
  129. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Rules/_rule/_filters/should_include_the_specified_fields.yml +39 -26
  130. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Rules/_rule/should_get_a_specific_rule.yml +40 -27
  131. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Rules/_rules/_filters/should_exclude_fields_not_specified.yml +80 -24
  132. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Rules/_rules/_filters/should_include_the_specified_fields.yml +82 -25
  133. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Rules/_rules/_filters/should_return_at_least_1_disabled_rule.yml +59 -27
  134. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Rules/_rules/_filters/should_return_at_least_1_enabled_rule.yml +68 -26
  135. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Rules/_rules/_filters/should_return_paginated_results.yml +78 -49
  136. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Rules/_rules/should_return_at_least_1_rule.yml +84 -28
  137. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Rules/_update_rule/should_update_the_disabled_rule_to_be_enabled.yml +40 -27
  138. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Rules/create_test_disabled_rule.yml +42 -29
  139. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Rules/create_test_enabled_rule.yml +42 -29
  140. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Stats/_active_users/should_have_at_least_one_active_user.yml +38 -23
  141. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Stats/_daily_stats/should_have_at_least_one_stats_entry_for_the_timeframe.yml +38 -27
  142. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Tenants/_get_tenant_settings/should_get_the_tenant_settings.yml +72 -59
  143. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Tenants/_get_tenant_settings_with_specific_fields/should_exclude_a_field_not_requested.yml +38 -25
  144. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Tenants/_get_tenant_settings_with_specific_fields/should_include_the_field_requested.yml +38 -25
  145. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Tenants/_update_tenant_settings/should_revert_the_tenant_name.yml +72 -58
  146. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Tenants/_update_tenant_settings/should_update_the_tenant_settings_with_a_new_tenant_name.yml +72 -58
  147. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Tickets/_post_email_verification/should_create_an_email_verification_ticket.yml +39 -26
  148. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Tickets/_post_password_change/should_create_a_password_change_ticket.yml +39 -26
  149. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Tickets/create_test_user.yml +40 -32
  150. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Tickets/delete_test_user.yml +38 -20
  151. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Users/_add_user_permissions/should_add_a_Permissions_for_a_User_successfully.yml +39 -35
  152. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Users/_add_user_roles/should_add_a_Role_to_a_User_successfully.yml +37 -25
  153. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Users/_delete_user/should_delete_the_User_successfully.yml +36 -24
  154. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Users/_delete_user/should_delete_the_secondary_User_successfully.yml +36 -24
  155. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Users/_delete_user_provider/should_attempt_to_delete_the_MFA_provider_for_the_User.yml +36 -24
  156. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Users/_get_enrollments/should_get_Enrollments_for_a_User_successfully.yml +37 -28
  157. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Users/_get_user_permissions/should_get_exactly_1_Permission_for_a_User_successfully.yml +37 -28
  158. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Users/_get_user_permissions/should_get_the_correct_Permission_for_a_User_successfully.yml +37 -28
  159. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Users/_get_user_roles/should_get_Roles_for_a_User_successfully.yml +38 -28
  160. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Users/_invalidate_browsers/should_invalidate_MFA_browsers_for_the_User_successfully.yml +36 -24
  161. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Users/_link_user_account/should_link_two_Users_successfully.yml +38 -29
  162. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Users/_patch_user/should_patch_the_User_successfully.yml +38 -28
  163. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Users/_remove_user_permissions/should_remove_a_Permission_from_a_User_successfully.yml +39 -27
  164. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Users/_remove_user_roles/should_remove_a_Role_from_a_User_successfully.yml +39 -27
  165. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Users/_unlink_user_account/should_unlink_two_Users_successfully.yml +37 -28
  166. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Users/_user/_filters/should_exclude_fields_not_indicated.yml +37 -28
  167. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Users/_user/_filters/should_exclude_the_fields_indicated.yml +37 -28
  168. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Users/_user/_filters/should_include_the_fields_indicated.yml +37 -28
  169. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Users/_user/should_retrieve_the_created_user.yml +37 -28
  170. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Users/_user_logs/should_get_Logs_for_a_User_successfully.yml +42 -30
  171. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Users/_users/_filters/is_expected_to_find_a_user_with_a_v2_search_engine_query.yml +36 -27
  172. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Users/_users/_filters/is_expected_to_find_a_user_with_a_v3_search_engine_query.yml +36 -27
  173. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Users/_users/_filters/should_exclude_the_indicated_fields_when_paginated.yml +36 -27
  174. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Users/_users/_filters/should_include_the_indicated_fields_when_paginated.yml +36 -27
  175. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Users/_users/_filters/should_not_include_other_fields_when_paginated.yml +36 -27
  176. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Users/_users/_filters/should_return_the_correct_number_of_results_when_paginated.yml +36 -27
  177. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Users/_users/should_have_at_least_one_user.yml +37 -27
  178. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Users/create_secondary_test_user.yml +38 -29
  179. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Users/create_test_api.yml +38 -29
  180. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Users/create_test_role.yml +39 -29
  181. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Users/create_test_user.yml +38 -29
  182. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Users/delete_test_api.yml +36 -24
  183. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Users/delete_test_role.yml +42 -28
  184. data/spec/integration/lib/auth0/api/v2/api_client_grants_spec.rb +0 -9
  185. data/spec/integration/lib/auth0/api/v2/api_connections_spec.rb +3 -3
  186. data/spec/integration/lib/auth0/api/v2/api_email_spec.rb +0 -31
  187. data/spec/integration/lib/auth0/api/v2/api_jobs_spec.rb +4 -4
  188. data/spec/integration/lib/auth0/api/v2/api_logs_spec.rb +5 -5
  189. data/spec/integration/lib/auth0/api/v2/api_roles_spec.rb +3 -3
  190. data/spec/integration/lib/auth0/api/v2/api_tickets_spec.rb +4 -4
  191. data/spec/integration/lib/auth0/api/v2/api_user_blocks_spec.rb +8 -5
  192. data/spec/integration/lib/auth0/api/v2/api_users_spec.rb +6 -33
  193. data/spec/lib/auth0/api/v2/actions_spec.rb +312 -0
  194. data/spec/lib/auth0/api/v2/attack_protection_spec.rb +132 -0
  195. data/spec/lib/auth0/api/v2/blacklists_spec.rb +2 -2
  196. data/spec/lib/auth0/api/v2/branding_spec.rb +4 -3
  197. data/spec/lib/auth0/api/v2/client_grants_spec.rb +8 -8
  198. data/spec/lib/auth0/api/v2/clients_spec.rb +12 -12
  199. data/spec/lib/auth0/api/v2/connections_spec.rb +10 -10
  200. data/spec/lib/auth0/api/v2/device_credentials_spec.rb +4 -4
  201. data/spec/lib/auth0/api/v2/emails_spec.rb +2 -2
  202. data/spec/lib/auth0/api/v2/grants_spec.rb +91 -0
  203. data/spec/lib/auth0/api/v2/jobs_spec.rb +18 -17
  204. data/spec/lib/auth0/api/v2/log_streams_spec.rb +3 -3
  205. data/spec/lib/auth0/api/v2/logs_spec.rb +2 -2
  206. data/spec/lib/auth0/api/v2/organizations_spec.rb +13 -13
  207. data/spec/lib/auth0/api/v2/resource_servers_spec.rb +5 -5
  208. data/spec/lib/auth0/api/v2/roles_spec.rb +22 -22
  209. data/spec/lib/auth0/api/v2/rules_spec.rb +7 -7
  210. data/spec/lib/auth0/api/v2/stats_spec.rb +1 -1
  211. data/spec/lib/auth0/api/v2/tenants_spec.rb +1 -1
  212. data/spec/lib/auth0/api/v2/tickets_spec.rb +14 -14
  213. data/spec/lib/auth0/api/v2/user_blocks_spec.rb +2 -2
  214. data/spec/lib/auth0/api/v2/users_by_email_spec.rb +2 -2
  215. data/spec/lib/auth0/api/v2/users_spec.rb +23 -23
  216. data/spec/lib/auth0/client_spec.rb +3 -3
  217. data/spec/lib/auth0/mixins/httpproxy_spec.rb +293 -0
  218. data/spec/lib/auth0/mixins/initializer_spec.rb +87 -0
  219. data/spec/lib/auth0/mixins/token_management_spec.rb +125 -0
  220. data/spec/spec_helper.rb +1 -1
  221. data/spec/support/dummy_class_for_proxy.rb +1 -0
  222. data/spec/support/dummy_class_for_tokens.rb +17 -0
  223. metadata +35 -25
  224. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Anomaly/_check_if_ip_is_blocked/should_return_200_response_code.yml +0 -65
  225. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_Anomaly/_remove_ip_block/should_remove_an_IP_successfully.yml +0 -60
  226. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_ClientGrants/_client_grants/should_return_the_first_page_of_one_result.yml +0 -66
  227. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_DeviceCredentials/_delete_device_credential/should_delete_the_test_credential_without_an_error.yml +0 -54
  228. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_DeviceCredentials/_device_credentials/_filter_by_type/should_exclude_the_test_credential.yml +0 -59
  229. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_DeviceCredentials/_device_credentials/should_have_at_least_1_entry.yml +0 -62
  230. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_DeviceCredentials/_device_credentials/should_include_the_test_credential.yml +0 -62
  231. data/spec/fixtures/vcr_cassettes/Auth0_Api_V2_DeviceCredentials/delete_test_credential.yml +0 -54
  232. data/spec/integration/lib/auth0/api/v2/api_anomaly_spec.rb +0 -17
  233. data/spec/integration/lib/auth0/api/v2/api_device_credentials_spec.rb +0 -128
@@ -6,6 +6,7 @@ describe Auth0::Mixins::HTTPProxy do
6
6
  dummy_instance = DummyClassForProxy.new
7
7
  dummy_instance.extend(Auth0::Mixins::HTTPProxy)
8
8
  dummy_instance.base_uri = "https://auth0.com"
9
+ dummy_instance.retry_count = 0
9
10
 
10
11
  @instance = dummy_instance
11
12
  @exception = DummyClassForRestClient.new
@@ -152,6 +153,100 @@ describe Auth0::Mixins::HTTPProxy do
152
153
  .and_return(StubResponse.new({}, true, 200))
153
154
  expect { @instance.send(http_method, '/te st#test') }.not_to raise_error
154
155
  end
156
+
157
+ context "when status 429 is recieved on send http #{http_method} method" do
158
+ it "should retry 3 times when retry_count is not set" do
159
+ retry_instance = DummyClassForProxy.new
160
+ retry_instance.extend(Auth0::Mixins::HTTPProxy)
161
+ retry_instance.base_uri = "https://auth0.com"
162
+
163
+ @exception.response = StubResponse.new({}, false, 429)
164
+ allow(RestClient::Request).to receive(:execute).with(method: http_method,
165
+ url: 'https://auth0.com/test',
166
+ timeout: nil,
167
+ headers: { params: {} },
168
+ payload: nil)
169
+ .and_raise(@exception)
170
+ expect(RestClient::Request).to receive(:execute).exactly(4).times
171
+
172
+ expect { retry_instance.send(http_method, '/test') }.to raise_error { |error|
173
+ expect(error).to be_a(Auth0::RateLimitEncountered)
174
+ }
175
+ end
176
+
177
+ it "should retry 2 times when retry_count is set to 2" do
178
+ retry_instance = DummyClassForProxy.new
179
+ retry_instance.extend(Auth0::Mixins::HTTPProxy)
180
+ retry_instance.base_uri = "https://auth0.com"
181
+ retry_instance.retry_count = 2
182
+
183
+ @exception.response = StubResponse.new({}, false, 429)
184
+ allow(RestClient::Request).to receive(:execute).with(method: http_method,
185
+ url: 'https://auth0.com/test',
186
+ timeout: nil,
187
+ headers: { params: {} },
188
+ payload: nil)
189
+ .and_raise(@exception)
190
+ expect(RestClient::Request).to receive(:execute).exactly(3).times
191
+
192
+ expect { retry_instance.send(http_method, '/test') }.to raise_error { |error|
193
+ expect(error).to be_a(Auth0::RateLimitEncountered)
194
+ }
195
+ end
196
+
197
+ it "should not retry when retry_count is set to 0" do
198
+ retry_instance = DummyClassForProxy.new
199
+ retry_instance.extend(Auth0::Mixins::HTTPProxy)
200
+ retry_instance.base_uri = "https://auth0.com"
201
+ retry_instance.retry_count = 0
202
+
203
+ @exception.response = StubResponse.new({}, false, 429)
204
+
205
+ allow(RestClient::Request).to receive(:execute).with(method: http_method,
206
+ url: 'https://auth0.com/test',
207
+ timeout: nil,
208
+ headers: { params: {} },
209
+ payload: nil)
210
+ .and_raise(@exception)
211
+
212
+ expect(RestClient::Request).to receive(:execute).exactly(1).times
213
+ expect { retry_instance.send(http_method, '/test') }.to raise_error { |error|
214
+ expect(error).to be_a(Auth0::RateLimitEncountered)
215
+ }
216
+ end
217
+
218
+ it "should have have random retry times grow with jitter backoff" do
219
+ retry_instance = DummyClassForProxy.new
220
+ retry_instance.extend(Auth0::Mixins::HTTPProxy)
221
+ retry_instance.base_uri = "https://auth0.com"
222
+ retry_instance.retry_count = 2
223
+ time_entries = []
224
+ @time_start
225
+
226
+ @exception.response = StubResponse.new({}, false, 429)
227
+ allow(RestClient::Request).to receive(:execute).with(method: http_method,
228
+ url: 'https://auth0.com/test',
229
+ timeout: nil,
230
+ headers: { params: {} },
231
+ payload: nil) do
232
+
233
+ time_entries.push(Time.now.to_f - @time_start.to_f)
234
+ @time_start = Time.now.to_f # restart the clock
235
+ raise @exception
236
+ end
237
+
238
+ @time_start = Time.now.to_f #start the clock
239
+ retry_instance.send(http_method, '/test') rescue nil
240
+ time_entries_first_set = time_entries.shift(time_entries.length)
241
+
242
+ retry_instance.send(http_method, '/test') rescue nil
243
+ time_entries.each_with_index do |entry, index|
244
+ if index > 0 #skip the first request
245
+ expect(entry != time_entries_first_set[index])
246
+ end
247
+ end
248
+ end
249
+ end
155
250
  end
156
251
  end
157
252
 
@@ -301,6 +396,204 @@ describe Auth0::Mixins::HTTPProxy do
301
396
  .and_return(StubResponse.new(res, true, 404))
302
397
  expect { @instance.send(http_method, '/test') }.to raise_error(Auth0::NotFound, res)
303
398
  end
399
+
400
+ context "when status 429 is recieved on send http #{http_method} method" do
401
+ it "should retry 3 times when retry_count is not set" do
402
+ retry_instance = DummyClassForProxy.new
403
+ retry_instance.extend(Auth0::Mixins::HTTPProxy)
404
+ retry_instance.base_uri = "https://auth0.com"
405
+
406
+ @exception.response = StubResponse.new({}, false, 429)
407
+ allow(RestClient::Request).to receive(:execute).with(method: http_method,
408
+ url: 'https://auth0.com/test',
409
+ timeout: nil,
410
+ headers: nil,
411
+ payload: '{}')
412
+ .and_raise(@exception)
413
+ expect(RestClient::Request).to receive(:execute).exactly(4).times
414
+
415
+ expect { retry_instance.send(http_method, '/test') }.to raise_error { |error|
416
+ expect(error).to be_a(Auth0::RateLimitEncountered)
417
+ }
418
+ end
419
+
420
+ it "should retry 2 times when retry_count is set to 2" do
421
+ retry_instance = DummyClassForProxy.new
422
+ retry_instance.extend(Auth0::Mixins::HTTPProxy)
423
+ retry_instance.base_uri = "https://auth0.com"
424
+ retry_instance.retry_count = 2
425
+
426
+ @exception.response = StubResponse.new({}, false, 429)
427
+ allow(RestClient::Request).to receive(:execute).with(method: http_method,
428
+ url: 'https://auth0.com/test',
429
+ timeout: nil,
430
+ headers: nil,
431
+ payload: '{}')
432
+ .and_raise(@exception)
433
+ expect(RestClient::Request).to receive(:execute).exactly(3).times
434
+
435
+ expect { retry_instance.send(http_method, '/test') }.to raise_error { |error|
436
+ expect(error).to be_a(Auth0::RateLimitEncountered)
437
+ }
438
+ end
439
+
440
+ it "should not retry when retry_count is set to 0" do
441
+ retry_instance = DummyClassForProxy.new
442
+ retry_instance.extend(Auth0::Mixins::HTTPProxy)
443
+ retry_instance.base_uri = "https://auth0.com"
444
+ retry_instance.retry_count = 0
445
+
446
+ @exception.response = StubResponse.new({}, false, 429)
447
+
448
+ allow(RestClient::Request).to receive(:execute).with(method: http_method,
449
+ url: 'https://auth0.com/test',
450
+ timeout: nil,
451
+ headers: nil,
452
+ payload: '{}')
453
+ .and_raise(@exception)
454
+
455
+ expect(RestClient::Request).to receive(:execute).exactly(1).times
456
+ expect { retry_instance.send(http_method, '/test') }.to raise_error { |error|
457
+ expect(error).to be_a(Auth0::RateLimitEncountered)
458
+ }
459
+ end
460
+
461
+ it "should have have random retry times grow with jitter backoff" do
462
+ retry_instance = DummyClassForProxy.new
463
+ retry_instance.extend(Auth0::Mixins::HTTPProxy)
464
+ retry_instance.base_uri = "https://auth0.com"
465
+ retry_instance.retry_count = 2
466
+ time_entries = []
467
+ @time_start
468
+
469
+ @exception.response = StubResponse.new({}, false, 429)
470
+ allow(RestClient::Request).to receive(:execute).with(method: http_method,
471
+ url: 'https://auth0.com/test',
472
+ timeout: nil,
473
+ headers: nil,
474
+ payload: '{}') do
475
+
476
+ time_entries.push(Time.now.to_f - @time_start.to_f)
477
+ @time_start = Time.now.to_f # restart the clock
478
+ raise @exception
479
+ end
480
+
481
+ @time_start = Time.now.to_f #start the clock
482
+ retry_instance.send(http_method, '/test') rescue nil
483
+ time_entries_first_set = time_entries.shift(time_entries.length)
484
+
485
+ retry_instance.send(http_method, '/test') rescue nil
486
+ time_entries.each_with_index do |entry, index|
487
+ if index > 0 #skip the first request
488
+ expect(entry != time_entries_first_set[index])
489
+ end
490
+ end
491
+ end
492
+ end
493
+ end
494
+ end
495
+
496
+ context "Renewing tokens" do
497
+ before :each do
498
+ @token_instance = DummyClassForTokens.new(
499
+ client_id: 'test-client-id',
500
+ client_secret: 'test-client-secret',
501
+ domain: 'auth0.com')
502
+ end
503
+
504
+ %i(get delete).each do |http_method|
505
+ context "for #{http_method}" do
506
+ it 'should renew the token' do
507
+ expect(RestClient::Request).to receive(:execute).with(hash_including(
508
+ method: :post,
509
+ url: 'https://auth0.com/oauth/token',
510
+ ) ).and_return(StubResponse.new({
511
+ "access_token" => "access_token",
512
+ "expires_in" => 86400},
513
+ true,
514
+ 200))
515
+
516
+ expect(RestClient::Request).to receive(:execute).with(hash_including(
517
+ method: http_method,
518
+ url: 'https://auth0.com/test',
519
+ headers: { params: {}, "Authorization" => "Bearer access_token" }
520
+ )).and_return(StubResponse.new('Some random text here', true, 200))
521
+
522
+ expect { @token_instance.send(http_method, '/test') }.not_to raise_error
523
+ end
524
+ end
525
+ end
526
+
527
+ %i(post put patch).each do |http_method|
528
+ context "for #{http_method}" do
529
+ it 'should renew the token' do
530
+ expect(RestClient::Request).to receive(:execute).with(hash_including(
531
+ method: :post,
532
+ url: 'https://auth0.com/oauth/token',
533
+ ) ).and_return(StubResponse.new({
534
+ "access_token" => "access_token",
535
+ "expires_in" => 86400},
536
+ true,
537
+ 200))
538
+
539
+ expect(RestClient::Request).to receive(:execute).with(hash_including(
540
+ method: http_method,
541
+ url: 'https://auth0.com/test',
542
+ headers: { "Authorization" => "Bearer access_token" }
543
+ )).and_return(StubResponse.new('Some random text here', true, 200))
544
+
545
+ expect { @token_instance.send(http_method, '/test') }.not_to raise_error
546
+ end
547
+ end
548
+ end
549
+ end
550
+
551
+ context "Using cached tokens" do
552
+ before :each do
553
+ @token_instance = DummyClassForTokens.new(
554
+ client_id: 'test-client-id',
555
+ client_secret: 'test-client-secret',
556
+ domain: 'auth0.com',
557
+ token: 'access_token',
558
+ token_expires_at: Time.now.to_i + 86400)
559
+ end
560
+
561
+ %i(get delete).each do |http_method|
562
+ context "for #{http_method}" do
563
+ it 'should use the cached token' do
564
+ expect(RestClient::Request).not_to receive(:execute).with(hash_including(
565
+ method: :post,
566
+ url: 'https://auth0.com/oauth/token',
567
+ ))
568
+
569
+ expect(RestClient::Request).to receive(:execute).with(hash_including(
570
+ method: http_method,
571
+ url: 'https://auth0.com/test',
572
+ headers: { params: {}, "Authorization" => "Bearer access_token" }
573
+ )).and_return(StubResponse.new('Some random text here', true, 200))
574
+
575
+ expect { @token_instance.send(http_method, '/test') }.not_to raise_error
576
+ end
577
+ end
578
+ end
579
+
580
+ %i(post put patch).each do |http_method|
581
+ context "for #{http_method}" do
582
+ it 'should use the cached token' do
583
+ expect(RestClient::Request).not_to receive(:execute).with(hash_including(
584
+ method: :post,
585
+ url: 'https://auth0.com/oauth/token',
586
+ ))
587
+
588
+ expect(RestClient::Request).to receive(:execute).with(hash_including(
589
+ method: http_method,
590
+ url: 'https://auth0.com/test',
591
+ headers: { "Authorization" => "Bearer access_token" }
592
+ )).and_return(StubResponse.new('Some random text here', true, 200))
593
+
594
+ expect { @token_instance.send(http_method, '/test') }.not_to raise_error
595
+ end
596
+ end
304
597
  end
305
598
  end
306
599
  end
@@ -1,18 +1,36 @@
1
1
  require 'spec_helper'
2
+ require 'timecop'
2
3
 
3
4
  class MockClass
4
5
  attr_reader :token
5
6
  include Auth0::Mixins::Initializer
6
7
  include Auth0::Mixins::HTTPProxy
7
8
  include Auth0::Mixins::Headers
9
+ include Auth0::Mixins::TokenManagement
8
10
  end
9
11
 
10
12
  describe Auth0::Mixins::Initializer do
11
13
  let(:params) { { namespace: 'samples.auth0.com' } }
12
14
  let(:instance) { DummyClassForProxy.send(:include, described_class).new(params) }
15
+ let(:time_now) { Time.now }
16
+
13
17
  context 'api v2' do
18
+ it 'sets retry_count when passed' do
19
+ params[:token] = '123'
20
+ params[:retry_count] = 10
21
+
22
+ expect(instance.instance_variable_get('@retry_count')).to eq(10)
23
+ end
24
+ end
25
+
26
+ context 'token initialization' do
14
27
  before do
15
28
  params[:api_version] = 2
29
+ Timecop.freeze(time_now)
30
+ end
31
+
32
+ after do
33
+ Timecop.return
16
34
  end
17
35
 
18
36
  it 'sets token when access_token is passed' do
@@ -26,5 +44,74 @@ describe Auth0::Mixins::Initializer do
26
44
 
27
45
  expect(instance.instance_variable_get('@token')).to eq('123')
28
46
  end
47
+
48
+ it 'fetches a token if none was given' do
49
+ params[:client_id] = client_id = 'test_client_id'
50
+ params[:client_secret] = client_secret = 'test_client_secret'
51
+ params[:api_identifier] = api_identifier = 'test'
52
+
53
+ payload = {
54
+ grant_type: 'client_credentials',
55
+ client_id: client_id,
56
+ client_secret: client_secret,
57
+ audience: api_identifier
58
+ }
59
+
60
+ expect(RestClient::Request).to receive(:execute).with(hash_including(
61
+ method: :post,
62
+ url: 'https://samples.auth0.com/oauth/token',
63
+ payload: payload.to_json
64
+ ))
65
+ .and_return(StubResponse.new({
66
+ "access_token" => "test",
67
+ "expires_in" => 86400},
68
+ true,
69
+ 200))
70
+
71
+ expect(instance.instance_variable_get('@token')).to eq('test')
72
+ expect(instance.instance_variable_get('@token_expires_at')).to eq(time_now.to_i + 86400)
73
+ end
74
+
75
+ it "doesn't get a new token if one was supplied using 'token'" do
76
+ params[:token] = 'access-token'
77
+
78
+ expect(RestClient::Request).not_to receive(:execute).with(hash_including(
79
+ method: :post,
80
+ url: 'https://samples.auth0.com/oauth/token',
81
+ ))
82
+
83
+ expect(instance.instance_variable_get('@token')).to eq('access-token')
84
+ expect(instance.instance_variable_get('@token_expires_at')).to eq(Time.now.to_i + 3600)
85
+ end
86
+
87
+ it "doesn't get a new token if one was supplied using 'access_token'" do
88
+ params[:access_token] = 'access-token'
89
+
90
+ expect(RestClient::Request).not_to receive(:execute).with(hash_including(
91
+ method: :post,
92
+ url: 'https://samples.auth0.com/oauth/token',
93
+ ))
94
+
95
+ expect(instance.instance_variable_get('@token')).to eq('access-token')
96
+ expect(instance.instance_variable_get('@token_expires_at')).to eq(Time.now.to_i + 3600)
97
+ end
98
+
99
+ it 'can supply token_expires_at option' do
100
+ params[:token] = 'access-token'
101
+ params[:token_expires_at] = time_now.to_i + 300
102
+
103
+ expect(RestClient::Request).not_to receive(:execute).with(hash_including(
104
+ method: :post,
105
+ url: 'https://samples.auth0.com/oauth/token',
106
+ ))
107
+
108
+ expect(instance.instance_variable_get('@token')).to eq('access-token')
109
+ expect(instance.instance_variable_get('@token_expires_at')).to eq(time_now.to_i + 300)
110
+ end
111
+
112
+ it 'throws if no token or credentials were given' do
113
+ params[:client_id] = 'test-client-id'
114
+ expect { instance }.to raise_error(Auth0::InvalidCredentials)
115
+ end
29
116
  end
30
117
  end
@@ -0,0 +1,125 @@
1
+ require 'spec_helper'
2
+ require 'timecop'
3
+
4
+ describe Auth0::Mixins::TokenManagement do
5
+ let(:client_id) { 'test-client-id' }
6
+ let(:client_secret) { 'test-client-secret' }
7
+ let(:api_identifier) { 'test-audience' }
8
+ let(:domain) { 'samples.auth0.com' }
9
+
10
+ let(:payload) { {
11
+ grant_type: 'client_credentials',
12
+ client_id: client_id,
13
+ client_secret: client_secret,
14
+ audience: api_identifier
15
+ } }
16
+
17
+ let(:params) { {
18
+ domain: domain,
19
+ client_id: client_id,
20
+ client_secret: client_secret,
21
+ api_identifier: api_identifier
22
+ } }
23
+
24
+ let(:instance) { DummyClassForTokens.send(:include, described_class).new(params) }
25
+ let(:time_now) { Time.now }
26
+
27
+ before :each do
28
+ Timecop.freeze(time_now)
29
+ end
30
+
31
+ after :each do
32
+ Timecop.return
33
+ end
34
+
35
+ context 'get_token' do
36
+ it 'renews the token if there is no token set' do
37
+ expect(RestClient::Request).to receive(:execute).with(hash_including(
38
+ method: :post,
39
+ url: 'https://samples.auth0.com/oauth/token',
40
+ payload: payload.to_json
41
+ ))
42
+ .and_return(StubResponse.new({
43
+ "access_token" => "test",
44
+ "expires_in" => 86400},
45
+ true,
46
+ 200))
47
+
48
+ instance.send(:get_token)
49
+
50
+ expect(instance.instance_variable_get('@token')).to eq('test')
51
+ expect(instance.instance_variable_get('@token_expires_at')).to eq(time_now.to_i + 86400)
52
+ end
53
+
54
+ it 'does not renew the token if the expiry time has not been reached' do
55
+ params[:token] = 'test-token'
56
+ params[:token_expires_at] = time_now.to_i + 86400
57
+
58
+ expect(RestClient::Request).not_to receive(:execute).with(hash_including(
59
+ method: :post,
60
+ url: 'https://samples.auth0.com/oauth/token',
61
+ ))
62
+
63
+ instance.send(:get_token)
64
+
65
+ expect(instance.instance_variable_get('@token')).to eq('test-token')
66
+ expect(instance.instance_variable_get('@token_expires_at')).to eq(time_now.to_i + 86400)
67
+ end
68
+
69
+ it 'renews the token if within 10 seconds of the expiry' do
70
+ params[:token] = 'test-token'
71
+ params[:token_expires_at] = time_now.to_i + 5
72
+
73
+ expect(RestClient::Request).to receive(:execute).with(hash_including(
74
+ method: :post,
75
+ url: 'https://samples.auth0.com/oauth/token',
76
+ payload: payload.to_json
77
+ ))
78
+ .and_return(StubResponse.new({
79
+ "access_token" => "renewed_token",
80
+ "expires_in" => 86400},
81
+ true,
82
+ 200))
83
+
84
+ instance.send(:get_token)
85
+
86
+ expect(instance.instance_variable_get('@token')).to eq('renewed_token')
87
+ expect(instance.instance_variable_get('@token_expires_at')).to eq(time_now.to_i + 86400)
88
+ end
89
+
90
+ it 'renews the token if past the expiry' do
91
+ params[:token] = 'test-token'
92
+ params[:token_expires_at] = time_now.to_i - 10
93
+
94
+ expect(RestClient::Request).to receive(:execute).with(hash_including(
95
+ method: :post,
96
+ url: 'https://samples.auth0.com/oauth/token',
97
+ payload: payload.to_json
98
+ ))
99
+ .and_return(StubResponse.new({
100
+ "access_token" => "renewed_token",
101
+ "expires_in" => 86400},
102
+ true,
103
+ 200))
104
+
105
+ instance.send(:get_token)
106
+
107
+ expect(instance.instance_variable_get('@token')).to eq('renewed_token')
108
+ expect(instance.instance_variable_get('@token_expires_at')).to eq(time_now.to_i + 86400)
109
+ end
110
+
111
+ it 'does not renew existing token if no token_expires_at' do
112
+ params[:token] = 'test-token'
113
+
114
+ expect(RestClient::Request).not_to receive(:execute).with(hash_including(
115
+ method: :post,
116
+ url: 'https://samples.auth0.com/oauth/token',
117
+ ))
118
+
119
+ instance.send(:get_token)
120
+
121
+ expect(instance.instance_variable_get('@token')).to eq('test-token')
122
+ expect(instance.instance_variable_get('@token_expires_at')).to be_nil
123
+ end
124
+ end
125
+ end
data/spec/spec_helper.rb CHANGED
@@ -62,7 +62,7 @@ rescue RSpec::Expectations::ExpectationNotMetError => e
62
62
  end
63
63
 
64
64
  def entity_suffix
65
- 'rubytest'
65
+ 'rubytest-210908'
66
66
  end
67
67
 
68
68
  puts "Entity suffix is #{entity_suffix}"
@@ -1,4 +1,5 @@
1
1
  class DummyClassForProxy
2
2
  include Auth0::Mixins::HTTPProxy
3
3
  include Auth0::Mixins::Headers
4
+ include Auth0::Mixins::TokenManagement
4
5
  end
@@ -0,0 +1,17 @@
1
+ class DummyClassForTokens
2
+ include Auth0::Mixins::HTTPProxy
3
+ include Auth0::Mixins::Headers
4
+ include Auth0::Mixins::TokenManagement
5
+ include Auth0::Mixins::Initializer
6
+
7
+ def initialize(config)
8
+ extend Auth0::Api::AuthenticationEndpoints
9
+ @client_id = config[:client_id]
10
+ @client_secret = config[:client_secret]
11
+ @audience = config[:api_identifier]
12
+ @domain = config[:domain]
13
+ @base_uri = "https://#{@domain}"
14
+ @token = config[:token]
15
+ @token_expires_at = config[:token_expires_at]
16
+ end
17
+ end