Class: ForestLiana::AuthenticationController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/forest_liana/authentication_controller.rb

Constant Summary collapse

START_AUTHENTICATION_ROUTE =
'authentication'
CALLBACK_AUTHENTICATION_ROUTE =
'authentication/callback'
LOGOUT_ROUTE =
'authentication/logout'
PUBLIC_ROUTES =
[
  "/#{START_AUTHENTICATION_ROUTE}",
  "/#{CALLBACK_AUTHENTICATION_ROUTE}",
  "/#{LOGOUT_ROUTE}",
]

Instance Method Summary collapse

Methods inherited from BaseController

#route_not_found

Constructor Details

#initializeAuthenticationController

Returns a new instance of AuthenticationController.



15
16
17
# File 'app/controllers/forest_liana/authentication_controller.rb', line 15

def initialize
  @authentication_service = ForestLiana::Authentication.new()
end

Instance Method Details

#authentication_callbackObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/controllers/forest_liana/authentication_controller.rb', line 56

def authentication_callback
  begin
    callback_url = get_callback_url()

    token = @authentication_service.verify_code_and_generate_token(
      callback_url,
      params,
    )

    response_body = {
      token: token,
      tokenData: JWT.decode(token, ForestLiana.auth_secret, true, { algorithm: 'HS256' })[0]
    }

    render json: response_body, status: 200

  rescue => error
    render json: { errors: [{ status: error.try(:error_code) || 500, detail: error.try(:message) }] },
      status: error.try(:status) || :internal_server_error, serializer: nil
  end
end

#get_and_check_rendering_idObject



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/controllers/forest_liana/authentication_controller.rb', line 25

def get_and_check_rendering_id
  if !params.has_key?('renderingId')
    raise ForestLiana::MESSAGES[:SERVER_TRANSACTION][:MISSING_RENDERING_ID]
  end

  rendering_id = params[:renderingId]
  
  if !(rendering_id.instance_of?(String) || rendering_id.instance_of?(Numeric)) || (rendering_id.instance_of?(Numeric) && rendering_id.nan?)
    raise ForestLiana::MESSAGES[:SERVER_TRANSACTION][:INVALID_RENDERING_ID]
  end

  return rendering_id.to_i
end

#get_callback_urlObject



19
20
21
22
23
# File 'app/controllers/forest_liana/authentication_controller.rb', line 19

def get_callback_url
  File.join(ForestLiana.application_url, "/forest/#{CALLBACK_AUTHENTICATION_ROUTE}").to_s
rescue => error
  raise "application_url is not valid or not defined" if error.is_a?(ArgumentError)
end

#logoutObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/controllers/forest_liana/authentication_controller.rb', line 78

def logout
  begin
    if cookies.has_key?(:forest_session_token)
      forest_session_token = cookies[:forest_session_token]
      
      if forest_session_token
        response.set_cookie(
          'forest_session_token',
          {
            value: forest_session_token,
            httponly: true,
            secure: true,
            expires: Time.at(0),
            same_site: :None,
            path: '/'
          },
        )
      end
    end

    render json: {}, status: 204
  rescue => error
    render json: { errors: [{ status: 500, detail: error.message }] },
    status: :internal_server_error, serializer: nil
  end
end

#start_authenticationObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/controllers/forest_liana/authentication_controller.rb', line 39

def start_authentication 
  begin
    rendering_id = get_and_check_rendering_id()
    callback_url = get_callback_url()

    result = @authentication_service.start_authentication(
      callback_url,
      { 'renderingId' => rendering_id },
    )

    render json: { authorizationUrl: result['authorization_url']}, status: 200
  rescue => error
    render json: { errors: [{ status: 500, detail: error.message }] },
      status: :internal_server_error, serializer: nil
  end
end