skip to content

Logins & Permissions

How a Rails app proves who a user or API client is and what they may do: generated sessions, gem sign-in, social login, OAuth tokens and policy objects. Interviewers ask which you would reach for.

part ofRuby on Railsread it →
on this pageshow

explore

questions

page 2 of 2

With Doorkeeper as your OAuth provider, how do you set up a public mobile client so its authorization code flow must use PKCE?

level: seniorimportance: should knowfreq 18%

basics

~10 s

Register the client with confidential: false, run Doorkeeper's doorkeeper:pkce migration so grants can store a code_challenge, enable force_pkce, and narrow pkce_code_challenge_methods to S256; without the columns Doorkeeper never compares the code_verifier.

open as a page

After a Rails app upgrades to OmniAuth 2, its GitHub sign-in link 404s and the replacement POST button fails until omniauth-rails_csrf_protection is added — why?

level: seniorimportance: should knowfreq 40%

basics

~20 s

OmniAuth 2 handles /auth/:provider on POST only, so a GET link falls through to Rails and 404s. Its default CSRF check is rack-protection's, which cannot read Rails' token; omniauth-rails_csrf_protection swaps in a verifier built on Rails' own forgery protection.

open as a page

With Pundit 2.5, how do you point policies at the right user and policy class when an app uses Rails 8's authentication generator and an admin area?

level: seniorimportance: should knowfreq 36%

basics

~10 s

Override pundit_user to return Current.user, since the generator defines no current_user. Pass an array such as [:admin, post] to use Admin::PostPolicy, and call pundit_reset! after impersonating another user, because Pundit memoises its context.

open as a page

A Rails controller uses Pundit's `after_action :verify_authorized`; what does that filter guarantee, and what can still slip through?

level: seniorimportance: should knowfreq 48%

basics

~10 s

verify_authorized only checks that authorize or skip_authorization was called during the action. It runs after the action, so side effects already happened, and it cannot tell whether the right record and predicate were checked.

open as a page

In Rails, when would you hand out record.signed_id(expires_in:, purpose:) instead of a generates_token_for token, and why can't you revoke one early?

level: seniorimportance: should knowfreq 25%

basics

~20 s

signed_id signs only the record's id with an optional expiry and purpose, so it suits short-lived links that need no revocation; with no record state in it, a leaked one works until it expires unless every link's purpose changes.

open as a page

Rails 8 now generates its own authentication — for a stock-room tool that keeps growing, when would you keep the generated code and when adopt Devise?

level: principalimportance: should knowfreq 35%

basics

~20 s

Keep the generated code while needs stay close to sign-in, sign-out and reset, because it is small and visible. Adopt Devise when registration, confirmation, lockout, timeouts or several user types pile up and you would otherwise maintain them all.

open as a page

In a Rails integration test, how do you drive OmniAuth's sign-in and its /auth/failure path without contacting the real provider?

level: middleimportance: nice to knowfreq 30%

basics

~20 s

Set OmniAuth.config.test_mode = true and put an OmniAuth::AuthHash in OmniAuth.config.mock_auth[:github]; posting to /auth/github then redirects straight to the callback with that hash. A Symbol mock such as :invalid_credentials makes the strategy fail and redirect to /auth/failure.

open as a page

What changes when you turn on Doorkeeper's hash_token_secrets in a provider that already has live access and refresh tokens?

level: seniorimportance: nice to knowfreq 12%

basics

~20 s

Doorkeeper then stores a SHA-256 digest of each new access token, refresh token and authorization code; existing plaintext tokens stop matching unless you add fallback: :plain, which upgrades each on its next use, and reuse_access_token is disabled.

open as a page

showing 31–38 of 38