Files
sure/app/views/snaptrade_items/_snaptrade_item.html.erb
Max Barbare 51c93649da feat(snaptrade): replace device-flow OAuth with authorization-code + PKCE flow (#2747)
* feat(snaptrade): replace device-flow OAuth with authorization-code + PKCE flow

Squashed from 16 commits on snaptrade-oauth-apps for a clean rebase onto
current upstream/main ahead of opening a PR.

* fix(snaptrade): address PR #2747 review feedback on OAuth PKCE flow

- Remove unreachable dead-code guard in import_latest_snaptrade_data
- Guard apply_oauth_tokens! against a malformed payload missing access_token
- Wrap token endpoint network errors in ApiError and retry like data calls
- Remove unused Provider::Snaptrade#revoke_token! instance method
- Preserve return_to/accountable_type through the SnapTrade portal callback
  so the account-linking flow no longer drops users back to accounts_path
- Show the real absolute OAuth callback URL in self-hosted setup instructions
- Refresh brakeman.ignore fingerprint for the connect redirect after the
  return_to/accountable_type params were added

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Y8SCCmKX6RphB5E73WSUQQ

* fix(snaptrade): don't retry non-idempotent OAuth/API requests

CodeRabbit flagged that Provider::Snaptrade retried OAuth token
exchanges/refreshes and all API POST/DELETE calls (get_connection_url,
delete_connection) after timeouts/connection failures. If the response
is lost after SnapTrade already consumed a single-use auth code,
rotated the refresh token, or applied a POST/DELETE, replaying the
request either fails with invalid_grant on a token that actually
succeeded, or risks duplicate side effects. Retries are now limited to
GET requests; OAuth token requests and non-GET API calls translate a
network failure straight into an ApiError without replay.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NrrGkgSBEqhjjBmmH1fcXL

* fix(snaptrade): stop querying non-deterministically encrypted token via empty-string compare

CodeRabbit flagged that the syncable scope's where.not(oauth_access_token:
[nil, ""]) re-encrypts "" with a random IV on every query, so the ""
comparison can never match a stored ciphertext and is a silent no-op.
No code path ever persists oauth_access_token as "" (only nil or a real
token via apply_oauth_tokens!), so the exclusion is unnecessary --
narrowed the scope to a plain NULL check, which encryption handles
transparently since nil is never encrypted.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NrrGkgSBEqhjjBmmH1fcXL

---------

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-24 22:45:44 +02:00

168 lines
7.2 KiB
Plaintext

