Azure.php
150 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Handles Token Authorization. |
| 4 | * |
| 5 | * @package embed-outlook-teams-calendar-events/API |
| 6 | */ |
| 7 | |
| 8 | namespace MoEmbedPowerBI\API; |
| 9 | |
| 10 | if ( ! defined( 'ABSPATH' ) ) { |
| 11 | exit; |
| 12 | } |
| 13 | |
| 14 | /** |
| 15 | * Class to handle token authorization and API endpoints' requests. |
| 16 | */ |
| 17 | class Azure { |
| 18 | |
| 19 | /** |
| 20 | * Holds the Azure class instance. |
| 21 | * |
| 22 | * @var Azure |
| 23 | */ |
| 24 | private static $obj; |
| 25 | |
| 26 | /** |
| 27 | * Holds the Azure class endpoints. |
| 28 | * |
| 29 | * @var Azure |
| 30 | */ |
| 31 | private $endpoints; |
| 32 | |
| 33 | /** |
| 34 | * Holds the Azure class config. |
| 35 | * |
| 36 | * @var Azure |
| 37 | */ |
| 38 | private $config; |
| 39 | |
| 40 | /** |
| 41 | * Holds the Azure class scope. |
| 42 | * |
| 43 | * @var Azure |
| 44 | */ |
| 45 | private $scope = 'https://graph.microsoft.com/.default'; |
| 46 | |
| 47 | /** |
| 48 | * Holds the Azure class handler. |
| 49 | * |
| 50 | * @var Azure |
| 51 | */ |
| 52 | private $handler; |
| 53 | |
| 54 | /** |
| 55 | * Constructor of the class. |
| 56 | * |
| 57 | * @param array $config Holds the configurations array. |
| 58 | */ |
| 59 | private function __construct( $config ) { |
| 60 | $this->config = $config; |
| 61 | $this->handler = Authorization::get_controller(); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Object instance(Azure) getter method. |
| 66 | * |
| 67 | * @param array $config Holds the array of configurations. |
| 68 | * @return Azure |
| 69 | */ |
| 70 | public static function get_client( $config ) { |
| 71 | if ( ! isset( self::$obj ) ) { |
| 72 | self::$obj = new Azure( $config ); |
| 73 | self::$obj->set_endpoints(); |
| 74 | } |
| 75 | return self::$obj; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Holds the Azure class endpoints. |
| 80 | * |
| 81 | * @return void |
| 82 | */ |
| 83 | private function set_endpoints() { |
| 84 | $this->endpoints['authorize'] = 'https://login.microsoftonline.com/' . $this->config['tenant_id'] . '/oauth2/v2.0/authorize'; |
| 85 | $this->endpoints['token'] = 'https://login.microsoftonline.com/' . $this->config['tenant_id'] . '/oauth2/v2.0/token'; |
| 86 | $this->endpoints['users'] = 'https://graph.microsoft.com/beta/users/'; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Getter method for the Azure class endpoints. |
| 91 | * |
| 92 | * @param string $endpoint Holds the endpoint name which you want. |
| 93 | * @return array |
| 94 | */ |
| 95 | public function get_endpoints( $endpoint ) { |
| 96 | if ( 'token' === $endpoint ) { |
| 97 | return $this->endpoints['token'];} |
| 98 | if ( 'authorize' === $endpoint ) { |
| 99 | return $this->endpoints['authorize'];} |
| 100 | if ( 'users' === $endpoint ) { |
| 101 | return $this->endpoints['users'];} |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Method to get the specific user detail. |
| 106 | * |
| 107 | * @return array |
| 108 | */ |
| 109 | public function mo_epbr_get_specific_user_detail() { |
| 110 | $this->access_token = $this->handler->mo_epbr_get_access_token( $this->endpoints, $this->config, $this->scope ); |
| 111 | |
| 112 | $args = array( |
| 113 | 'Authorization' => 'Bearer ' . $this->access_token, |
| 114 | ); |
| 115 | $user_info_url = $this->endpoints['users'] . $this->config['upn_id']; |
| 116 | $users = $this->handler->mo_epbr_get_request( $user_info_url, $args ); |
| 117 | if ( ! is_array( $users ) || count( $users ) <= 0 ) { |
| 118 | wp_die( esc_html( 'Unknown error occurred. Please try again later.' ) ); |
| 119 | } |
| 120 | return $users; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Method to get the specific user detail. |
| 125 | * |
| 126 | * @return array |
| 127 | */ |
| 128 | public function mo_epbr_get_new_access_token() { |
| 129 | $access_token = $this->handler->mo_epbr_get_access_token( $this->endpoints, $this->config, $this->scope ); |
| 130 | if ( ! isset( $access_token['error'] ) ) { |
| 131 | $this->access_token = $access_token; |
| 132 | $this->args = array( |
| 133 | 'Authorization' => 'Bearer ' . $access_token, |
| 134 | ); |
| 135 | return $access_token; |
| 136 | } |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Holds the Azure class scope. |
| 142 | * |
| 143 | * @param string $scopes Hold the scope passed to be set. |
| 144 | * @return void |
| 145 | */ |
| 146 | public function set_scope( $scopes ) { |
| 147 | $this->scope = $scopes; |
| 148 | } |
| 149 | } |
| 150 |