Sha256: 056c7dd3d7d0f632eaa34a08003e160a3b587b4b39c4ba78b583a576681ac2de

Contents?: true

Size: 1.11 KB

Versions: 2

Compression:

Stored size: 1.11 KB

Contents

module Skydrive
  class ApplicationController < ActionController::Base
    protected

    # Renders a 401 status code if the current user is not authorized
    def ensure_authenticated_user
      head :unauthorized unless current_user
    end

    # Returns the active user associated with the access token if available
    def current_user
      return @current_user if @current_user
      if current_api_key
        return @current_user = current_api_key.user
      else
        return nil
      end
    end

    # Parses the access token from the header
    def current_api_key
      return @current_api_key if @current_api_key.present?

      bearer = request.headers["HTTP_AUTHORIZATION"]

      bearer ||= params[:access_token]

      # allows our tests to pass
      bearer ||= request.headers["rack.session"].try(:[], 'Authorization')

      if bearer.present?
        token = bearer.split.last
        @current_api_key = ApiKey.where(access_token: token).first
      else
        nil
      end
      @current_api_key
    end

    def skydrive_client
      @skydrive_client ||= current_user.skydrive_client
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
lti_skydrive-1.2.2 app/controllers/skydrive/application_controller.rb
lti_skydrive-1.2.1 app/controllers/skydrive/application_controller.rb