Service.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * MageSpecialist
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to info@magespecialist.it so we can send you a copy immediately.
  14. *
  15. * @category MSP
  16. * @package MSP_NoSpam
  17. * @copyright Copyright (c) 2017 Skeeller srl (http://www.magespecialist.it)
  18. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  19. */
  20. namespace MSP\TwoFactorAuth\Model\Provider\Engine\Authy;
  21. use Magento\Framework\App\Config\ScopeConfigInterface;
  22. class Service
  23. {
  24. const XML_PATH_API_KEY = 'msp_securitysuite_twofactorauth/authy/api_key';
  25. const AUTHY_BASE_ENDPOINT = 'https://api.authy.com/';
  26. /**
  27. * @var ScopeConfigInterface
  28. */
  29. private $scopeConfig;
  30. /**
  31. * Service constructor.
  32. * @param ScopeConfigInterface $scopeConfig
  33. */
  34. public function __construct(ScopeConfigInterface $scopeConfig)
  35. {
  36. $this->scopeConfig = $scopeConfig;
  37. }
  38. /**
  39. * Get API key
  40. * @return string
  41. */
  42. public function getApiKey()
  43. {
  44. return $this->scopeConfig->getValue(static::XML_PATH_API_KEY);
  45. }
  46. /**
  47. * Get authy API endpoint
  48. * @param string $path
  49. * @return string
  50. */
  51. public function getProtectedApiEndpoint($path)
  52. {
  53. return static::AUTHY_BASE_ENDPOINT . 'protected/json/' . $path;
  54. }
  55. /**
  56. * Get authy API endpoint
  57. * @param string $path
  58. * @return string
  59. */
  60. public function getOneTouchApiEndpoint($path)
  61. {
  62. return static::AUTHY_BASE_ENDPOINT . 'onetouch/json/' . $path;
  63. }
  64. /**
  65. * Get error from response
  66. * @param array $response
  67. * @return string
  68. */
  69. public function getErrorFromResponse($response)
  70. {
  71. if ($response === false) {
  72. return 'Invalid authy webservice response';
  73. }
  74. if (!isset($response['success']) || !$response['success']) {
  75. return $response['message'];
  76. }
  77. return false;
  78. }
  79. }