Class: BlueviaController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/bluevia_controller.rb

Overview

This controller handles the two OAuth steps to authenticate an user in BlueVia and authorize an application a specific set of BlueVia APIs on behalf of the user.

Instance Method Summary collapse

Instance Method Details

#codeObject

This action handles the OAuth step2: retrieve the code from BlueVia, ask for a valid access token and forward to the configured Rails action



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/controllers/bluevia_controller.rb', line 51

def code

  token, token_secret = get_token_from_cookie(cookies[:token])
  
  if token.nil? or token.empty? or token_secret.nil? or token_secret.empty?
    raise BlueviaEngine::Error.new("Unable to retrieve the previously saved data")
  end

  logger.debug "Retrieve this token from user cookie: #{token}"
  logger.debug "Retrieve this token_secret from user cookie: #{token_secret}"
  
  # Get the access_token once user has granted the application
  request_token = OAuth::RequestToken.new(oauth_consumer, token, token_secret)

  access_token = request_token.get_access_token(:oauth_verifier => params[:oauth_verifier])

  token, token_secret = access_token.params[:oauth_token], access_token.params[:oauth_token_secret]

  if Rails.application.config.bluevia.forward_action.nil? or !Rails.application.config.bluevia.forward_action.is_a?(String)
    raise BlueviaEngine::InvalidConfig.new("Invalid forward_action value")
  end
      
  url = Rails.application.config.bluevia.forward_action.split("#")

  url.length == 2 or raise BlueviaEngine::InvalidConfig.new("Invalid forward_action value")

  redirect_to ({
    :controller => url.first,
    :action => url.last,
    :token => token,
    :token_secret => token_secret
    })
end

#indexObject

This action handles the OAuth step1: redirect to BlueVia endpoint with the application credentials and the oauth callback uri as parameters



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/controllers/bluevia_controller.rb', line 28

def index

  request_token = oauth_consumer.get_request_token({:oauth_callback => redirect_uri})
  
  # Retrieve a set of token, secret to start the OAuth process
  token = request_token.token
  token_secret = request_token.secret
  
  if token.nil? or token.empty? or token_secret.nil? or token_secret.empty?
    raise BlueviaEngine::UnauthorizedError.new("Unable to get a token and token secret from BlueVia")
  end

  # token and secret must be stored temporally, use a cookie
  cookies[:token] = "#{token}|#{token_secret}"
  logger.debug "Retrieve this token from BlueVia: #{token}"
  logger.debug "Retrieve this token_secret from BlueVia: #{token_secret}"

  url = "https://connect.bluevia.com/authorise/?oauth_token=#{token}"
  redirect_to(url)
end

#redirect_uriObject

Redirect uri to be sent to BlueVia OAuth mechanism as callback uri



93
94
95
# File 'app/controllers/bluevia_controller.rb', line 93

def redirect_uri
  "#{request.protocol}#{request.host_with_port}/bluevia/code"
end

#showObject

Default action to show the valid user credentials



87
88
89
# File 'app/controllers/bluevia_controller.rb', line 87

def show
  @data = {:token => params[:token], :token_secret => params[:token_secret]}
end