Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit dc6a956
Showing
24 changed files
with
816 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
var | ||
vendor | ||
composer.phar | ||
phpunit.xml | ||
composer.lock | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
language: php | ||
php: | ||
- "5.5" | ||
before_script: | ||
- composer install |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2014 Devon Weller | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
A Laravel package for applications that wish to use Tokenly Accounts for user authentication. | ||
|
||
# Installation | ||
|
||
|
||
### Add the Laravel package via composer | ||
|
||
composer require tokenly/accounts-client | ||
|
||
|
||
|
||
### Add Service Provider | ||
|
||
Add the following to the `providers` array in your application config: | ||
|
||
`Tokenly\AccountsClient\Provider\TokenlyAccountsServiceProvider::class` | ||
|
||
|
||
|
||
### Publish and set the config | ||
|
||
`artisan vendor:publish --provider="Tokenly\AccountsClient\Provider\TokenlyAccountsServiceProvider"` | ||
|
||
Then edit the file at `config/tokenlyaccounts.php`. | ||
|
||
You will need a client id and client secret generated by Tokenly Accounts. | ||
|
||
|
||
|
||
### Configure your controller, routes and views | ||
|
||
See the example [controller](examples/controllers/AccountController.php), [views](examples/view) and [routes](examples/routes.php). | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"name": "tokenly/accounts-client", | ||
"description": "A Laravel package for applications that wish to use Tokenly Accounts for user authentication.", | ||
"keywords": ["laravel","tokenly","accounts","oauth"], | ||
"type": "library", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Devon Weller", | ||
"email": "devon@tokenly.com", | ||
"homepage": "http://tokenly.com" | ||
} | ||
], | ||
"require": { | ||
"php": ">=5.5.0" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "~4" | ||
}, | ||
"autoload": { | ||
"psr-4": {"Tokenly\\AccountsClient\\": "src/"} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
return [ | ||
// Enter your client id and client secret from Tokenly Accounts here | ||
'client_id' => 'YOUR_TOKENLY_ACCOUNTS_CLIENT_ID_HERE', | ||
'client_secret' => 'YOUR_TOKENLY_ACCOUNTS_CLIENT_SECRET_HERE', | ||
|
||
// this is the URL that Tokenly Accounts uses to redirect the user back to your application | ||
'redirect' => 'https://YourSiteHere.com/account/authorize/callback', | ||
|
||
// this is the Tokenly Accounts URL | ||
'base_url' => 'https://accounts.tokenly.com', | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Account; | ||
|
||
use Exception; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\Auth; | ||
use Laravel\Socialite\Facades\Socialite; | ||
use App\Http\Controllers\Controller; | ||
use App\Models\User; | ||
use Tokenly\AccountsClient\Facade\TokenlyAccounts; | ||
|
||
class AccountController extends Controller | ||
{ | ||
|
||
|
||
/** | ||
* Show the welcome page or redirect | ||
*/ | ||
public function welcome() { | ||
// ensure the user is signed in. If not, then redirect to the login page | ||
$user = Auth::user(); | ||
if (!$user) { return redirect('/account/login'); } | ||
|
||
return view('account.welcome', ['user' => $user]); | ||
} | ||
|
||
|
||
|
||
/** | ||
* Login or redirect | ||
*/ | ||
public function login() { | ||
// if the user is already signed in, go straight to the welcome page | ||
$user = Auth::user(); | ||
if ($user) { return redirect('/account/welcome'); } | ||
|
||
return view('account.login', ['user' => $user]); | ||
} | ||
|
||
|
||
/** | ||
* Logout | ||
*/ | ||
public function logout() { | ||
Auth::logout(); | ||
return view('account.loggedout', []); | ||
} | ||
|
||
|
||
/** | ||
* Redirect the user to Tokenly Accounts to get authorization | ||
*/ | ||
public function redirectToProvider() | ||
{ | ||
return Socialite::redirect(); | ||
} | ||
|
||
|
||
|
||
/** | ||
* Obtain the user information from Accounts. | ||
* | ||
* This is the route called after Tokenly Accounts has granted (or denied) permission to this application | ||
* This application is now responsible for loading the user information from Tokenly Accounts and storing | ||
* it in the local user database. | ||
* | ||
* @return Response | ||
*/ | ||
public function handleProviderCallback(Request $request) | ||
{ | ||
|
||
try { | ||
// check for an error returned from Tokenly Accounts | ||
$error_description = TokenlyAccounts::checkForError($request); | ||
if ($error_description) { | ||
return view('authorization-failed', ['error_msg' => $error_description]); | ||
} | ||
|
||
|
||
// retrieve the user from Tokenly Accounts | ||
$oauth_user = Socialite::user(); | ||
|
||
// get all the properties from the oAuth user object | ||
$tokenly_uuid = $oauth_user->id; | ||
$oauth_token = $oauth_user->token; | ||
$username = $oauth_user->user['username']; | ||
$name = $oauth_user->user['name']; | ||
$email = $oauth_user->user['email']; | ||
$email_is_confirmed = $oauth_user->user['email_is_confirmed']; | ||
|
||
// find an existing user based on the credentials provided | ||
$existing_user = User::where('tokenly_uuid', $tokenly_uuid); | ||
|
||
// if an existing user wasn't found, we might need to find a user to merge into | ||
$mergable_user = ($existing_user ? null : User::where('username', $username)->where('tokenly_uuid', null)); | ||
|
||
if ($existing_user) { | ||
// update the user | ||
$existing_user->update(['oauth_token' => $oauth_token, 'name' => $name, 'email' => $email, /* etc */ ]); | ||
|
||
// login | ||
Auth::login($existing_user); | ||
} else if ($mergable_user) { | ||
// an existing user was found with a matching username | ||
// migrate it to the tokenly accounts control | ||
|
||
if ($mergable_user['tokenly_uuid']) { | ||
throw new Exception("Can't merge a user already associated with a different tokenly account", 1); | ||
} | ||
|
||
// update if needed | ||
$mergable_user->update(['name' => $name, 'email' => $email, /* etc */ ]); | ||
|
||
// login | ||
Auth::login($mergable_user); | ||
|
||
} else { | ||
// no user was found - create a new user based on the information we received | ||
$new_user = User::create(['tokenly_uuid' => $tokenly_uuid, 'oauth_token' => $oauth_token, 'name' => $name, 'username' => $username, 'email' => $email, /* etc */ ]); | ||
|
||
// login | ||
Auth::login($mergable_user); | ||
} | ||
|
||
|
||
return redirect('/account/login'); | ||
|
||
} catch (Exception $e) { | ||
// some unexpected error happened | ||
return view('authorization-failed', ['error_msg' => 'Failed to authenticate this user.']); | ||
} | ||
} | ||
|
||
|
||
|
||
/** | ||
* Obtain the user information from Tokenly Accounts. | ||
* | ||
* And sync it with our local database | ||
* | ||
* @return Response | ||
*/ | ||
public function sync(Request $request) | ||
{ | ||
|
||
try { | ||
$logged_in_user = Auth::user(); | ||
|
||
$oauth_user = null; | ||
if ($logged_in_user['oauth_token']) { | ||
$oauth_user = Socialite::getUserByExistingToken($logged_in_user['oauth_token']); | ||
} | ||
|
||
if ($oauth_user) { | ||
$tokenly_uuid = $oauth_user->id; | ||
$oauth_token = $oauth_user->token; | ||
$username = $oauth_user->user['username']; | ||
$name = $oauth_user->user['name']; | ||
$email = $oauth_user->user['email']; | ||
$email_is_confirmed = $oauth_user->user['email_is_confirmed']; | ||
|
||
// find an existing user based on the credentials provided | ||
$existing_user = User::where('tokenly_uuid', $tokenly_uuid); | ||
if ($existing_user) { | ||
// update | ||
$existing_user->update(['name' => $name, 'email' => $email, /* etc */ ]); | ||
} | ||
|
||
$synced = true; | ||
} else { | ||
// not able to sync this user | ||
$synced = false; | ||
} | ||
|
||
return view('account.sync', ['synced' => $synced, 'user' => $logged_in_user, ]); | ||
|
||
} catch (Exception $e) { | ||
return view('sync-failed', ['error_msg' => 'Failed to sync this user.']); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
// The welcome page for the user that requires a logged in user | ||
$router->get('/account/welcome', 'Account\AccountController@welcome'); | ||
|
||
// routes for logging in and logging out | ||
$router->get('/account/login', 'Account\AccountController@login'); | ||
$router->get('/account/logout', 'Account\AccountController@logout'); | ||
|
||
// This is a route to sync the user with their Tokenly Accounts information | ||
// Redirect the user here to update their local user information with their Tokenly Accounts information | ||
$router->get('/account/sync', 'Account\AccountController@sync'); | ||
|
||
// oAuth handlers | ||
$router->get('/account/authorize', 'Account\AccountController@redirectToProvider'); | ||
$router->get('/account/authorize/callback', 'Account\AccountController@handleProviderCallback'); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
@extends('layouts.base') | ||
|
||
@section('content') | ||
<div class="container-fluid"> | ||
<div class="row"> | ||
<div class="col-md-9"> | ||
<div class="alert alert-danger"> | ||
<p><strong>This login was not successful.</strong></p> | ||
<p>{{$error_msg}}</p> | ||
</div> | ||
|
||
<div class="spacer1"></div> | ||
<p>Sorry about that.</p> | ||
|
||
<div class="spacer2"></div> | ||
<p><a class="btn btn-primary" href="/account/login">Try Again ?</a></p> | ||
</div> | ||
</div> | ||
</div> | ||
@stop |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<!doctype html> | ||
|
||
<head> | ||
<meta charset="utf-8"> | ||
<title>My App</title> | ||
<meta name="description" content=""> | ||
<meta name="viewport" content="width=device-width"> | ||
</head> | ||
|
||
<body> | ||
|
||
<!-- CONTENT SECTION --> | ||
<div id="content"> | ||
@yield('content') | ||
</div> | ||
|
||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
@extends('layouts.base') | ||
|
||
@section('content') | ||
<h2>You are logged out.</h2> | ||
@stop |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
@extends('layouts.base') | ||
|
||
@section('content') | ||
<div class="container-fluid"> | ||
<div class="row"> | ||
<div class="col-md-9"> | ||
<h2>Login or Register</h2> | ||
|
||
<p>You are not logged in yet.</p> | ||
|
||
<a href="/account/authorize" class="btn btn-primary">Login or Register Now</a> | ||
</div> | ||
</div> | ||
</div> | ||
@stop |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
@extends('layouts.base') | ||
|
||
@section('content') | ||
<div class="container-fluid"> | ||
<div class="row"> | ||
<div class="col-md-9"> | ||
<div class="alert alert-danger"> | ||
<p><strong>This sync attempt was not successful.</strong></p> | ||
<p>{{$error_msg}}</p> | ||
</div> | ||
|
||
<div class="spacer1"></div> | ||
<p>Sorry about that.</p> | ||
|
||
<div class="spacer2"></div> | ||
<p><a class="btn btn-primary" href="/account/sync">Try Again ?</a></p> | ||
</div> | ||
</div> | ||
</div> | ||
@stop |
Oops, something went wrong.