Improved device detection.

This commit is contained in:
Martin Edenhofer 2015-08-19 02:46:14 +02:00
parent 1112af9e52
commit c3be77edd4
3 changed files with 197 additions and 6 deletions

View file

@ -174,8 +174,6 @@ class ApplicationController < ActionController::Base
if sso_userdata
session[:persistent] = true
current_user_set(sso_userdata)
return {
auth: true
}

View file

@ -55,16 +55,19 @@ store device for user
}
# generate device name
name = browser[:plattform] || ''
if browser[:name]
if name
name = ''
if browser[:plattform] && browser[:plattform] != 'Other'
name = browser[:plattform]
end
if browser[:name] && browser[:name] != 'Other'
if name && !name.empty?
name += ', '
end
name += browser[:name]
end
# if not identified, use user agent
if name == 'Other, Other'
if !name || name == '' || name == 'Other, Other' || name == 'Other'
name = user_agent
browser[:name] = user_agent
end

View file

@ -0,0 +1,190 @@
require 'test_helper'
class UserDeviceTest < ActiveSupport::TestCase
setup do
# create agent
groups = Group.all
roles = Role.where( name: 'Agent' )
UserInfo.current_user_id = 1
@agent = User.create_or_update(
login: 'user-device-agent@example.com',
firstname: 'UserDevice',
lastname: 'Agent',
email: 'user-device-agent@example.com',
password: 'agentpw',
active: true,
roles: roles,
groups: groups,
)
end
test 'session test' do
# signin with fingerprint A from country A via session -> new device #1
user_device1 = UserDevice.add(
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36',
'91.115.248.231',
@agent.id,
'fingerprint1234',
'session',
)
# signin with fingerprint A from country B via session -> new device #2
user_device2 = UserDevice.add(
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36',
'176.198.137.254',
@agent.id,
'fingerprint1234',
'session',
)
assert_not_equal(user_device1.id, user_device2.id)
# signin with fingerprint B from country A via session -> new device #3
user_device3 = UserDevice.add(
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36',
'91.115.248.231',
@agent.id,
'fingerprintABC',
'session',
)
assert_not_equal(user_device2.id, user_device3.id)
# signin with fingerprint A from country A via session -> new device #1
user_device4 = UserDevice.add(
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36',
'91.115.248.231',
@agent.id,
'fingerprint1234',
'session',
)
assert_equal(user_device1.id, user_device4.id)
# signin with fingerprint A from country B via session -> new device #2
user_device5 = UserDevice.add(
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36',
'176.198.137.254',
@agent.id,
'fingerprint1234',
'session',
)
assert_equal(user_device2.id, user_device5.id)
# signin with fingerprint B from country A via session -> new device #3
user_device6 = UserDevice.add(
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36',
'91.115.248.231',
@agent.id,
'fingerprintABC',
'session',
)
assert_equal(user_device3.id, user_device6.id)
end
test 'session test - user agent (unknown)' do
# known user agent
user_device1 = UserDevice.add(
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36',
'91.115.248.231',
@agent.id,
nil,
'session',
)
assert_equal('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36', user_device1.user_agent)
assert_equal('Mac, Chrome', user_device1.name)
# unknown user agent
user_device2 = UserDevice.add(
'ABC 123',
'91.115.248.231',
@agent.id,
nil,
'session',
)
assert_equal('ABC 123', user_device2.user_agent)
assert_equal('ABC 123', user_device2.browser)
assert_equal('ABC 123', user_device2.name)
# partently known
user_device3 = UserDevice.add(
'Mozilla/5.0 (iPhone; CPU iPhone OS 8_4 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12H143 Safari/600.1.4',
'91.115.248.231',
@agent.id,
nil,
'session',
)
assert_equal('Mozilla/5.0 (iPhone; CPU iPhone OS 8_4 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12H143 Safari/600.1.4', user_device3.user_agent)
assert_equal('iPhone', user_device3.browser)
assert_equal('iPhone', user_device3.name)
end
test 'api test' do
# signin with ua from country A via basic auth -> new device #1
user_device1 = UserDevice.add(
'curl/7.43.0',
'91.115.248.231',
@agent.id,
nil,
'basic_auth',
)
# signin with ua from country B via basic auth -> new device #2
user_device2 = UserDevice.add(
'curl/7.43.0',
'176.198.137.254',
@agent.id,
nil,
'basic_auth',
)
assert_not_equal(user_device1.id, user_device2.id)
# signin with ua from country A via basic auth -> new device #1
user_device3 = UserDevice.add(
'curl/7.43.0',
'91.115.248.231',
@agent.id,
nil,
'basic_auth',
)
assert_equal(user_device1.id, user_device3.id)
# signin with ua from country B via basic auth -> new device #2
user_device4 = UserDevice.add(
'curl/7.43.0',
'176.198.137.254',
@agent.id,
nil,
'basic_auth',
)
assert_equal(user_device2.id, user_device4.id)
# signin with ua from country A via token auth -> new device #1
user_device5 = UserDevice.add(
'curl/7.43.0',
'91.115.248.231',
@agent.id,
nil,
'token_auth',
)
assert_equal(user_device1.id, user_device5.id)
# signin with ua from country B via token auth -> new device #2
user_device6 = UserDevice.add(
'curl/7.43.0',
'176.198.137.254',
@agent.id,
nil,
'token_auth',
)
assert_equal(user_device2.id, user_device6.id)
end
end