Skip to content

Commit

Permalink
Merge pull request #8 from konsulting/feature__tests_and_improvements
Browse files Browse the repository at this point in the history
Feature  tests and improvements
  • Loading branch information
Keoghan authored Apr 19, 2021
2 parents 45d229f + 5d8deca commit 4ac3907
Show file tree
Hide file tree
Showing 37 changed files with 921 additions and 123 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/vendor/
composer.lock
.phpunit.result.cache
26 changes: 26 additions & 0 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
build:
environment:
php:
version: 7.2
nodes:
analysis:
tests:
override:
- php-scrutinizer-run
-
command: 'vendor/bin/phpunit --coverage-clover=coverage-file'
coverage:
file: 'coverage-file'
format: 'clover'
tests: true
filter:
excluded_paths:
- 'tests/*'
checks:
php: true
build_failure_conditions:
- 'elements.rating(<= D).exists' # No classes/methods with a rating of D or worse
- 'elements.rating(<= B).new.exists' # No new classes/methods with a rating of B or worse
- 'issues.severity(>= MAJOR).new.exists' # New issues of major or higher severity
- 'project.metric("scrutinizer.quality", < 9.5)' # Code Quality Rating drops below 9.5
- 'project.metric_change("scrutinizer.test_coverage", < -0.001)' # Code Coverage decreased from previous inspection less than 0.1%
9 changes: 1 addition & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
language: php

php:
- 5.6
- 7.0
- 7.1
- 7.2
- 7.3

env:
global:
- setup=basic

matrix:
include:
- php: 5.6.4
env: setup=lowest
- php: 5.6.4
env: setup=stable

sudo: false

