diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-05-10 18:13:04 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-05-10 18:13:04 +0300 |
commit | 4e9ceea7f8ff3e097ad45f6f54c1b7165248e848 (patch) | |
tree | 6cc413c085934f15c789d539f69dcb89600c71fb /app/services/personal_access_tokens | |
parent | 0e0df204c1a0d859ccbbe1be83a5e09a53381f17 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/services/personal_access_tokens')
-rw-r--r-- | app/services/personal_access_tokens/rotate_service.rb | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/app/services/personal_access_tokens/rotate_service.rb b/app/services/personal_access_tokens/rotate_service.rb new file mode 100644 index 00000000000..64b0c5c98a9 --- /dev/null +++ b/app/services/personal_access_tokens/rotate_service.rb @@ -0,0 +1,49 @@ +# frozen_string_literal: true + +module PersonalAccessTokens + class RotateService + EXPIRATION_PERIOD = 1.week + + def initialize(current_user, token) + @current_user = current_user + @token = token + end + + def execute + return ServiceResponse.error(message: _('token already revoked')) if token.revoked? + + response = ServiceResponse.success + + PersonalAccessToken.transaction do + unless token.revoke! + response = ServiceResponse.error(message: _('failed to revoke token')) + raise ActiveRecord::Rollback + end + + target_user = token.user + new_token = target_user.personal_access_tokens.create(create_token_params(token)) + + if new_token.persisted? + response = ServiceResponse.success(payload: { personal_access_token: new_token }) + else + response = ServiceResponse.error(message: new_token.errors.full_messages.to_sentence) + + raise ActiveRecord::Rollback + end + end + + response + end + + private + + attr_reader :current_user, :token + + def create_token_params(token) + { name: token.name, + impersonation: token.impersonation, + scopes: token.scopes, + expires_at: Date.today + EXPIRATION_PERIOD } + end + end +end |