A Laravel package for use with the 🔐 sdlogin OpenID connect server.
Warning
Please make sure your app is using https, to prevent unwanted exposure of token, secrets, etc.
To use sdclient
in your project:
-
In your laravel project run:
composer require curio/sdclient
-
Set these keys in your .env file:
SD_CLIENT_ID
SD_CLIENT_SECRET
SD_API_LOG
(optional)- Default:
no
- Set to
yes
to make SdClient log all usage of access_tokens and refresh_tokens to the default log-channel.
- Default:
SD_APP_FOR
(optional)- Default:
teachers
- This key determines if students can login to your application.
- May be one of:
- Default:
all
: everyone can login, you may restrict access using guards or middleware.teachers
: a student will be completely blocked and no user will be created when they try to login.SD_USE_MIGRATION
(optional)- Default:
yes
- Set to no if you want to use your own migration instead of the users migration this package provides
- Default:
SD_SSL_VERIFYPEER
(optional)- Default:
yes
- Set to
no
if you want to disable SSL verification. This is only recommended for during development and only on trusted networks.
- Default:
-
Alter your User model and add the line:
public $incrementing = false;
-
(Recommended) Remove any default users-migration from your app, because SdClient will conflict with it. Do not remove the user-model. If you want to keep using your own migration, in your .env file set:
SD_USE_MIGRATION=no
.Note that (unlike default user migrations) SD users have a string as their primary key. Any foreign keys pointing to the users table should also be of type string.
-
Lastly, run
php artisan migrate
.
Read the required implementations below to see how to redirect your users to the login-server and how to catch the after-login redirect.
Note
SdClient is not compatible in combination with Laravel's make:auth
command.
Redirect your users to http://yoursite/sdclient/redirect
. From here sdclient
will send your user to sdlogin for authentication.
Example:
Implement a named route that will serve your users with a button or direct redirect to
/sdclient/redirect.
:Route::get('/login', function() { return redirect('/sdclient/redirect'); })->name('login');
The sdlogin server will ask the user if they want to allow your application to access their data. After the user has made their choice, they will be redirected to the /sdclient/ready
or /sdclient/error
route in your application.
After confirming a successful login with sdlogin, the sdclient
package will redirect you to /sdclient/ready
.
Example:
Define a route in your applications
routes/web.php
file to handle this:Route::get('/sdclient/ready', function() { return redirect('/educations'); })
If the user denies access to your application, or if something else goes wrong, the user will be redirected to /sdclient/error
. The error and error_description will be stored in the session (as sdclient.error
and sdclient.error_description
respectively).
Example:
Define a route in your applications
routes/web.php
file to handle this:Route::get('/sdclient/error', function() { $error = session('sdclient.error'); $error_description = session('sdclient.error_description'); return view('errors.sdclient', compact('error', 'error_description')); // or simply: // return 'There was an error signing in: ' . $error_description . ' (' . $error . ')<br><a href="/login">Try again</a>'; })
Send your user to /sdclient/logout
.
Note
A real logout cannot be accomplished at this time. If you log-out of your app, but are still logged-in to the sdlogin-server, this will have no effect. This is because the sdlogin-server is a single-sign-on server, and is designed to keep you logged in to all applications that use it.
Apart from being the central login-server, login.amo.rocks also exposes an api. Please note this api is currently undocumented, although there are options to explore the api:
- Refer to sdlogin's routes/api.php file.
- Play around at apitest.curio.codes.
An example of calling the api through SdClient:
namespace App\Http\Controllers;
use \Curio\SdClient\Facades\SdApi;
class MyController extends Controller
{
// This method should be protected by the auth-middleware
public function index()
{
$users = SdApi::get('users');
return view('users.index')->with(compact('users'));
}
}
Known 'bug': Currently the SdApi class doesn't check if the token expired but just refreshes it anytime you use it.
- Performs an HTTP-request like
GET https://api.curio.codes/$endpoint
. - This method relies on a user being authenticated through the
sdclient
first. Only call this method from routes and/or controllers protected by the auth middleware. - Returns a Laravel-collection
We welcome contributions from the community. Please see the contributing guide for more information.