Microsoft.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 = 'wl.basic';
  13. const SCOPE_OFFLINE = 'wl.offline_access';
  14. const SCOPE_SIGNIN = 'wl.signin';
  15. const SCOPE_BIRTHDAY = 'wl.birthday';
  16. const SCOPE_CALENDARS = 'wl.calendars';
  17. const SCOPE_CALENDARS_UPDATE = 'wl.calendars_update';
  18. const SCOPE_CONTACTS_BIRTHDAY = 'wl.contacts_birthday';
  19. const SCOPE_CONTACTS_CREATE = 'wl.contacts_create';
  20. const SCOPE_CONTACTS_CALENDARS = 'wl.contacts_calendars';
  21. const SCOPE_CONTACTS_PHOTOS = 'wl.contacts_photos';
  22. const SCOPE_CONTACTS_SKYDRIVE = 'wl.contacts_skydrive';
  23. const SCOPE_EMAILS = 'wl.emails';
  24. const SCOPE_EVENTS_CREATE = 'wl.events_create';
  25. const SCOPE_MESSENGER = 'wl.messenger';
  26. const SCOPE_PHONE_NUMBERS = 'wl.phone_numbers';
  27. const SCOPE_PHOTOS = 'wl.photos';
  28. const SCOPE_POSTAL_ADDRESSES = 'wl.postal_addresses';
  29. const SCOPE_SHARE = 'wl.share';
  30. const SCOPE_SKYDRIVE = 'wl.skydrive';
  31. const SCOPE_SKYDRIVE_UPDATE = 'wl.skydrive_update';
  32. const SCOPE_WORK_PROFILE = 'wl.work_profile';
  33. const SCOPE_APPLICATIONS = 'wl.applications';
  34. const SCOPE_APPLICATIONS_CREATE = 'wl.applications_create';
  35. const SCOPE_IMAP = 'wl.imap';
  36. /**
  37. * MS uses some magical not officialy supported scope to get even moar info like full emailaddresses.
  38. * They agree that giving 3rd party apps access to 3rd party emailaddresses is a pretty lame thing to do so in all
  39. * their wisdom they added this scope because fuck you that's why.
  40. *
  41. * https://github.com/Lusitanian/PHPoAuthLib/issues/214
  42. * http://social.msdn.microsoft.com/Forums/live/en-US/c6dcb9ab-aed4-400a-99fb-5650c393a95d/how-retrieve-users-
  43. * contacts-email-address?forum=messengerconnect
  44. *
  45. * Considering this scope is not officially supported: use with care
  46. */
  47. const SCOPE_CONTACTS_EMAILS = 'wl.contacts_emails';
  48. public function __construct(
  49. CredentialsInterface $credentials,
  50. ClientInterface $httpClient,
  51. TokenStorageInterface $storage,
  52. $scopes = array(),
  53. UriInterface $baseApiUri = null
  54. ) {
  55. parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri);
  56. if (null === $baseApiUri) {
  57. $this->baseApiUri = new Uri('https://apis.live.net/v5.0/');
  58. }
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function getAuthorizationEndpoint()
  64. {
  65. return new Uri('https://login.live.com/oauth20_authorize.srf');
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function getAccessTokenEndpoint()
  71. {
  72. return new Uri('https://login.live.com/oauth20_token.srf');
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. public function getAuthorizationMethod()
  78. {
  79. return static::AUTHORIZATION_METHOD_QUERY_STRING;
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. protected function parseAccessTokenResponse($responseBody)
  85. {
  86. $data = json_decode($responseBody, true);
  87. if (null === $data || !is_array($data)) {
  88. throw new TokenResponseException('Unable to parse response.');
  89. } elseif (isset($data['error'])) {
  90. throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
  91. }
  92. $token = new StdOAuth2Token();
  93. $token->setAccessToken($data['access_token']);
  94. $token->setLifetime($data['expires_in']);
  95. if (isset($data['refresh_token'])) {
  96. $token->setRefreshToken($data['refresh_token']);
  97. unset($data['refresh_token']);
  98. }
  99. unset($data['access_token']);
  100. unset($data['expires_in']);
  101. $token->setExtraParams($data);
  102. return $token;
  103. }
  104. }