Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 26 |
|
0.00% |
0 / 13 |
CRAP | |
0.00% |
0 / 1 |
| Client | |
0.00% |
0 / 26 |
|
0.00% |
0 / 13 |
552 | |
0.00% |
0 / 1 |
| boot | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| user | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| authCodes | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| tokens | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getPlainSecretAttribute | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setSecretAttribute | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| firstParty | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| skipsAuthorization | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| confidential | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getKeyType | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| getIncrementing | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| getConnectionName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| hasGrantType | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Models\Passport; |
| 4 | |
| 5 | use App\Http\Models\Moloquent; |
| 6 | use Illuminate\Database\Eloquent\Relations\BelongsTo; |
| 7 | use Illuminate\Database\Eloquent\Relations\HasMany; |
| 8 | use Illuminate\Support\Str; |
| 9 | use Laravel\Passport\Passport; |
| 10 | |
| 11 | class Client extends Moloquent |
| 12 | { |
| 13 | /** |
| 14 | * The database table used by the model. |
| 15 | * |
| 16 | * @var string |
| 17 | */ |
| 18 | protected $table = 'oauth_clients'; |
| 19 | |
| 20 | /** |
| 21 | * The guarded attributes on the model. |
| 22 | * |
| 23 | * @var array |
| 24 | */ |
| 25 | protected $guarded = []; |
| 26 | |
| 27 | /** |
| 28 | * The attributes excluded from the model's JSON form. |
| 29 | * |
| 30 | * @var array |
| 31 | */ |
| 32 | protected $hidden = [ |
| 33 | 'secret', |
| 34 | ]; |
| 35 | |
| 36 | /** |
| 37 | * The attributes that should be cast to native types. |
| 38 | * |
| 39 | * @var array |
| 40 | */ |
| 41 | protected $casts = [ |
| 42 | 'grant_types' => 'array', |
| 43 | 'personal_access_client' => 'bool', |
| 44 | 'password_client' => 'bool', |
| 45 | 'revoked' => 'bool', |
| 46 | ]; |
| 47 | |
| 48 | /** |
| 49 | * The temporary plain-text client secret. |
| 50 | * |
| 51 | * @var string|null |
| 52 | */ |
| 53 | protected $plainSecret; |
| 54 | |
| 55 | /** |
| 56 | * Bootstrap the model and its traits. |
| 57 | */ |
| 58 | public static function boot(): void |
| 59 | { |
| 60 | parent::boot(); |
| 61 | |
| 62 | static::creating(function ($model) { |
| 63 | if (config('passport.client_uuids')) { |
| 64 | $model->{$model->getKeyName()} = $model->{$model->getKeyName()} ?: (string) Str::orderedUuid(); |
| 65 | } |
| 66 | }); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Get the user that the client belongs to. |
| 71 | */ |
| 72 | public function user(): BelongsTo |
| 73 | { |
| 74 | $provider = $this->provider ?: config('auth.guards.api.provider'); |
| 75 | |
| 76 | return $this->belongsTo( |
| 77 | config("auth.providers.{$provider}.model") |
| 78 | ); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Get all of the authentication codes for the client. |
| 83 | */ |
| 84 | public function authCodes(): HasMany |
| 85 | { |
| 86 | return $this->hasMany(Passport::authCodeModel(), 'client_id'); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Get all of the tokens that belong to the client. |
| 91 | */ |
| 92 | public function tokens(): HasMany |
| 93 | { |
| 94 | return $this->hasMany(Passport::tokenModel(), 'client_id'); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * The temporary non-hashed client secret. |
| 99 | * |
| 100 | * This is only available once during the request that created the client. |
| 101 | */ |
| 102 | public function getPlainSecretAttribute(): ?string |
| 103 | { |
| 104 | return $this->plainSecret; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Set the value of the secret attribute. |
| 109 | */ |
| 110 | public function setSecretAttribute(?string $value): void |
| 111 | { |
| 112 | $this->plainSecret = $value; |
| 113 | |
| 114 | if (is_null($value) || ! Passport::$hashesClientSecrets) { |
| 115 | $this->attributes['secret'] = $value; |
| 116 | |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | $this->attributes['secret'] = password_hash($value, PASSWORD_BCRYPT); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Determine if the client is a "first party" client. |
| 125 | */ |
| 126 | public function firstParty(): bool |
| 127 | { |
| 128 | return $this->personal_access_client || $this->password_client; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Determine if the client should skip the authorization prompt. |
| 133 | */ |
| 134 | public function skipsAuthorization(): bool |
| 135 | { |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Determine if the client is a confidential client. |
| 141 | */ |
| 142 | public function confidential(): bool |
| 143 | { |
| 144 | return ! empty($this->secret); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Get the auto-incrementing key type. |
| 149 | */ |
| 150 | public function getKeyType(): string |
| 151 | { |
| 152 | return Passport::clientUuids() ? 'string' : $this->keyType; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Get the value indicating whether the IDs are incrementing. |
| 157 | */ |
| 158 | public function getIncrementing(): bool |
| 159 | { |
| 160 | return Passport::clientUuids() ? false : $this->incrementing; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Get the current connection name for the model. |
| 165 | */ |
| 166 | public function getConnectionName(): ?string |
| 167 | { |
| 168 | return config('passport.storage.database.connection') ?? $this->connection; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Determine if the client has the given grant type. |
| 173 | * |
| 174 | * @param string $grantType |
| 175 | * @return bool |
| 176 | */ |
| 177 | public function hasGrantType($grantType) |
| 178 | { |
| 179 | if (! isset($this->grant_types) || ! is_array($this->grant_types)) { |
| 180 | return true; |
| 181 | } |
| 182 | |
| 183 | return in_array($grantType, $this->grant_types); |
| 184 | } |
| 185 | } |