stormpath/sdk
Stormpath PHP SDK provides PHP access to Stormpath’s user management API: authentication and authorization, user profiles, hosted login/SSO, social and SAML logins, and API key authentication. Install via Composer (stormpath/sdk).
Installation
composer require stormpath/sdk
Add to config/services.php:
'stormpath' => [
'client_id' => env('STORMPATH_CLIENT_ID'),
'client_secret' => env('STORMPATH_CLIENT_SECRET'),
'tenant' => env('STORMPATH_TENANT'),
],
First Use Case: Authentication Initialize the client in a service provider or controller:
use Stormpath\Stormpath;
$client = Stormpath::createClient(
config('services.stormpath.client_id'),
config('services.stormpath.client_secret')
);
$tenant = $client->getTenant(config('services.stormpath.tenant'));
Key Entry Points
Stormpath::createClient() – Initialize the SDK.$tenant->getAccountStore() – Access user accounts.$tenant->getGroupStore() – Manage groups.$tenant->getApplication() – Retrieve application settings.// Login
$account = $tenant->getAccountStore()->authenticate(
$email,
$password,
['ipAddress' => request()->ip()]
);
// Register
$account = $tenant->getAccountStore()->createAccount([
'email' => $email,
'password' => $password,
'givenName' => 'John',
'surname' => 'Doe',
]);
// Create a group
$group = $tenant->getGroupStore()->createGroup(['name' => 'Admins']);
// Add user to group
$account->addToGroup($group);
$application = $tenant->getApplication('your-app-href');
$application->setPasswordPolicy([
'minPasswordLength' => 8,
'requireNonAlphanumeric' => true,
]);
Middleware for Auth Create a middleware to handle Stormpath auth:
class StormpathAuthenticate
{
public function handle($request, Closure $next)
{
$account = $request->user('stormpath');
if (!$account) {
return redirect()->route('login');
}
return $next($request);
}
}
Service Provider Binding
Bind the Stormpath client in AppServiceProvider:
public function boot()
{
$this->app->singleton('stormpath.client', function ($app) {
return Stormpath::createClient(
config('services.stormpath.client_id'),
config('services.stormpath.client_secret')
);
});
}
Custom User Model
Extend Laravel’s User model to integrate Stormpath:
class User extends Model implements Authenticatable
{
public function getAuthIdentifier()
{
return $this->email;
}
public static function findForPassport($identifier)
{
return self::where('email', $identifier)->first();
}
}
Deprecated API
Stormpath’s migration to Okta means this SDK is archived. Use Okta’s PHP SDK (okta/okta-php) for new projects.
Rate Limiting Stormpath enforces rate limits. Cache responses aggressively:
$cacheKey = "stormpath_account_{$account->getHref()}";
return Cache::remember($cacheKey, now()->addHours(1), function () use ($account) {
return $account->getDetails();
});
HREF vs. ID Confusion
Stormpath uses HREFs (e.g., /v1/accounts/123) instead of numeric IDs. Always use the full HREF when referencing entities.
Password Hashing Stormpath handles hashing internally. Never hash passwords before sending to Stormpath.
Stormpath::setDebugMode(true); // Logs HTTP requests/responses
404 for missing resources (e.g., invalid HREF). Validate HREFs before operations.Custom Account Attributes
Extend the Account model to add custom fields:
$account->setCustomData(['role' => 'admin']);
$customData = $account->getCustomData();
Webhook Listeners Use Stormpath’s webhooks for real-time events (e.g., account creation):
$webhook = $tenant->getWebhookStore()->createWebhook([
'url' => 'https://your-app.com/stormpath-webhook',
'eventTypes' => ['ACCOUNT_CREATED'],
]);
Bulk Operations Use pagination for large datasets:
$accounts = $tenant->getAccountStore()->getAccounts();
while ($accounts->hasNext()) {
$accounts = $accounts->getNext();
foreach ($accounts as $account) {
// Process account
}
}
$tenant = $client->getTenant('your-tenant-href'); // Not just the name!
STORMPATH_CLIENT_ID, STORMPATH_CLIENT_SECRET, and STORMPATH_TENANT in .env:
STORMPATH_TENANT=https://api.stormpath.com/v1/tenants/your-tenant-href
How can I help you explore Laravel packages today?