From 00942431ee72034b348f2ba2d0f4ac4b1c2af061 Mon Sep 17 00:00:00 2001 From: Jarrett Lusso Date: Tue, 28 Jun 2022 18:06:37 -0400 Subject: [PATCH] Add browser info parsing. --- README.md | 18 ++++++++++++++++++ lib/authtrail.rb | 12 +++++++++++- .../templates/login_activities_migration.rb.tt | 2 ++ test/internal/db/schema.rb | 2 ++ 4 files changed, 33 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7d11162..4bc5655 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,7 @@ A `LoginActivity` record is created every time a user tries to login. You can th - `ip` - IP address - `user_agent` and `referrer` - from browser - `city`, `region`, `country`, `latitude`, and `longitude` - from IP +- `browser` and `platform` - from `user_agent` parsing - `created_at` - time of event ## Features @@ -188,6 +189,23 @@ end Check out [this example](https://github.com/ankane/authtrail/issues/40) +## Browser Info + +AuthTrail uses [Browser](https://github.com/fnando/browser) for parsing +information about the browser from the User-Agent. + +To enable browser info, add this line to your application’s Gemfile: + +```ruby +gem "browser" +``` + +And update `config/initializers/authtrail.rb`: + +```ruby +AuthTrail.browser_info = true +``` + ## Data Retention Delete older data with: diff --git a/lib/authtrail.rb b/lib/authtrail.rb index b5702ed..9579f20 100644 --- a/lib/authtrail.rb +++ b/lib/authtrail.rb @@ -8,9 +8,11 @@ module AuthTrail class << self - attr_accessor :exclude_method, :geocode, :track_method, :identity_method, :job_queue, :transform_method + attr_accessor :exclude_method, :geocode, :track_method, :identity_method, + :job_queue, :transform_method, :browser_info end self.geocode = false + self.browser_info = false self.identity_method = lambda do |request, opts, user| if user user.try(:email) @@ -33,6 +35,14 @@ def self.track(strategy:, scope:, identity:, success:, request:, user: nil, fail referrer: request.referrer } + if AuthTrail.browser_info + browser = Browser.new(request.user_agent) + data.merge!( + browser: browser.name, + platform: browser.platform.name + ) + end + if request.params[:controller] data[:context] = "#{request.params[:controller]}##{request.params[:action]}" end diff --git a/lib/generators/authtrail/templates/login_activities_migration.rb.tt b/lib/generators/authtrail/templates/login_activities_migration.rb.tt index 7ddeef2..481ad29 100644 --- a/lib/generators/authtrail/templates/login_activities_migration.rb.tt +++ b/lib/generators/authtrail/templates/login_activities_migration.rb.tt @@ -16,6 +16,8 @@ class <%= migration_class_name %> < ActiveRecord::Migration<%= migration_version t.string :country t.float :latitude t.float :longitude + t.string :browser + t.string :platform t.datetime :created_at end end diff --git a/test/internal/db/schema.rb b/test/internal/db/schema.rb index 549855e..9c1c26a 100644 --- a/test/internal/db/schema.rb +++ b/test/internal/db/schema.rb @@ -15,6 +15,8 @@ t.string :country t.float :latitude t.float :longitude + t.string :browser + t.string :platform t.datetime :created_at # app-specific fields