install:
Expand Down
17 changes: 9 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
"name": "konsulting/laravel-butler",
"description": "Butler manages multiple Socialite authentication for your Laravel app.",
"require-dev": {
"phpunit/phpunit": "~5.7|~6.0|~7.0",
"orchestra/testbench": ">= 3.0 <= 3.7",
"orchestra/testbench-browser-kit": "~3.1@dev",
"orchestra/database": "~3.1@dev"
"phpunit/phpunit": "^8.0",
"orchestra/testbench": "^4.0",
"orchestra/testbench-browser-kit": "^4.0",
"orchestra/database": "^4.0",
"mockery/mockery": "^1.2"
},
"license": "MIT",
"authors": [
Expand All @@ -15,10 +16,10 @@
}
],
"require": {
"php": "^5.6|^7.0",
"nesbot/carbon": "^1.21 || ^2.0 ",
"laravel/socialite": "~3.0",
"laravel/framework": "~5.3.30|~5.4.0|~5.5.0|~5.6.0|~5.7.0|~5.8.0"
"php": "^7.1",
"nesbot/carbon": "^1.21 || ^2.0",
"laravel/socialite": "^4.0",
"laravel/framework": "^5.7 || ^6.0 || ^7.0 || ^8.0"
},
"autoload": {
"psr-4": {
Expand Down
12 changes: 11 additions & 1 deletion config/butler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
*/
'user_provider' => \Konsulting\Butler\EloquentUserProvider::class,

/*
* Social Identities Table Name
*/
'social_identities_table_name' => 'social_identities',

/*
* The user class for mapping identities to.
*/
Expand All @@ -21,7 +26,7 @@
],

/*
* Can Butler create users if social login is requested for non-exiting user?
* Can Butler create users if social login is requested for non-existing user?
*/
'can_create_users' => false,

Expand All @@ -40,6 +45,11 @@
*/
'login_immediately_after_confirm' => false,

/**
* Allow Butler to assicate a new identity to the logged in user, rather than matching on email address.
*/
'can_associate_to_logged_in_user' => false,

/*
* The application routes for us to use when redirecting the user after actions
*/
Expand Down
12 changes: 6 additions & 6 deletions migrations/2016_11_01_000000_create_social_identities_table.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class CreateSocialIdentitiesTable extends Migration
{
Expand All @@ -14,13 +14,13 @@ public function up()
{
Schema::create('social_identities', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned()->index();
$table->string('provider');
$table->string('reference');
$table->text('access_token');
$table->integer('user_id')->unsigned()->index()->nullable();
$table->string('provider')->nullable();
$table->string('reference')->nullable();
$table->text('access_token')->nullable();
$table->string('expires_at')->nullable();
$table->string('refresh_token')->nullable();
$table->string('confirm_token')->default('');
$table->string('confirm_token')->nullable();
$table->dateTime('confirm_until')->nullable();
$table->dateTime('confirmed_at')->nullable();
$table->timestamps();
Expand Down
5 changes: 2 additions & 3 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
>
stopOnFailure="false">
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix=".php">tests</directory>
<directory suffix="Test.php">tests</directory>
</testsuite>
</testsuites>
<php>
Expand Down
84 changes: 68 additions & 16 deletions src/Butler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,51 @@

namespace Konsulting\Butler;

use stdClass;
use Konsulting\Butler\Exceptions\NoUser;
use Konsulting\Butler\Exceptions\SocialIdentityAlreadyAssociated;
use Konsulting\Butler\Exceptions\SocialIdentityAssociatedToLoggedInUser;
use Konsulting\Butler\Exceptions\UnknownProvider;
use Laravel\Socialite\Contracts\User as Identity;
use Konsulting\Butler\Exceptions\UserAlreadyHasSocialIdentity;
use Konsulting\Butler\Exceptions\SocialIdentityAlreadyAssociated;
use Laravel\Socialite\Contracts\Factory as SocialiteFactory;
use Laravel\Socialite\Contracts\User as Identity;
use Laravel\Socialite\SocialiteManager;
use stdClass;

class Butler
{
/**
* The Socialite instance.
*
* @var SocialiteManager
*/
protected $socialite;

/**
* The social provider config.
*
* @var \Illuminate\Support\Collection
*/
protected $providers;

/**
* The mapping between route names and URLs within the host application.
*
* @var \Illuminate\Support\Collection
*/
protected $routeNames;

public function __construct($providers, $routeNames)
/**
* Butler constructor.
*
* @param SocialiteFactory $socialite
* @param array[] $providers
* @param array $routeNames
*/
public function __construct(SocialiteFactory $socialite, $providers, $routeNames)
{
$this->providers = $this->prepareProviders($providers);
$this->routeNames = collect($routeNames);
$this->socialite = $socialite;
}

/**
Expand All @@ -37,7 +66,7 @@ protected function prepareProviders($providers)
/**
* Check that the provider is one that is supported by the app.
*
* @param $name
* @param string $name
*
* @throws \Konsulting\Butler\Exceptions\UnknownProvider
*/
Expand Down Expand Up @@ -74,6 +103,18 @@ public function provider($provider)
return $this->providers[$provider];
}

/**
* Get the Butler driver for the given Socialite provider. This wraps the underlying Socialite driver and provides
* some extra functionality.
*
* @param string $providerName
* @return ButlerDriver
*/
public function driver($providerName)
{
return new ButlerDriver($this->socialite->driver($providerName));
}

/**
* Simple function to include the routes where needed.
*/
Expand All @@ -97,14 +138,14 @@ public function routeName($key)
}

/**
* Authenticate a Socialite User (Identity) and update the token
* information for a given provider --- only if appropriate
* We also check whether the provider is set up for use.
* Authenticate a Socialite User (Identity) and update the token information for a given provider --- only if
* appropriate. We also check whether the provider is set up for use.
*
* @param $provider
* @param string $provider The provider name
* @param \Laravel\Socialite\Contracts\User $identity
*
* @return bool
* @throws UnknownProvider
*/
public function authenticate($provider, Identity $identity)
{
Expand Down Expand Up @@ -138,21 +179,29 @@ protected function guard()
}

/**
* Register an Identity with a user. We'll use the authenticated user,
* or if we can't find an appropriate user, create one if allowed.
* Otherwise, we will fail through a graceful Exception :).
* Register an Identity with a user. We'll use the authenticated user, or if we can't find an appropriate user,
* create one if allowed. Otherwise, we will fail through a graceful Exception :).
*
* @param $provider
* @param string $provider The provider name
* @param \Laravel\Socialite\Contracts\User $identity
*
* @return SocialIdentity
* @throws \Konsulting\Butler\Exceptions\NoUser
* @throws \Konsulting\Butler\Exceptions\SocialIdentityAlreadyAssociated
* @throws NoUser
* @throws SocialIdentityAlreadyAssociated
* @throws UnknownProvider
* @throws UserAlreadyHasSocialIdentity
*/
public function register($provider, Identity $identity)
{
$this->checkProvider($provider);
$this->guardExistingSocialIdentities($provider, $identity);

if (config('butler.can_associate_to_logged_in_user', false) === true) {
SocialIdentity::createFromOauthIdentity($provider, $this->guard()->user(), $identity);

throw new SocialIdentityAssociatedToLoggedInUser('Social Identity linked to your account');
}

$user = $this->userProvider()->retrieveByOauthIdentity($identity);

if (! $user) {
Expand Down Expand Up @@ -194,6 +243,9 @@ protected function guardExistingSocialIdentities($provider, Identity $identity)
* Confirm a SocialIdentity by providing the token.
*
* @param $token
*
* @return SocialIdentity
* @throws Exceptions\UnableToConfirm
*/
public static function confirmIdentityByToken($token)
{
Expand All @@ -203,7 +255,7 @@ public static function confirmIdentityByToken($token)
/**
* Obtain the UserProvider Instance.
*
* @return \Illuminate\Foundation\Application|mixed
* @return EloquentUserProvider
*/
public function userProvider()
{
Expand Down
Loading

0 comments on commit 4ac3907

Please sign in to comment.