Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
DraperStudio committed Feb 28, 2015
0 parents commit 3587618
Show file tree
Hide file tree
Showing 7 changed files with 343 additions and 0 deletions.
58 changes: 58 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Created by https://www.gitignore.io

### Composer ###
composer.phar
vendor/

# Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
# composer.lock


### PhpStorm ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm

*.iml

## Directory-based project format:
.idea/
# if you remove the above rule, at least ignore the following:

# User-specific stuff:
# .idea/workspace.xml
# .idea/tasks.xml
# .idea/dictionaries

# Sensitive or high-churn files:
# .idea/dataSources.ids
# .idea/dataSources.xml
# .idea/sqlDataSources.xml
# .idea/dynamic.xml
# .idea/uiDesigner.xml

# Gradle:
# .idea/gradle.xml
# .idea/libraries

# Mongo Explorer plugin:
# .idea/mongoSettings.xml

## File-based project format:
*.ipr
*.iws

## Plugin-specific files:

# IntelliJ
out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2015 DraperStudio

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
77 changes: 77 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# xREL OAuth1 Provider for Laravel Socialite

[![Scrutinizer Code Quality](https://img.shields.io/scrutinizer/g/SocialiteProviders/xREL.svg?style=flat-square)](https://scrutinizer-ci.com/g/SocialiteProviders/xREL/?branch=master)
[![Latest Stable Version](https://img.shields.io/packagist/v/socialiteproviders/xrel.svg?style=flat-square)](https://packagist.org/packages/socialiteproviders/xrel)
[![Total Downloads](https://img.shields.io/packagist/dt/socialiteproviders/xrel.svg?style=flat-square)](https://packagist.org/packages/socialiteproviders/xrel)
[![Latest Unstable Version](https://img.shields.io/packagist/vpre/socialiteproviders/xrel.svg?style=flat-square)](https://packagist.org/packages/socialiteproviders/xrel)
[![License](https://img.shields.io/packagist/l/socialiteproviders/xrel.svg?style=flat-square)](https://packagist.org/packages/socialiteproviders/xrel)

<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
## Contents

- [Installation](#installation)
- [1. Composer](#1-composer)
- [2. Service Provider](#2-service-provider)
- [3. Add the Event and Listeners](#3-add-the-event-and-listeners)
- [4. Services Array and .env](#4-services-array-and-env)
- [Add to `config/services.php`.](#add-to-configservicesphp)
- [Append provider values to your `.env` file](#append-provider-values-to-your-env-file)
- [Usage](#usage)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->


## Installation

### 1. Composer

```bash
// This assumes that you have composer installed globally
composer require socialiteproviders/xrel
```

### 2. Service Provider

* [See the docs on how to install the `SocialiteProviders` service provider.](https://github.com/SocialiteProviders/Manager#2-service-provider)


### 3. Add the Event and Listeners

* The listener that you will be adding is `'SocialiteProviders\xREL\xRELExtendSocialite@handle',`.

* [See the docs on how to install](https://github.com/SocialiteProviders/Manager#3-add-the-event-and-listeners)

### 4. Services Array and .env

#### Add to `config/services.php`.

```php
'xrel' => [
'client_id' => env('XREL_KEY'),
'client_secret' => env('XREL_SECRET'),
'redirect' => env('XREL_REDIRECT_URI'),
],
```

#### Append provider values to your `.env` file

```php
// other values above
XREL_KEY=yourkeyfortheservice
XREL_SECRET=yoursecretfortheservice
XREL_REDIRECT_URI=https://example.com/login
```

* [See the main docs for more information](https://github.com/SocialiteProviders/Manager#4-services-array-and-env)


## Usage

* You should now be able to use it like you would regularly use Socialite:

```php
return Socialite::with('xrel')->redirect();
```

* [See the main docs for more information](https://github.com/SocialiteProviders/Manager#usage)
18 changes: 18 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "socialiteproviders/xrel",
"description": "xREL.to OAuth1 Provider for Laravel Socialite",
"license": "MIT",
"authors": [{
"name": "DraperStudio",
"email": "[email protected]"
}],
"require": {
"php": ">=5.4.0",
"socialiteproviders/manager": "0.1.*"
},
"autoload": {
"psr-4": {
"SocialiteProviders\\xREL\\": "src/"
}
}
}
25 changes: 25 additions & 0 deletions src/Provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
namespace SocialiteProviders\xREL;

use Laravel\Socialite\One\AbstractProvider;
use Laravel\Socialite\One\User;

class Provider extends AbstractProvider
{
/**
* {@inheritDoc}
*/
public function user()
{
if (!$this->hasNecessaryVerifier()) {
throw new \InvalidArgumentException("Invalid request. Missing OAuth verifier.");
}

$user = $this->server->getUserDetails($token = $this->getToken());

return (new User())->setRaw($user->extra)->map([
'id' => $user->id, 'nickname' => $user->nickname,
'name' => null, 'email' => null, 'avatar' => $user->avatar,
])->setToken($token->getIdentifier(), $token->getSecret());
}
}
125 changes: 125 additions & 0 deletions src/Server.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php
namespace SocialiteProviders\xREL;

use Laravel\Socialite\One\User;
use League\OAuth1\Client\Credentials\TokenCredentials;
use League\OAuth1\Client\Server\Server as BaseServer;

class Server extends BaseServer
{
/**
* {@inheritDoc}
*/
public function urlTemporaryCredentials()
{
return 'http://api.xrel.to/api/oauth/temp_token';
}

/**
* {@inheritDoc}
*/
public function urlAuthorization()
{
return 'http://api.xrel.to/api/oauth/authorize';
}

/**
* {@inheritDoc}
*/
public function urlTokenCredentials()
{
return 'http://api.xrel.to/api/oauth/access_token';
}

/**
* {@inheritDoc}
*/
public function urlUserDetails()
{
return 'http://api.xrel.to/api/user/get_authd_user.json';
}

/**
* {@inheritDoc}
*/
public function userDetails($data, TokenCredentials $tokenCredentials)
{
$user = new User();
$user->id = $data['id'];
$user->nickname = $data['name'];
$user->avatar = array_get($data, 'avatar_url');
$user->extra = array_diff_key($data, array_flip([
'id', 'name', 'avatar_url',
]));

return $user;
}

/**
* {@inheritDoc}
*/
public function userUid($data, TokenCredentials $tokenCredentials)
{
return $data['id'];
}

/**
* {@inheritDoc}
*/
public function userEmail($data, TokenCredentials $tokenCredentials)
{
return;
}

/**
* {@inheritDoc}
*/
public function userScreenName($data, TokenCredentials $tokenCredentials)
{
return $data['name'];
}

/**
* {@inheritDoc}
*/
protected function fetchUserDetails(TokenCredentials $tokenCredentials, $force = true)
{
if (!$this->cachedUserDetailsResponse || $force == true) {
$url = $this->urlUserDetails();

$client = $this->createHttpClient();

$header = $this->protocolHeader('GET', $url, $tokenCredentials);
$authorizationHeader = ['Authorization' => $header];
$headers = $this->buildHttpClientHeaders($authorizationHeader);

try {
$response = $client->get($url, $headers)->send();
} catch (BadResponseException $e) {
$response = $e->getResponse();
$body = $response->getBody();
$statusCode = $response->getStatusCode();

throw new \Exception(
"Received error [$body] with status code [$statusCode] when retrieving token credentials."
);
}

$this->cachedUserDetailsResponse = $this->parseJson($response->getBody());
}

return $this->cachedUserDetailsResponse;
}

/**
* Parse the JSON Response.
*
* @param string $data
*
* @return string
*/
private function parseJson($data)
{
return json_decode(trim(substr($data, 10, -2)), true)['payload'];
}
}
19 changes: 19 additions & 0 deletions src/xRELExtendSocialite.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
namespace SocialiteProviders\xREL;

use SocialiteProviders\Manager\SocialiteWasCalled;

class xRELExtendSocialite
{
/**
* Execute the provider.
*
* @param \SocialiteProviders\Manager\SocialiteWasCalled $socialiteWasCalled
*/
public function handle(SocialiteWasCalled $socialiteWasCalled)
{
$socialiteWasCalled->extendSocialite(
'xrel', __NAMESPACE__.'\Provider', __NAMESPACE__.'\Server'
);
}
}

0 comments on commit 3587618

Please sign in to comment.