Two modules add Scute to a controller. They work in Rails and in any class that has a request method returning a Rails or Rack request.
class ApplicationController < ActionController::Base include Scute::Authentication include Scute::Authorization before_action :scute_authenticate! rescue_from Scute::Unauthenticated, with: -> { head :unauthorized } rescue_from Scute::Forbidden, with: ->(e) { render json: { error: e.message }, status: :forbidden }endScute::Authentication#
| Method | Does |
|---|---|
| scute_authenticate!(remote: false) | Verifies the request's token and stores the session. Raises Scute::Unauthenticated with the token's failure reason. remote: true also asks Scute that the session is live (one call per request). |
| scute_session | The verified Scute::Session, or nil. Verifies on first use when scute_authenticate! did not run. |
| scute_signed_in? | true when scute_session is not nil. |
| scute_user_id | scute_session&.user_id. |
| scute_access_token | The token from the request. Override it to read the token elsewhere. |
| scute_client | A Scute::Client for this request that passes scute_client_ip. |
| scute_client_ip | The person's IP address: request.remote_ip in Rails, Rack::Request#ip otherwise. Override it to read the address elsewhere. |
scute_access_token reads, in order:
- X-Authorization (what the Scute SDKs send).
- Authorization: Bearer <token>.
- The browser SDK's cookie sc-access-token__<app id>.
class ApiController < ApplicationController private def scute_access_token = request.headers["X-Session-Token"]endScute::Authorization#
| Method | Does |
|---|---|
| scute_authorize!(action, resource = nil, context: nil, challenge: nil, approval: nil) | Runs authz.check as the signed-in user. Returns the decision on a plain allow, raises Scute::Forbidden otherwise. |
| scute_can?(action, resource = nil, context: nil) | true only for a plain allow. |
| scute_user_id | With Scute::Authentication included, the signed-in user (nil when signed out). Without it, define it yourself or it raises NotImplementedError. |
| scute_client, scute_client_ip | Same as above. |
When nobody is signed in, scute_authorize! raises Scute::Unauthenticated (reason :missing) and scute_can? returns false, both without a call to Scute.
In a session someone started as the user (support access), the check gets impersonated: true and the actor in its context, so permissions marked "not while impersonating" are refused. This wins over the context: you pass.
class InvoicesController < ApplicationController def show @invoice = Invoice.find(params[:id]) @can_refund = scute_can?("refund", "invoice:#{@invoice.id}") end def refund scute_authorize!("refund", "invoice:#{params[:id]}", challenge: params[:challenge]) # ... endendScute::Forbidden#decision is the Scute::Authz::Decision. Check step_up? (see Step-up), needs_approval? (see Access requests) or denied?.
Without Scute::Authentication#
When your app has its own sign-in and stores the Scute user id, include only Scute::Authorization and define scute_user_id:
class InvoicesController < ApplicationController include Scute::Authorization private def scute_user_id = current_user.scute_idendClient IP for every client#
scute_client passes the request's address. To have a client you created at boot pass it too, add the Rack middleware after ActionDispatch::RemoteIp:
config.middleware.use Scute::ClientIP::Middleware