Class: Leash::Provider::AuthorizeController

Inherits:
Leash::ProviderController show all
Defined in:
app/controllers/leash/provider/authorize_controller.rb

Constant Summary collapse

RESPONSE_TYPES =
[ "token", "code" ].freeze

Constants inherited from Leash::ProviderController

Leash::ProviderController::CLIENT_ID_REGEXP

Instance Method Summary collapse

Instance Method Details

#authorizeObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/controllers/leash/provider/authorize_controller.rb', line 12

def authorize
  case @response_type
  when "token"
    if Leash::Provider.reuse_access_tokens == true
      access_token_obj = Leash::Provider::AccessToken.find_by_app_name_and_owner @app_name, current_owner

      if access_token_obj
        access_token = access_token_obj.access_token
      else
        access_token = Leash::Provider::AccessToken.assign! @app_name, current_owner
      end

    else
      access_token = Leash::Provider::AccessToken.assign! @app_name, current_owner
    end

    Rails.logger.info "[Leash::Provider] Authorize ok: response_type=#{@response_type} app_name=#{@app_name} current_owner=#{current_owner.class.name}##{current_owner.id} access_token=#{access_token} request_ip=#{request.remote_ip} request_user_agent=#{request.user_agent}"
    redirect_to params[:redirect_uri] + "#access_token=#{URI.encode(access_token)}"

  when "code"
    auth_code = Leash::Provider::AuthCode.assign! @app_name, current_owner, params[:redirect_uri]

    Rails.logger.info "[Leash::Provider] Authorize ok: response_type=#{@response_type} current_owner=#{current_owner.class.name}##{current_owner.id} auth_code=#{auth_code} request_ip=#{request.remote_ip} request_user_agent=#{request.user_agent}"

    if params.has_key? :state
      redirect_to params[:redirect_uri] + "?code=#{URI.encode(auth_code)}&state=#{URI.encode(params[:state])}" # FIXME ensure that params are joined correctly

    else
      redirect_to params[:redirect_uri] + "?code=#{URI.encode(auth_code)}" # FIXME ensure that params are joined correctly
    end
    
  else
    fail "Should not be reached"
  end
end