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 =
%W[/#{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.



11
12
13
# File 'app/controllers/forest_liana/authentication_controller.rb', line 11

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

Instance Method Details

#authentication_callbackObject



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

def authentication_callback
  return authentication_exception if params.key?(:error)

  begin
    token = @authentication_service.verify_code_and_generate_token(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

#authentication_exceptionObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/controllers/forest_liana/authentication_controller.rb', line 60

def authentication_exception
  begin
    raise ForestLiana::Errors::AuthenticationOpenIdClientException.new(params[:error], params[:error_description], params[:state])
  rescue => error
    FOREST_REPORTER.report error
    FOREST_LOGGER.error "AuthenticationOpenIdClientException: #{error.error_description}"

    render json: {
      error: error.error,
      error_description: error.error_description,
      state: error.state
    }, status: :unauthorized
  end
end

#get_and_check_rendering_idObject



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/controllers/forest_liana/authentication_controller.rb', line 15

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

#logoutObject



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

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



29
30
31
32
33
34
35
36
37
38
39
# File 'app/controllers/forest_liana/authentication_controller.rb', line 29

def start_authentication
  begin
    rendering_id = get_and_check_rendering_id()
    result = @authentication_service.start_authentication({ '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