Class: CoalescingPanda::Oauth2Controller

Inherits:
ApplicationController show all
Defined in:
app/controllers/coalescing_panda/oauth2_controller.rb

Instance Method Summary collapse

Instance Method Details

#oauth2Object



6
7
# File 'app/controllers/coalescing_panda/oauth2_controller.rb', line 6

def oauth2
end

#redirectObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/controllers/coalescing_panda/oauth2_controller.rb', line 9

def redirect
  use_secure_headers_override(:allow_inline_scripts)

  if !params[:error] && retrieve_oauth_state
     = LtiAccount.find_by_key(@oauth_state.data[:key])
    client_id = .oauth2_client_id
    client_key = .oauth2_client_key
    user_id = @oauth_state.data[:user_id]
    api_domain = @oauth_state.data[:api_domain]
    prefix = @oauth_state.data[:api_url]
    @oauth_state.destroy

    Rails.logger.info "Creating Bearcat client for auth token retrieval pointed to: #{prefix}"
    client = Bearcat::Client.new(prefix: prefix)

    token_body = client.retrieve_token(client_id, coalescing_panda.oauth2_redirect_url, client_key, params['code'])
    auth = CanvasApiAuth.where('user_id = ? and api_domain = ?', user_id, api_domain).first_or_initialize
    auth.api_token = token_body['access_token']
    auth.refresh_token = token_body['refresh_token']
    auth.expires_at = Time.now + token_body['expires_in'] if token_body['expires_in']
    auth.user_id = user_id
    auth.api_domain = api_domain
    auth.save!
  end
end