Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

12.6 KB
Raw Blame
<?php

use App\Auth\Oidc\OidcUser;
use App\Models\InstanceSettings;
use App\Models\OauthIdentity;
use App\Models\OauthSetting;
use App\Models\Team;
use App\Models\User;
use App\Services\Auth\OauthLoginService;
use Illuminate\Database\UniqueConstraintViolationException;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Once;
use Laravel\Socialite\Facades\Socialite;

uses(RefreshDatabase::class);

beforeEach(function () {
    config()->set('app.maintenance.driver', 'file');

    InstanceSettings::forceCreate([
        'id' => 0,
        'is_registration_enabled' => false,
    ]);

    Once::flush();

    OauthSetting::create([
        'provider' => 'oidc',
        'enabled' => true,
        'client_id' => 'client-id',
        'client_secret' => 'client-secret',
        'base_url' => 'https://idp.example.com',
        'redirect_uri' => 'https://coolify.example.com/auth/oidc/callback',
        'allow_registration' => false,
    ]);
});

function fakeOidcProvider(array $claims = []): void
{
    $user = (new OidcUser)->setRaw(array_merge([
        'iss' => 'https://idp.example.com',
        'sub' => 'okta-user-1',
        'email' => '[email protected]',
        'email_verified' => true,
        'name' => 'Okta User',
    ], $claims))->map([
        'id' => $claims['sub'] ?? 'okta-user-1',
        'name' => $claims['name'] ?? 'Okta User',
        'email' => $claims['email'] ?? '[email protected]',
    ]);

    $provider = Mockery::mock();
    $provider->shouldReceive('setConfig')->andReturnSelf();
    $provider->shouldReceive('user')->andReturn($user);

    Socialite::shouldReceive('driver')->with('oidc')->andReturn($provider);
}

it('logs in a user through an existing oidc identity', function () {
    $user = User::factory()->create(['email' => '[email protected]']);
    OauthIdentity::create([
        'user_id' => $user->id,
        'provider' => 'oidc',
        'issuer' => 'https://idp.example.com',
        'provider_user_id' => 'okta-user-1',
        'email' => '[email protected]',
    ]);

    fakeOidcProvider(['email' => '[email protected]']);

    $response = $this->get(route('auth.callback', 'oidc'));

    $response->assertRedirect('/');
    $this->assertAuthenticatedAs($user);
});

it('continues oidc login when another request creates the identity first', function () {
    $user = User::factory()->create(['email' => '[email protected]']);
    $eventName = 'eloquent.creating: '.OauthIdentity::class;

    Event::listen($eventName, function (OauthIdentity $identity): void {
        $attributes = $identity->getAttributes();

        DB::afterRollBack(fn () => DB::table('oauth_identities')->insert($attributes));

        throw new UniqueConstraintViolationException(
            DB::getDefaultConnection(),
            'insert into oauth_identities',
            [],
            new PDOException('duplicate identity'),
        );
    });

    try {
        $resolvedUser = app(OauthLoginService::class)->login('oidc', (new OidcUser)->setRaw([
            'iss' => 'https://idp.example.com',
            'sub' => 'oidc-race-id',
            'email' => '[email protected]',
            'email_verified' => true,
            'name' => 'Race User',
        ])->map([
            'id' => 'oidc-race-id',
            'name' => 'Race User',
            'email' => '[email protected]',
        ]), OauthSetting::where('provider', 'oidc')->firstOrFail());
    } finally {
        Event::forget($eventName);
    }

    expect($resolvedUser->is($user))->toBeTrue()
        ->and(OauthIdentity::where('provider_user_id', 'oidc-race-id')->count())->toBe(1);
    $this->assertAuthenticatedAs($user);
});

it('creates a new oidc user when provider registration is allowed while normal registration is disabled', function () {
    OauthSetting::where('provider', 'oidc')->update(['allow_registration' => true]);

    fakeOidcProvider(['email' => '[email protected]']);

    $response = $this->get(route('auth.callback', 'oidc'));

    $response->assertRedirect('/');
    $user = User::whereEmail('[email protected]')->first();
    expect($user)->not->toBeNull()
        ->and($user->password)->not->toBeNull();
    $this->assertAuthenticatedAs($user);
    $this->assertDatabaseHas('oauth_identities', [
        'user_id' => $user->id,
        'provider' => 'oidc',
        'issuer' => 'https://idp.example.com',
        'provider_user_id' => 'okta-user-1',
    ]);
});

