|
| 1 | +require 'uri' |
| 2 | +require 'json' |
| 3 | + |
| 4 | +module ForestLiana |
| 5 | + class AuthenticationController < ForestLiana::BaseController |
| 6 | + START_AUTHENTICATION_ROUTE = 'authentication' |
| 7 | + CALLBACK_AUTHENTICATION_ROUTE = 'authentication/callback' |
| 8 | + LOGOUT_ROUTE = 'authentication/logout'; |
| 9 | + PUBLIC_ROUTES = [ |
| 10 | + "/#{START_AUTHENTICATION_ROUTE}", |
| 11 | + "/#{CALLBACK_AUTHENTICATION_ROUTE}", |
| 12 | + "/#{LOGOUT_ROUTE}", |
| 13 | + ] |
| 14 | + |
| 15 | + def initialize |
| 16 | + @authentication_service = ForestLiana::Authentication.new() |
| 17 | + end |
| 18 | + |
| 19 | + def get_callback_url |
| 20 | + URI.join(ForestLiana.application_url, "/forest/#{CALLBACK_AUTHENTICATION_ROUTE}").to_s |
| 21 | + rescue => error |
| 22 | + raise "application_url is not valid or not defined" if error.is_a?(ArgumentError) |
| 23 | + end |
| 24 | + |
| 25 | + def get_and_check_rendering_id |
| 26 | + if !params.has_key?('renderingId') |
| 27 | + raise ForestLiana::MESSAGES[:SERVER_TRANSACTION][:MISSING_RENDERING_ID] |
| 28 | + end |
| 29 | + |
| 30 | + rendering_id = params[:renderingId] |
| 31 | + |
| 32 | + if !(rendering_id.instance_of?(String) || rendering_id.instance_of?(Numeric)) || (rendering_id.instance_of?(Numeric) && rendering_id.nan?) |
| 33 | + raise ForestLiana::MESSAGES[:SERVER_TRANSACTION][:INVALID_RENDERING_ID] |
| 34 | + end |
| 35 | + |
| 36 | + return rendering_id.to_i |
| 37 | + end |
| 38 | + |
| 39 | + def start_authentication |
| 40 | + begin |
| 41 | + rendering_id = get_and_check_rendering_id() |
| 42 | + callback_url = get_callback_url() |
| 43 | + |
| 44 | + result = @authentication_service.start_authentication( |
| 45 | + callback_url, |
| 46 | + { 'renderingId' => rendering_id }, |
| 47 | + ) |
| 48 | + |
| 49 | + render json: { authorizationUrl: result['authorization_url']}, status: 200 |
| 50 | + rescue => error |
| 51 | + render json: { errors: [{ status: 500, detail: error.message }] }, |
| 52 | + status: :internal_server_error, serializer: nil |
| 53 | + end |
| 54 | + end |
| 55 | + |
| 56 | + def authentication_callback |
| 57 | + begin |
| 58 | + callback_url = get_callback_url() |
| 59 | + |
| 60 | + token = @authentication_service.verify_code_and_generate_token( |
| 61 | + callback_url, |
| 62 | + params, |
| 63 | + ) |
| 64 | + |
| 65 | + response.set_cookie( |
| 66 | + 'forest_session_token', |
| 67 | + { |
| 68 | + value: token, |
| 69 | + httponly: true, |
| 70 | + secure: true, |
| 71 | + expires: ForestLiana::Token.expiration_in_days, |
| 72 | + samesite: 'none', |
| 73 | + path: '/' |
| 74 | + }, |
| 75 | + ) |
| 76 | + |
| 77 | + response_body = { |
| 78 | + tokenData: JWT.decode(token, ForestLiana.auth_secret, true, { algorithm: 'HS256' })[0] |
| 79 | + } |
| 80 | + |
| 81 | + # The token is sent decoded, because we don't want to share the whole, signed token |
| 82 | + # that is used to authenticate people |
| 83 | + # but the token itself contains interesting values, such as its expiration date |
| 84 | + response_body[:token] = token if !ForestLiana.application_url.start_with?('https://') |
| 85 | + |
| 86 | + render json: response_body, status: 200 |
| 87 | + |
| 88 | + rescue => error |
| 89 | + render json: { errors: [{ status: error.error_code || 500, detail: error.message }] }, |
| 90 | + status: error.status || :internal_server_error, serializer: nil |
| 91 | + end |
| 92 | + end |
| 93 | + |
| 94 | + def logout |
| 95 | + begin |
| 96 | + if cookies.has_key?(:forest_session_token) |
| 97 | + forest_session_token = cookies[:forest_session_token] |
| 98 | + |
| 99 | + if forest_session_token |
| 100 | + response.set_cookie( |
| 101 | + 'forest_session_token', |
| 102 | + { |
| 103 | + value: forest_session_token, |
| 104 | + httponly: true, |
| 105 | + secure: true, |
| 106 | + expires: Time.at(0), |
| 107 | + samesite: 'none', |
| 108 | + path: '/' |
| 109 | + }, |
| 110 | + ) |
| 111 | + end |
| 112 | + end |
| 113 | + |
| 114 | + render json: {}, status: 204 |
| 115 | + rescue => error |
| 116 | + render json: { errors: [{ status: 500, detail: error.message }] }, |
| 117 | + status: :internal_server_error, serializer: nil |
| 118 | + end |
| 119 | + end |
| 120 | + |
| 121 | + end |
| 122 | +end |
0 commit comments