Skip to content

Commit 9518200

Browse files
committed
Initial commit
0 parents  commit 9518200

6 files changed

+202
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 JhaoDa
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# MailRu OAuth2 Provider for Laravel Socialite
2+
3+
### 1. Installation
4+
5+
`composer require jhaoda/socialite-mailru`
6+
7+
### 2. Service Provider
8+
9+
* Remove `Laravel\Socialite\SocialiteServiceProvider` from your `providers[]` array in `config\app.php` if you have added it already.
10+
* Add `SocialiteProviders\Manager\ServiceProvider` to your `providers[]` array in `config\app.php`.
11+
12+
For example:
13+
```php
14+
'providers' => [
15+
// a whole bunch of providers
16+
// remove 'Laravel\Socialite\SocialiteServiceProvider',
17+
'SocialiteProviders\Manager\ServiceProvider', // add
18+
];
19+
```
20+
* Note: If you would like to use the Socialite Facade, you need to [install it](http://laravel.com/docs/5.0/authentication#social-authentication).
21+
22+
### 3. Add the Event and Listeners
23+
24+
* Add `SocialiteProviders\Manager\SocialiteWasCalled` event to your `listen[]` array in `<app_name>/Providers/EventServiceProvider`.
25+
26+
* Add your listeners (i.e. the ones from the providers) to the `SocialiteProviders\Manager\SocialiteWasCalled[]` that you just created.
27+
28+
* The listener that you add for this provider is `'JhaoDa\SocialiteProviders\MailRu\MailRuExtendSocialite@handle'`.
29+
30+
* Note: You do not need to add anything for the built-in socialite providers unless you override them with your own providers.
31+
32+
For example:
33+
```php
34+
/**
35+
* The event handler mappings for the application.
36+
*
37+
* @var array
38+
*/
39+
protected $listen = [
40+
`SocialiteProviders\Manager\SocialiteWasCalled` => [
41+
'JhaoDa\SocialiteProviders\MailRu\MailRuExtendSocialite@handle'
42+
],
43+
];
44+
```
45+
46+
### 4. Services Array and .env
47+
48+
Add to `config/services.php`:
49+
```php
50+
'vkontakte' => [
51+
'client_id' => env('MAILRU_ID'),
52+
'client_secret' => env('MAILRU_SECRET'),
53+
'redirect' => env('MAILRU_REDIRECT'),
54+
],
55+
```
56+
57+
Append provider values to your `.env` file:
58+
```
59+
// other values above
60+
MAILRU_ID=your_app_id_for_the_service
61+
MAILRU_SECRET=your_app_secret_for_the_service
62+
MAILRU_REDIRECT=https://example.com/login
63+
```

composer.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "jhaoda/socialite-mailru",
3+
"description": "Mail.ru OAuth2 Provider for Laravel Socialite",
4+
"license": "MIT",
5+
"authors": [{
6+
"name": "JhaoDa",
7+
"email": "jhaoda@gmail.com"
8+
}],
9+
"require": {
10+
"php": ">=5.4.0",
11+
"socialiteproviders/manager": "0.1.*"
12+
},
13+
"autoload": {
14+
"psr-4": {
15+
"JhaoDa\\SocialiteProviders\\MailRu\\": "src/"
16+
}
17+
}
18+
}

src/MailRuExtendSocialite.php

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
namespace JhaoDa\SocialiteProviders\MailRu;
3+
4+
use SocialiteProviders\Manager\SocialiteWasCalled;
5+
6+
class MailRuExtendSocialite
7+
{
8+
/**
9+
* Register the provider.
10+
*
11+
* @param SocialiteWasCalled $socialiteWasCalled
12+
*/
13+
public function handle(SocialiteWasCalled $socialiteWasCalled)
14+
{
15+
$socialiteWasCalled->extendSocialite(
16+
'mailru', __NAMESPACE__ . '\Provider'
17+
);
18+
}
19+
}

src/Provider.php

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
namespace Jhaoda\SocialiteProviders\MailRu;
4+
5+
use Laravel\Socialite\Two\User;
6+
use Laravel\Socialite\Two\AbstractProvider;
7+
use Laravel\Socialite\Two\ProviderInterface;
8+
9+
class Provider extends AbstractProvider implements ProviderInterface
10+
{
11+
/**
12+
* {@inheritdoc}
13+
*/
14+
protected function getAuthUrl($state)
15+
{
16+
return $this->buildAuthUrlFromBase(
17+
'https://connect.mail.ru/oauth/authorize', $state
18+
);
19+
}
20+
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
protected function getTokenUrl()
25+
{
26+
return 'https://connect.mail.ru/oauth/token';
27+
}
28+
29+
/**
30+
* {@inheritdoc}
31+
*/
32+
protected function getUserByToken($token)
33+
{
34+
$params = [
35+
'secure' => 1,
36+
'format' => 'json',
37+
'method' => 'users.getInfo',
38+
'app_id' => $this->clientId,
39+
'session_key' => $token
40+
];
41+
42+
ksort($params);
43+
44+
$_params = array_map(function($key, $value) {
45+
return $key . '=' . $value;
46+
}, array_keys($params), array_values($params));
47+
48+
$params['sig'] = md5(join('', $_params) . $this->clientSecret);
49+
50+
$response = $this->getHttpClient()->get(
51+
'http://www.appsmail.ru/platform/api?' . http_build_query($params)
52+
);
53+
54+
return json_decode($response->getBody(), true)[0];
55+
}
56+
57+
/**
58+
* {@inheritdoc}
59+
*/
60+
protected function mapUserToObject(array $user)
61+
{
62+
return (new User())->setRaw($user)->map([
63+
'id' => $user['uid'],
64+
'name' => $user['first_name'].' '.$user['last_name'],
65+
'email' => array_get($user, 'email'),
66+
'nickname' => array_reverse(explode('/', $user['link']))[1],
67+
'avatar' => $user['has_pic'] ? $user['pic_190'] : null,
68+
]);
69+
}
70+
71+
/**
72+
* {@inheritdoc}
73+
*/
74+
protected function getTokenFields($code)
75+
{
76+
return array_merge(parent::getTokenFields($code), [
77+
'grant_type' => 'authorization_code',
78+
]);
79+
}
80+
}

0 commit comments

Comments
 (0)