it('creates a new oidc user in the root team only when provider root auto-join is enabled', function () {
    Team::forceCreate(['id' => 0, 'name' => 'Root Team', 'personal_team' => true]);
    (new User)->forceFill([
        'id' => 0,
        'name' => 'Root User',
        'email' => '[email protected]',
        'password' => 'password',
    ])->save();

    OauthSetting::where('provider', 'oidc')->update([
        'allow_registration' => true,
        'auto_join_root_team' => true,
    ]);

    fakeOidcProvider(['email' => '[email protected]', 'name' => 'Root Member']);

    $response = $this->get(route('auth.callback', 'oidc'));

    $response->assertRedirect('/');
    $user = User::whereEmail('[email protected]')->first();
    expect($user)->not->toBeNull()
        ->and($user->teams()->count())->toBe(1);

    $rootMembership = $user->teams()->where('teams.id', 0)->first();
    expect($rootMembership)->not->toBeNull()
        ->and($rootMembership->pivot->role)->toBe('member');

    $this->assertDatabaseMissing('teams', [
        'name' => "Root Member's Team",
    ]);
    expect(session('currentTeam')->id)->toBe(0);
    $this->assertAuthenticatedAs($user);
});

it('rejects linking an unverified oidc email to an existing local account', function () {
    $user = User::factory()->create(['email' => '[email protected]']);

    fakeOidcProvider(['email' => '[email protected]', 'email_verified' => false]);

    $response = $this->from('/login')->get(route('auth.callback', 'oidc'));

    $response->assertRedirect('/login');
    $this->assertGuest();
    $this->assertDatabaseMissing('oauth_identities', [
        'user_id' => $user->id,
        'provider' => 'oidc',
    ]);
});

it('rejects linking a new oidc identity to an account with another provider identity', function () {
    $user = User::factory()->create(['email' => '[email protected]']);
    OauthIdentity::create([
        'user_id' => $user->id,
        'provider' => 'github',
        'issuer' => 'github',
        'provider_user_id' => 'github-user-id',
        'email' => $user->email,
    ]);

    fakeOidcProvider(['email' => $user->email, 'email_verified' => true]);

    $this->from('/login')->get(route('auth.callback', 'oidc'))->assertRedirect('/login');

    $this->assertGuest();
    expect(OauthIdentity::count())->toBe(1);
});

it('rejects new oidc users when neither normal nor provider registration is enabled', function () {
    fakeOidcProvider(['email' => '[email protected]']);

    $response = $this->from('/login')->get(route('auth.callback', 'oidc'));

    $response->assertRedirect('/login');
    expect(User::whereEmail('[email protected]')->exists())->toBeFalse();
});

it('creates the root user when oidc provisions the first account', function () {
    Team::forceCreate(['id' => 0, 'name' => 'Root Team', 'personal_team' => true]);
    OauthSetting::where('provider', 'oidc')->update(['allow_registration' => true]);

    fakeOidcProvider(['email' => '[email protected]', 'name' => 'Root User']);

    $response = $this->get(route('auth.callback', 'oidc'));

    $response->assertRedirect('/');
    $this->assertDatabaseHas('users', ['id' => 0, 'email' => '[email protected]']);
    expect(User::whereEmail('[email protected]')->firstOrFail()->email_verified_at)->toBeNull();
    $this->assertDatabaseHas('team_user', ['team_id' => 0, 'user_id' => 0, 'role' => 'owner']);
    expect(InstanceSettings::find(0)->is_registration_enabled)->toBeFalse();
});

it('persists raw claims as an array on the oauth identity', function () {
    OauthSetting::where('provider', 'oidc')->update(['allow_registration' => true]);

    fakeOidcProvider(['email' => '[email protected]']);

    $this->get(route('auth.callback', 'oidc'))->assertRedirect('/');

    $identity = OauthIdentity::where('email', '[email protected]')->first();
    expect($identity->raw_claims)->toBeArray()
        ->and($identity->raw_claims['sub'])->toBe('okta-user-1');
});

