Sha256: 599f1e4bd3ddbe854a44f660f9ab6b2f5efe362c37f2bdc663182475062f26d4

Contents?: true

Size: 1.42 KB

Versions: 8

Compression:

Stored size: 1.42 KB

Contents

require_dependency "coalescing_panda/application_controller"

module CoalescingPanda
  class Oauth2Controller < ApplicationController

    def oauth2
    end

    def redirect
      if !params[:error] && valid_state_token
        lti_account = LtiAccount.find_by_key(params[:key])
        client_id = lti_account.oauth2_client_id
        client_key = lti_account.oauth2_client_key
        user_id = params[:user_id]
        api_domain = params[:api_domain]
        prefix = [oauth2_protocol, '://', api_domain].join
        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 oauth2_protocol
      ENV['OAUTH_PROTOCOL'] || 'https'
    end

    def valid_state_token
      return false unless params['state'].present? && session['state'].present?
      params['state'] == session['state']
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
coalescing_panda-1.2.2 app/controllers/coalescing_panda/oauth2_controller.rb
coalescing_panda-4.4.1 app/controllers/coalescing_panda/oauth2_controller.rb
coalescing_panda-4.4.0 app/controllers/coalescing_panda/oauth2_controller.rb
coalescing_panda-1.3.0 app/controllers/coalescing_panda/oauth2_controller.rb
coalescing_panda-4.3.2 app/controllers/coalescing_panda/oauth2_controller.rb
coalescing_panda-1.2.1 app/controllers/coalescing_panda/oauth2_controller.rb
coalescing_panda-1.2.0 app/controllers/coalescing_panda/oauth2_controller.rb
coalescing_panda-4.3.0 app/controllers/coalescing_panda/oauth2_controller.rb