2024-03-06 16:30:19 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
use Illuminate\Support\Facades\Crypt;
|
|
|
|
|
|
|
|
|
|
class OauthSetting extends Model
|
|
|
|
|
{
|
|
|
|
|
use HasFactory;
|
|
|
|
|
|
2026-07-02 13:13:39 +00:00
|
|
|
protected $fillable = ['provider', 'client_id', 'client_secret', 'redirect_uri', 'tenant', 'base_url', 'enabled', 'custom_label', 'scopes', 'allow_registration', 'auto_join_root_team', 'require_email_verified', 'use_pkce', 'clock_skew_seconds'];
|
2026-06-04 11:59:08 +00:00
|
|
|
|
|
|
|
|
protected function casts(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'enabled' => 'boolean',
|
|
|
|
|
'allow_registration' => 'boolean',
|
2026-07-02 13:13:39 +00:00
|
|
|
'auto_join_root_team' => 'boolean',
|
2026-06-04 11:59:08 +00:00
|
|
|
'require_email_verified' => 'boolean',
|
|
|
|
|
'use_pkce' => 'boolean',
|
|
|
|
|
'clock_skew_seconds' => 'integer',
|
|
|
|
|
];
|
|
|
|
|
}
|
2024-12-13 11:03:05 +00:00
|
|
|
|
2026-05-11 19:18:07 +00:00
|
|
|
protected $hidden = [
|
|
|
|
|
'client_secret',
|
|
|
|
|
];
|
|
|
|
|
|
2024-03-06 16:30:19 +00:00
|
|
|
protected function clientSecret(): Attribute
|
|
|
|
|
{
|
|
|
|
|
return Attribute::make(
|
2024-06-10 20:43:34 +00:00
|
|
|
get: fn (?string $value) => empty($value) ? null : Crypt::decryptString($value),
|
|
|
|
|
set: fn (?string $value) => empty($value) ? null : Crypt::encryptString($value),
|
2024-03-06 16:30:19 +00:00
|
|
|
);
|
|
|
|
|
}
|
2024-12-13 11:03:05 +00:00
|
|
|
|
|
|
|
|
public function couldBeEnabled(): bool
|
|
|
|
|
{
|
|
|
|
|
switch ($this->provider) {
|
|
|
|
|
case 'azure':
|
2025-05-20 12:35:39 +00:00
|
|
|
return filled($this->client_id) && filled($this->client_secret) && filled($this->tenant);
|
2024-12-13 11:03:05 +00:00
|
|
|
case 'authentik':
|
2025-06-26 10:23:08 +00:00
|
|
|
case 'clerk':
|
2026-06-04 11:59:08 +00:00
|
|
|
case 'oidc':
|
2025-05-20 12:35:39 +00:00
|
|
|
return filled($this->client_id) && filled($this->client_secret) && filled($this->base_url);
|
2024-12-13 11:03:05 +00:00
|
|
|
default:
|
2025-05-20 12:35:39 +00:00
|
|
|
return filled($this->client_id) && filled($this->client_secret);
|
2024-12-13 11:03:05 +00:00
|
|
|
}
|
|
|
|
|
}
|
2026-06-04 11:59:08 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return array<int, string>
|
|
|
|
|
*/
|
|
|
|
|
public function scopeList(): array
|
|
|
|
|
{
|
|
|
|
|
$scopes = str($this->scopes ?: 'openid email profile')
|
|
|
|
|
->replace(',', ' ')
|
|
|
|
|
->explode(' ')
|
|
|
|
|
->map(fn (string $scope) => trim($scope))
|
|
|
|
|
->filter()
|
|
|
|
|
->unique()
|
|
|
|
|
->values()
|
|
|
|
|
->all();
|
|
|
|
|
|
|
|
|
|
return $scopes === [] ? ['openid', 'email', 'profile'] : $scopes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function loginLabel(): string
|
|
|
|
|
{
|
|
|
|
|
if (filled($this->custom_label)) {
|
|
|
|
|
return $this->custom_label;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$envLabel = config("services.{$this->provider}.custom_label");
|
|
|
|
|
if (filled($envLabel)) {
|
|
|
|
|
return $envLabel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return __("auth.login.{$this->provider}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function isOidc(): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->provider === 'oidc';
|
|
|
|
|
}
|
2024-03-06 16:30:19 +00:00
|
|
|
}
|