From 90664150afaac8867b90305e4063b8559ff19f48 Mon Sep 17 00:00:00 2001 From: Andrew Lau Date: Sun, 13 Nov 2016 11:02:47 +1100 Subject: [PATCH] Initial commit --- .gitignore | 58 +++++++++++++++++++++ .styleci.yml | 1 + LICENSE | 21 ++++++++ README.md | 79 +++++++++++++++++++++++++++++ composer.json | 18 +++++++ src/OpenShiftExtendSocialite.php | 18 +++++++ src/Provider.php | 87 ++++++++++++++++++++++++++++++++ 7 files changed, 282 insertions(+) create mode 100644 .gitignore create mode 100644 .styleci.yml create mode 100644 LICENSE create mode 100644 README.md create mode 100644 composer.json create mode 100644 src/OpenShiftExtendSocialite.php create mode 100644 src/Provider.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..359163a --- /dev/null +++ b/.gitignore @@ -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 diff --git a/.styleci.yml b/.styleci.yml new file mode 100644 index 0000000..974f5fa --- /dev/null +++ b/.styleci.yml @@ -0,0 +1 @@ +preset: symfony diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..2dfcfab --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2016 Andrew Lau + +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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..79af215 --- /dev/null +++ b/README.md @@ -0,0 +1,79 @@ +# OpenShift OAuth2 Provider for Laravel Socialite + +## Documentation + +This package makes use of the `SocialiteProviders` package located [here](http://socialiteproviders.github.io/). + +### Install the package + +```sh +composer require andrewklau/socialite-openshift +``` + +### Install the Service Provider + +* Remove `Laravel\Socialite\SocialiteServiceProvider` from your providers[] array in config\app.php if you have added it already. + +* Add `\SocialiteProviders\Manager\ServiceProvider::class` to your providers[] array in config\app.php. + + +### Install the event listener + +* Add `SocialiteProviders\Manager\SocialiteWasCalled` event to your listen[] array in `/Providers/EventServiceProvider`. + + +* The listener that you add for this provider is `'Andrewklau\Socialite\OpenShift\OpenShiftkExtendSocialite@handle',`. + +For example: + +```php +/** + * The event handler mappings for the application. + * + * @var array + */ +protected $listen = [ + \SocialiteProviders\Manager\SocialiteWasCalled::class => [ + // add your listeners (aka providers) here + 'Andrewklau\Socialite\OpenShift\OpenShiftExtendSocialite@handle', + ], +]; +``` + +### Environment variables + +If you add environment values to your `.env` as exactly shown below, you do not need to add an entry to the services array. + +#### Append to .env + +``` +// other values above +OPENSHIFT_URL=https://api.xyz.com +OPENSHIFT_OAUTH_CLIENT_ID=yourkeyfortheservice +OPENSHIFT_OAUTH_CLIENT_SECRET=yoursecretfortheservice +``` + +#### Append to config/services.php + +You do not need to add this if you add the values to the `.env` exactly as shown above. The values below are provided as a convenience in the case that a developer is not able to use the .env method + +```php +'openshift' => [ + 'client_id' => env('OPENSHIFT_OAUTH_CLIENT_ID'), + 'client_secret' => env('OPENSHIFT_OAUTH_CLIENT_SECRET'), + 'url' => env('OPENSHIFT_URL'), + 'redirect' => env('APP_URL').'/login/callback', +], +``` + +## Usage + +Redirect to OpenShift with the scopes you want to access: + +```php +return Socialite::with('OpenShift')->scopes()->redirect(); +``` + +## License + +MIT diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..347f0ce --- /dev/null +++ b/composer.json @@ -0,0 +1,18 @@ +{ + "name": "andrewklau/socialite-openshift", + "description": "OpenShift OAuth2 Provider for Laravel Socialite", + "license": "MIT", + "authors": [{ + "name": "Andrew Lau", + "email": "andrew@andrewklau.com" + }], + "require": { + "php": ">=5.5.9", + "socialiteproviders/manager": "2.*" + }, + "autoload": { + "psr-4": { + "Andrewklau\\Socialite\\OpenShift\\": "src/" + } + } +} diff --git a/src/OpenShiftExtendSocialite.php b/src/OpenShiftExtendSocialite.php new file mode 100644 index 0000000..5c946e3 --- /dev/null +++ b/src/OpenShiftExtendSocialite.php @@ -0,0 +1,18 @@ +extendSocialite( + 'openshift', __NAMESPACE__.'\Provider' + ); + } +} diff --git a/src/Provider.php b/src/Provider.php new file mode 100644 index 0000000..23aa86e --- /dev/null +++ b/src/Provider.php @@ -0,0 +1,87 @@ +buildAuthUrlFromBase(config('services.openshift.url').'oauth/authorize', $state); + } + + /** + * Get the token URL for the provider. + * + * @return string + */ + protected function getTokenUrl() + { + return config('services.openshift.url').'oauth/token'; + } + + /** + * Get the raw user for the given access token. + * + * @param string $token + * + * @return array + */ + protected function getUserByToken($token) + { + $url = config('services.openshift.url').'oapi/v1/users/~'; + + $response = $this->getHttpClient()->get($url, [ + 'headers' => [ + 'Accept' => 'application/json', + 'Authorization' => 'Bearer '.$token, + ], + ]); + + return json_decode($response->getBody(), true); + } + + /** + * Map the raw user array to a Socialite User instance. + * + * @param array $user + * + * @return \Laravel\Socialite\User + */ + protected function mapUserToObject(array $user) + { + return (new User())->setRaw($user)->map([ + 'id' => $user['metadata']['name'], + ]); + } + + /** + * {@inheritdoc} + */ + protected function getTokenFields($code) + { + return array_merge(parent::getTokenFields($code), [ + 'grant_type' => 'authorization_code', + ]); + } +}