PHPackages                             eludadev/passage - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. eludadev/passage

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

eludadev/passage
================

1Password Passage SDK for Laravel PHP Developers.

1.0.0(3y ago)6434MITPHP

Since Jun 30Pushed 3y ago1 watchersCompare

[ Source](https://github.com/eludadev/Passage)[ Packagist](https://packagist.org/packages/eludadev/passage)[ RSS](/packages/eludadev-passage/feed)WikiDiscussions main Synced today

READMEChangelogDependencies (3)Versions (2)Used By (0)

[![Contributors](https://camo.githubusercontent.com/1bb27c987831245cb7159a431280709a440cff5ffa25d37926071b5e135d72ce/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f636f6e7472696275746f72732f656c7564616465762f506173736167652e7376673f7374796c653d666f722d7468652d6261646765)](https://github.com/eludadev/Passage/graphs/contributors)[![Forks](https://camo.githubusercontent.com/cf7bae0f64191c40c11424809186ff89aa58e33bf6cbd69c034809620b3a651a/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f666f726b732f656c7564616465762f506173736167652e7376673f7374796c653d666f722d7468652d6261646765)](https://github.com/eludadev/Passage/network/members)[![Stargazers](https://camo.githubusercontent.com/e3cc6944a6ebc484ded909c739f9aee68ada652816f3d722187f6eccdcbe40cc/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73746172732f656c7564616465762f506173736167652e7376673f7374796c653d666f722d7468652d6261646765)](https://github.com/eludadev/Passage/stargazers)[![Issues](https://camo.githubusercontent.com/5a19862725699967b2462c72531fae9a213e65b2883b0594f03cb02cde621bf4/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732f656c7564616465762f506173736167652e7376673f7374796c653d666f722d7468652d6261646765)](https://github.com/eludadev/Passage/issues)[![MIT License](https://camo.githubusercontent.com/71c060bf8646ca156566fbf9e78f847dfae3ce87f55d86fc2a9d0430bdfcb270/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f656c7564616465762f506173736167652e7376673f7374796c653d666f722d7468652d6261646765)](https://github.com/eludadev/Passage/blob/master/LICENSE)

 [ ![Logo](assets/logo.svg) ](https://github.com/eludadev/Passage)### Passage PHP SDK

[](#passage-php-sdk)

 1Password Passage SDK for PHP Developers.
 [**Read the article »**](https://shelf.eluda.dev/passage-php-sdk)

 [Report Bug](https://github.com/eludadev/Passage/issues) · [Request Feature](https://github.com/eludadev/Passage/issues)

 Table of Contents1. [About The Project](#about-the-project)
    - [Built With](#built-with)
2. [Getting Started](#getting-started)
    - [Prerequisites](#prerequisites)
    - [Installation](#installation)
3. [Usage](#usage)
    - [Authenticating Requests](#authenticating-requests)
    - [Authenticating a Request With PHP Middleware](#authenticating-a-request-with-PHP-middleware)
    - [App Information](#app-information)
    - [User Management](#user-management)
    - [User Device Management](#user-device-management)
    - [Creating Magic Links](#creating-magic-links)
4. [Example usage](#example-usage)
5. [Contributing](#contributing)
6. [License](#license)
7. [Contact](#contact)
8. [Acknowledgments](#acknowledgments)

About The Project
-----------------

[](#about-the-project)

[![1Password’s Passage SDK for PHP Developers.](assets/banner.png)](https://github.com/eludadev/Passage)

Introducing Passage SDK for PHP:

- Seamless integration with 1Password's Passage API
- Secure storage and retrieval of secrets
- Magic link authentication implementation
- Robust error handling
- Automatic conversion to native PHP `DateTime` objects
- Middleware support for easy integration
- Powerful SDK features for enhanced security in PHP projects

([back to top](#readme-top))

Getting Started
---------------

[](#getting-started)

### Prerequisites

[](#prerequisites)

Start by creating a `laravel/laravel` project using PHP composer:

- composer ```
    laravel new my-php-project
    ```

### Installation

[](#installation)

1. Create a [Passage account](https://console.passage.id/register)
2. Create a new [Passage application](https://console.passage.id/)
3. Retrieve your application's `` and [``](https://console.passage.id/settings)
4. Add `` and `` to your local `.env` file: ```
    APP_ID=your_app_id
    API_KEY=your_api_key
    ```
5. Install package from [Packagist](https://packagist.org/packages/eludadev/passage)```
    composer require eludadev/passage
    ```
6. Import the `Passage` class ```
    use Eludadev\Passage\Passage;
    ```
7. Create an instance of the `Passage` class.
    Set `` to either `'COOKIES'` (*default*) or `'HEADER'`. ```
    $passage = new Passage(env('APP_ID'), env('API_KEY'), '' /* optional */);
    ```

([back to top](#readme-top))

Usage
-----

[](#usage)

Authenticate requests and manage Passage users with Node.js.

> **Warning**To use the Passage PHP SDK, you'll need your Passage App ID. You can create a new Passage App in the [console](https://console.passage.id/).

> **Note**Assuming you followed the above prerequisites to create a new `laravel/laravel` project, you can quickly get started using this SDK in your API routes by opening the `routes/web.php` file and adding the following code:

```
use Illuminate\Http\Request;
use Eludadev\Passage\Passage;
use Illuminate\Support\Facades\Route;

Route::get('/passage', function (Request $request) {
    $passage = new Passage(env('APP_ID'), env('API_KEY'));
    return $passage->createMagicLink("example@domain.com", "/redirect");
});
```

### Authenticating Requests

[](#authenticating-requests)

Passage makes it easy to associate an HTTP request with an authenticated user. The following code can be used to validate that a request was made by an authenticated user.

```
use Illuminate\Http\Request;
use Eludadev\Passage\Passage;
use Illuminate\Support\Facades\Route;

// Authentication using Passage class instance
Route::get('authenticatedRoute', function (Request $request) {
    $passage = new Passage(env('APP_ID'), env('API_KEY'));

    try {
        // Authenticate request using Passage
        $userID = $passage->authenticateRequest($request);
        if ($userID) {
            // User is authenticated
            $userData = $passage->user->get($userID);
            return;
        }
    } catch (\Exception $e) {
        // Authentication failed
        return "Authentication failed!";
    }
});
```

By default, Passage looks for the user JWT from a cookie that is set by the Passage Element (`psg_auth_token`). If your application uses Authorization headers instead, you can pass the following option to the Passage PHP SDK.

```
$passage = new Passage(env('APP_ID'), env('API_KEY'), 'HEADER');
```

### Authenticating a Request With PHP Middleware

[](#authenticating-a-request-with-php-middleware)

If you used the `laravel/laravel` project, Passage provides a middleware that can be used directly. This middleware will authenticate a request and return a 401 Unauthorized if the token is invalid. If it succeeds, the Passage User ID will be available in the response. The following code shows how the Passage middleware can be used in a PHP application.

```
use Illuminate\Support\Facades\Route;
use Illuminate\Http\Request;

use Eludadev\Passage\Passage;
use Eludadev\Passage\Middleware\PassageAuthMiddleware;

// Example of passage middleware
Route::get('authenticatedRoute', function (Request $request) {
  $passage = new Passage(env('APP_ID'), env('API_KEY'));
  $userID = $request->userID;
})->middleware(PassageAuthMiddleware::class);
```

If you are not using `eludadev/passage` in a request context, or your application is passing the JWT in a custom way, you can pass the JWT directly to the `validAuthToken` method to perform validation.

```
$userID = $passage->validAuthToken($token);

if ($userID) {
  //authenticated
}
// otherwise, unauthorized
```

### App Information

[](#app-information)

The Passage SDK provides a way to retrieve information about an app.

```
use Eludadev\Passage\Passage;

$passage = new Passage(env('APP_ID'), env('API_KEY'));
$appInfo = $passage->getApp();
```

### User Management

[](#user-management)

In addition to authenticating requests, the Passage PHP SDK also provides a way to securely manage your users. These functions require authentication using a Passage API key. API keys can be managed in the [**Passage Console**](https://console.passage.id/).

The functionality currently available on a user is:

- Get a user's information (including any defined user metadata)
- Activate or deactivate a user (a deactivated user will not be able to log in)
- Update a user's information (email address or phone number)
- Delete a user
- Create a user

> **Warning**Passage API Keys are sensitive! You should store them securely along with your other application secrets.

 Get```
use Illuminate\Http\Request;

use Eludadev\Passage\Passage;
use Eludadev\Passage\Middleware\PassageAuthMiddleware;

Route::get('authenticatedRoute', function (Request $request) {
$passage = new Passage(env('APP_ID'), env('API_KEY'));

$userID = $request->userID;
  $passageUser = $passage->user->get($userID);
return $passageUser->email;
})->middleware(PassageAuthMiddleware::class);
```

 Activate/Deactivate```
use Illuminate\Http\Request;

use Eludadev\Passage\Passage;
use Eludadev\Passage\Middleware\PassageAuthMiddleware;

Route::get('authenticatedRoute', function (Request $request) {
  $passage = new Passage(env('APP_ID'), env('API_KEY'));
  $userID = $request->userID;

  $deactivatedUser = $passage->user->deactivate($userID);
  $deactivatedUser->active; // false

  $activatedUser = $passage->user->activate($userID);
  $activatedUser->active; // true
})->middleware(PassageAuthMiddleware::class);
```

 Update```
use Illuminate\Http\Request;

use Eludadev\Passage\Passage;
use Eludadev\Passage\Middleware\PassageAuthMiddleware;

Route::get('authenticatedRoute', function (Request $request) {
  $passage = new Passage(env('APP_ID'), env('API_KEY'));

  $userID = $request->userID;
  $passageUser = $passage->user->update($userID, [
    'email' => 'testEmail@domain.com',
    'phone' => '+15005550006'
  ]);

  $passageUser->email; // testEmail@domain.com
  $passageUser->phone; // +15005550006
})->middleware(PassageAuthMiddleware::class);
```

 Delete```
use Illuminate\Http\Request;

use Eludadev\Passage\Passage;
use Eludadev\Passage\Middleware\PassageAuthMiddleware;

Route::get('authenticatedRoute', function (Request $request) {
  $passage = new Passage(env('APP_ID'), env('API_KEY'));

  $userID = $request->userID;
  $deletedUser = $passage->user->delete($userID);
  $deletedUser; // true
})->middleware(PassageAuthMiddleware::class);
```

Create```
use Eludadev\Passage\Passage;

$passage = new Passage(env('APP_ID'), env('API_KEY'));

$newUser1 = $passage->user->create('testEmail@domain.com');
$newUser1->email; // testEmail@domain.com

$newUser2 = $passage->user->create(phone:'+15005550006');
$newUser2->phone; // +15005550006
```

FieldTypeidstringemailstringphonestringactivebooleanemail\_verifiedbooleancreated\_atDateTimelast\_login\_atDateTimewebauthnbooleanuser\_metadataarraywebauthn\_devicesarray of strings (e.g. "Mac OS X")recent\_eventsarray of strings### User Device Management

[](#user-device-management)

The functionality currently available is:

- List all devices for a user
- Revoke a particular device from a user

List Devices```
use Illuminate\Http\Request;

use Eludadev\Passage\Passage;
use Eludadev\Passage\Middleware\PassageAuthMiddleware;

Route::get('authenticatedRoute', function (Request $request) {
  $passage = new Passage(env('APP_ID'), env('API_KEY'));

  $userID = $request->userID;
  $devices = $passage->user->listDevices($userID);
  return $devices;
})->middleware(PassageAuthMiddleware::class);
```

Revoke Device```
use Illuminate\Http\Request;

use Eludadev\Passage\Passage;
use Eludadev\Passage\Middleware\PassageAuthMiddleware;

Route::get('authenticatedRoute', function (Request $request) {
  $passage = new Passage(env('APP_ID'), env('API_KEY'));

  $userID = $request->userID;
  $success = $passage->user->revokeDevice($userID, '');
  return $success; // true
})->middleware(PassageAuthMiddleware::class);
```

### Creating Magic Links

[](#creating-magic-links)

The PHP SDK can be used to generate custom magic links (called "smart links") for users, that can be embedded into any content medium. To learn more, see our full guide on [Smart Links](https://docs.passage.id/auth-configuration/embedded-magic-links).

```
use Eludadev\Passage\Passage;

$passage = new Passage(env('APP_ID'), env('API_KEY'));

$magicLink = $passage->createMagicLink('newEmail@domain.com', '/custom-path/1234');

// use Magic Link URL
$magicLink->url;
```

([back to top](#readme-top))

Example Usage
-------------

[](#example-usage)

We built an example full-stack application on React and a PHP backend. Get started by opening the following directory: `cd ./examples`

> **Warning**Make sure you have [PHP](https://www.php.net/downloads) and [Composer](https://getcomposer.org/download/) installed on your local machine before continuing with these steps.

[![Demo of Passage PHP SDK passwordless authentication with email](./assets/passage-php-sdk.gif)](./assets/passage-php-sdk.gif)

### Configuring a new Passage Project

[](#configuring-a-new-passage-project)

[![Creating a new Passage project](./assets/console.passage.id_settings_new.png)](./assets/console.passage.id_settings_new.png)

Create a [new Passage project](https://console.passage.id/create/complete). Make sure to input `http://localhost:3000` for the domain, and `/dashboard` for the redirect URL.

### Updating an existing Passage project

[](#updating-an-existing-passage-project)

[![Updating an existing Passage project](./assets/console.passage.id_settings_existing.png)](./assets/console.passage.id_settings_existing.png)

Head over to your project settings.

Input `http://localhost:3000` for the domain, and `/dashboard` for the redirect URL, and `/` for the login URL.

### Running the server

[](#running-the-server)

1. Go to the backend directory: `cd ./backend`
2. Install the dependencies: `composer install`
3. Copy the environment variables file: `cp .env.example .env`
4. Replace your [Passage credentials](https://console.passage.id/) in `.env`: ```
    PASSAGE_APP_ID=
    PASSAGE_API_KEY=

    ```
5. Run the server: `php artisan serve`

### Running the frontend

[](#running-the-frontend)

1. Go to the frontend directory: `cd ./frontend`
2. Install the dependencies: `yarn`
3. Copy the environment variables file: `cp EXAMPLE.env .env`
4. Replace your Passage credentials in `.env`: ```
    REACT_APP_PASSAGE_APP_ID=

    ```
5. Run the server: `yarn start`

### How it works

[](#how-it-works)

Here's the code powering the PHP backend:

```
// routes/api.php
