diff --git a/.env.sample b/.env.sample index dd1798ca1..de0a417b5 100644 --- a/.env.sample +++ b/.env.sample @@ -1,10 +1,19 @@ AWS_ACCESS_KEY_ID=YOUR-AWS-ACCESS-KEY-ID AWS_SECRET_ACCESS_KEY=YOUR-AWS-SECRET-ACCESS-KEY +AWS_REGION= + +LOGIN_GOV_CLIENT_ID='urn:gov:gsa:openidconnect:sp:myapp' +LOGIN_GOV_IDP_BASE_URL='http://localhost:3000/' +LOGIN_GOV_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----............." +LOGIN_GOV_REDIRECT_URI='http://localhost:3002/users/auth/login_dot_gov/callback' + NEW_RELIC_KEY=YOUR-NEW-RELIC-KEY-HERE + S3_AWS_ACCESS_KEY_ID= S3_AWS_SECRET_ACCESS_KEY= +S3_AWS_BUCKET_NAME= S3_AWS_REGION= S3_AWS_HOST= -S3_AWS_BUCKET_NAME= -TOUCHPOINTS_EMAIL_SENDER=from@example.com + TOUCHPOINTS_GTM_CONTAINER_ID=GTM-your-number +TOUCHPOINTS_EMAIL_SENDER=from@example.com diff --git a/.env.test b/.env.test index 207671790..e5d3f323e 100644 --- a/.env.test +++ b/.env.test @@ -2,6 +2,11 @@ AWS_ACCESS_KEY_ID= AWS_SECRET_ACCESS_KEY= AWS_REGION= +LOGIN_GOV_CLIENT_ID= +LOGIN_GOV_IDP_BASE_URL= +LOGIN_GOV_PRIVATE_KEY= +LOGIN_GOV_REDIRECT_URI= + NEW_RELIC_KEY= S3_AWS_ACCESS_KEY_ID= diff --git a/app/controllers/users/omniauth_callbacks_controller.rb b/app/controllers/users/omniauth_callbacks_controller.rb new file mode 100644 index 000000000..3c6ae1cf4 --- /dev/null +++ b/app/controllers/users/omniauth_callbacks_controller.rb @@ -0,0 +1,33 @@ +class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController + def login_dot_gov + if (auth_hash && auth_hash["info"]["email_verified"]) + @email = auth_hash["info"]["email"] + end + + login + end + + + private + + def auth_hash + request.env["omniauth.auth"] + end + + def login + if @email.present? + @user = User.from_omniauth(auth_hash) + end + + # If user exists + # Else, if valid email and no user, we create an account. + if !@user.errors.present? + sign_in_and_redirect(:user, @user) + set_flash_message(:notice, :success, kind: "Login.gov") + elsif @user.errors.present? + redirect_to root_path, alert: @user.errors.full_messages.join(",") + else + redirect_to root_path, notice: "Error: During oAuth Login" + end + end +end diff --git a/app/models/user.rb b/app/models/user.rb index b4ffba02f..d93eedf79 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -3,9 +3,12 @@ class User < ApplicationRecord # :lockable, and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable, - :trackable, :confirmable, + :trackable, + # :confirmable, :timeoutable + devise :omniauthable, omniauth_providers: [:login_dot_gov] + belongs_to :organization, optional: true has_many :user_services has_many :services, through: :user_services @@ -15,6 +18,23 @@ class User < ApplicationRecord validates :email, presence: true, if: :tld_check + def self.from_omniauth(auth) + # Set login_dot_gov as Provider for legacy TP Devise accounts + # TODO: Remove once all accounts are migrated/have `provider` and `uid` set + @existing_user = User.find_by_email(auth.info.email) + if @existing_user && !@existing_user.provider.present? + @existing_user.provider = auth.provider + @existing_user.uid = auth.uid + @existing_user.save + end + + # For login.gov native accounts + where(provider: auth.provider, uid: auth.uid).first_or_create do |user| + user.email = auth.info.email + user.password = Devise.friendly_token[0,20] + end + end + def tld_check unless APPROVED_DOMAINS.any? { |word| email.end_with?(word) } errors.add(:email, "is not from a valid TLD - .gov and .mil domains only") @@ -44,7 +64,7 @@ def ensure_organization if org = Organization.find_by_domain(address.domain) self.organization_id = org.id else - errors.add(:organization, "#{address.domain} is not a valid organization - Please contact Feedback Analytics Team for assistance") + errors.add(:organization, "'#{address.domain}' has not yet been configured for Touchpoints - Please contact the Feedback Analytics Team for assistance.") end end diff --git a/app/views/components/_header.html.erb b/app/views/components/_header.html.erb index 32832e800..2178de19c 100644 --- a/app/views/components/_header.html.erb +++ b/app/views/components/_header.html.erb @@ -94,14 +94,9 @@