Sha256: 8889ac4cdbf827aaeb4fc9814372f7d57bc259b11d8d46d71b2b1f7afbd73602

Contents?: true

Size: 849 Bytes

Versions: 2

Compression:

Stored size: 849 Bytes

Contents

module CruLib
  module AccessTokenProtectedConcern
    extend ActiveSupport::Concern

    included do
      before_action :authenticate_request
    end

    protected

    def authenticate_request
      authenticate_token || render_unauthorized
    end

    def authenticate_token
      token = oauth_access_token_from_header
      return unless oauth_access_token_from_header
      @access_token = AccessToken.read(token)
    end

    # grabs access_token from header if one is present
    def oauth_access_token_from_header
      auth_header = request.env['HTTP_AUTHORIZATION'] || ''
      match = auth_header.match(/^Bearer\s(.*)/)
      return match[1] if match.present?
      false
    end

    def render_unauthorized
      headers['WWW-Authenticate'] = %{CAS realm="Application"}
      render_error('Bad token', status: 401)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cru_lib-0.0.4 lib/cru_lib/access_token_protected_concern.rb
cru_lib-0.0.3 lib/cru_lib/access_token_protected_concern.rb