Sha256: 1489519d51a349fd1d74a05c63c2f87467cf26258f4faa3ce4151cd96ac05221

Contents?: true

Size: 1.44 KB

Versions: 7

Compression:

Stored size: 1.44 KB

Contents

require_dependency "coalescing_panda/application_controller"

module CoalescingPanda
  class Oauth2Controller < ApplicationController

    def oauth2
    end

    def redirect
      use_secure_headers_override(:allow_inline_scripts)

      if !params[:error] && retrieve_oauth_state
        lti_account = LtiAccount.find_by_key(@oauth_state.data[:key])
        client_id = lti_account.oauth2_client_id
        client_key = lti_account.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

    private

    def retrieve_oauth_state
      @oauth_state ||= params[:state].present? && OauthState.find_by(state_key: params[:state])
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
coalescing_panda-5.3.0 app/controllers/coalescing_panda/oauth2_controller.rb
coalescing_panda-5.2.2 app/controllers/coalescing_panda/oauth2_controller.rb
coalescing_panda-5.2.2.beta1 app/controllers/coalescing_panda/oauth2_controller.rb
coalescing_panda-5.2.1 app/controllers/coalescing_panda/oauth2_controller.rb
coalescing_panda-5.2.0 app/controllers/coalescing_panda/oauth2_controller.rb
coalescing_panda-5.2.0.beta2 app/controllers/coalescing_panda/oauth2_controller.rb
coalescing_panda-5.2.0.beta1 app/controllers/coalescing_panda/oauth2_controller.rb