|
| 1 | +<?php namespace App\Jobs; |
| 2 | +/* |
| 3 | + * Copyright 2024 OpenStack Foundation |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + **/ |
| 14 | + |
| 15 | +use Auth\User; |
| 16 | +use Illuminate\Bus\Queueable; |
| 17 | +use Illuminate\Contracts\Queue\ShouldQueue; |
| 18 | +use Illuminate\Foundation\Bus\Dispatchable; |
| 19 | +use Illuminate\Queue\InteractsWithQueue; |
| 20 | +use Illuminate\Queue\SerializesModels; |
| 21 | +use Illuminate\Support\Facades\Log; |
| 22 | +use OAuth2\Services\ITokenService; |
| 23 | +use Utils\IPHelper; |
| 24 | + |
| 25 | +/** |
| 26 | + * Class RevokeUserGrants |
| 27 | + * @package App\Jobs |
| 28 | + */ |
| 29 | +abstract class RevokeUserGrants implements ShouldQueue |
| 30 | +{ |
| 31 | + use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; |
| 32 | + |
| 33 | + public $tries = 5; |
| 34 | + |
| 35 | + public $timeout = 0; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var int |
| 39 | + */ |
| 40 | + private int $user_id; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var string|null |
| 44 | + */ |
| 45 | + private ?string $client_id; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var string |
| 49 | + */ |
| 50 | + private string $reason; |
| 51 | + |
| 52 | + /** |
| 53 | + * @var string |
| 54 | + */ |
| 55 | + private string $client_ip; |
| 56 | + |
| 57 | + /** |
| 58 | + * @param User $user |
| 59 | + * @param string|null $client_id null = revoke across all clients |
| 60 | + * @param string $reason audit message suffix |
| 61 | + */ |
| 62 | + public function __construct(User $user, ?string $client_id, string $reason) |
| 63 | + { |
| 64 | + $this->user_id = $user->getId(); |
| 65 | + $this->client_id = $client_id; |
| 66 | + $this->reason = $reason; |
| 67 | + $this->client_ip = IPHelper::getUserIp(); |
| 68 | + Log::debug(sprintf( |
| 69 | + "RevokeUserGrants::constructor user %s client_id %s reason %s", |
| 70 | + $this->user_id, |
| 71 | + $client_id ?? 'N/A', |
| 72 | + $reason |
| 73 | + )); |
| 74 | + } |
| 75 | + |
| 76 | + public function handle(ITokenService $service): void |
| 77 | + { |
| 78 | + Log::debug("RevokeUserGrants::handle"); |
| 79 | + |
| 80 | + try { |
| 81 | + $service->revokeUsersToken($this->user_id, $this->client_id); |
| 82 | + } catch (\Exception $ex) { |
| 83 | + Log::warning(sprintf("RevokeUserGrants::handle attempt %d failed: %s", |
| 84 | + $this->attempts(), $ex->getMessage())); |
| 85 | + throw $ex; |
| 86 | + } |
| 87 | + |
| 88 | + $scope = !empty($this->client_id) |
| 89 | + ? sprintf("client %s", $this->client_id) |
| 90 | + : "all clients"; |
| 91 | + |
| 92 | + $action = sprintf( |
| 93 | + "Revoking all grants for user %s on %s due to %s.", |
| 94 | + $this->user_id, |
| 95 | + $scope, |
| 96 | + $this->reason |
| 97 | + ); |
| 98 | + |
| 99 | + AddUserAction::dispatch($this->user_id, $this->client_ip, $action); |
| 100 | + |
| 101 | + // Emit to OTEL audit log (Elasticsearch) if enabled |
| 102 | + if (config('opentelemetry.enabled', false)) { |
| 103 | + EmitAuditLogJob::dispatch('audit.security.tokens_revoked', [ |
| 104 | + 'audit.action' => 'revoke_tokens', |
| 105 | + 'audit.entity' => 'User', |
| 106 | + 'audit.entity_id' => (string) $this->user_id, |
| 107 | + 'audit.timestamp' => now()->toISOString(), |
| 108 | + 'audit.description' => $action, |
| 109 | + 'audit.reason' => $this->reason, |
| 110 | + 'audit.scope' => $scope, |
| 111 | + 'auth.user.id' => $this->user_id, |
| 112 | + 'client.ip' => $this->client_ip, |
| 113 | + 'elasticsearch.index' => config('opentelemetry.logs.elasticsearch_index', 'logs-audit'), |
| 114 | + ]); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + public function failed(\Throwable $exception): void |
| 119 | + { |
| 120 | + Log::error(sprintf("RevokeUserGrants::failed %s", $exception->getMessage())); |
| 121 | + } |
| 122 | +} |
0 commit comments