<%# locals: (snaptrade_item:) %>
<%= tag.div id: dom_id(snaptrade_item) do %>
<% unlinked_count = snaptrade_item.unlinked_accounts_count %>
<% snaptrade_registered = snaptrade_item.oauth_configured? %>
<% brokerage_connect_href = snaptrade_registered ? connect_snaptrade_item_path(snaptrade_item) : oauth_authorize_snaptrade_items_path(item_id: snaptrade_item.id) %>
<% brokerage_connect_frame = snaptrade_registered ? "_top" : :drawer %>
<%= render DS::Disclosure.new(variant: :card, open: true) do |disclosure| %>
<% disclosure.with_summary_content do %>
<div class="flex items-center justify-between gap-2">
<div class="flex items-center gap-2">
<%= icon "chevron-right", class: "group-open:rotate-90 motion-safe:transition-transform motion-safe:duration-150" %>
<div class="flex items-center justify-center h-8 w-8 bg-success/10 rounded-full">
<% if snaptrade_item.logo.attached? %>
<%= image_tag snaptrade_item.logo, class: "rounded-full h-full w-full", loading: "lazy" %>
<% else %>
<div class="flex items-center justify-center">
<%= tag.p snaptrade_item.name.first.upcase, class: "text-success text-xs font-medium" %>
</div>
<% end %>
</div>
<div class="pl-1 text-sm">
<div class="flex items-center gap-2">
<%= tag.p snaptrade_item.name, class: "font-medium text-primary" %>
<% if snaptrade_item.scheduled_for_deletion? %>
<p class="text-destructive text-sm animate-pulse"><%= t(".deletion_in_progress") %></p>
<% end %>
</div>
<% if snaptrade_item.snaptrade_accounts.any? %>
<p class="text-xs text-secondary">
<%= snaptrade_item.brokerage_summary %>
</p>
<% end %>
<% if snaptrade_item.syncing? %>
<div class="text-secondary flex items-center gap-1">
<%= icon "loader", size: "sm", class: "animate-spin" %>
<%= tag.span t(".syncing") %>
</div>
<% elsif snaptrade_item.requires_update? %>
<div class="text-warning flex items-center gap-1">
<%= icon "alert-triangle", size: "sm", color: "warning" %>
<%= tag.span t(".requires_update") %>
</div>
<% elsif snaptrade_item.sync_error.present? %>
<div class="text-secondary flex items-center gap-1">
<%= render DS::Tooltip.new(text: snaptrade_item.sync_error, icon: "alert-circle", size: "sm", color: "destructive", as: :span) %>
<%= tag.span t(".error"), class: "text-destructive" %>
</div>
<% else %>
<p class="text-secondary">
<% if snaptrade_item.last_synced_at %>
<%= t(".status", timestamp: time_ago_in_words(snaptrade_item.last_synced_at), summary: snaptrade_item.sync_status_summary) %>
<% else %>
<%= t(".status_never") %>
<% end %>
</p>
<% end %>
</div>
</div>
<% if Current.user&.admin? %>
<div class="flex items-center gap-2">
<% if snaptrade_item.requires_update? || !snaptrade_registered %>
<%= render DS::Link.new(
text: t(".reconnect"),
icon: "link",
variant: "secondary",
href: brokerage_connect_href,
frame: brokerage_connect_frame
) %>
<% else %>
<%= icon(
"refresh-cw",
as_button: true,
href: sync_snaptrade_item_path(snaptrade_item),
disabled: snaptrade_item.syncing?
) %>
<% end %>
<%= render DS::Menu.new do |menu| %>
<% menu.with_item(
variant: "link",
text: t(".connect_brokerage"),
icon: "plus",
href: brokerage_connect_href,
frame: brokerage_connect_frame
) %>
<% if unlinked_count > 0 %>
<% menu.with_item(
variant: "link",
text: t(".setup_accounts_menu"),
icon: "settings",
href: setup_accounts_snaptrade_item_path(snaptrade_item),
frame: :modal
) %>
<% end %>
<% menu.with_item(
variant: "link",
text: t(".manage_connections"),
icon: "cable",
href: settings_providers_path(manage: "1")
) %>
<% menu.with_item(
variant: "button",
text: t(".delete"),
icon: "trash-2",
href: snaptrade_item_path(snaptrade_item),
method: :delete,
confirm: CustomConfirm.for_resource_deletion(snaptrade_item.name, high_severity: true)
) %>
<% end %>
</div>
<% end %>
</div>
<% end %>
<% unless snaptrade_item.scheduled_for_deletion? %>
<div class="space-y-4 mt-4">
<% if snaptrade_item.accounts.any? %>
<%= render "accounts/index/account_groups", accounts: snaptrade_item.accounts %>
<% end %>
<%# Sync summary (collapsible) - using shared ProviderSyncSummary component %>
<% stats = snaptrade_item.syncs.ordered.first&.sync_stats || {} %>
<% activities_pending = snaptrade_item.snaptrade_accounts.any?(&:activities_fetch_pending) %>
<%= render ProviderSyncSummary.new(
stats: stats,
provider_item: snaptrade_item,
institutions_count: snaptrade_item.snaptrade_accounts.map(&:brokerage_name).uniq.compact.size,
activities_pending: activities_pending
) %>
<% if Current.user&.admin? %>
<% if unlinked_count > 0 && snaptrade_item.accounts.empty? %>
<%# No accounts imported yet - show prominent setup prompt %>
<div class="p-4 flex flex-col gap-3 items-center justify-center">
<p class="text-primary font-medium text-sm"><%= t(".setup_needed") %></p>
<p class="text-secondary text-sm"><%= t(".setup_description") %></p>
<%= render DS::Link.new(
text: t(".setup_action"),
icon: "settings",
variant: "primary",
href: setup_accounts_snaptrade_item_path(snaptrade_item),
frame: :modal
) %>
</div>
<% elsif snaptrade_item.snaptrade_accounts.empty? %>
<div class="p-4 flex flex-col gap-3 items-center justify-center">
<p class="text-primary font-medium text-sm"><%= t(".no_accounts_title") %></p>
<p class="text-secondary text-sm"><%= t(".no_accounts_description") %></p>
<%= render DS::Link.new(
text: t(".connect_brokerage"),
icon: "link",
variant: "primary",
href: brokerage_connect_href,
frame: brokerage_connect_frame
) %>
</div>
<% end %>
<% end %>
</div>
<% end %>
<% end %>
<% end %>