Skip to content

Commit 5436190

Browse files
committed
Add handling for missing internal OAuth app
`InternalUserAccessTokenService`: add `application!` (lookup + raise) and `application_present?` (safe check with logging) `_v2_api_token.html.erb`: gate token UI on `application_present?` and show a warning when missing.
1 parent b4fc307 commit 5436190

2 files changed

Lines changed: 43 additions & 19 deletions

File tree

app/services/api/v2/internal_user_access_token_service.rb

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ module V2
2121
# This service does NOT support third-party OAuth clients or delegated consent flows.
2222
class InternalUserAccessTokenService
2323
READ_SCOPE = 'read'
24-
APPLICATION = Doorkeeper::Application.find_by(
25-
name: Rails.application.config.x.application.internal_oauth_app_name
26-
)
24+
INTERNAL_OAUTH_APP_NAME = Rails.application.config.x.application.internal_oauth_app_name
2725

2826
class << self
2927
def for_user(user)
@@ -46,8 +44,27 @@ def rotate!(user)
4644
)
4745
end
4846

47+
# Used by views (e.g. devise/registrations/_v2_api_token.html.erb) to safely
48+
# gate token UI if the internal OAuth application is missing.
49+
def application_present?
50+
application!
51+
true
52+
rescue StandardError => e
53+
Rails.logger.error(e.message)
54+
false
55+
end
56+
4957
private
5058

59+
def application!
60+
Doorkeeper::Application.find_by(name: INTERNAL_OAUTH_APP_NAME) ||
61+
raise(
62+
StandardError,
63+
"Required Doorkeeper application '#{INTERNAL_OAUTH_APP_NAME}' not found. " \
64+
'Please ensure the application exists in the database.'
65+
)
66+
end
67+
5168
def revoke_existing!(user)
5269
Doorkeeper::AccessToken.revoke_all_for(application!.id, user)
5370
end
Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,33 @@
11
<%# locals: user %>
22

3-
<% token = Api::V2::InternalUserAccessTokenService.for_user(user) %>
43
<div id="v2-api-token" class="card mb-4">
54
<div class="card-heading">
65
<%= _('V2 API') %>
76
</div>
87
<div class="card-body">
9-
<div class="form-control mb-3 col-xs-8">
10-
<%= label_tag(:api_token, _('Access token'), class: 'form-label') %>
11-
<% if token.present? %>
12-
<code><%= token.token %></code>
13-
<% else %>
14-
<%= _("Click the button below to generate an API token") %>
15-
<% end %>
16-
</div>
8+
<% if Api::V2::InternalUserAccessTokenService.application_present? %>
9+
<% token = Api::V2::InternalUserAccessTokenService.for_user(user) %>
10+
<div class="form-control mb-3 col-xs-8">
11+
<%= label_tag(:api_token, _('Access token'), class: 'form-label') %>
12+
<% if token.present? %>
13+
<code><%= token.token %></code>
14+
<% else %>
15+
<%= _("Click the button below to generate an API token") %>
16+
<% end %>
17+
</div>
1718

18-
<div class="form-control mb-3 col-xs-8">
19-
<%= link_to _("Regenerate token"),
20-
api_v2_internal_user_access_token_path(format: :js),
21-
method: :post,
22-
class: 'btn btn-secondary',
23-
remote: true %>
24-
</div>
19+
<div class="form-control mb-3 col-xs-8">
20+
<%= link_to _("Regenerate token"),
21+
api_v2_internal_user_access_token_path(format: :js),
22+
method: :post,
23+
class: 'btn btn-secondary',
24+
remote: true %>
25+
</div>
26+
<% else %>
27+
<div class="alert alert-warning">
28+
<%= _("V2 API token service is currently unavailable. Please contact us for help.") %>
29+
<%= mail_to Rails.application.config.x.organisation.helpdesk_email %>
30+
</div>
31+
<% end %>
2532
</div>
2633
</div>

0 commit comments

Comments
 (0)