1
|
|
|
<?php |
2
|
|
|
namespace Strava\API; |
3
|
|
|
|
4
|
|
|
use League\OAuth2\Client\Entity\User; |
5
|
|
|
use League\OAuth2\Client\Token\AccessToken as AccessToken; |
6
|
|
|
use League\OAuth2\Client\Provider\AbstractProvider as AbstractProvider; |
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Strava OAuth |
11
|
|
|
* The Strava implementation of the OAuth client |
12
|
|
|
* |
13
|
|
|
* @see: https://github.com/thephpleague/oauth2-client |
14
|
|
|
* @author Bas van Dorst |
15
|
|
|
*/ |
16
|
|
|
class OAuth extends AbstractProvider |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
public $scopes = array('write'); |
20
|
|
|
public $responseType = 'json'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @see AbstractProvider::__construct |
24
|
|
|
* @param array $options |
25
|
|
|
*/ |
26
|
8 |
|
public function __construct($options) |
27
|
|
|
{ |
28
|
8 |
|
parent::__construct($options); |
29
|
8 |
|
$this->headers = array( |
|
|
|
|
30
|
|
|
'Authorization' => 'Bearer' |
31
|
8 |
|
); |
32
|
8 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @see AbstractProvider::urlAuthorize |
36
|
|
|
*/ |
37
|
1 |
|
public function urlAuthorize() |
38
|
|
|
{ |
39
|
1 |
|
return 'https://www.strava.com/oauth/authorize'; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @see AbstractProvider::urlAuthorize |
44
|
|
|
*/ |
45
|
1 |
|
public function urlAccessToken() |
46
|
|
|
{ |
47
|
1 |
|
return 'https://www.strava.com/oauth/token'; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @see AbstractProvider::urlUserDetails |
52
|
|
|
*/ |
53
|
1 |
|
public function urlUserDetails(AccessToken $token) |
54
|
|
|
{ |
55
|
1 |
|
return 'https://www.strava.com/api/v3/athlete/?access_token='.$token; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @see AbstractProvider::userDetails |
60
|
|
|
*/ |
61
|
1 |
|
public function userDetails($response, AccessToken $token) |
|
|
|
|
62
|
|
|
{ |
63
|
1 |
|
$user = new \stdClass; |
64
|
|
|
|
65
|
1 |
|
$user->uid = $response->id; |
66
|
1 |
|
$user->name = implode(" ", array($response->firstname, $response->lastname)); |
67
|
1 |
|
$user->firstName = $response->firstname; |
68
|
1 |
|
$user->lastName = $response->lastname; |
69
|
1 |
|
$user->email = $response->email; |
70
|
1 |
|
$user->location = $response->country; |
71
|
1 |
|
$user->imageUrl = $response->profile; |
72
|
1 |
|
$user->gender = $response->sex; |
73
|
|
|
|
74
|
1 |
|
return $user; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @see AbstractProvider::userUid |
79
|
|
|
*/ |
80
|
1 |
|
public function userUid($response, AccessToken $token) |
|
|
|
|
81
|
|
|
{ |
82
|
1 |
|
return $response->id; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @see AbstractProvider::userUid |
87
|
|
|
*/ |
88
|
1 |
|
public function userEmail($response, AccessToken $token) |
|
|
|
|
89
|
|
|
{ |
90
|
1 |
|
return isset($response->email) && $response->email ? $response->email : null; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @see AbstractProvider::userScreenName |
95
|
|
|
*/ |
96
|
1 |
|
public function userScreenName($response, AccessToken $token) |
|
|
|
|
97
|
|
|
{ |
98
|
1 |
|
return implode(" ", array($response->firstname, $response->lastname)); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @see AbstractProvider::getBaseAuthorizationUrl |
103
|
|
|
*/ |
104
|
|
|
public function getBaseAuthorizationUrl() |
105
|
|
|
{ |
106
|
|
|
return 'https://www.strava.com/oauth/authorize'; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @see AbstractProvider::getBaseAccessTokenUrl |
111
|
|
|
*/ |
112
|
|
|
public function getBaseAccessTokenUrl(array $params) |
113
|
|
|
{ |
114
|
|
|
return 'https://www.strava.com/oauth/token'; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @see AbstractProvider::getResourceOwnerDetailsUrl |
119
|
|
|
*/ |
120
|
1 |
|
public function getResourceOwnerDetailsUrl(AccessToken $token) |
121
|
|
|
{ |
122
|
|
|
return ''; |
123
|
1 |
|
} |
124
|
|
|
|
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @see AbstractProvider::getDefaultScopes |
128
|
|
|
*/ |
129
|
1 |
|
protected function getDefaultScopes() |
130
|
1 |
|
{ |
131
|
|
|
return array('view_private', 'write'); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @see AbstractProvider::checkResponse |
137
|
|
|
*/ |
138
|
|
|
protected function checkResponse(ResponseInterface $response, $data) |
139
|
|
|
{ |
140
|
|
|
|
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @see AbstractProvider::createResourceOwner |
145
|
|
|
*/ |
146
|
|
|
protected function createResourceOwner(array $response, AccessToken $token) |
147
|
|
|
{ |
148
|
|
|
|
149
|
|
|
} |
150
|
|
|
} |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: