├── public ├── favicon.ico ├── robots.txt ├── .htaccess └── index.php ├── app ├── Listeners │ └── .gitkeep ├── Policies │ └── .gitkeep ├── Events │ └── Event.php ├── Repositories │ ├── User │ │ ├── UserRepositoryInterface.php │ │ └── UserRepository.php │ ├── Profile │ │ ├── ProfileRepositoryInterface.php │ │ └── ProfileRepository.php │ └── RepositoryServiceProvider.php ├── Http │ ├── Requests │ │ └── Request.php │ ├── Middleware │ │ ├── EncryptCookies.php │ │ ├── VerifyCsrfToken.php │ │ ├── RedirectIfAuthenticated.php │ │ └── Authenticate.php │ ├── Controllers │ │ └── Controller.php │ ├── routes.php │ ├── api_routes.php │ └── Kernel.php ├── Enums │ ├── Gender.php │ └── AuthType.php ├── Providers │ ├── AppServiceProvider.php │ ├── AuthServiceProvider.php │ ├── EventServiceProvider.php │ ├── OAuthServiceProvider.php │ └── RouteServiceProvider.php ├── Jobs │ └── Job.php ├── Console │ ├── Commands │ │ └── Inspire.php │ └── Kernel.php ├── Models │ ├── PasswordVerifier.php │ └── User.php ├── Exceptions │ └── Handler.php ├── User.php └── Api │ └── V1 │ └── Controllers │ ├── BaseController.php │ └── AuthController.php ├── database ├── seeds │ ├── .gitkeep │ ├── DatabaseSeeder.php │ └── ClientTableSeeder.php ├── migrations │ ├── .gitkeep │ ├── 2014_10_12_100000_create_password_resets_table.php │ ├── 2014_10_12_000000_create_users_table.php │ ├── 2014_04_24_110304_create_oauth_grants_table.php │ ├── 2016_01_18_190206_create_profiles_table.php │ ├── 2014_04_24_110151_create_oauth_scopes_table.php │ ├── 2014_04_24_110459_create_oauth_clients_table.php │ ├── 2016_01_18_190148_remove_name_column_users_table.php │ ├── 2014_04_24_111810_create_oauth_refresh_tokens_table.php │ ├── 2014_04_24_111254_create_oauth_auth_codes_table.php │ ├── 2014_04_24_111518_create_oauth_access_tokens_table.php │ ├── 2014_04_24_110557_create_oauth_client_endpoints_table.php │ ├── 2014_04_24_111002_create_oauth_sessions_table.php │ ├── 2014_04_24_110403_create_oauth_grant_scopes_table.php │ ├── 2014_04_24_110705_create_oauth_client_scopes_table.php │ ├── 2014_04_24_111109_create_oauth_session_scopes_table.php │ ├── 2014_04_24_111403_create_oauth_auth_code_scopes_table.php │ ├── 2014_04_24_111657_create_oauth_access_token_scopes_table.php │ └── 2014_04_24_110817_create_oauth_client_grants_table.php ├── .gitignore └── factories │ └── ModelFactory.php ├── resources ├── views │ ├── vendor │ │ └── .gitkeep │ ├── emails │ │ └── password.blade.php │ ├── welcome.blade.php │ └── errors │ │ └── 503.blade.php ├── assets │ └── sass │ │ └── app.scss └── lang │ └── en │ ├── pagination.php │ ├── auth.php │ ├── passwords.php │ └── validation.php ├── storage ├── app │ └── .gitignore ├── logs │ └── .gitignore └── framework │ ├── cache │ └── .gitignore │ ├── views │ └── .gitignore │ ├── sessions │ └── .gitignore │ └── .gitignore ├── bootstrap ├── cache │ └── .gitignore ├── autoload.php └── app.php ├── .gitattributes ├── .gitignore ├── phpspec.yml ├── package.json ├── tests ├── ExampleTest.php └── TestCase.php ├── gulpfile.js ├── .env.example ├── server.php ├── config ├── cors.php ├── compile.php ├── services.php ├── view.php ├── broadcasting.php ├── cache.php ├── auth.php ├── boilerplate.php ├── filesystems.php ├── queue.php ├── database.php ├── mail.php ├── oauth2.php ├── session.php ├── api.php └── app.php ├── phpunit.xml ├── composer.json ├── artisan └── readme.md /public/favicon.ico: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /app/Listeners/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /app/Policies/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /database/seeds/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /database/migrations/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /database/.gitignore: -------------------------------------------------------------------------------- 1 | *.sqlite 2 | -------------------------------------------------------------------------------- /resources/views/vendor/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /resources/views/emails/password.blade.php: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /storage/app/.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | !.gitignore -------------------------------------------------------------------------------- /bootstrap/cache/.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | !.gitignore 3 | -------------------------------------------------------------------------------- /storage/logs/.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | !.gitignore 3 | -------------------------------------------------------------------------------- /public/robots.txt: -------------------------------------------------------------------------------- 1 | User-agent: * 2 | Disallow: 3 | -------------------------------------------------------------------------------- /storage/framework/cache/.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | !.gitignore -------------------------------------------------------------------------------- /storage/framework/views/.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | !.gitignore 3 | -------------------------------------------------------------------------------- /storage/framework/sessions/.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | !.gitignore 3 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | *.css linguist-vendored 3 | *.less linguist-vendored 4 | -------------------------------------------------------------------------------- /resources/assets/sass/app.scss: -------------------------------------------------------------------------------- 1 | // @import "node_modules/bootstrap-sass/assets/stylesheets/bootstrap"; 2 | 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.idea 2 | /vendor 3 | /node_modules 4 | Homestead.yaml 5 | Homestead.json 6 | .env 7 | composer.lock 8 | -------------------------------------------------------------------------------- /app/Events/Event.php: -------------------------------------------------------------------------------- 1 | call(UserTableSeeder::class); 18 | 19 | Model::reguard(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /tests/ExampleTest.php: -------------------------------------------------------------------------------- 1 | visit('/') 17 | ->see('Laravel 5'); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /app/Http/routes.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | Options -MultiViews 4 | 5 | 6 | RewriteEngine On 7 | 8 | # Redirect Trailing Slashes If Not A Folder... 9 | RewriteCond %{REQUEST_FILENAME} !-d 10 | RewriteRule ^(.*)/$ /$1 [L,R=301] 11 | 12 | # Handle Front Controller... 13 | RewriteCond %{REQUEST_FILENAME} !-d 14 | RewriteCond %{REQUEST_FILENAME} !-f 15 | RewriteRule ^ index.php [L] 16 | 17 | # Authorization Headers 18 | RewriteCond %{HTTP:Authorization} . 19 | RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] 20 | 21 | -------------------------------------------------------------------------------- /resources/lang/en/pagination.php: -------------------------------------------------------------------------------- 1 | '« Previous', 17 | 'next' => 'Next »', 18 | 19 | ]; 20 | -------------------------------------------------------------------------------- /tests/TestCase.php: -------------------------------------------------------------------------------- 1 | make(Illuminate\Contracts\Console\Kernel::class)->bootstrap(); 22 | 23 | return $app; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /server.php: -------------------------------------------------------------------------------- 1 | 8 | */ 9 | 10 | $uri = urldecode( 11 | parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) 12 | ); 13 | 14 | // This file allows us to emulate Apache's "mod_rewrite" functionality from the 15 | // built-in PHP web server. This provides a convenient way to test a Laravel 16 | // application without having installed a "real" web server software here. 17 | if ($uri !== '/' && file_exists(__DIR__.'/public'.$uri)) { 18 | return false; 19 | } 20 | 21 | require_once __DIR__.'/public/index.php'; 22 | -------------------------------------------------------------------------------- /database/seeds/ClientTableSeeder.php: -------------------------------------------------------------------------------- 1 | insert([ 18 | 'id' => $client_id, 19 | 'secret' => $client_secret, 20 | 'name' => 'App Client', 21 | 'created_at' => \Carbon\Carbon::now(), 22 | 'updated_at' => \Carbon\Carbon::now(), 23 | ]); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /config/cors.php: -------------------------------------------------------------------------------- 1 | false, 14 | 'allowedOrigins' => ['*'], 15 | 'allowedHeaders' => ['*'], 16 | 'allowedMethods' => ['GET', 'POST', 'PUT', 'DELETE'], 17 | 'exposedHeaders' => [], 18 | 'maxAge' => 0, 19 | 'hosts' => [], 20 | ]; 21 | -------------------------------------------------------------------------------- /app/Http/api_routes.php: -------------------------------------------------------------------------------- 1 | version('v1', ['prefix' => 'api/v1'], function ($api) { 7 | 8 | // Authentication Module routes 9 | $api->post('login', 'App\Api\V1\Controllers\AuthController@login'); 10 | $api->post('signup', 'App\Api\V1\Controllers\AuthController@signup'); 11 | $api->post('auth/recovery', 'App\Api\V1\Controllers\AuthController@recovery'); 12 | $api->post('auth/reset', 'App\Api\V1\Controllers\AuthController@reset'); 13 | 14 | }); 15 | 16 | 17 | // All protected routes 18 | $api->version('v1', ['prefix' => 'api/v1', 'middleware' => 'api.auth', 'providers' => ['oauth']], function ($api) { 19 | 20 | 21 | }); 22 | -------------------------------------------------------------------------------- /resources/lang/en/auth.php: -------------------------------------------------------------------------------- 1 | 'These credentials do not match our records.', 17 | 'throttle' => 'Too many login attempts. Please try again in :seconds seconds.', 18 | 19 | ]; 20 | -------------------------------------------------------------------------------- /database/factories/ModelFactory.php: -------------------------------------------------------------------------------- 1 | define(App\User::class, function (Faker\Generator $faker) { 15 | return [ 16 | 'name' => $faker->name, 17 | 'email' => $faker->email, 18 | 'password' => bcrypt(str_random(10)), 19 | 'remember_token' => str_random(10), 20 | ]; 21 | }); 22 | -------------------------------------------------------------------------------- /app/Console/Commands/Inspire.php: -------------------------------------------------------------------------------- 1 | comment(PHP_EOL.Inspiring::quote().PHP_EOL); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /app/Console/Kernel.php: -------------------------------------------------------------------------------- 1 | command('inspire') 28 | ->hourly(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /database/migrations/2014_10_12_100000_create_password_resets_table.php: -------------------------------------------------------------------------------- 1 | string('email')->index(); 17 | $table->string('token')->index(); 18 | $table->timestamp('created_at'); 19 | }); 20 | } 21 | 22 | /** 23 | * Reverse the migrations. 24 | * 25 | * @return void 26 | */ 27 | public function down() 28 | { 29 | Schema::drop('password_resets'); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /app/Providers/AuthServiceProvider.php: -------------------------------------------------------------------------------- 1 | 'App\Policies\ModelPolicy', 17 | ]; 18 | 19 | /** 20 | * Register any application authentication / authorization services. 21 | * 22 | * @param \Illuminate\Contracts\Auth\Access\Gate $gate 23 | * @return void 24 | */ 25 | public function boot(GateContract $gate) 26 | { 27 | parent::registerPolicies($gate); 28 | 29 | // 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /resources/lang/en/passwords.php: -------------------------------------------------------------------------------- 1 | 'Passwords must be at least six characters and match the confirmation.', 17 | 'reset' => 'Your password has been reset!', 18 | 'sent' => 'We have e-mailed your password reset link!', 19 | 'token' => 'This password reset token is invalid.', 20 | 'user' => "We can't find a user with that e-mail address.", 21 | 22 | ]; 23 | -------------------------------------------------------------------------------- /app/Providers/EventServiceProvider.php: -------------------------------------------------------------------------------- 1 | [ 17 | 'App\Listeners\EventListener', 18 | ], 19 | ]; 20 | 21 | /** 22 | * Register any other events for your application. 23 | * 24 | * @param \Illuminate\Contracts\Events\Dispatcher $events 25 | * @return void 26 | */ 27 | public function boot(DispatcherContract $events) 28 | { 29 | parent::boot($events); 30 | 31 | // 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /database/migrations/2014_10_12_000000_create_users_table.php: -------------------------------------------------------------------------------- 1 | increments('id'); 17 | $table->string('name'); 18 | $table->string('email')->unique(); 19 | $table->string('password', 60); 20 | $table->rememberToken(); 21 | $table->timestamps(); 22 | }); 23 | } 24 | 25 | /** 26 | * Reverse the migrations. 27 | * 28 | * @return void 29 | */ 30 | public function down() 31 | { 32 | Schema::drop('users'); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /app/Repositories/RepositoryServiceProvider.php: -------------------------------------------------------------------------------- 1 | User\UserRepository::class, 18 | Buddy\BuddyRepositoryInterface::class => Buddy\BuddyRepository::class, 19 | Profile\ProfileRepositoryInterface::class => Profile\ProfileRepository::class, 20 | ]; 21 | 22 | /** 23 | * @return void 24 | */ 25 | public function register() 26 | { 27 | //dd($this->bindings); 28 | foreach ($this->bindings as $interface => $implementation) { 29 | $this->app->bind($interface, $implementation); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /app/Http/Middleware/RedirectIfAuthenticated.php: -------------------------------------------------------------------------------- 1 | auth = $auth; 26 | } 27 | 28 | /** 29 | * Handle an incoming request. 30 | * 31 | * @param \Illuminate\Http\Request $request 32 | * @param \Closure $next 33 | * @return mixed 34 | */ 35 | public function handle($request, Closure $next) 36 | { 37 | if ($this->auth->check()) { 38 | return redirect('/home'); 39 | } 40 | 41 | return $next($request); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 14 | ./tests/ 15 | 16 | 17 | 18 | 19 | app/ 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /app/Providers/OAuthServiceProvider.php: -------------------------------------------------------------------------------- 1 | app[Auth::class]->extend('oauth', function ($app) { 15 | $provider = new OAuth2($app['oauth2-server.authorizer']->getChecker()); 16 | 17 | $provider->setUserResolver(function ($id) { 18 | // Logic to return a user by their ID. 19 | //dd(User::find($id)); 20 | return User::find($id); 21 | }); 22 | 23 | $provider->setClientResolver(function ($id) { 24 | // Logic to return a client by their ID. 25 | //return 'client@fake.com'; 26 | //dd($id); 27 | }); 28 | 29 | return $provider; 30 | }); 31 | } 32 | 33 | public function register() 34 | { 35 | // 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /app/Http/Middleware/Authenticate.php: -------------------------------------------------------------------------------- 1 | auth = $auth; 26 | } 27 | 28 | /** 29 | * Handle an incoming request. 30 | * 31 | * @param \Illuminate\Http\Request $request 32 | * @param \Closure $next 33 | * @return mixed 34 | */ 35 | public function handle($request, Closure $next) 36 | { 37 | if ($this->auth->guest()) { 38 | if ($request->ajax()) { 39 | return response('Unauthorized.', 401); 40 | } else { 41 | return redirect()->guest('auth/login'); 42 | } 43 | } 44 | 45 | return $next($request); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /config/compile.php: -------------------------------------------------------------------------------- 1 | [ 17 | // 18 | ], 19 | 20 | /* 21 | |-------------------------------------------------------------------------- 22 | | Compiled File Providers 23 | |-------------------------------------------------------------------------- 24 | | 25 | | Here you may list service providers which define a "compiles" function 26 | | that returns additional files that should be compiled, providing an 27 | | easy way to get common files from any packages you are utilizing. 28 | | 29 | */ 30 | 31 | 'providers' => [ 32 | // 33 | ], 34 | 35 | ]; 36 | -------------------------------------------------------------------------------- /config/services.php: -------------------------------------------------------------------------------- 1 | [ 18 | 'domain' => env('MAILGUN_DOMAIN'), 19 | 'secret' => env('MAILGUN_SECRET'), 20 | ], 21 | 22 | 'mandrill' => [ 23 | 'secret' => env('MANDRILL_SECRET'), 24 | ], 25 | 26 | 'ses' => [ 27 | 'key' => env('SES_KEY'), 28 | 'secret' => env('SES_SECRET'), 29 | 'region' => 'us-east-1', 30 | ], 31 | 32 | 'stripe' => [ 33 | 'model' => App\User::class, 34 | 'key' => env('STRIPE_KEY'), 35 | 'secret' => env('STRIPE_SECRET'), 36 | ], 37 | 38 | ]; 39 | -------------------------------------------------------------------------------- /config/view.php: -------------------------------------------------------------------------------- 1 | [ 17 | realpath(base_path('resources/views')), 18 | ], 19 | 20 | /* 21 | |-------------------------------------------------------------------------- 22 | | Compiled View Path 23 | |-------------------------------------------------------------------------- 24 | | 25 | | This option determines where all the compiled Blade templates will be 26 | | stored for your application. Typically, this is within the storage 27 | | directory. However, as usual, you are free to change this value. 28 | | 29 | */ 30 | 31 | 'compiled' => realpath(storage_path('framework/views')), 32 | 33 | ]; 34 | -------------------------------------------------------------------------------- /database/migrations/2014_04_24_110304_create_oauth_grants_table.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | use Illuminate\Database\Migrations\Migration; 13 | use Illuminate\Database\Schema\Blueprint; 14 | use Illuminate\Support\Facades\Schema; 15 | 16 | /** 17 | * This is the create oauth grants table migration class. 18 | * 19 | * @author Luca Degasperi 20 | */ 21 | class CreateOauthGrantsTable extends Migration 22 | { 23 | /** 24 | * Run the migrations. 25 | * 26 | * @return void 27 | */ 28 | public function up() 29 | { 30 | Schema::create('oauth_grants', function (Blueprint $table) { 31 | $table->string('id', 40)->primary(); 32 | $table->timestamps(); 33 | }); 34 | } 35 | 36 | /** 37 | * Reverse the migrations. 38 | * 39 | * @return void 40 | */ 41 | public function down() 42 | { 43 | Schema::drop('oauth_grants'); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /app/Models/PasswordVerifier.php: -------------------------------------------------------------------------------- 1 | request = $request; 24 | $this->userRepo = $userRepo; 25 | } 26 | 27 | public function verify($username, $password) 28 | { 29 | $credentials = [ 30 | 'email' => $username, 31 | 'password' => $password, 32 | ]; 33 | 34 | // Check for FB login 35 | if ($this->request->has('token_facebook')) { 36 | $user = $this->userRepo->createUser($this->request->all()); 37 | 38 | return $user->id; 39 | } 40 | 41 | // For normal users 42 | if (Auth::once($credentials)) { 43 | return Auth::user()->id; 44 | } 45 | 46 | return false; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /bootstrap/autoload.php: -------------------------------------------------------------------------------- 1 | increments('id'); 19 | $table->integer('user_id')->unsigned(); 20 | $table->string('first_name')->nullable(); 21 | $table->string('last_name')->nullable(); 22 | $table->enum('gender', Gender::toArray())->nullable(); 23 | $table->date('date_of_birth')->nullable(); 24 | 25 | $table->timestamps(); 26 | }); 27 | 28 | Schema::table('profiles', function(Blueprint $table) { 29 | $table->foreign('user_id')->references('id')->on('users'); 30 | }); 31 | } 32 | 33 | /** 34 | * Reverse the migrations. 35 | * 36 | * @return void 37 | */ 38 | public function down() 39 | { 40 | Schema::dropIfExists('profiles'); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /resources/views/welcome.blade.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Laravel 5 | 6 | 7 | 8 | 37 | 38 | 39 |
40 |
41 |
Laravel 5
42 |
43 |
44 | 45 | 46 | -------------------------------------------------------------------------------- /database/migrations/2014_04_24_110151_create_oauth_scopes_table.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | use Illuminate\Database\Migrations\Migration; 13 | use Illuminate\Database\Schema\Blueprint; 14 | use Illuminate\Support\Facades\Schema; 15 | 16 | /** 17 | * This is the create oauth scopes table migration class. 18 | * 19 | * @author Luca Degasperi 20 | */ 21 | class CreateOauthScopesTable extends Migration 22 | { 23 | /** 24 | * Run the migrations. 25 | * 26 | * @return void 27 | */ 28 | public function up() 29 | { 30 | Schema::create('oauth_scopes', function (Blueprint $table) { 31 | $table->string('id', 40)->primary(); 32 | $table->string('description'); 33 | 34 | $table->timestamps(); 35 | }); 36 | } 37 | 38 | /** 39 | * Reverse the migrations. 40 | * 41 | * @return void 42 | */ 43 | public function down() 44 | { 45 | Schema::drop('oauth_scopes'); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /app/Repositories/Profile/ProfileRepository.php: -------------------------------------------------------------------------------- 1 | errors()); 19 | } else { 20 | throw new \Dingo\Api\Exception\StoreResourceFailedException($message); 21 | } 22 | } 23 | 24 | public function getAll() 25 | { 26 | return 'get all'; 27 | } 28 | 29 | public function find($id) 30 | { 31 | return ''; 32 | } 33 | 34 | public function findByUserId($userId) 35 | { 36 | $profile = Profile::where('user_id', $userId)->first(); 37 | if ($profile) { 38 | return $profile; 39 | } else { 40 | $this->throwStoreResourceFailedException(); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /app/Providers/RouteServiceProvider.php: -------------------------------------------------------------------------------- 1 | group(['namespace' => $this->namespace], function ($router) { 41 | require app_path('Http/api_routes.php'); 42 | require app_path('Http/routes.php'); 43 | }); 44 | } 45 | } -------------------------------------------------------------------------------- /resources/views/errors/503.blade.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Be right back. 5 | 6 | 7 | 8 | 39 | 40 | 41 |
42 |
43 |
Be right back.
44 |
45 |
46 | 47 | 48 | -------------------------------------------------------------------------------- /database/migrations/2014_04_24_110459_create_oauth_clients_table.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | use Illuminate\Database\Migrations\Migration; 13 | use Illuminate\Database\Schema\Blueprint; 14 | use Illuminate\Support\Facades\Schema; 15 | 16 | /** 17 | * This is the create oauth client table migration class. 18 | * 19 | * @author Luca Degasperi 20 | */ 21 | class CreateOauthClientsTable extends Migration 22 | { 23 | /** 24 | * Run the migrations. 25 | * 26 | * @return void 27 | */ 28 | public function up() 29 | { 30 | Schema::create('oauth_clients', function (BluePrint $table) { 31 | $table->string('id', 40)->primary(); 32 | $table->string('secret', 40); 33 | $table->string('name'); 34 | $table->timestamps(); 35 | 36 | $table->unique(['id', 'secret']); 37 | }); 38 | } 39 | 40 | /** 41 | * Reverse the migrations. 42 | * 43 | * @return void 44 | */ 45 | public function down() 46 | { 47 | Schema::drop('oauth_clients'); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /database/migrations/2016_01_18_190148_remove_name_column_users_table.php: -------------------------------------------------------------------------------- 1 | enum('auth_type', AuthType::toArray())->nullable()->after('remember_token'); 19 | $table->string('vendor_auth_token')->nullable()->after('auth_type'); 20 | $table->text('vendor_auth_data')->nullable()->after('vendor_auth_token'); 21 | 22 | $table->timestamp('activated_at')->nullable()->after('vendor_auth_data'); 23 | $table->softDeletes(); 24 | 25 | $table->dropColumn('name'); 26 | }); 27 | } 28 | 29 | /** 30 | * Reverse the migrations. 31 | * 32 | * @return void 33 | */ 34 | public function down() 35 | { 36 | Schema::table('users', function (Blueprint $table) { 37 | $table->string('name')->nullable()->after('id'); 38 | $table->dropColumn(['auth_type', 'vendor_auth_token', 'vendor_auth_data', 'activated_at', 'deleted_at']); 39 | }); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "muhammadshakeel/laravel-api-boilerplate-oauth", 3 | "description": "A RESTful API starter pack for Laravel 5 with OAuth2.", 4 | "keywords": ["restful", "api", "laravel", "dingo", "oauth2", "oauth", "server"], 5 | "license": "MIT", 6 | "type": "project", 7 | "require": { 8 | "php": ">=5.5.9", 9 | "laravel/framework": "5.1.*", 10 | "dingo/api": "1.0.*@dev", 11 | "lucadegasperi/oauth2-server-laravel": "5.0.*", 12 | "myclabs/php-enum": "^1.4" 13 | }, 14 | "require-dev": { 15 | "fzaninotto/faker": "~1.4", 16 | "mockery/mockery": "0.9.*", 17 | "phpunit/phpunit": "~4.0", 18 | "phpspec/phpspec": "~2.1" 19 | }, 20 | "autoload": { 21 | "classmap": [ 22 | "database" 23 | ], 24 | "psr-4": { 25 | "App\\": "app/" 26 | } 27 | }, 28 | "autoload-dev": { 29 | "classmap": [ 30 | "tests/TestCase.php" 31 | ] 32 | }, 33 | "scripts": { 34 | "post-install-cmd": [ 35 | "php artisan clear-compiled", 36 | "php artisan optimize" 37 | ], 38 | "pre-update-cmd": [ 39 | "php artisan clear-compiled" 40 | ], 41 | "post-update-cmd": [ 42 | "php artisan optimize" 43 | ] 44 | }, 45 | "config": { 46 | "preferred-install": "dist" 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /app/Exceptions/Handler.php: -------------------------------------------------------------------------------- 1 | getMessage(), $e); 47 | } 48 | 49 | return parent::render($request, $e); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /app/User.php: -------------------------------------------------------------------------------- 1 | attributes['password'] = \Hash::make($value); 48 | } 49 | } -------------------------------------------------------------------------------- /config/broadcasting.php: -------------------------------------------------------------------------------- 1 | env('BROADCAST_DRIVER', 'pusher'), 17 | 18 | /* 19 | |-------------------------------------------------------------------------- 20 | | Broadcast Connections 21 | |-------------------------------------------------------------------------- 22 | | 23 | | Here you may define all of the broadcast connections that will be used 24 | | to broadcast events to other systems or over websockets. Samples of 25 | | each available type of connection are provided inside this array. 26 | | 27 | */ 28 | 29 | 'connections' => [ 30 | 31 | 'pusher' => [ 32 | 'driver' => 'pusher', 33 | 'key' => env('PUSHER_KEY'), 34 | 'secret' => env('PUSHER_SECRET'), 35 | 'app_id' => env('PUSHER_APP_ID'), 36 | ], 37 | 38 | 'redis' => [ 39 | 'driver' => 'redis', 40 | 'connection' => 'default', 41 | ], 42 | 43 | 'log' => [ 44 | 'driver' => 'log', 45 | ], 46 | 47 | ], 48 | 49 | ]; 50 | -------------------------------------------------------------------------------- /app/Http/Kernel.php: -------------------------------------------------------------------------------- 1 | \App\Http\Middleware\VerifyCsrfToken::class, 32 | 'auth' => \App\Http\Middleware\Authenticate::class, 33 | 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, 34 | 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, 35 | // OAuth2 36 | 'oauth' => \LucaDegasperi\OAuth2Server\Middleware\OAuthMiddleware::class, 37 | 'oauth-user' => \LucaDegasperi\OAuth2Server\Middleware\OAuthUserOwnerMiddleware::class, 38 | 'oauth-client' => \LucaDegasperi\OAuth2Server\Middleware\OAuthClientOwnerMiddleware::class, 39 | 'check-authorization-params' => \LucaDegasperi\OAuth2Server\Middleware\CheckAuthCodeRequestMiddleware::class, 40 | ]; 41 | } 42 | -------------------------------------------------------------------------------- /database/migrations/2014_04_24_111810_create_oauth_refresh_tokens_table.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | use Illuminate\Database\Migrations\Migration; 13 | use Illuminate\Database\Schema\Blueprint; 14 | use Illuminate\Support\Facades\Schema; 15 | 16 | /** 17 | * This is the create oauth refresh tokens table migration class. 18 | * 19 | * @author Luca Degasperi 20 | */ 21 | class CreateOauthRefreshTokensTable extends Migration 22 | { 23 | /** 24 | * Run the migrations. 25 | * 26 | * @return void 27 | */ 28 | public function up() 29 | { 30 | Schema::create('oauth_refresh_tokens', function (Blueprint $table) { 31 | $table->string('id', 40)->unique(); 32 | $table->string('access_token_id', 40)->primary(); 33 | $table->integer('expire_time'); 34 | 35 | $table->timestamps(); 36 | 37 | $table->foreign('access_token_id') 38 | ->references('id')->on('oauth_access_tokens') 39 | ->onDelete('cascade'); 40 | }); 41 | } 42 | 43 | /** 44 | * Reverse the migrations. 45 | * 46 | * @return void 47 | */ 48 | public function down() 49 | { 50 | Schema::table('oauth_refresh_tokens', function (Blueprint $table) { 51 | $table->dropForeign('oauth_refresh_tokens_access_token_id_foreign'); 52 | }); 53 | 54 | Schema::drop('oauth_refresh_tokens'); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /database/migrations/2014_04_24_111254_create_oauth_auth_codes_table.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | use Illuminate\Database\Migrations\Migration; 13 | use Illuminate\Database\Schema\Blueprint; 14 | use Illuminate\Support\Facades\Schema; 15 | 16 | /** 17 | * This is the create oauth auth codes table migration class. 18 | * 19 | * @author Luca Degasperi 20 | */ 21 | class CreateOauthAuthCodesTable extends Migration 22 | { 23 | /** 24 | * Run the migrations. 25 | * 26 | * @return void 27 | */ 28 | public function up() 29 | { 30 | Schema::create('oauth_auth_codes', function (Blueprint $table) { 31 | $table->string('id', 40)->primary(); 32 | $table->integer('session_id')->unsigned(); 33 | $table->string('redirect_uri'); 34 | $table->integer('expire_time'); 35 | 36 | $table->timestamps(); 37 | 38 | $table->index('session_id'); 39 | 40 | $table->foreign('session_id') 41 | ->references('id')->on('oauth_sessions') 42 | ->onDelete('cascade'); 43 | }); 44 | } 45 | 46 | /** 47 | * Reverse the migrations. 48 | * 49 | * @return void 50 | */ 51 | public function down() 52 | { 53 | Schema::table('oauth_auth_codes', function (Blueprint $table) { 54 | $table->dropForeign('oauth_auth_codes_session_id_foreign'); 55 | }); 56 | Schema::drop('oauth_auth_codes'); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /database/migrations/2014_04_24_111518_create_oauth_access_tokens_table.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | use Illuminate\Database\Migrations\Migration; 13 | use Illuminate\Database\Schema\Blueprint; 14 | use Illuminate\Support\Facades\Schema; 15 | 16 | /** 17 | * This is the create oauth access tokens table migration class. 18 | * 19 | * @author Luca Degasperi 20 | */ 21 | class CreateOauthAccessTokensTable extends Migration 22 | { 23 | /** 24 | * Run the migrations. 25 | * 26 | * @return void 27 | */ 28 | public function up() 29 | { 30 | Schema::create('oauth_access_tokens', function (Blueprint $table) { 31 | $table->string('id', 40)->primary(); 32 | $table->integer('session_id')->unsigned(); 33 | $table->integer('expire_time'); 34 | 35 | $table->timestamps(); 36 | 37 | $table->unique(['id', 'session_id']); 38 | $table->index('session_id'); 39 | 40 | $table->foreign('session_id') 41 | ->references('id')->on('oauth_sessions') 42 | ->onDelete('cascade'); 43 | }); 44 | } 45 | 46 | /** 47 | * Reverse the migrations. 48 | * 49 | * @return void 50 | */ 51 | public function down() 52 | { 53 | Schema::table('oauth_access_tokens', function (Blueprint $table) { 54 | $table->dropForeign('oauth_access_tokens_session_id_foreign'); 55 | }); 56 | Schema::drop('oauth_access_tokens'); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /database/migrations/2014_04_24_110557_create_oauth_client_endpoints_table.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | use Illuminate\Database\Migrations\Migration; 13 | use Illuminate\Database\Schema\Blueprint; 14 | use Illuminate\Support\Facades\Schema; 15 | 16 | /** 17 | * This is the create oauth client endpoints table migration class. 18 | * 19 | * @author Luca Degasperi 20 | */ 21 | class CreateOauthClientEndpointsTable extends Migration 22 | { 23 | /** 24 | * Run the migrations. 25 | * 26 | * @return void 27 | */ 28 | public function up() 29 | { 30 | Schema::create('oauth_client_endpoints', function (Blueprint $table) { 31 | $table->increments('id'); 32 | $table->string('client_id', 40); 33 | $table->string('redirect_uri'); 34 | 35 | $table->timestamps(); 36 | 37 | $table->unique(['client_id', 'redirect_uri']); 38 | 39 | $table->foreign('client_id') 40 | ->references('id')->on('oauth_clients') 41 | ->onDelete('cascade') 42 | ->onUpdate('cascade'); 43 | }); 44 | } 45 | 46 | /** 47 | * Reverse the migrations. 48 | * 49 | * @return void 50 | */ 51 | public function down() 52 | { 53 | Schema::table('oauth_client_endpoints', function (Blueprint $table) { 54 | $table->dropForeign('oauth_client_endpoints_client_id_foreign'); 55 | }); 56 | 57 | Schema::drop('oauth_client_endpoints'); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /bootstrap/app.php: -------------------------------------------------------------------------------- 1 | singleton( 30 | Illuminate\Contracts\Http\Kernel::class, 31 | App\Http\Kernel::class 32 | ); 33 | 34 | $app->singleton( 35 | Illuminate\Contracts\Console\Kernel::class, 36 | App\Console\Kernel::class 37 | ); 38 | 39 | $app->singleton( 40 | Illuminate\Contracts\Debug\ExceptionHandler::class, 41 | App\Exceptions\Handler::class 42 | ); 43 | 44 | /* 45 | |-------------------------------------------------------------------------- 46 | | Return The Application 47 | |-------------------------------------------------------------------------- 48 | | 49 | | This script returns the application instance. The instance is given to 50 | | the calling script so we can separate the building of the instances 51 | | from the actual running of the application and sending responses. 52 | | 53 | */ 54 | 55 | return $app; 56 | -------------------------------------------------------------------------------- /artisan: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env php 2 | make(Illuminate\Contracts\Console\Kernel::class); 32 | 33 | $status = $kernel->handle( 34 | $input = new Symfony\Component\Console\Input\ArgvInput, 35 | new Symfony\Component\Console\Output\ConsoleOutput 36 | ); 37 | 38 | /* 39 | |-------------------------------------------------------------------------- 40 | | Shutdown The Application 41 | |-------------------------------------------------------------------------- 42 | | 43 | | Once Artisan has finished running. We will fire off the shutdown events 44 | | so that any final work may be done by the application before we shut 45 | | down the process. This is the last thing to happen to the request. 46 | | 47 | */ 48 | 49 | $kernel->terminate($input, $status); 50 | 51 | exit($status); 52 | -------------------------------------------------------------------------------- /database/migrations/2014_04_24_111002_create_oauth_sessions_table.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | use Illuminate\Database\Migrations\Migration; 13 | use Illuminate\Database\Schema\Blueprint; 14 | use Illuminate\Support\Facades\Schema; 15 | 16 | /** 17 | * This is the create oauth sessions table migration class. 18 | * 19 | * @author Luca Degasperi 20 | */ 21 | class CreateOauthSessionsTable extends Migration 22 | { 23 | /** 24 | * Run the migrations. 25 | * 26 | * @return void 27 | */ 28 | public function up() 29 | { 30 | Schema::create('oauth_sessions', function (Blueprint $table) { 31 | $table->increments('id'); 32 | $table->string('client_id', 40); 33 | $table->enum('owner_type', ['client', 'user'])->default('user'); 34 | $table->string('owner_id'); 35 | $table->string('client_redirect_uri')->nullable(); 36 | $table->timestamps(); 37 | 38 | $table->index(['client_id', 'owner_type', 'owner_id']); 39 | 40 | $table->foreign('client_id') 41 | ->references('id')->on('oauth_clients') 42 | ->onDelete('cascade') 43 | ->onUpdate('cascade'); 44 | }); 45 | } 46 | 47 | /** 48 | * Reverse the migrations. 49 | * 50 | * @return void 51 | */ 52 | public function down() 53 | { 54 | Schema::table('oauth_sessions', function (Blueprint $table) { 55 | $table->dropForeign('oauth_sessions_client_id_foreign'); 56 | }); 57 | Schema::drop('oauth_sessions'); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /database/migrations/2014_04_24_110403_create_oauth_grant_scopes_table.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | use Illuminate\Database\Migrations\Migration; 13 | use Illuminate\Database\Schema\Blueprint; 14 | use Illuminate\Support\Facades\Schema; 15 | 16 | /** 17 | * This is the create oauth grant scopes table migration class. 18 | * 19 | * @author Luca Degasperi 20 | */ 21 | class CreateOauthGrantScopesTable extends Migration 22 | { 23 | /** 24 | * Run the migrations. 25 | * 26 | * @return void 27 | */ 28 | public function up() 29 | { 30 | Schema::create('oauth_grant_scopes', function (Blueprint $table) { 31 | $table->increments('id'); 32 | $table->string('grant_id', 40); 33 | $table->string('scope_id', 40); 34 | 35 | $table->timestamps(); 36 | 37 | $table->index('grant_id'); 38 | $table->index('scope_id'); 39 | 40 | $table->foreign('grant_id') 41 | ->references('id')->on('oauth_grants') 42 | ->onDelete('cascade'); 43 | 44 | $table->foreign('scope_id') 45 | ->references('id')->on('oauth_scopes') 46 | ->onDelete('cascade'); 47 | }); 48 | } 49 | 50 | /** 51 | * Reverse the migrations. 52 | * 53 | * @return void 54 | */ 55 | public function down() 56 | { 57 | Schema::table('oauth_grant_scopes', function (Blueprint $table) { 58 | $table->dropForeign('oauth_grant_scopes_grant_id_foreign'); 59 | $table->dropForeign('oauth_grant_scopes_scope_id_foreign'); 60 | }); 61 | Schema::drop('oauth_grant_scopes'); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /database/migrations/2014_04_24_110705_create_oauth_client_scopes_table.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | use Illuminate\Database\Migrations\Migration; 13 | use Illuminate\Database\Schema\Blueprint; 14 | use Illuminate\Support\Facades\Schema; 15 | 16 | /** 17 | * This is the create oauth client scopes table migration class. 18 | * 19 | * @author Luca Degasperi 20 | */ 21 | class CreateOauthClientScopesTable extends Migration 22 | { 23 | /** 24 | * Run the migrations. 25 | * 26 | * @return void 27 | */ 28 | public function up() 29 | { 30 | Schema::create('oauth_client_scopes', function (Blueprint $table) { 31 | $table->increments('id'); 32 | $table->string('client_id', 40); 33 | $table->string('scope_id', 40); 34 | 35 | $table->timestamps(); 36 | 37 | $table->index('client_id'); 38 | $table->index('scope_id'); 39 | 40 | $table->foreign('client_id') 41 | ->references('id')->on('oauth_clients') 42 | ->onDelete('cascade'); 43 | 44 | $table->foreign('scope_id') 45 | ->references('id')->on('oauth_scopes') 46 | ->onDelete('cascade'); 47 | }); 48 | } 49 | 50 | /** 51 | * Reverse the migrations. 52 | * 53 | * @return void 54 | */ 55 | public function down() 56 | { 57 | Schema::table('oauth_client_scopes', function (Blueprint $table) { 58 | $table->dropForeign('oauth_client_scopes_client_id_foreign'); 59 | $table->dropForeign('oauth_client_scopes_scope_id_foreign'); 60 | }); 61 | Schema::drop('oauth_client_scopes'); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /database/migrations/2014_04_24_111109_create_oauth_session_scopes_table.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | use Illuminate\Database\Migrations\Migration; 13 | use Illuminate\Database\Schema\Blueprint; 14 | use Illuminate\Support\Facades\Schema; 15 | 16 | /** 17 | * This is the create oauth session scopes table migration class. 18 | * 19 | * @author Luca Degasperi 20 | */ 21 | class CreateOauthSessionScopesTable extends Migration 22 | { 23 | /** 24 | * Run the migrations. 25 | * 26 | * @return void 27 | */ 28 | public function up() 29 | { 30 | Schema::create('oauth_session_scopes', function (Blueprint $table) { 31 | $table->increments('id'); 32 | $table->integer('session_id')->unsigned(); 33 | $table->string('scope_id', 40); 34 | 35 | $table->timestamps(); 36 | 37 | $table->index('session_id'); 38 | $table->index('scope_id'); 39 | 40 | $table->foreign('session_id') 41 | ->references('id')->on('oauth_sessions') 42 | ->onDelete('cascade'); 43 | 44 | $table->foreign('scope_id') 45 | ->references('id')->on('oauth_scopes') 46 | ->onDelete('cascade'); 47 | }); 48 | } 49 | 50 | /** 51 | * Reverse the migrations. 52 | * 53 | * @return void 54 | */ 55 | public function down() 56 | { 57 | Schema::table('oauth_session_scopes', function (Blueprint $table) { 58 | $table->dropForeign('oauth_session_scopes_session_id_foreign'); 59 | $table->dropForeign('oauth_session_scopes_scope_id_foreign'); 60 | }); 61 | Schema::drop('oauth_session_scopes'); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /database/migrations/2014_04_24_111403_create_oauth_auth_code_scopes_table.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | use Illuminate\Database\Migrations\Migration; 13 | use Illuminate\Database\Schema\Blueprint; 14 | use Illuminate\Support\Facades\Schema; 15 | 16 | /** 17 | * This is the create oauth code scopes table migration class. 18 | * 19 | * @author Luca Degasperi 20 | */ 21 | class CreateOauthAuthCodeScopesTable extends Migration 22 | { 23 | /** 24 | * Run the migrations. 25 | * 26 | * @return void 27 | */ 28 | public function up() 29 | { 30 | Schema::create('oauth_auth_code_scopes', function (Blueprint $table) { 31 | $table->increments('id'); 32 | $table->string('auth_code_id', 40); 33 | $table->string('scope_id', 40); 34 | 35 | $table->timestamps(); 36 | 37 | $table->index('auth_code_id'); 38 | $table->index('scope_id'); 39 | 40 | $table->foreign('auth_code_id') 41 | ->references('id')->on('oauth_auth_codes') 42 | ->onDelete('cascade'); 43 | 44 | $table->foreign('scope_id') 45 | ->references('id')->on('oauth_scopes') 46 | ->onDelete('cascade'); 47 | }); 48 | } 49 | 50 | /** 51 | * Reverse the migrations. 52 | * 53 | * @return void 54 | */ 55 | public function down() 56 | { 57 | Schema::table('oauth_auth_code_scopes', function (Blueprint $table) { 58 | $table->dropForeign('oauth_auth_code_scopes_auth_code_id_foreign'); 59 | $table->dropForeign('oauth_auth_code_scopes_scope_id_foreign'); 60 | }); 61 | Schema::drop('oauth_auth_code_scopes'); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /public/index.php: -------------------------------------------------------------------------------- 1 | 8 | */ 9 | 10 | /* 11 | |-------------------------------------------------------------------------- 12 | | Register The Auto Loader 13 | |-------------------------------------------------------------------------- 14 | | 15 | | Composer provides a convenient, automatically generated class loader for 16 | | our application. We just need to utilize it! We'll simply require it 17 | | into the script here so that we don't have to worry about manual 18 | | loading any of our classes later on. It feels nice to relax. 19 | | 20 | */ 21 | 22 | require __DIR__.'/../bootstrap/autoload.php'; 23 | 24 | /* 25 | |-------------------------------------------------------------------------- 26 | | Turn On The Lights 27 | |-------------------------------------------------------------------------- 28 | | 29 | | We need to illuminate PHP development, so let us turn on the lights. 30 | | This bootstraps the framework and gets it ready for use, then it 31 | | will load up this application so that we can run it and send 32 | | the responses back to the browser and delight our users. 33 | | 34 | */ 35 | 36 | $app = require_once __DIR__.'/../bootstrap/app.php'; 37 | 38 | /* 39 | |-------------------------------------------------------------------------- 40 | | Run The Application 41 | |-------------------------------------------------------------------------- 42 | | 43 | | Once we have the application, we can handle the incoming request 44 | | through the kernel, and send the associated response back to 45 | | the client's browser allowing them to enjoy the creative 46 | | and wonderful application we have prepared for them. 47 | | 48 | */ 49 | 50 | $kernel = $app->make(Illuminate\Contracts\Http\Kernel::class); 51 | 52 | $response = $kernel->handle( 53 | $request = Illuminate\Http\Request::capture() 54 | ); 55 | 56 | $response->send(); 57 | 58 | $kernel->terminate($request, $response); 59 | -------------------------------------------------------------------------------- /database/migrations/2014_04_24_111657_create_oauth_access_token_scopes_table.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | use Illuminate\Database\Migrations\Migration; 13 | use Illuminate\Database\Schema\Blueprint; 14 | use Illuminate\Support\Facades\Schema; 15 | 16 | /** 17 | * This is the create oauth access token scopes table migration class. 18 | * 19 | * @author Luca Degasperi 20 | */ 21 | class CreateOauthAccessTokenScopesTable extends Migration 22 | { 23 | /** 24 | * Run the migrations. 25 | * 26 | * @return void 27 | */ 28 | public function up() 29 | { 30 | Schema::create('oauth_access_token_scopes', function (Blueprint $table) { 31 | $table->increments('id'); 32 | $table->string('access_token_id', 40); 33 | $table->string('scope_id', 40); 34 | 35 | $table->timestamps(); 36 | 37 | $table->index('access_token_id'); 38 | $table->index('scope_id'); 39 | 40 | $table->foreign('access_token_id') 41 | ->references('id')->on('oauth_access_tokens') 42 | ->onDelete('cascade'); 43 | 44 | $table->foreign('scope_id') 45 | ->references('id')->on('oauth_scopes') 46 | ->onDelete('cascade'); 47 | }); 48 | } 49 | 50 | /** 51 | * Reverse the migrations. 52 | * 53 | * @return void 54 | */ 55 | public function down() 56 | { 57 | Schema::table('oauth_access_token_scopes', function (Blueprint $table) { 58 | $table->dropForeign('oauth_access_token_scopes_scope_id_foreign'); 59 | $table->dropForeign('oauth_access_token_scopes_access_token_id_foreign'); 60 | }); 61 | Schema::drop('oauth_access_token_scopes'); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /database/migrations/2014_04_24_110817_create_oauth_client_grants_table.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | use Illuminate\Database\Migrations\Migration; 13 | use Illuminate\Database\Schema\Blueprint; 14 | use Illuminate\Support\Facades\Schema; 15 | 16 | /** 17 | * This is the create oauth client grants table migration class. 18 | * 19 | * @author Luca Degasperi 20 | */ 21 | class CreateOauthClientGrantsTable extends Migration 22 | { 23 | /** 24 | * Run the migrations. 25 | * 26 | * @return void 27 | */ 28 | public function up() 29 | { 30 | Schema::create('oauth_client_grants', function (Blueprint $table) { 31 | $table->increments('id'); 32 | $table->string('client_id', 40); 33 | $table->string('grant_id', 40); 34 | $table->timestamps(); 35 | 36 | $table->index('client_id'); 37 | $table->index('grant_id'); 38 | 39 | $table->foreign('client_id') 40 | ->references('id')->on('oauth_clients') 41 | ->onDelete('cascade') 42 | ->onUpdate('no action'); 43 | 44 | $table->foreign('grant_id') 45 | ->references('id')->on('oauth_grants') 46 | ->onDelete('cascade') 47 | ->onUpdate('no action'); 48 | }); 49 | } 50 | 51 | /** 52 | * Reverse the migrations. 53 | * 54 | * @return void 55 | */ 56 | public function down() 57 | { 58 | Schema::table('oauth_client_grants', function (Blueprint $table) { 59 | $table->dropForeign('oauth_client_grants_client_id_foreign'); 60 | $table->dropForeign('oauth_client_grants_grant_id_foreign'); 61 | }); 62 | Schema::drop('oauth_client_grants'); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /config/cache.php: -------------------------------------------------------------------------------- 1 | env('CACHE_DRIVER', 'file'), 17 | 18 | /* 19 | |-------------------------------------------------------------------------- 20 | | Cache Stores 21 | |-------------------------------------------------------------------------- 22 | | 23 | | Here you may define all of the cache "stores" for your application as 24 | | well as their drivers. You may even define multiple stores for the 25 | | same cache driver to group types of items stored in your caches. 26 | | 27 | */ 28 | 29 | 'stores' => [ 30 | 31 | 'apc' => [ 32 | 'driver' => 'apc', 33 | ], 34 | 35 | 'array' => [ 36 | 'driver' => 'array', 37 | ], 38 | 39 | 'database' => [ 40 | 'driver' => 'database', 41 | 'table' => 'cache', 42 | 'connection' => null, 43 | ], 44 | 45 | 'file' => [ 46 | 'driver' => 'file', 47 | 'path' => storage_path('framework/cache'), 48 | ], 49 | 50 | 'memcached' => [ 51 | 'driver' => 'memcached', 52 | 'servers' => [ 53 | [ 54 | 'host' => '127.0.0.1', 'port' => 11211, 'weight' => 100, 55 | ], 56 | ], 57 | ], 58 | 59 | 'redis' => [ 60 | 'driver' => 'redis', 61 | 'connection' => 'default', 62 | ], 63 | 64 | ], 65 | 66 | /* 67 | |-------------------------------------------------------------------------- 68 | | Cache Key Prefix 69 | |-------------------------------------------------------------------------- 70 | | 71 | | When utilizing a RAM based store such as APC or Memcached, there might 72 | | be other applications utilizing the same cache. So, we'll specify a 73 | | value to get prefixed to all our keys so we can avoid collisions. 74 | | 75 | */ 76 | 77 | 'prefix' => 'laravel', 78 | 79 | ]; 80 | -------------------------------------------------------------------------------- /config/auth.php: -------------------------------------------------------------------------------- 1 | 'eloquent', 19 | 20 | /* 21 | |-------------------------------------------------------------------------- 22 | | Authentication Model 23 | |-------------------------------------------------------------------------- 24 | | 25 | | When using the "Eloquent" authentication driver, we need to know which 26 | | Eloquent model should be used to retrieve your users. Of course, it 27 | | is often just the "User" model but you may use whatever you like. 28 | | 29 | */ 30 | 31 | 'model' => App\User::class, 32 | 33 | /* 34 | |-------------------------------------------------------------------------- 35 | | Authentication Table 36 | |-------------------------------------------------------------------------- 37 | | 38 | | When using the "Database" authentication driver, we need to know which 39 | | table should be used to retrieve your users. We have chosen a basic 40 | | default value but you may easily change it to any table you like. 41 | | 42 | */ 43 | 44 | 'table' => 'users', 45 | 46 | /* 47 | |-------------------------------------------------------------------------- 48 | | Password Reset Settings 49 | |-------------------------------------------------------------------------- 50 | | 51 | | Here you may set the options for resetting passwords including the view 52 | | that is your password reset e-mail. You can also set the name of the 53 | | table that maintains all of the reset tokens for your application. 54 | | 55 | | The expire time is the number of minutes that the reset token should be 56 | | considered valid. This security feature keeps tokens short-lived so 57 | | they have less time to be guessed. You may change this as needed. 58 | | 59 | */ 60 | 61 | 'password' => [ 62 | 'email' => 'emails.password', 63 | 'table' => 'password_resets', 64 | 'expire' => 60, 65 | ], 66 | 67 | ]; 68 | -------------------------------------------------------------------------------- /config/boilerplate.php: -------------------------------------------------------------------------------- 1 | [ 16 | 'name', 'email', 'password' 17 | ], 18 | 19 | /* 20 | |-------------------------------------------------------------------------- 21 | | Signup Fields Rules 22 | |-------------------------------------------------------------------------- 23 | | 24 | | Here you can put the rules you want to use for the validator instance 25 | | in the signup method. 26 | | 27 | */ 28 | 'signup_fields_rules' => [ 29 | 'name' => 'required', 30 | 'email' => 'required|email|unique:users', 31 | 'password' => 'required|min:6' 32 | ], 33 | 34 | /* 35 | |-------------------------------------------------------------------------- 36 | | Signup Token Release 37 | |-------------------------------------------------------------------------- 38 | | 39 | | If this field is "true", an authentication token will be automatically 40 | | released after signup. Otherwise, the signup method will return a simple 41 | | success message. 42 | | 43 | */ 44 | 'signup_token_release' => env('API_SIGNUP_TOKEN_RELEASE', true), 45 | 46 | /* 47 | |-------------------------------------------------------------------------- 48 | | Password Reset Token Release 49 | |-------------------------------------------------------------------------- 50 | | 51 | | If this field is "true", an authentication token will be automatically 52 | | released after password reset. Otherwise, the signup method will return a 53 | | simple success message. 54 | | 55 | */ 56 | 'reset_token_release' => env('API_RESET_TOKEN_RELEASE', true), 57 | 58 | /* 59 | |-------------------------------------------------------------------------- 60 | | Recovery Email Subject 61 | |-------------------------------------------------------------------------- 62 | | 63 | | The email address you want use to send the recovery email. 64 | | 65 | */ 66 | 'recovery_email_subject' => env('API_RECOVERY_EMAIL_SUBJECT', true), 67 | 68 | ]; 69 | -------------------------------------------------------------------------------- /app/Api/V1/Controllers/BaseController.php: -------------------------------------------------------------------------------- 1 | limit = ($request->get('limit') ? $request->get('limit') : config('mm.page_limit')); 30 | // $this->debugQueries(); 31 | } 32 | 33 | public function throwStoreResourceFailedException($message='Failed to store your requested resource.', Validator $validator=null) 34 | { 35 | if ($validator instanceof Validator) { 36 | throw new \Dingo\Api\Exception\StoreResourceFailedException($message, $validator->errors()); 37 | } else { 38 | throw new \Dingo\Api\Exception\StoreResourceFailedException($message); 39 | } 40 | } 41 | 42 | public function throwResourceException($message='Failed to process your requested resource.') 43 | { 44 | throw new \Dingo\Api\Exception\ResourceException($message); 45 | } 46 | 47 | protected function validateOrFail($data, $validationRules, $options=[]) 48 | { 49 | if ($this->auth->user()) { 50 | $data['user_id'] = $this->auth->user()->id; // Get User id from User Resolver 51 | } 52 | 53 | $validator = app('validator')->make($data, $validationRules, $options); 54 | 55 | if ($validator->fails()) { 56 | $message = (isset($options['message']) ? $options['message']:'Could not process your request, following are the errors.'); 57 | throw new ValidationHttpException($validator->errors()->all()); 58 | } 59 | } 60 | 61 | protected function getAuthenticatedUserId() 62 | { 63 | if (null !== $this->auth->user() && isset($this->auth->user()->id)) { 64 | return $this->auth->user()->id; 65 | } else { 66 | throw new \Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException('Unable to get authenticated user info.', 'Unable to get authenticated user info.'); 67 | } 68 | } 69 | 70 | public function debugQueries() 71 | { 72 | if (app()->environment('local')) { 73 | DB::listen(function($sql, $bindings) { 74 | var_dump($sql); 75 | var_dump($bindings); 76 | }); 77 | } 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /config/filesystems.php: -------------------------------------------------------------------------------- 1 | 'local', 19 | 20 | /* 21 | |-------------------------------------------------------------------------- 22 | | Default Cloud Filesystem Disk 23 | |-------------------------------------------------------------------------- 24 | | 25 | | Many applications store files both locally and in the cloud. For this 26 | | reason, you may specify a default "cloud" driver here. This driver 27 | | will be bound as the Cloud disk implementation in the container. 28 | | 29 | */ 30 | 31 | 'cloud' => 's3', 32 | 33 | /* 34 | |-------------------------------------------------------------------------- 35 | | Filesystem Disks 36 | |-------------------------------------------------------------------------- 37 | | 38 | | Here you may configure as many filesystem "disks" as you wish, and you 39 | | may even configure multiple disks of the same driver. Defaults have 40 | | been setup for each driver as an example of the required options. 41 | | 42 | */ 43 | 44 | 'disks' => [ 45 | 46 | 'local' => [ 47 | 'driver' => 'local', 48 | 'root' => storage_path('app'), 49 | ], 50 | 51 | 'ftp' => [ 52 | 'driver' => 'ftp', 53 | 'host' => 'ftp.example.com', 54 | 'username' => 'your-username', 55 | 'password' => 'your-password', 56 | 57 | // Optional FTP Settings... 58 | // 'port' => 21, 59 | // 'root' => '', 60 | // 'passive' => true, 61 | // 'ssl' => true, 62 | // 'timeout' => 30, 63 | ], 64 | 65 | 's3' => [ 66 | 'driver' => 's3', 67 | 'key' => 'your-key', 68 | 'secret' => 'your-secret', 69 | 'region' => 'your-region', 70 | 'bucket' => 'your-bucket', 71 | ], 72 | 73 | 'rackspace' => [ 74 | 'driver' => 'rackspace', 75 | 'username' => 'your-username', 76 | 'key' => 'your-key', 77 | 'container' => 'your-container', 78 | 'endpoint' => 'https://identity.api.rackspacecloud.com/v2.0/', 79 | 'region' => 'IAD', 80 | 'url_type' => 'publicURL', 81 | ], 82 | 83 | ], 84 | 85 | ]; 86 | -------------------------------------------------------------------------------- /app/Repositories/User/UserRepository.php: -------------------------------------------------------------------------------- 1 | profileRepo = $profileRepo; 21 | } 22 | 23 | public function throwStoreResourceFailedException($message='Failed to store your requested resource.', Validator $validator=null) 24 | { 25 | if ($validator instanceof Validator) { 26 | throw new \Dingo\Api\Exception\StoreResourceFailedException($message, $validator->errors()); 27 | } else { 28 | throw new \Dingo\Api\Exception\StoreResourceFailedException($message); 29 | } 30 | } 31 | 32 | public function getAll() 33 | { 34 | return 'get all'; 35 | } 36 | 37 | public function find($id) 38 | { 39 | $user = User::find($id); 40 | if ($user) { 41 | return $user; 42 | } else { 43 | $this->throwStoreResourceFailedException(); 44 | } 45 | } 46 | 47 | public function createUser($data) 48 | { 49 | $user = new User; 50 | if (!empty($data['token_facebook'])) { 51 | $user = User::firstOrNew(['email' => $data['username']]); 52 | $user->auth_type = AuthType::FACEBOOK; 53 | $user->vendor_auth_token = $data['token_facebook']; 54 | } else { 55 | $user->email = $data['email']; 56 | $user->auth_type = AuthType::EMAIL; 57 | $user->password = bcrypt($data['password']); 58 | } 59 | 60 | DB::beginTransaction(); 61 | try { 62 | $user->save(); 63 | if (!empty($data['profile'])) { 64 | $user = $this->createOrUpdateProfile($user, $data['profile']); 65 | } 66 | DB::commit(); 67 | return $user->load('profile'); 68 | } catch (Exception $ex) { 69 | DB::rollback(); 70 | return false; 71 | } 72 | } 73 | 74 | public function createOrUpdateProfile($user, $data) 75 | { 76 | $profile = $user->profile; 77 | 78 | if (!$profile) { 79 | $profile = new Profile; 80 | } 81 | 82 | DB::beginTransaction(); 83 | try { 84 | $profile->fill($data); 85 | 86 | if ($user->profile()->save($profile)) { 87 | DB::commit(); 88 | return $user; 89 | } else { 90 | DB::rollback(); 91 | return false; 92 | } 93 | } catch (Exception $ex) { 94 | DB::rollback(); 95 | return false; 96 | } 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /config/queue.php: -------------------------------------------------------------------------------- 1 | env('QUEUE_DRIVER', 'sync'), 20 | 21 | /* 22 | |-------------------------------------------------------------------------- 23 | | Queue Connections 24 | |-------------------------------------------------------------------------- 25 | | 26 | | Here you may configure the connection information for each server that 27 | | is used by your application. A default configuration has been added 28 | | for each back-end shipped with Laravel. You are free to add more. 29 | | 30 | */ 31 | 32 | 'connections' => [ 33 | 34 | 'sync' => [ 35 | 'driver' => 'sync', 36 | ], 37 | 38 | 'database' => [ 39 | 'driver' => 'database', 40 | 'table' => 'jobs', 41 | 'queue' => 'default', 42 | 'expire' => 60, 43 | ], 44 | 45 | 'beanstalkd' => [ 46 | 'driver' => 'beanstalkd', 47 | 'host' => 'localhost', 48 | 'queue' => 'default', 49 | 'ttr' => 60, 50 | ], 51 | 52 | 'sqs' => [ 53 | 'driver' => 'sqs', 54 | 'key' => 'your-public-key', 55 | 'secret' => 'your-secret-key', 56 | 'queue' => 'your-queue-url', 57 | 'region' => 'us-east-1', 58 | ], 59 | 60 | 'iron' => [ 61 | 'driver' => 'iron', 62 | 'host' => 'mq-aws-us-east-1.iron.io', 63 | 'token' => 'your-token', 64 | 'project' => 'your-project-id', 65 | 'queue' => 'your-queue-name', 66 | 'encrypt' => true, 67 | ], 68 | 69 | 'redis' => [ 70 | 'driver' => 'redis', 71 | 'connection' => 'default', 72 | 'queue' => 'default', 73 | 'expire' => 60, 74 | ], 75 | 76 | ], 77 | 78 | /* 79 | |-------------------------------------------------------------------------- 80 | | Failed Queue Jobs 81 | |-------------------------------------------------------------------------- 82 | | 83 | | These options configure the behavior of failed queue job logging so you 84 | | can control which database and table are used to store the jobs that 85 | | have failed. You may change them to any database / table you wish. 86 | | 87 | */ 88 | 89 | 'failed' => [ 90 | 'database' => 'mysql', 'table' => 'failed_jobs', 91 | ], 92 | 93 | ]; 94 | -------------------------------------------------------------------------------- /config/database.php: -------------------------------------------------------------------------------- 1 | PDO::FETCH_CLASS, 17 | 18 | /* 19 | |-------------------------------------------------------------------------- 20 | | Default Database Connection Name 21 | |-------------------------------------------------------------------------- 22 | | 23 | | Here you may specify which of the database connections below you wish 24 | | to use as your default connection for all database work. Of course 25 | | you may use many connections at once using the Database library. 26 | | 27 | */ 28 | 29 | 'default' => env('DB_CONNECTION', 'mysql'), 30 | 31 | /* 32 | |-------------------------------------------------------------------------- 33 | | Database Connections 34 | |-------------------------------------------------------------------------- 35 | | 36 | | Here are each of the database connections setup for your application. 37 | | Of course, examples of configuring each database platform that is 38 | | supported by Laravel is shown below to make development simple. 39 | | 40 | | 41 | | All database work in Laravel is done through the PHP PDO facilities 42 | | so make sure you have the driver for your particular database of 43 | | choice installed on your machine before you begin development. 44 | | 45 | */ 46 | 47 | 'connections' => [ 48 | 49 | 'sqlite' => [ 50 | 'driver' => 'sqlite', 51 | 'database' => storage_path('database.sqlite'), 52 | 'prefix' => '', 53 | ], 54 | 55 | 'mysql' => [ 56 | 'driver' => 'mysql', 57 | 'host' => env('DB_HOST', 'localhost'), 58 | 'database' => env('DB_DATABASE', 'forge'), 59 | 'username' => env('DB_USERNAME', 'forge'), 60 | 'password' => env('DB_PASSWORD', ''), 61 | 'charset' => 'utf8', 62 | 'collation' => 'utf8_unicode_ci', 63 | 'prefix' => '', 64 | 'strict' => false, 65 | ], 66 | 67 | 'pgsql' => [ 68 | 'driver' => 'pgsql', 69 | 'host' => env('DB_HOST', 'localhost'), 70 | 'database' => env('DB_DATABASE', 'forge'), 71 | 'username' => env('DB_USERNAME', 'forge'), 72 | 'password' => env('DB_PASSWORD', ''), 73 | 'charset' => 'utf8', 74 | 'prefix' => '', 75 | 'schema' => 'public', 76 | ], 77 | 78 | 'sqlsrv' => [ 79 | 'driver' => 'sqlsrv', 80 | 'host' => env('DB_HOST', 'localhost'), 81 | 'database' => env('DB_DATABASE', 'forge'), 82 | 'username' => env('DB_USERNAME', 'forge'), 83 | 'password' => env('DB_PASSWORD', ''), 84 | 'charset' => 'utf8', 85 | 'prefix' => '', 86 | ], 87 | 88 | ], 89 | 90 | /* 91 | |-------------------------------------------------------------------------- 92 | | Migration Repository Table 93 | |-------------------------------------------------------------------------- 94 | | 95 | | This table keeps track of all the migrations that have already run for 96 | | your application. Using this information, we can determine which of 97 | | the migrations on disk haven't actually been run in the database. 98 | | 99 | */ 100 | 101 | 'migrations' => 'migrations', 102 | 103 | /* 104 | |-------------------------------------------------------------------------- 105 | | Redis Databases 106 | |-------------------------------------------------------------------------- 107 | | 108 | | Redis is an open source, fast, and advanced key-value store that also 109 | | provides a richer set of commands than a typical key-value systems 110 | | such as APC or Memcached. Laravel makes it easy to dig right in. 111 | | 112 | */ 113 | 114 | 'redis' => [ 115 | 116 | 'cluster' => false, 117 | 118 | 'default' => [ 119 | 'host' => '127.0.0.1', 120 | 'port' => 6379, 121 | 'database' => 0, 122 | ], 123 | 124 | ], 125 | 126 | ]; 127 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | ## Laravel API Boilerplate (OAuth2 Edition) 2 | [![Latest Stable Version](https://poser.pugx.org/muhammadshakeel/laravel-api-boilerplate-oauth/v/stable)](https://packagist.org/packages/muhammadshakeel/laravel-api-boilerplate-oauth) [![Total Downloads](https://poser.pugx.org/muhammadshakeel/laravel-api-boilerplate-oauth/downloads)](https://packagist.org/packages/muhammadshakeel/laravel-api-boilerplate-oauth) [![Latest Unstable Version](https://poser.pugx.org/muhammadshakeel/laravel-api-boilerplate-oauth/v/unstable)](https://packagist.org/packages/muhammadshakeel/laravel-api-boilerplate-oauth) [![License](https://poser.pugx.org/muhammadshakeel/laravel-api-boilerplate-oauth/license)](https://packagist.org/packages/muhammadshakeel/laravel-api-boilerplate-oauth) 3 | [![Codacy Badge](https://api.codacy.com/project/badge/grade/b7b5fed8a7ed4981a5276f5af21161e7)](https://www.codacy.com/app/mshakeel/laravel-api-boilerplate-oauth) 4 | ### Based on [francescomalatesta/laravel-api-boilerplate-jwt](https://github.com/francescomalatesta/laravel-api-boilerplate-jwt) 5 | 6 | Laravel API Boilerplate is a ready-to-use "starting pack" that you can use to build your first API in seconds. As you can easily imagine, it is built on top of the awesome Laravel Framework. 7 | 8 | It also benefits from three pacakages: 9 | 10 | * OAuth2 - [lucadegasperi/oauth2-server-laravel](https://github.com/lucadegasperi/oauth2-server-laravel) 11 | * Dingo API - [dingo/api](https://github.com/dingo/api) 12 | 13 | With a similar foundation is really easy to get up and running in no time. I just made an "integration" work, adding here and there something that I found useful. 14 | 15 | ## Installation 16 | 17 | * composer create-project muhammadshakeel/laravel-api-boilerplate-oauth your-project 18 | * cd your-project 19 | * php -r "copy('.env.example', '.env');" 20 | * php artisan key:generate 21 | * chmod -R 777 storage/ bootstrap/cache/ 22 | * php artisan vendor:publish 23 | * php artisan migrate 24 | * php artisan db:seed --class=ClientTableSeeder 25 | 26 | Done! 27 | 28 | ## Main Features 29 | 30 | ### A Ready-To-Use AuthController 31 | 32 | I've put an "AuthController" in _App\Api\V1\Controllers_. It supports the four basic authentication/password recovery operations: 33 | 34 | * _login()_; 35 | * _signup()_; 36 | * _recovery()_; 37 | * _reset()_; 38 | 39 | In order to work with them, you just have to make a POST request with the required data. 40 | 41 | You will need: 42 | 43 | * _login_: just email and password; 44 | * _signup_: whatever you like: you can specify it in the config file; 45 | * _recovery_: just the user email address; 46 | * _reset_: token, email, password and password confirmation; 47 | 48 | ### A Separate File for Routes 49 | 50 | You can specify your routes in the *api_routes.php_ file, that will be automatically loaded. In this file you will find many examples of routes. 51 | 52 | ## Configuration 53 | 54 | As I already told before, this boilerplate is based on _dingo/api_ and _lucadegasperi/oauth2-server-laravel_ packages. So, you can find many informations about configuration here and here. 55 | 56 | However, there are some extra options that I placed in a _config/boilerplate.php_ file. 57 | 58 | * **signup_fields**: you can use this option to specify what fields you want to use to create your user; 59 | * **signup_fields_rules**: you can use this option to specify the rules you want to use for the validator instance in the signup method; 60 | * **signup_token_release**: if "true", an access token will be released from the signup endpoint if everything goes well. Otherwise, you will just get a _201 Created_ response; 61 | * **reset_token_release**: if "true", an access token will be released from the signup endpoint if everything goes well. Otherwise, you will just get a _200_ response; 62 | * **recovery_email_subject**: here you can specify the subject for your recovery data email; 63 | 64 | ## Creating Endpoints 65 | 66 | You can create endpoints in the same way you could to with using the single _dingo/api_ package. You can read its documentation for details. 67 | 68 | After all, that's just a boilerplate! :) 69 | 70 | ## Notes 71 | 72 | I currently removed the _VerifyCsrfToken_ middleware from the _$middleware_ array in _app/Http/Kernel.php_ file. If you want to use it in your project, just use the route middleware _csrf_ you can find, in the same class, in the _$routeMiddleware_ array. 73 | 74 | ## Feedback 75 | 76 | I currently made this project for personal purposes. I decided to share it here to help anyone with the same needs. If you have any feedback to improve it, feel free to make a suggestion, or open a PR! 77 | -------------------------------------------------------------------------------- /config/mail.php: -------------------------------------------------------------------------------- 1 | env('MAIL_DRIVER', 'smtp'), 19 | 20 | /* 21 | |-------------------------------------------------------------------------- 22 | | SMTP Host Address 23 | |-------------------------------------------------------------------------- 24 | | 25 | | Here you may provide the host address of the SMTP server used by your 26 | | applications. A default option is provided that is compatible with 27 | | the Mailgun mail service which will provide reliable deliveries. 28 | | 29 | */ 30 | 31 | 'host' => env('MAIL_HOST', 'smtp.mailgun.org'), 32 | 33 | /* 34 | |-------------------------------------------------------------------------- 35 | | SMTP Host Port 36 | |-------------------------------------------------------------------------- 37 | | 38 | | This is the SMTP port used by your application to deliver e-mails to 39 | | users of the application. Like the host we have set this value to 40 | | stay compatible with the Mailgun e-mail application by default. 41 | | 42 | */ 43 | 44 | 'port' => env('MAIL_PORT', 587), 45 | 46 | /* 47 | |-------------------------------------------------------------------------- 48 | | Global "From" Address 49 | |-------------------------------------------------------------------------- 50 | | 51 | | You may wish for all e-mails sent by your application to be sent from 52 | | the same address. Here, you may specify a name and address that is 53 | | used globally for all e-mails that are sent by your application. 54 | | 55 | */ 56 | 57 | 'from' => ['address' => null, 'name' => null], 58 | 59 | /* 60 | |-------------------------------------------------------------------------- 61 | | E-Mail Encryption Protocol 62 | |-------------------------------------------------------------------------- 63 | | 64 | | Here you may specify the encryption protocol that should be used when 65 | | the application send e-mail messages. A sensible default using the 66 | | transport layer security protocol should provide great security. 67 | | 68 | */ 69 | 70 | 'encryption' => env('MAIL_ENCRYPTION', 'tls'), 71 | 72 | /* 73 | |-------------------------------------------------------------------------- 74 | | SMTP Server Username 75 | |-------------------------------------------------------------------------- 76 | | 77 | | If your SMTP server requires a username for authentication, you should 78 | | set it here. This will get used to authenticate with your server on 79 | | connection. You may also set the "password" value below this one. 80 | | 81 | */ 82 | 83 | 'username' => env('MAIL_USERNAME'), 84 | 85 | /* 86 | |-------------------------------------------------------------------------- 87 | | SMTP Server Password 88 | |-------------------------------------------------------------------------- 89 | | 90 | | Here you may set the password required by your SMTP server to send out 91 | | messages from your application. This will be given to the server on 92 | | connection so that the application will be able to send messages. 93 | | 94 | */ 95 | 96 | 'password' => env('MAIL_PASSWORD'), 97 | 98 | /* 99 | |-------------------------------------------------------------------------- 100 | | Sendmail System Path 101 | |-------------------------------------------------------------------------- 102 | | 103 | | When using the "sendmail" driver to send e-mails, we will need to know 104 | | the path to where Sendmail lives on this server. A default path has 105 | | been provided here, which will work well on most of your systems. 106 | | 107 | */ 108 | 109 | 'sendmail' => '/usr/sbin/sendmail -bs', 110 | 111 | /* 112 | |-------------------------------------------------------------------------- 113 | | Mail "Pretend" 114 | |-------------------------------------------------------------------------- 115 | | 116 | | When this option is enabled, e-mail will not actually be sent over the 117 | | web and will instead be written to your application's logs files so 118 | | you may inspect the message. This is great for local development. 119 | | 120 | */ 121 | 122 | 'pretend' => true, 123 | 124 | ]; 125 | -------------------------------------------------------------------------------- /app/Api/V1/Controllers/AuthController.php: -------------------------------------------------------------------------------- 1 | 'required', 26 | 'client_id' => 'required', 27 | 'client_secret' => 'required', 28 | 'username' => 'required|email', 29 | ]; 30 | } 31 | 32 | /** 33 | * Verify user credentials and generates authentication token 34 | * 35 | * @Get("/login") 36 | * @Versions({"v1"}) 37 | * 38 | * @Request({"grant_type":"password", "client_id":"{{client_id}}", "client_secret":"{{client_secret}}", "username":"fake@fake.com", "password":"secret"}) 39 | * 40 | * @Response(200, body={"access_token":"{{generated_token}}","token_type":"Bearer","expires_in":86400}) 41 | * 42 | * @param \Illuminate\Http\Request $request 43 | * @return \Illuminate\Http\Response 44 | */ 45 | public function login(Request $request) 46 | { 47 | $credentials = $request->only(['grant_type', 'client_id', 'client_secret', 'username', 'password']); 48 | 49 | $validationRules = $this->getLoginValidationRules(); 50 | $validationRules['password'] = 'required'; 51 | $this->validateOrFail($credentials, $validationRules); 52 | 53 | try { 54 | if (! $accessToken = Authorizer::issueAccessToken()) { 55 | return $this->response->errorUnauthorized(); 56 | } 57 | } catch (\League\OAuth2\Server\Exception\OAuthException $e) { 58 | throw $e; 59 | return $this->response->error('could_not_create_token', 500); 60 | } 61 | 62 | return response()->json(compact('accessToken')); 63 | } 64 | 65 | public function signup(Request $request) 66 | { 67 | $signupFields = Config::get('boilerplate.signup_fields'); 68 | $hasToReleaseToken = Config::get('boilerplate.signup_token_release'); 69 | 70 | $userData = $request->only($signupFields); 71 | 72 | $validator = Validator::make($userData, Config::get('boilerplate.signup_fields_rules')); 73 | 74 | if($validator->fails()) { 75 | throw new ValidationHttpException($validator->errors()->all()); 76 | } 77 | 78 | User::unguard(); 79 | $user = User::create($userData); 80 | User::reguard(); 81 | 82 | if(!$user->id) { 83 | return $this->response->error('could_not_create_user', 500); 84 | } 85 | 86 | if($hasToReleaseToken) { 87 | return $this->login($request); 88 | } 89 | 90 | return $this->response->created(); 91 | } 92 | 93 | public function recovery(Request $request) 94 | { 95 | $validator = Validator::make($request->only('email'), [ 96 | 'email' => 'required' 97 | ]); 98 | 99 | if($validator->fails()) { 100 | throw new ValidationHttpException($validator->errors()->all()); 101 | } 102 | 103 | $response = Password::sendResetLink($request->only('email'), function (Message $message) { 104 | $message->subject(Config::get('boilerplate.recovery_email_subject')); 105 | }); 106 | 107 | switch ($response) { 108 | case Password::RESET_LINK_SENT: 109 | return $this->response->noContent(); 110 | case Password::INVALID_USER: 111 | return $this->response->errorNotFound(); 112 | } 113 | } 114 | 115 | public function reset(Request $request) 116 | { 117 | $credentials = $request->only( 118 | 'email', 'password', 'password_confirmation', 'token' 119 | ); 120 | 121 | $validator = Validator::make($credentials, [ 122 | 'token' => 'required', 123 | 'email' => 'required|email', 124 | 'password' => 'required|confirmed|min:6', 125 | ]); 126 | 127 | if($validator->fails()) { 128 | throw new ValidationHttpException($validator->errors()->all()); 129 | } 130 | 131 | $response = Password::reset($credentials, function ($user, $password) { 132 | $user->password = $password; 133 | $user->save(); 134 | }); 135 | 136 | switch ($response) { 137 | case Password::PASSWORD_RESET: 138 | if(Config::get('boilerplate.reset_token_release')) { 139 | return $this->login($request); 140 | } 141 | return $this->response->noContent(); 142 | 143 | default: 144 | return $this->response->error('could_not_reset_password', 500); 145 | } 146 | } 147 | } 148 | -------------------------------------------------------------------------------- /config/oauth2.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | return [ 13 | 14 | /* 15 | |-------------------------------------------------------------------------- 16 | | Supported Grant Types 17 | |-------------------------------------------------------------------------- 18 | | 19 | | Your OAuth2 Server can issue an access token based on different grant 20 | | types you can even provide your own grant type. 21 | | 22 | | To choose which grant type suits your scenario, see 23 | | http://oauth2.thephpleague.com/authorization-server/which-grant 24 | | 25 | | Please see this link to find available grant types 26 | | http://git.io/vJLAv 27 | | 28 | */ 29 | 30 | 'grant_types' => [ 31 | 'client_credentials' => [ 32 | 'class' => '\League\OAuth2\Server\Grant\ClientCredentialsGrant', 33 | 'access_token_ttl' => 86400 34 | ], 35 | 36 | 'password' => [ 37 | 'class' => '\League\OAuth2\Server\Grant\PasswordGrant', 38 | 'callback' => '\App\Models\PasswordVerifier@verify', 39 | 'access_token_ttl' => 31536000 40 | ] 41 | ], 42 | 43 | /* 44 | |-------------------------------------------------------------------------- 45 | | Output Token Type 46 | |-------------------------------------------------------------------------- 47 | | 48 | | This will tell the authorization server the output format for the access 49 | | token and the resource server how to parse the access token used. 50 | | 51 | | Default value is League\OAuth2\Server\TokenType\Bearer 52 | | 53 | */ 54 | 55 | 'token_type' => 'League\OAuth2\Server\TokenType\Bearer', 56 | 57 | /* 58 | |-------------------------------------------------------------------------- 59 | | State Parameter 60 | |-------------------------------------------------------------------------- 61 | | 62 | | Whether or not the state parameter is required in the query string. 63 | | 64 | */ 65 | 66 | 'state_param' => false, 67 | 68 | /* 69 | |-------------------------------------------------------------------------- 70 | | Scope Parameter 71 | |-------------------------------------------------------------------------- 72 | | 73 | | Whether or not the scope parameter is required in the query string. 74 | | 75 | */ 76 | 77 | 'scope_param' => false, 78 | 79 | /* 80 | |-------------------------------------------------------------------------- 81 | | Scope Delimiter 82 | |-------------------------------------------------------------------------- 83 | | 84 | | Which character to use to split the scope parameter in the query string. 85 | | 86 | */ 87 | 88 | 'scope_delimiter' => ',', 89 | 90 | /* 91 | |-------------------------------------------------------------------------- 92 | | Default Scope 93 | |-------------------------------------------------------------------------- 94 | | 95 | | The default scope to use if not present in the query string. 96 | | 97 | */ 98 | 99 | 'default_scope' => null, 100 | 101 | /* 102 | |-------------------------------------------------------------------------- 103 | | Access Token TTL 104 | |-------------------------------------------------------------------------- 105 | | 106 | | For how long the issued access token is valid (in seconds) this can be 107 | | also set on a per grant-type basis. 108 | | 109 | */ 110 | 111 | 'access_token_ttl' => 3600, 112 | 113 | /* 114 | |-------------------------------------------------------------------------- 115 | | Limit clients to specific grants 116 | |-------------------------------------------------------------------------- 117 | | 118 | | Whether or not to limit clients to specific grant types. This is useful 119 | | to allow only trusted clients to access your API differently. 120 | | 121 | */ 122 | 123 | 'limit_clients_to_grants' => false, 124 | 125 | /* 126 | |-------------------------------------------------------------------------- 127 | | Limit clients to specific scopes 128 | |-------------------------------------------------------------------------- 129 | | 130 | | Whether or not to limit clients to specific scopes. This is useful to 131 | | only allow specific clients to use some scopes. 132 | | 133 | */ 134 | 135 | 'limit_clients_to_scopes' => false, 136 | 137 | /* 138 | |-------------------------------------------------------------------------- 139 | | Limit scopes to specific grants 140 | |-------------------------------------------------------------------------- 141 | | 142 | | Whether or not to limit scopes to specific grants. This is useful to 143 | | allow certain scopes to be used only with certain grant types. 144 | | 145 | */ 146 | 147 | 'limit_scopes_to_grants' => false, 148 | 149 | /* 150 | |-------------------------------------------------------------------------- 151 | | HTTP Header Only 152 | |-------------------------------------------------------------------------- 153 | | 154 | | This will tell the resource server where to check for the access_token. 155 | | By default it checks both the query string and the http headers. 156 | | 157 | */ 158 | 159 | 'http_headers_only' => false, 160 | 161 | ]; 162 | -------------------------------------------------------------------------------- /config/session.php: -------------------------------------------------------------------------------- 1 | env('SESSION_DRIVER', 'file'), 20 | 21 | /* 22 | |-------------------------------------------------------------------------- 23 | | Session Lifetime 24 | |-------------------------------------------------------------------------- 25 | | 26 | | Here you may specify the number of minutes that you wish the session 27 | | to be allowed to remain idle before it expires. If you want them 28 | | to immediately expire on the browser closing, set that option. 29 | | 30 | */ 31 | 32 | 'lifetime' => 120, 33 | 34 | 'expire_on_close' => false, 35 | 36 | /* 37 | |-------------------------------------------------------------------------- 38 | | Session Encryption 39 | |-------------------------------------------------------------------------- 40 | | 41 | | This option allows you to easily specify that all of your session data 42 | | should be encrypted before it is stored. All encryption will be run 43 | | automatically by Laravel and you can use the Session like normal. 44 | | 45 | */ 46 | 47 | 'encrypt' => false, 48 | 49 | /* 50 | |-------------------------------------------------------------------------- 51 | | Session File Location 52 | |-------------------------------------------------------------------------- 53 | | 54 | | When using the native session driver, we need a location where session 55 | | files may be stored. A default has been set for you but a different 56 | | location may be specified. This is only needed for file sessions. 57 | | 58 | */ 59 | 60 | 'files' => storage_path('framework/sessions'), 61 | 62 | /* 63 | |-------------------------------------------------------------------------- 64 | | Session Database Connection 65 | |-------------------------------------------------------------------------- 66 | | 67 | | When using the "database" or "redis" session drivers, you may specify a 68 | | connection that should be used to manage these sessions. This should 69 | | correspond to a connection in your database configuration options. 70 | | 71 | */ 72 | 73 | 'connection' => null, 74 | 75 | /* 76 | |-------------------------------------------------------------------------- 77 | | Session Database Table 78 | |-------------------------------------------------------------------------- 79 | | 80 | | When using the "database" session driver, you may specify the table we 81 | | should use to manage the sessions. Of course, a sensible default is 82 | | provided for you; however, you are free to change this as needed. 83 | | 84 | */ 85 | 86 | 'table' => 'sessions', 87 | 88 | /* 89 | |-------------------------------------------------------------------------- 90 | | Session Sweeping Lottery 91 | |-------------------------------------------------------------------------- 92 | | 93 | | Some session drivers must manually sweep their storage location to get 94 | | rid of old sessions from storage. Here are the chances that it will 95 | | happen on a given request. By default, the odds are 2 out of 100. 96 | | 97 | */ 98 | 99 | 'lottery' => [2, 100], 100 | 101 | /* 102 | |-------------------------------------------------------------------------- 103 | | Session Cookie Name 104 | |-------------------------------------------------------------------------- 105 | | 106 | | Here you may change the name of the cookie used to identify a session 107 | | instance by ID. The name specified here will get used every time a 108 | | new session cookie is created by the framework for every driver. 109 | | 110 | */ 111 | 112 | 'cookie' => 'laravel_session', 113 | 114 | /* 115 | |-------------------------------------------------------------------------- 116 | | Session Cookie Path 117 | |-------------------------------------------------------------------------- 118 | | 119 | | The session cookie path determines the path for which the cookie will 120 | | be regarded as available. Typically, this will be the root path of 121 | | your application but you are free to change this when necessary. 122 | | 123 | */ 124 | 125 | 'path' => '/', 126 | 127 | /* 128 | |-------------------------------------------------------------------------- 129 | | Session Cookie Domain 130 | |-------------------------------------------------------------------------- 131 | | 132 | | Here you may change the domain of the cookie used to identify a session 133 | | in your application. This will determine which domains the cookie is 134 | | available to in your application. A sensible default has been set. 135 | | 136 | */ 137 | 138 | 'domain' => null, 139 | 140 | /* 141 | |-------------------------------------------------------------------------- 142 | | HTTPS Only Cookies 143 | |-------------------------------------------------------------------------- 144 | | 145 | | By setting this option to true, session cookies will only be sent back 146 | | to the server if the browser has a HTTPS connection. This will keep 147 | | the cookie from being sent to you if it can not be done securely. 148 | | 149 | */ 150 | 151 | 'secure' => false, 152 | 153 | ]; 154 | -------------------------------------------------------------------------------- /resources/lang/en/validation.php: -------------------------------------------------------------------------------- 1 | 'The :attribute must be accepted.', 17 | 'active_url' => 'The :attribute is not a valid URL.', 18 | 'after' => 'The :attribute must be a date after :date.', 19 | 'alpha' => 'The :attribute may only contain letters.', 20 | 'alpha_dash' => 'The :attribute may only contain letters, numbers, and dashes.', 21 | 'alpha_num' => 'The :attribute may only contain letters and numbers.', 22 | 'array' => 'The :attribute must be an array.', 23 | 'before' => 'The :attribute must be a date before :date.', 24 | 'between' => [ 25 | 'numeric' => 'The :attribute must be between :min and :max.', 26 | 'file' => 'The :attribute must be between :min and :max kilobytes.', 27 | 'string' => 'The :attribute must be between :min and :max characters.', 28 | 'array' => 'The :attribute must have between :min and :max items.', 29 | ], 30 | 'boolean' => 'The :attribute field must be true or false.', 31 | 'confirmed' => 'The :attribute confirmation does not match.', 32 | 'date' => 'The :attribute is not a valid date.', 33 | 'date_format' => 'The :attribute does not match the format :format.', 34 | 'different' => 'The :attribute and :other must be different.', 35 | 'digits' => 'The :attribute must be :digits digits.', 36 | 'digits_between' => 'The :attribute must be between :min and :max digits.', 37 | 'email' => 'The :attribute must be a valid email address.', 38 | 'exists' => 'The selected :attribute is invalid.', 39 | 'filled' => 'The :attribute field is required.', 40 | 'image' => 'The :attribute must be an image.', 41 | 'in' => 'The selected :attribute is invalid.', 42 | 'integer' => 'The :attribute must be an integer.', 43 | 'ip' => 'The :attribute must be a valid IP address.', 44 | 'json' => 'The :attribute must be a valid JSON string.', 45 | 'max' => [ 46 | 'numeric' => 'The :attribute may not be greater than :max.', 47 | 'file' => 'The :attribute may not be greater than :max kilobytes.', 48 | 'string' => 'The :attribute may not be greater than :max characters.', 49 | 'array' => 'The :attribute may not have more than :max items.', 50 | ], 51 | 'mimes' => 'The :attribute must be a file of type: :values.', 52 | 'min' => [ 53 | 'numeric' => 'The :attribute must be at least :min.', 54 | 'file' => 'The :attribute must be at least :min kilobytes.', 55 | 'string' => 'The :attribute must be at least :min characters.', 56 | 'array' => 'The :attribute must have at least :min items.', 57 | ], 58 | 'not_in' => 'The selected :attribute is invalid.', 59 | 'numeric' => 'The :attribute must be a number.', 60 | 'regex' => 'The :attribute format is invalid.', 61 | 'required' => 'The :attribute field is required.', 62 | 'required_if' => 'The :attribute field is required when :other is :value.', 63 | 'required_with' => 'The :attribute field is required when :values is present.', 64 | 'required_with_all' => 'The :attribute field is required when :values is present.', 65 | 'required_without' => 'The :attribute field is required when :values is not present.', 66 | 'required_without_all' => 'The :attribute field is required when none of :values are present.', 67 | 'same' => 'The :attribute and :other must match.', 68 | 'size' => [ 69 | 'numeric' => 'The :attribute must be :size.', 70 | 'file' => 'The :attribute must be :size kilobytes.', 71 | 'string' => 'The :attribute must be :size characters.', 72 | 'array' => 'The :attribute must contain :size items.', 73 | ], 74 | 'string' => 'The :attribute must be a string.', 75 | 'timezone' => 'The :attribute must be a valid zone.', 76 | 'unique' => 'The :attribute has already been taken.', 77 | 'url' => 'The :attribute format is invalid.', 78 | 79 | /* 80 | |-------------------------------------------------------------------------- 81 | | Custom Validation Language Lines 82 | |-------------------------------------------------------------------------- 83 | | 84 | | Here you may specify custom validation messages for attributes using the 85 | | convention "attribute.rule" to name the lines. This makes it quick to 86 | | specify a specific custom language line for a given attribute rule. 87 | | 88 | */ 89 | 90 | 'custom' => [ 91 | 'attribute-name' => [ 92 | 'rule-name' => 'custom-message', 93 | ], 94 | ], 95 | 96 | /* 97 | |-------------------------------------------------------------------------- 98 | | Custom Validation Attributes 99 | |-------------------------------------------------------------------------- 100 | | 101 | | The following language lines are used to swap attribute place-holders 102 | | with something more reader friendly such as E-Mail Address instead 103 | | of "email". This simply helps us make messages a little cleaner. 104 | | 105 | */ 106 | 107 | 'attributes' => [], 108 | 109 | ]; 110 | -------------------------------------------------------------------------------- /config/api.php: -------------------------------------------------------------------------------- 1 | env('API_STANDARDS_TREE', 'x'), 22 | 23 | /* 24 | |-------------------------------------------------------------------------- 25 | | API Subtype 26 | |-------------------------------------------------------------------------- 27 | | 28 | | Your subtype will follow the standards tree you use when used in the 29 | | "Accept" header to negotiate the content type and version. 30 | | 31 | | For example: Accept: application/x.SUBTYPE.v1+json 32 | | 33 | */ 34 | 35 | 'subtype' => env('API_SUBTYPE', ''), 36 | 37 | /* 38 | |-------------------------------------------------------------------------- 39 | | Default API Version 40 | |-------------------------------------------------------------------------- 41 | | 42 | | This is the default version when strict mode is disabled and your API 43 | | is accessed via a web browser. It's also used as the default version 44 | | when generating your APIs documentation. 45 | | 46 | */ 47 | 48 | 'version' => env('API_VERSION', 'v1'), 49 | 50 | /* 51 | |-------------------------------------------------------------------------- 52 | | Default API Prefix 53 | |-------------------------------------------------------------------------- 54 | | 55 | | A default prefix to use for your API routes so you don't have to 56 | | specify it for each group. 57 | | 58 | */ 59 | 60 | 'prefix' => env('API_PREFIX', null), 61 | 62 | /* 63 | |-------------------------------------------------------------------------- 64 | | Default API Domain 65 | |-------------------------------------------------------------------------- 66 | | 67 | | A default domain to use for your API routes so you don't have to 68 | | specify it for each group. 69 | | 70 | */ 71 | 72 | 'domain' => env('API_DOMAIN', null), 73 | 74 | /* 75 | |-------------------------------------------------------------------------- 76 | | Name 77 | |-------------------------------------------------------------------------- 78 | | 79 | | When documenting your API using the API Blueprint syntax you can 80 | | configure a default name to avoid having to manually specify 81 | | one when using the command. 82 | | 83 | */ 84 | 85 | 'name' => env('API_NAME', null), 86 | 87 | /* 88 | |-------------------------------------------------------------------------- 89 | | Conditional Requests 90 | |-------------------------------------------------------------------------- 91 | | 92 | | Globally enable conditional requests so that an ETag header is added to 93 | | any successful response. Subsequent requests will perform a check and 94 | | will return a 304 Not Modified. This can also be enabled or disabled 95 | | on certain groups or routes. 96 | | 97 | */ 98 | 99 | 'conditionalRequest' => env('API_CONDITIONAL_REQUEST', true), 100 | 101 | /* 102 | |-------------------------------------------------------------------------- 103 | | Strict Mode 104 | |-------------------------------------------------------------------------- 105 | | 106 | | Enabling strict mode will require clients to send a valid Accept header 107 | | with every request. This also voids the default API version, meaning 108 | | your API will not be browsable via a web browser. 109 | | 110 | */ 111 | 112 | 'strict' => env('API_STRICT', false), 113 | 114 | /* 115 | |-------------------------------------------------------------------------- 116 | | Debug Mode 117 | |-------------------------------------------------------------------------- 118 | | 119 | | Enabling debug mode will result in error responses caused by thrown 120 | | exceptions to have a "debug" key that will be populated with 121 | | more detailed information on the exception. 122 | | 123 | */ 124 | 125 | 'debug' => env('API_DEBUG', false), 126 | 127 | /* 128 | |-------------------------------------------------------------------------- 129 | | Generic Error Format 130 | |-------------------------------------------------------------------------- 131 | | 132 | | When some HTTP exceptions are not caught and dealt with the API will 133 | | generate a generic error response in the format provided. Any 134 | | keys that aren't replaced with corresponding values will be 135 | | removed from the final response. 136 | | 137 | */ 138 | 'errorFormat' => [ 139 | 'error' => [ 140 | 'message' => ':message', 141 | 'errors' => ':errors', 142 | 'code' => ':code', 143 | 'status_code' => ':status_code', 144 | 'debug' => ':debug' 145 | ] 146 | ], 147 | 148 | /* 149 | |-------------------------------------------------------------------------- 150 | | Authentication Providers 151 | |-------------------------------------------------------------------------- 152 | | 153 | | The authentication providers that should be used when attempting to 154 | | authenticate an incoming API request. 155 | | 156 | */ 157 | 158 | 'auth' => [ 159 | //'jwt' => 'Dingo\Api\Auth\Provider\JWT' 160 | ], 161 | 162 | /* 163 | |-------------------------------------------------------------------------- 164 | | Throttling / Rate Limiting 165 | |-------------------------------------------------------------------------- 166 | | 167 | | Consumers of your API can be limited to the amount of requests they can 168 | | make. You can create your own throttles or simply change the default 169 | | throttles. 170 | | 171 | */ 172 | 173 | 'throttling' => [ 174 | 175 | ], 176 | 177 | /* 178 | |-------------------------------------------------------------------------- 179 | | Response Transformer 180 | |-------------------------------------------------------------------------- 181 | | 182 | | Responses can be transformed so that they are easier to format. By 183 | | default a Fractal transformer will be used to transform any 184 | | responses prior to formatting. You can easily replace 185 | | this with your own transformer. 186 | | 187 | */ 188 | 189 | 'transformer' => env('API_TRANSFORMER', 'Dingo\Api\Transformer\Adapter\Fractal'), 190 | 191 | /* 192 | |-------------------------------------------------------------------------- 193 | | Response Formats 194 | |-------------------------------------------------------------------------- 195 | | 196 | | Responses can be returned in multiple formats by registering different 197 | | response formatters. You can also customize an existing response 198 | | formatter. 199 | | 200 | */ 201 | 202 | 'defaultFormat' => env('API_DEFAULT_FORMAT', 'json'), 203 | 204 | 'formats' => [ 205 | 206 | 'json' => 'Dingo\Api\Http\Response\Format\Json', 207 | 208 | ], 209 | 210 | ]; 211 | -------------------------------------------------------------------------------- /app/Models/User.php: -------------------------------------------------------------------------------- 1 | hasOne('App\Models\Profile'); 61 | } 62 | 63 | public function albums() 64 | { 65 | return $this->hasMany('App\Models\Album'); 66 | } 67 | 68 | public function contents() 69 | { 70 | return $this->hasManyThrough('App\Models\Content', 'App\Models\Album'); 71 | } 72 | 73 | public function recent_contents() 74 | { 75 | return $this->contents() 76 | ->select(['contents.id', 'album_id', 'content_path', 'content_thumb_path', 'content_type']) 77 | ->orderBy('contents.created_at', 'desc') 78 | ->limit(6); 79 | } 80 | 81 | public function allBuddies() 82 | { 83 | return $this->belongsToMany('App\Models\User', 'user_buddies', 'user_id', 'buddy_id'); 84 | } 85 | 86 | public function buddies($request_status=RequestStatus::ACCEPTED) 87 | { 88 | if (RequestStatus::ALL == $request_status) { 89 | $buddies = $this->allBuddies(); 90 | } else { 91 | $buddies = $this->allBuddies()->where('user_buddies.request_status', $request_status); 92 | } 93 | return $buddies; 94 | } 95 | 96 | public function mutual_buddies($buddyId) 97 | { 98 | $mutual_buddies = $this->buddies() 99 | ->join('user_buddies as bb', function($join) use ($buddyId) { 100 | $join->on('bb.buddy_id', '=', 'ub.buddy_id') 101 | ->where('bb.user_id', '=', $buddyId) 102 | ->where('bb.request_status', '=', RequestStatus::ACCEPTED); 103 | }) 104 | ->where('ub.user_id', '=', $this->id) 105 | ->where('ub.request_status', '=', RequestStatus::ACCEPTED); 106 | return $mutual_buddies; 107 | } 108 | 109 | public function requests() 110 | { 111 | return $requests = $this->buddies(RequestStatus::PENDING)->where('is_requester', 0); 112 | } 113 | 114 | public static function isUserExists($email) 115 | { 116 | return DB::table('users')->where('email', $email)->value('id') ? 0:1; 117 | } 118 | 119 | /************************ Scopes *************************/ 120 | 121 | /** 122 | * Scope a query to only include popular users. 123 | * 124 | * @return \Illuminate\Database\Eloquent\Builder 125 | */ 126 | public function scopeRecommended($query, $filterByIds=[]) 127 | { 128 | $optionFilter = $filterByIds; 129 | // Get All skill level metadata ids Query 130 | $skillMetaDataQuery = DB::table('metadata_field_options') 131 | ->select('id') 132 | ->where('metadata_field_id', '=', function ($query) { 133 | $query->select('id') 134 | ->from('metadata_fields') 135 | ->where('field', '=', 'skill_level'); 136 | }); 137 | $skillMetaDataIds = $skillMetaDataQuery->lists('id'); 138 | $mySkillWeight = DB::table('metadata_field_options')->where('value', $this->profile->skill_level)->value('weight'); 139 | 140 | if (empty($filterByIds)) { 141 | $optionFilter = $skillMetaDataIds; 142 | // $optionFilter = $filterCallback = function ($query) use ($skillMetaDataQuery) { 143 | // $query->select('metadata_field_option_id') 144 | // ->from('profile_extended') 145 | // ->where('profile_id', $this->profile->id) 146 | // ->union($skillMetaDataQuery); 147 | // }; 148 | } else { 149 | $optionFilter = array_merge($filterByIds, $skillMetaDataIds); 150 | } 151 | 152 | $rankQuery = 'SUM(CASE 153 | WHEN pe.metadata_field_option_id in ('.implode($skillMetaDataIds, ',').') 154 | THEN -(ABS('.$mySkillWeight.'-CAST(pe.metadata_field_option_weight as SIGNED)))+4 155 | ELSE pe.metadata_field_option_weight 156 | END) as rank'; 157 | 158 | $query = $query->where('users.id', '<>', $this->id) 159 | ->with('profile') 160 | ->select('users.id', 'users.email', 'users.auth_type', DB::raw($rankQuery)) 161 | ->whereIn('pe.metadata_field_option_id', $optionFilter) 162 | ->whereNotIn('users.id', function($query) { 163 | $query->select('buddy_id') 164 | ->from('user_buddies') 165 | ->where('user_id', $this->id); 166 | }) 167 | ->join('profiles as p', 'users.id', '=', 'p.user_id') 168 | ->join('profile_extended as pe', 'pe.profile_id', '=', 'p.id') 169 | ->groupBy('pe.profile_id') 170 | ->orderBy('rank', 'desc') 171 | ->orderBy('p.first_name', 'asc') 172 | ->orderBy('p.last_name', 'asc'); 173 | 174 | // Add Interested in factor for recommended users 175 | switch ($this->profile->settings_interested_in) { 176 | case MeetingInterest::MEN: 177 | $query = $query->where('p.gender', Gender::MALE); 178 | break; 179 | 180 | case MeetingInterest::WOMEN: 181 | $query = $query->where('p.gender', Gender::FEMALE); 182 | break; 183 | 184 | default: 185 | break; 186 | } 187 | // Add age range factors for recommended users 188 | if (!empty($this->profile->settings_min_age)) { 189 | $query = $query->where('p.date_of_birth', '<=', \Carbon\Carbon::now()->subYears($this->profile->settings_min_age)->toDateString()); 190 | } 191 | if (!empty($this->profile->settings_max_age)) { 192 | $query = $query->where('p.date_of_birth', '>', \Carbon\Carbon::now()->subYears($this->profile->settings_max_age+1)->addDay(1)->toDateString()); 193 | } 194 | 195 | return $query; 196 | } 197 | 198 | public function scopeByLocation($query, $lat, $lng, $radius) 199 | { 200 | $longitude = (float) $lng; 201 | $latitude = (float) $lat; 202 | $radius = (int) $radius; // in miles 203 | 204 | $lng_min = $longitude - $radius / abs(cos(deg2rad($latitude)) * 69); 205 | $lng_max = $longitude + $radius / abs(cos(deg2rad($latitude)) * 69); 206 | $lat_min = $latitude - ($radius / 69); 207 | $lat_max = $latitude + ($radius / 69); 208 | 209 | $query = $query->whereBetween('cur_lat', [$lat_min, $lat_max]) 210 | ->whereBetween('cur_lng', [$lng_min, $lng_max]); 211 | 212 | return $query; 213 | } 214 | 215 | // Accessors & Mutators 216 | public function getSocialNetworkAttribute($value) 217 | { 218 | return ($this->auth_type == 'email' ? 'shredd' : $this->auth_type); 219 | } 220 | } 221 | -------------------------------------------------------------------------------- /config/app.php: -------------------------------------------------------------------------------- 1 | env('APP_DEBUG', false), 17 | 18 | /* 19 | |-------------------------------------------------------------------------- 20 | | Application URL 21 | |-------------------------------------------------------------------------- 22 | | 23 | | This URL is used by the console to properly generate URLs when using 24 | | the Artisan command line tool. You should set this to the root of 25 | | your application so that it is used when running Artisan tasks. 26 | | 27 | */ 28 | 29 | 'url' => 'http://localhost', 30 | 31 | /* 32 | |-------------------------------------------------------------------------- 33 | | Application Timezone 34 | |-------------------------------------------------------------------------- 35 | | 36 | | Here you may specify the default timezone for your application, which 37 | | will be used by the PHP date and date-time functions. We have gone 38 | | ahead and set this to a sensible default for you out of the box. 39 | | 40 | */ 41 | 42 | 'timezone' => 'UTC', 43 | 44 | /* 45 | |-------------------------------------------------------------------------- 46 | | Application Locale Configuration 47 | |-------------------------------------------------------------------------- 48 | | 49 | | The application locale determines the default locale that will be used 50 | | by the translation service provider. You are free to set this value 51 | | to any of the locales which will be supported by the application. 52 | | 53 | */ 54 | 55 | 'locale' => 'en', 56 | 57 | /* 58 | |-------------------------------------------------------------------------- 59 | | Application Fallback Locale 60 | |-------------------------------------------------------------------------- 61 | | 62 | | The fallback locale determines the locale to use when the current one 63 | | is not available. You may change the value to correspond to any of 64 | | the language folders that are provided through your application. 65 | | 66 | */ 67 | 68 | 'fallback_locale' => 'en', 69 | 70 | /* 71 | |-------------------------------------------------------------------------- 72 | | Encryption Key 73 | |-------------------------------------------------------------------------- 74 | | 75 | | This key is used by the Illuminate encrypter service and should be set 76 | | to a random, 32 character string, otherwise these encrypted strings 77 | | will not be safe. Please do this before deploying an application! 78 | | 79 | */ 80 | 81 | 'key' => env('APP_KEY', 'SomeRandomString'), 82 | 83 | 'cipher' => 'AES-256-CBC', 84 | 85 | /* 86 | |-------------------------------------------------------------------------- 87 | | Logging Configuration 88 | |-------------------------------------------------------------------------- 89 | | 90 | | Here you may configure the log settings for your application. Out of 91 | | the box, Laravel uses the Monolog PHP logging library. This gives 92 | | you a variety of powerful log handlers / formatters to utilize. 93 | | 94 | | Available Settings: "single", "daily", "syslog", "errorlog" 95 | | 96 | */ 97 | 98 | 'log' => 'single', 99 | 100 | /* 101 | |-------------------------------------------------------------------------- 102 | | Autoloaded Service Providers 103 | |-------------------------------------------------------------------------- 104 | | 105 | | The service providers listed here will be automatically loaded on the 106 | | request to your application. Feel free to add your own services to 107 | | this array to grant expanded functionality to your applications. 108 | | 109 | */ 110 | 111 | 'providers' => [ 112 | 113 | /* 114 | * Laravel Framework Service Providers... 115 | */ 116 | Illuminate\Foundation\Providers\ArtisanServiceProvider::class, 117 | Illuminate\Auth\AuthServiceProvider::class, 118 | Illuminate\Broadcasting\BroadcastServiceProvider::class, 119 | Illuminate\Bus\BusServiceProvider::class, 120 | Illuminate\Cache\CacheServiceProvider::class, 121 | Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class, 122 | Illuminate\Routing\ControllerServiceProvider::class, 123 | Illuminate\Cookie\CookieServiceProvider::class, 124 | Illuminate\Database\DatabaseServiceProvider::class, 125 | Illuminate\Encryption\EncryptionServiceProvider::class, 126 | Illuminate\Filesystem\FilesystemServiceProvider::class, 127 | Illuminate\Foundation\Providers\FoundationServiceProvider::class, 128 | Illuminate\Hashing\HashServiceProvider::class, 129 | Illuminate\Mail\MailServiceProvider::class, 130 | Illuminate\Pagination\PaginationServiceProvider::class, 131 | Illuminate\Pipeline\PipelineServiceProvider::class, 132 | Illuminate\Queue\QueueServiceProvider::class, 133 | Illuminate\Redis\RedisServiceProvider::class, 134 | Illuminate\Auth\Passwords\PasswordResetServiceProvider::class, 135 | Illuminate\Session\SessionServiceProvider::class, 136 | Illuminate\Translation\TranslationServiceProvider::class, 137 | Illuminate\Validation\ValidationServiceProvider::class, 138 | Illuminate\View\ViewServiceProvider::class, 139 | 140 | /* 141 | * Third Party Providers... 142 | */ 143 | Dingo\Api\Provider\LaravelServiceProvider::class, 144 | // Authentication providers 145 | LucaDegasperi\OAuth2Server\Storage\FluentStorageServiceProvider::class, 146 | LucaDegasperi\OAuth2Server\OAuth2ServerServiceProvider::class, 147 | App\Providers\OAuthServiceProvider::class, 148 | 149 | /* 150 | * Application Service Providers... 151 | */ 152 | App\Providers\AppServiceProvider::class, 153 | App\Providers\AuthServiceProvider::class, 154 | App\Providers\EventServiceProvider::class, 155 | App\Providers\RouteServiceProvider::class, 156 | App\Repositories\RepositoryServiceProvider::class, 157 | ], 158 | 159 | /* 160 | |-------------------------------------------------------------------------- 161 | | Class Aliases 162 | |-------------------------------------------------------------------------- 163 | | 164 | | This array of class aliases will be registered when this application 165 | | is started. However, feel free to register as many as you wish as 166 | | the aliases are "lazy" loaded so they don't hinder performance. 167 | | 168 | */ 169 | 170 | 'aliases' => [ 171 | 172 | 'App' => Illuminate\Support\Facades\App::class, 173 | 'Artisan' => Illuminate\Support\Facades\Artisan::class, 174 | 'Auth' => Illuminate\Support\Facades\Auth::class, 175 | 'Blade' => Illuminate\Support\Facades\Blade::class, 176 | 'Bus' => Illuminate\Support\Facades\Bus::class, 177 | 'Cache' => Illuminate\Support\Facades\Cache::class, 178 | 'Config' => Illuminate\Support\Facades\Config::class, 179 | 'Cookie' => Illuminate\Support\Facades\Cookie::class, 180 | 'Crypt' => Illuminate\Support\Facades\Crypt::class, 181 | 'DB' => Illuminate\Support\Facades\DB::class, 182 | 'Eloquent' => Illuminate\Database\Eloquent\Model::class, 183 | 'Event' => Illuminate\Support\Facades\Event::class, 184 | 'File' => Illuminate\Support\Facades\File::class, 185 | 'Gate' => Illuminate\Support\Facades\Gate::class, 186 | 'Hash' => Illuminate\Support\Facades\Hash::class, 187 | 'Input' => Illuminate\Support\Facades\Input::class, 188 | 'Inspiring' => Illuminate\Foundation\Inspiring::class, 189 | 'Lang' => Illuminate\Support\Facades\Lang::class, 190 | 'Log' => Illuminate\Support\Facades\Log::class, 191 | 'Mail' => Illuminate\Support\Facades\Mail::class, 192 | 'Password' => Illuminate\Support\Facades\Password::class, 193 | 'Queue' => Illuminate\Support\Facades\Queue::class, 194 | 'Redirect' => Illuminate\Support\Facades\Redirect::class, 195 | 'Redis' => Illuminate\Support\Facades\Redis::class, 196 | 'Request' => Illuminate\Support\Facades\Request::class, 197 | 'Response' => Illuminate\Support\Facades\Response::class, 198 | 'Route' => Illuminate\Support\Facades\Route::class, 199 | 'Schema' => Illuminate\Support\Facades\Schema::class, 200 | 'Session' => Illuminate\Support\Facades\Session::class, 201 | 'Storage' => Illuminate\Support\Facades\Storage::class, 202 | 'URL' => Illuminate\Support\Facades\URL::class, 203 | 'Validator' => Illuminate\Support\Facades\Validator::class, 204 | 'View' => Illuminate\Support\Facades\View::class, 205 | 206 | /* 207 | * Third Party Facades... 208 | */ 209 | 'API' => Dingo\Api\Facade\API::class, 210 | 'Authorizer'=> LucaDegasperi\OAuth2Server\Facades\Authorizer::class, 211 | ], 212 | 213 | ]; 214 | --------------------------------------------------------------------------------