Microsoft.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace OAuth\OAuth2\Service;
  3. use OAuth\OAuth2\Token\StdOAuth2Token;
  4. use OAuth\Common\Http\Exception\TokenResponseException;
  5. use OAuth\Common\Http\Uri\Uri;
  6. use OAuth\Common\Consumer\CredentialsInterface;
  7. use OAuth\Common\Http\Client\ClientInterface;
  8. use OAuth\Common\Storage\TokenStorageInterface;
  9. use OAuth\Common\Http\Uri\UriInterface;
  10. class Microsoft extends AbstractService
  11. {
  12. const SCOPE_BASIC = 'basic';
  13. const SCOPE_OFFLINE_ACCESS = 'offline_access';
  14. const SCOPE_SIGNIN = 'signin';
  15. const SCOPE_BIRTHDAY = 'birthday';
  16. const SCOPE_CALENDARS = 'calendars';
  17. const SCOPE_CALENDARS_UPDATE = 'calendars_update';
  18. const SCOPE_CONTACTS_BIRTHDAY = 'contacts_birthday';
  19. const SCOPE_CONTACTS_CREATE = 'contacts_create';
  20. const SCOPE_CONTACTS_CALENDARS = 'contacts_calendars';
  21. const SCOPE_CONTACTS_PHOTOS = 'contacts_photos';
  22. const SCOPE_CONTACTS_SKYDRIVE = 'contacts_skydrive';
  23. const SCOPE_EMAIL = 'email';
  24. const SCOPE_EVENTS_CREATE = 'events_create';
  25. const SCOPE_MESSENGER = 'messenger';
  26. const SCOPE_OPENID = 'openid';
  27. const SCOPE_PHONE_NUMBERS = 'phone_numbers';
  28. const SCOPE_PHOTOS = 'photos';
  29. const SCOPE_POSTAL_ADDRESSES = 'postal_addresses';
  30. const SCOPE_PROFILE = 'profile';
  31. const SCOPE_SHARE = 'share';
  32. const SCOPE_SKYDRIVE = 'skydrive';
  33. const SCOPE_SKYDRIVE_UPDATE = 'skydrive_update';
  34. const SCOPE_WORK_PROFILE = 'work_profile';
  35. const SCOPE_APPLICATIONS = 'applications';
  36. const SCOPE_APPLICATIONS_CREATE = 'applications_create';
  37. const SCOPE_IMAP = 'imap';
  38. const SOCPE_IMAP_ACCESSASUSERALL = 'https://outlook.office365.com/IMAP.AccessAsUser.All';
  39. const SOCPE_SMTPSEND = 'https://outlook.office365.com/SMTP.Send';
  40. const SOCPE_USERREAD = 'User.Read';
  41. const SOCPE_MAILREAD = 'Mail.Read';
  42. const SOCPE_MAILSEND = 'Mail.Send';
  43. protected $storage;
  44. /**
  45. * MS uses some magical not officialy supported scope to get even moar info like full emailaddresses.
  46. * They agree that giving 3rd party apps access to 3rd party emailaddresses is a pretty lame thing to do so in all
  47. * their wisdom they added this scope because fuck you that's why.
  48. *
  49. * https://github.com/Lusitanian/PHPoAuthLib/issues/214
  50. * http://social.msdn.microsoft.com/Forums/live/en-US/c6dcb9ab-aed4-400a-99fb-5650c393a95d/how-retrieve-users-
  51. * contacts-email-address?forum=messengerconnect
  52. *
  53. * Considering this scope is not officially supported: use with care
  54. */
  55. const SCOPE_CONTACTS_EMAILS = 'contacts_emails';
  56. public function __construct(
  57. CredentialsInterface $credentials,
  58. ClientInterface $httpClient,
  59. TokenStorageInterface $storage,
  60. $scopes = array(),
  61. UriInterface $baseApiUri = null
  62. ) {
  63. parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri);
  64. $this->storage = $storage;
  65. if (null === $baseApiUri) {
  66. $this->baseApiUri = new Uri('https://apis.live.net/v5.0/');
  67. }
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. public function getAuthorizationEndpoint()
  73. {
  74. $tenant = $this->storage->getTenant();
  75. //return new Uri('https://login.live.com/oauth20_authorize.srf');
  76. //return new Uri('https://login.microsoftonline.com/organizations/oauth2/v2.0/authorize');
  77. return new Uri('https://login.microsoftonline.com/'.$tenant.'/oauth2/v2.0/authorize');
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function getAccessTokenEndpoint()
  83. {
  84. $tenant = $this->storage->getTenant();
  85. //return new Uri('https://login.live.com/oauth20_token.srf');
  86. //return new Uri('https://login.microsoftonline.com/organizations/oauth2/v2.0/token');
  87. return new Uri('https://login.microsoftonline.com/'.$tenant.'/oauth2/v2.0/token');
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public function getAuthorizationMethod()
  93. {
  94. return static::AUTHORIZATION_METHOD_QUERY_STRING;
  95. }
  96. /**
  97. * {@inheritdoc}
  98. */
  99. protected function parseAccessTokenResponse($responseBody)
  100. {
  101. $data = json_decode($responseBody, true);
  102. if (null === $data || !is_array($data)) {
  103. throw new TokenResponseException('Unable to parse response.');
  104. } elseif (isset($data['error'])) {
  105. throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
  106. }
  107. //print $data['access_token'];exit;
  108. $token = new StdOAuth2Token();
  109. $token->setAccessToken($data['access_token']);
  110. $token->setLifetime($data['expires_in']);
  111. if (isset($data['refresh_token'])) {
  112. $token->setRefreshToken($data['refresh_token']);
  113. unset($data['refresh_token']);
  114. }
  115. unset($data['access_token']);
  116. unset($data['expires_in']);
  117. $token->setExtraParams($data);
  118. return $token;
  119. }
  120. }