BigW Consortium Gitlab

authorizations_controller.rb 1.44 KB
Newer Older
Valery Sizov committed
1
class Oauth::AuthorizationsController < Doorkeeper::AuthorizationsController
2
  before_action :authenticate_resource_owner!
3 4

  layout 'profile'
Valery Sizov committed
5 6 7 8 9

  def new
    if pre_auth.authorizable?
      if skip_authorization? || matching_token?
        auth = authorization.authorize
10
        session.delete(:user_return_to)
Valery Sizov committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
        redirect_to auth.redirect_uri
      else
        render "doorkeeper/authorizations/new"
      end
    else
      render "doorkeeper/authorizations/error"
    end
  end

  # TODO: Handle raise invalid authorization
  def create
    redirect_or_render authorization.authorize
  end

  def destroy
    redirect_or_render authorization.deny
  end

  private

  def matching_token?
Dmitriy Zaporozhets committed
32 33 34
    Doorkeeper::AccessToken.matching_token_for(pre_auth.client,
                                               current_resource_owner.id,
                                               pre_auth.scopes)
Valery Sizov committed
35 36 37 38 39 40 41 42 43 44 45
  end

  def redirect_or_render(auth)
    if auth.redirectable?
      redirect_to auth.redirect_uri
    else
      render json: auth.body, status: auth.status
    end
  end

  def pre_auth
Dmitriy Zaporozhets committed
46 47
    @pre_auth ||=
      Doorkeeper::OAuth::PreAuthorization.new(Doorkeeper.configuration,
Valery Sizov committed
48 49 50 51 52 53 54 55 56
                                              server.client_via_uid,
                                              params)
  end

  def authorization
    @authorization ||= strategy.request
  end

  def strategy
Dmitriy Zaporozhets committed
57
    @strategy ||= server.authorization_request(pre_auth.response_type)
Valery Sizov committed
58 59
  end
end