Ruby SDK

Rails integration

The Scute::Authentication and Scute::Authorization concerns, the helpers they add and how to override them.

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.

app/controllers/application_controller.rbruby
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 }end

Scute::Authentication#

MethodDoes
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_sessionThe 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_idscute_session&.user_id.
scute_access_tokenThe token from the request. Override it to read the token elsewhere.
scute_clientA Scute::Client for this request that passes scute_client_ip.
scute_client_ipThe 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:

  1. X-Authorization (what the Scute SDKs send).
  2. Authorization: Bearer <token>.
  3. The browser SDK's cookie sc-access-token__<app id>.
class ApiController < ApplicationController  private  def scute_access_token = request.headers["X-Session-Token"]end

Scute::Authorization#

MethodDoes
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_idWith Scute::Authentication included, the signed-in user (nil when signed out). Without it, define it yourself or it raises NotImplementedError.
scute_client, scute_client_ipSame 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])    # ...  endend

Scute::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_idend

Client 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/application.rbruby
config.middleware.use Scute::ClientIP::Middleware

See Rack middleware and client IP.