|
| 1 | +class AuthService |
| 2 | + class DataMissingError < StandardError; end |
| 3 | + |
| 4 | + attr_reader :auth_store, :return_from_one_login_store |
| 5 | + |
| 6 | + def initialize(store) |
| 7 | + @store = store |
| 8 | + @return_from_one_login_store = Store::ReturnFromOneLoginStore.new(store) |
| 9 | + @auth_store = Store::AuthStore.new(store) |
| 10 | + end |
| 11 | + |
| 12 | + delegate :logged_in?, to: :auth_store |
| 13 | + delegate :store_return_params, :form_path_params, to: :return_from_one_login_store |
| 14 | + |
| 15 | + def store_auth_details(auth_hash) |
| 16 | + form_id = @return_from_one_login_store.form_id |
| 17 | + |
| 18 | + raise DataMissingError, "Auth hash is missing on request" if auth_hash.blank? |
| 19 | + |
| 20 | + email = auth_hash.dig("info", "email") |
| 21 | + raise DataMissingError, "Email is missing in OmniAuth auth hash" if email.blank? |
| 22 | + |
| 23 | + token = auth_hash.dig("credentials", "id_token") |
| 24 | + raise DataMissingError, "Token is missing in OmniAuth auth hash" if token.blank? |
| 25 | + |
| 26 | + @auth_store.store_token(token) |
| 27 | + Store::ConfirmationDetailsStore.new(@store, form_id).save_copy_of_answers_email_address(email) |
| 28 | + end |
| 29 | + |
| 30 | + def logout_redirect_uri(post_logout_redirect_uri) |
| 31 | + token = @auth_store.get_token |
| 32 | + logout_request = logout_utility.build_request( |
| 33 | + id_token_hint: token, |
| 34 | + post_logout_redirect_uri: url_without_params(post_logout_redirect_uri), |
| 35 | + ) |
| 36 | + logout_request.redirect_uri |
| 37 | + end |
| 38 | + |
| 39 | + def clear_auth_session |
| 40 | + @auth_store.clear |
| 41 | + end |
| 42 | + |
| 43 | +private |
| 44 | + |
| 45 | + def logout_utility |
| 46 | + @logout_utility ||= OmniAuth::GovukOneLogin::LogoutUtility.new( |
| 47 | + idp_configuration: Rails.application.config.x.one_login.idp_configuration, |
| 48 | + ) |
| 49 | + end |
| 50 | + |
| 51 | + def url_without_params(url) |
| 52 | + url = URI.parse(url) |
| 53 | + url.query = nil |
| 54 | + url.to_s |
| 55 | + end |
| 56 | +end |
0 commit comments