The Rublon PHP SDK library is a client-side implementation of the Rublon API written in PHP. The library includes methods for embedding the Rublon API’s GUI in an HTML-based environment. The Rublon PHP SDK forms a convenient PHP coding language facade for Rublon API’s REST interface.
Rublon adds an extra layer of security by prompting the user to authenticate using an extra authentication method such as Mobile Push. Even if a malicious actor compromises the user’s password, the hacker be able to log in to the user’s account because the second secure factor will thwart them.
Rublon can add an extra layer of security in the following two use cases:
When a user signs in to a system, the second authentication factor should be initiated only after:
Before you start implementing the Rublon PHP SDK library into your code, you must create an application in the Rublon Admin Console. We also recommend that you install the Rublon Authenticator mobile app for Mobile Push, Mobile Passcode, and QR Code authentication methods.
For increased security of Multi-Factor Authentication (MFA), end-users are recommended to install the Rublon Authenticator mobile app.
Download the Rublon Authenticator for:
After installing the mobile app, users can authenticate using the following authentication methods:
In some cases, users may not want to install any additional apps on their phones. Also, some users own older phones that do not support modern mobile applications. These users can authenticate using one of the following authentication methods instead:
Follow the steps below to configure Rublon PHP SDK.
Let’s assume there is a superglobal session associative array
$_SESSION
. It has access to an object that stores user data
of the currently logged-in user.
The $_SESSION
array will be used in PHP code examples
later in this document.
The Rublon
class implements a few public methods, which,
when needed, can be overridden using class inheritance.
We strongly discourage you from modifying any part of the library, as
it usually leads to difficulties during library updates. If you need to
change the flow or internal structure of the Rublon
or
RublonCallback
classes, do not hesitate to subclass them
according to your needs.
To initialize the Rublon PHP SDK library, you need to instantiate a
Rublon
class object. Its constructor takes three
arguments.
Name | Type | Description |
---|---|---|
$systemToken
|
string | The System Token value you copied from the Rublon Admin Console. |
$secretKey
|
string | The Secret Key value you copied from the Rublon Admin Console. |
$apiServer
|
string |
Rublon API Server URI Default: https://core.rublon.net |
require_once "libs/Rublon/Rublon.php";
$rublon = new Rublon(
"D166A6E9996A40F0A88252432FA5E490",
"913eda929c96cf52141b39f5717e25",
"https://core.rublon.net"
);
The Rublon::checkApplication()
method verifies the validity of the configuration. Your application should call this method every time you change or save the configuration. A configuration change can be, for example, changing the systemToken or secretKey.
Name | Type | Description |
---|---|---|
appVer | string | The version of the current application. |
params | array | Optional. Additional application parameters. |
Rublon::checkApplication()
may throw one of the following exceptions:
The Rublon::auth()
method uses the username to check the
user’s protection status and returns a URL address the user should be
redirected to in their web browser.
Name | Type | Description |
---|---|---|
$callbackUrl
|
string |
The integrated system’s callback URL. Rublon will redirect the user to this URL after successful authentication. |
$username
|
string | The user’s username, which allows the user to sign in |
$userEmail
|
string | The user’s email address, which allows to check the user’s protection status and match the user to a Rublon account |
$params
|
array | Additional transaction parameters (optional) |
$isPasswordless
|
boolean | Whether the sign-in attempt is passwordless (optional and false by default) |
/**
* An example method used to log the user in (integrated system's method)
*
* @param string $login
* @param string $password
*/
function login($login, $password) {
if (loginPreListener()) {
if ($user = authenticate($login, $password)) {
// The user has been authenticated.
$_SESSION["user"] = $user;
loginPostListener();
}
}
}
/**
* Listener (hook) invoked after a successful first factor user authentication,
* implemented for Rublon integration purposes.
*/
function loginPostListener() {
// Make sure that the user is not logged-in
unset($_SESSION['user']);
$rublon = new Rublon(
"D166A6E9996A40F0A88252432FA5E490",
"913eda929c96cf52141b39f5717e25",
"https://core.rublon.net"
);
try { // Initiate a Rublon authentication transaction
$authUrl = $rublon->auth(
$callbackUrl = "http://example.com?rublon=callback",
$_SESSION["user"]["login"], // Username
$_SESSION["user"]["email"] // User email
);
if (!empty($authUrl)) { // User protection is active
// Redirect the user's web browser to Rublon servers to verify the protection:
header('Location: ' . $authUrl);
} else {
// User is not protected by Rublon, so bypass the second factor.
header('Location: index.php');
}
} catch (UserDeniedException $e) {
// Access Denied
header('Location: ./');
} catch (UserBypassedException $e) {
// User bypassed
header('Location: ./');
} catch (RublonException $e) {
// An error occurred
die($e->getMessage());
}
}
Note: Make sure that your code checks that the user is not signed in. The user should be signed in only after successful Rublon authentication.
After successful authentication, Rublon redirects the user to the callback URL. The callback flow continues and finalizes the authentication process.
The callback URL will receive input arguments in the URL address itself (query string).
Name | Type | Description |
---|---|---|
rublonState
|
string |
Authentication result: ok .
|
rublonToken
|
string | Access token (60 alphanumeric characters, upper- and lowercase), which allows to verify the authentication using a background Rublon API connection |
Note: If the callback URL has been set to, e.g.,
http://example.com/auth
, the params will be appended to the
URL address:
http://example.com/auth?rublonState=ok&rublonToken=Kmad4hAS…
Note: If you want to construct the callback URL
differently (e.g., by using mod_rewrite), you can set the callback URL’s
template using the meta-tags: %rublonToken%
and
%rublonState%
, like so:
http://example.com/auth/%rublonState%/%rublonToken%
After the callback is invoked, you need to instantiate a
RublonCallback
class object for proper finalization of the
authentication process.
Name | Type | Description |
---|---|---|
$rublon
|
Rublon |
An instance of the Rublon class
|
Next, call the RublonCallback::call()
method. It takes
two arguments:
Name | Type | Description |
---|---|---|
$successHandler
|
callable | The name of the function/method, or an anonymous function/closure, to be invoked on successful authentication |
$cancelHandler
|
callable | The name of the function/method, or an anonymous function/closure, to be invoked when the callback is canceled |
Name | Type | Description |
---|---|---|
$username
|
string |
The user’s unique username in the integrated system, that was passed as
an argument to the Rublon::auth() method
|
$callback
|
RublonCallback |
An instance of the RublonCallback class
|
Name | Type | Description |
---|---|---|
$callback
|
RublonCallback |
An instance of the RublonCallback class
|
An example portraying how to use the RublonCallback
class in the callback:
$rublon = new Rublon(
"D166A6E9996A40F0A88252432FA5E490",
"913eda929c96cf52141b39f5717e25",
"https://code.rublon.net"
);
try {
$callback = new RublonCallback($rublon);
$callback->call(
$successHandler = function($username, RublonCallback $callback) {
// The user is finally logged in
$_SESSION["user"] = $username;
},
$cancelHandler = function(RublonCallback $callback) {
// Cancel the authentication process
header("Location: ./login");
exit;
}
);
// The authentication process was successful, redirect to the main page:
header("Location: ./");
exit;
} catch (RublonException $e) {
// Please handle this error in the better way
die($e->getMessage());
}
This Laravel configuration example uses the Breeze starting kit.
After you create the application and install Breeze, you need to add Rublon PHP SDK:
composer require Rublon/rublon-sdk-php
Add those to .env:
RUBLON_TOKEN="your rublon token"
RUBLON_KEY="your rublon key"
RUBLON_URL="https://core.rublon.net"
Create new route for Rublon callback in routes/auth.php:
Route::get('rublon-callback', [AuthenticatedSessionController::class, 'rublonCallback'])->name('rublon-callback');
Modify the store method in the controller:
Http/Controllers/Auth/AuthenticatedSessionController.php
public function store(LoginRequest $request)
{
$request->authenticate();
$rublon = new Rublon(
env('RUBLON_TOKEN'),
env('RUBLON_KEY'),
env('RUBLON_URL'),
);
try { // Initiate a Rublon authentication transaction
$url = $rublon->auth(
$callbackUrl = url('/rublon-callback'),
Auth::user()->email, // User email used as username
Auth::user()->email // User email
);
if (!empty($url)) {
Auth::logout();
return redirect()->away($url);
} else {
// User is not protected by Rublon, so bypass the second factor.
$request->session()->regenerate();
return redirect()->to('dashboard');
}
} catch (UserBypassedException $e) {
return redirect()->to('login');
} catch (RublonException $e) {
// An error occurred
die($e->getMessage());
}
return redirect()->intended(RouteServiceProvider::HOME);
}
Add a new method for Rublon callback:
public function rublonCallback(Request $request)
{
$rublon = new Rublon(
env('RUBLON_TOKEN'),
env('RUBLON_KEY'),
env('RUBLON_URL'),
);
try {
$callback = new RublonCallback($rublon);
$request->session()->regenerate();
$callback->call(
$successHandler = function($username, RublonCallback $callback) {
$user = User::where('email', $username)->firstOrFail();
Auth::login($user);
if (Auth::check()) {
return redirect()->to('dashboard');
} else {
return redirect()->to('login');
}
},
$cancelHandler = function(RublonCallback $callback) {
return redirect()->to('login');
}
);
return redirect()->to('dashboard');
} catch (Rublon Exception $e) {
die($e->getMessage());
}
return redirect()->to('dashboard');
}
If you encounter any issues with your Rublon integration, please contact Rublon Support.