Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ctyler's avatar

Connecting One Laravel App to another over API in Scheduled Task

Hello, I am working on a project that entails creating an intranet website application that is not accessible from internet. I am also creating an external application that will be used for mass communication for employees. On the intranet side there is an LDAP application that people use for creating new users in Active Directory. When new users are created on the intranet, I want these to also be pushed out to the notification application. I can do that easily enough using Laravels API functionality. However, I would like the ability for a scheduled task to run that could check all the user IDs on the intranet side with all of the users of the communication side and remove users on the communication side. I could connect to the communication app database directly from the intranet application over the internet - but I am not comfortable with that.

So my question is how can I connect to the communication app over the Laravel API without a user actually logging in and doing it securely like you would need to do in a scheduled task.

0 likes
4 replies
rodrigo.pedra's avatar
Level 56

I could connect to the communication app database directly from the intranet application over the internet - but I am not comfortable with that.

Why not?

Sharing storage between applications that belong to the same domain/organization is a common strategy to share data between different apps.

For example, when running multiple instances of the same app across several servers within the same infra-structure, you need a shared storage (database, redis, memcached, etc.) to synchronize queues or share session between them.

If you want to have an endpoint to totally separate the apps, or are planning to have these apps deployed on different infra-structure that cannot be shared you can have an API endpoint on one app that the other can call.

To secure this endpoint you can add a token based authentication with a shared secret (token) between both apps.

The easiest way to implement it is to use a closure request guard:

https://laravel.com/docs/8.x/authentication#closure-request-guards

The code sample in the docs fetches a User from the users table. But you can return any object that implements the \Illuminate\Contracts\Auth\Authenticatable interface.

For example, you could have Dummy a class like this:

<?php

namespace App;

use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Support\Str;

class Client implements Authenticatable
{
    private $client;

    public function __construct($client)
    {
        $this->client = $client;
    }

    public function getAuthIdentifierName()
    {
        return 'client';
    }

    public function getAuthIdentifier()
    {
        return $this->client;
    }

    public function getAuthPassword()
    {
        return '';
    }

    public function getRememberToken()
    {
        // avoid using this with a session-based guard
        return Str::random(40);
    }

    public function setRememberToken($value)
    {
    }

    public function getRememberTokenName()
    {
        return '';
    }
}

And register a closure request guard like this:

Auth::viaRequest('intranet', function (Request $request) {
    $token = $request->header('Authorization');

    if (! \hash_equals('my-shared-secret', Str::after($token, 'Bearer '))) {
        return null;
    }

    return new Client('intranet');
});

Then in a route in your ./routes/api.php you would have:

Route::post('/sync-intranet', [SyncController::class, 'sync'])->middleware('auth:intranet');

And from the client application's scheduled job you can use Laravel's HTTP Client to call this endpoint:

Http::asJson()
    ->withToken('my-shared-secret')
    ->post('https://example.com/sync/intranet', [
        'data' => [...],
    ]);

Refer to https://laravel.com/docs/8.x/http-client if you are not familiar with the HTTP Client.

Notes:

  • Here the shared secret is hard-coded in the code to exemplify usage. Ideally you would have it in a .env file on both apps, and accessed from a config file.
  • Security in this approach can be improved by signing the payload using the shared secret in the sender application and comparing it on the receiving application to ensure the data was not compromised during the request. It is out of the scope of this question to handle this.
  • Another security measure is to check the request origin and compare it on an allowed domain list to avoid requests from unexpected domains.
  • If both applications can access the same database, I, personally, would prefer the client application to access it. Configure a separate connection on the ./config/database.php file and treat it as an external storage.
  • One upside on sharing storage vs using endpoints is that if someone knows the shared secret (token) and you don't secure your endpoint with the aforementioned measures, they can call this endpoint from an external machine and send compromised data. It is much more common to have a database behind a firewall and manage different credentials for different applications with adequate access permissions.

Hope this helps.

3 likes
rodrigo.pedra's avatar

One thing noted after answering to this other thread:

https://laracasts.com/discuss/channels/code-review/custom-user-provider

Is that just using Auth::viaRequest won't add a new guard, but a new "guard driver".

Before using it with the auth middleware you need to add a new guard that uses this driver on your ./config/auth.php:

'guards' => [
    'intranet' => [
        'driver' => 'intranet',
    ],
],

Then the code samples above should work.

This is outlined in the linked docs, but I oversight it.

3 likes
ctyler's avatar

Awesome response as always rodrigo.pedra.

Shared storage would be much easier. I will need to do some testing on both but I am leaning towards keeping them completely separate but I want to make sure, my discomfort is not from ignorance - it probably is.

Thank you rodrigo.pedra.

1 like

Please or to participate in this conversation.