-
Notifications
You must be signed in to change notification settings - Fork 0
fix(security): revoke tokens and invalidate sessions on password change #117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+557
−65
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3d6cd09
fix(security): revoke tokens and invalidate sessions on password change
mulldug feab579
refactor(security): replace reason flag with RevokeUserGrants subclas…
mulldug 1afebad
Merge branch 'main' into fix/password_change_revoke_tokens
mulldug 630d325
refactor(security): move security side-effects into service layer and…
mulldug f61514b
fix(security): eliminate pre-commit side-effects and stale IP in Revo…
mulldug 5381c79
tyle(session): use Session facade consistently in UserService
mulldug File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| <?php namespace App\Jobs; | ||
| /* | ||
| * Copyright 2024 OpenStack Foundation | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| **/ | ||
|
|
||
| use Auth\User; | ||
| use Illuminate\Bus\Queueable; | ||
| use Illuminate\Contracts\Queue\ShouldQueue; | ||
| use Illuminate\Foundation\Bus\Dispatchable; | ||
| use Illuminate\Queue\InteractsWithQueue; | ||
| use Illuminate\Queue\SerializesModels; | ||
| use Illuminate\Support\Facades\Log; | ||
| use OAuth2\Services\ITokenService; | ||
| use Utils\IPHelper; | ||
|
|
||
| /** | ||
| * Class RevokeUserGrants | ||
| * @package App\Jobs | ||
| */ | ||
| abstract class RevokeUserGrants implements ShouldQueue | ||
| { | ||
| use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; | ||
|
|
||
| public $tries = 5; | ||
|
|
||
| public $timeout = 0; | ||
|
|
||
| /** | ||
| * @var int | ||
| */ | ||
| private int $user_id; | ||
|
|
||
| /** | ||
| * @var string|null | ||
| */ | ||
| private ?string $client_id; | ||
|
|
||
| /** | ||
| * @var string | ||
| */ | ||
| private string $reason; | ||
|
|
||
| /** | ||
| * @var string | ||
| */ | ||
| private string $client_ip; | ||
|
|
||
| /** | ||
| * @param User $user | ||
| * @param string|null $client_id null = revoke across all clients | ||
| * @param string $reason audit message suffix | ||
| */ | ||
| public function __construct(User $user, ?string $client_id, string $reason) | ||
| { | ||
| $this->user_id = $user->getId(); | ||
| $this->client_id = $client_id; | ||
| $this->reason = $reason; | ||
| $this->client_ip = IPHelper::getUserIp(); | ||
| Log::debug(sprintf( | ||
| "RevokeUserGrants::constructor user %s client_id %s reason %s", | ||
| $this->user_id, | ||
| $client_id ?? 'N/A', | ||
| $reason | ||
| )); | ||
| } | ||
|
|
||
| public function handle(ITokenService $service): void | ||
| { | ||
| Log::debug("RevokeUserGrants::handle"); | ||
|
|
||
| try { | ||
| $service->revokeUsersToken($this->user_id, $this->client_id); | ||
| } catch (\Exception $ex) { | ||
| Log::warning(sprintf("RevokeUserGrants::handle attempt %d failed: %s", | ||
| $this->attempts(), $ex->getMessage())); | ||
| throw $ex; | ||
| } | ||
|
|
||
| $scope = !empty($this->client_id) | ||
| ? sprintf("client %s", $this->client_id) | ||
| : "all clients"; | ||
|
|
||
| $action = sprintf( | ||
| "Revoking all grants for user %s on %s due to %s.", | ||
| $this->user_id, | ||
| $scope, | ||
| $this->reason | ||
| ); | ||
|
|
||
| AddUserAction::dispatch($this->user_id, $this->client_ip, $action); | ||
|
|
||
| // Emit to OTEL audit log (Elasticsearch) if enabled | ||
| if (config('opentelemetry.enabled', false)) { | ||
| EmitAuditLogJob::dispatch('audit.security.tokens_revoked', [ | ||
| 'audit.action' => 'revoke_tokens', | ||
| 'audit.entity' => 'User', | ||
| 'audit.entity_id' => (string) $this->user_id, | ||
| 'audit.timestamp' => now()->toISOString(), | ||
| 'audit.description' => $action, | ||
| 'audit.reason' => $this->reason, | ||
| 'audit.scope' => $scope, | ||
| 'auth.user.id' => $this->user_id, | ||
| 'client.ip' => $this->client_ip, | ||
| 'elasticsearch.index' => config('opentelemetry.logs.elasticsearch_index', 'logs-audit'), | ||
| ]); | ||
| } | ||
| } | ||
|
|
||
| public function failed(\Throwable $exception): void | ||
| { | ||
| Log::error(sprintf("RevokeUserGrants::failed %s", $exception->getMessage())); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| <?php namespace App\Jobs; | ||
| /* | ||
| * Copyright 2024 OpenStack Foundation | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| **/ | ||
|
|
||
| use Auth\User; | ||
|
|
||
| /** | ||
| * Class RevokeUserGrantsOnPasswordChange | ||
| * Revokes all OAuth2 grants for a user across all clients after a password change. | ||
| * @package App\Jobs | ||
| */ | ||
| class RevokeUserGrantsOnPasswordChange extends RevokeUserGrants | ||
| { | ||
| const REASON = 'password change'; | ||
|
|
||
| public function __construct(User $user) | ||
| { | ||
| parent::__construct($user, null, self::REASON); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| <?php namespace App\Jobs; | ||
| /* | ||
| * Copyright 2024 OpenStack Foundation | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| **/ | ||
|
|
||
| use Auth\User; | ||
|
|
||
| /** | ||
| * Class RevokeUserGrantsOnSessionRevocation | ||
| * Revokes all OAuth2 grants for a user when they explicitly sign out all other devices. | ||
| * @package App\Jobs | ||
| */ | ||
| class RevokeUserGrantsOnSessionRevocation extends RevokeUserGrants | ||
| { | ||
| const REASON = 'user-initiated session revocation'; | ||
|
|
||
| public function __construct(User $user) | ||
| { | ||
| parent::__construct($user, null, self::REASON); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.