it('stores empty raw claims when the provider returns no user payload', function () {
    OauthSetting::where('provider', 'oidc')->update(['allow_registration' => true]);

    $user = (new OidcUser)->setIdTokenClaims([
        'iss' => 'https://idp.example.com',
        'sub' => 'okta-no-payload',
        'email_verified' => true,
    ])->map([
        'id' => 'okta-no-payload',
        'name' => 'No Payload',
        'email' => '[email protected]',
    ]);
    $user->user = null;

    $provider = Mockery::mock();
    $provider->shouldReceive('setConfig')->andReturnSelf();
    $provider->shouldReceive('user')->andReturn($user);
    Socialite::shouldReceive('driver')->with('oidc')->andReturn($provider);

    $this->get(route('auth.callback', 'oidc'))->assertRedirect('/');

    $identity = OauthIdentity::where('email', '[email protected]')->first();
    expect($identity->raw_claims)->toBe([]);
});

it('rejects callbacks for disabled oidc provider', function () {
    OauthSetting::where('provider', 'oidc')->update(['enabled' => false]);

    $response = $this->from('/login')->get(route('auth.callback', 'oidc'));

    $response->assertRedirect('/login');
});

it('logs callback failures with diagnostic context', function () {
    Log::spy();

    $provider = Mockery::mock();
    $provider->shouldReceive('setConfig')->andReturnSelf();
    $provider->shouldReceive('user')->andThrow(new RuntimeException('Token exchange failed'));
    Socialite::shouldReceive('driver')->with('oidc')->andReturn($provider);

    $response = $this->from('/login')->get(route('auth.callback', ['provider' => 'oidc', 'code' => 'secret-code', 'state' => 'state-value']));

    $response->assertRedirect('/login');
    Log::shouldHaveReceived('error')->once()->withArgs(function (string $message, array $context) {
        return $message === 'OAuth callback failed.'
            && $context['provider'] === 'oidc'
            && $context['exception_class'] === RuntimeException::class
            && $context['exception_message'] === 'Token exchange failed'
            && $context['has_code'] === true
            && $context['has_state'] === true
            && $context['exception'] instanceof RuntimeException;
    });
});

it('does not mark a newly provisioned oidc account verified without a verified email claim', function () {
    User::factory()->create(['email' => '[email protected]']);
    OauthSetting::where('provider', 'oidc')->update(['allow_registration' => true, 'require_email_verified' => false]);

    fakeOidcProvider(['email' => '[email protected]', 'email_verified' => false]);

    $this->get(route('auth.callback', 'oidc'))->assertRedirect('/');

    $user = User::whereEmail('[email protected]')->firstOrFail();
    expect($user->email_verified_at)->toBeNull();
});

it('links an existing account when Microsoft Entra ID sends xms_edov instead of email_verified', function () {
    $user = User::factory()->create(['email' => '[email protected]']);

    fakeOidcProvider(['email' => '[email protected]', 'email_verified' => null, 'xms_edov' => true]);

    $this->get(route('auth.callback', 'oidc'))->assertRedirect('/');

    $this->assertAuthenticatedAs($user);
    $this->assertDatabaseHas('oauth_identities', ['user_id' => $user->id, 'provider' => 'oidc']);
});

it('rejects linking an existing account when Microsoft Entra ID sends a false xms_edov', function () {
    $user = User::factory()->create(['email' => '[email protected]']);

    fakeOidcProvider(['email' => '[email protected]', 'email_verified' => null, 'xms_edov' => false]);

    $this->from('/login')->get(route('auth.callback', 'oidc'))->assertRedirect('/login');

    $this->assertGuest();
    $this->assertDatabaseMissing('oauth_identities', ['user_id' => $user->id]);
});

it('treats only a boolean true email_verified or xms_edov id token claim as verified', function (array $claims, bool $expected) {
    $oidcUser = (new OidcUser)->setIdTokenClaims(['iss' => 'https://idp.example.com', 'sub' => 'user-1', ...$claims]);

    expect($oidcUser->emailVerified)->toBe($expected);
})->with([
    'email_verified true' => [['email_verified' => true], true],
    'xms_edov true' => [['xms_edov' => true], true],
    'xms_edov false' => [['xms_edov' => false], false],
    'xms_edov string' => [['xms_edov' => 'true'], false],
    'no claim' => [[], false],
]);