Skip to content

Commit

Permalink
Remove city
Browse files Browse the repository at this point in the history
  • Loading branch information
dnfd committed Sep 5, 2019
1 parent 67c0bbc commit 5958bff
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 65 deletions.
4 changes: 1 addition & 3 deletions app/models/restriction.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
# frozen_string_literal: true

class Restriction < ApplicationRecord
NET_ADDR_SCOPES = %w[ip ip_subnet]
LOCATION_SCOPES = %w[continent country city]
SCOPES = NET_ADDR_SCOPES | LOCATION_SCOPES
SCOPES = %w[continent country ip ip_subnet]
STATES = %w[enabled disabled]
SUBNET_REGEX = /\A([0-9]{1,3}\.){3}[0-9]{1,3}\/([0-9]|[1-2][0-9]|3[0-2])\z/

Expand Down
11 changes: 4 additions & 7 deletions lib/barong/authorize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,13 @@ def validate_restrictions!
restrictions = Rails.cache.fetch('restrictions', expires_in: 5.minutes) { fetch_restrictions }

request_ip = @request.remote_ip
country = Barong::GeoIP.info(ip: request_ip, key: :country)
continent = Barong::GeoIP.info(ip: request_ip, key: :continent)

restrict! if restrictions['ip'].include?(request_ip)
restrict! if restrictions['ip_subnet'].any? { |r| IPAddr.new(r).include?(request_ip) }

scopes = Restriction::LOCATION_SCOPES
values = Barong::GeoIP.info(request_ip, *scopes)

Hash[scopes.zip(values)].each do |scope, value|
restrict! if value && restrictions[scope].any? { |r| r.casecmp?(value) }
end
restrict! if restrictions['continent'].any? { |r| r.casecmp?(continent) }
restrict! if restrictions['country'].any? { |r| r.casecmp?(country) }
end

def fetch_restrictions
Expand Down
42 changes: 7 additions & 35 deletions lib/barong/geo_ip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,51 +6,23 @@ module GeoIP
class << self
attr_accessor :lang

# Usage: country, city = Barong::GeoIP.info(request_ip, :country, :city)
def info(ip, *keys)
record = reader.get(ip)
keys.map { |key| fetch(record, key) }
end

# Usage: city = Barong::GeoIP.get(ip: ip, key: :city)
def get(ip:, key:)
fetch(reader.get(ip), key)
end

private
def reader
@reader ||= MaxMind::DB.new(Barong::App.config.barong_maxminddb_path, mode: MaxMind::DB::MODE_MEMORY)
end

def fetch(record, key)
def info(ip:, key:)
record = reader.get(ip)
return unless record

case key.to_sym
when :country
return country(record)
return record['country']['names'][lang] if record['country']
when :continent
return continent(record)
when :city
return city(record)
return record['continent']['names'][lang] if record['continent']
end
end

def country(record)
record['country']['names'][lang] if record['country']
end

def continent(record)
record['continent']['names'][lang] if record['continent']
end
private

def city(record)
if record['city']
record['city']['names'][lang]
elsif record['subdivisions']
record['subdivisions'].first['names'][lang]
else
nil
end
def reader
@reader ||= MaxMind::DB.new(Barong::App.config.barong_maxminddb_path, mode: MaxMind::DB::MODE_MEMORY)
end
end
end
Expand Down
18 changes: 0 additions & 18 deletions spec/api/v2/auth/restriction_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,24 +72,6 @@
end

context 'geoip' do
context 'restricts with city' do
let!(:restriction) { create(:restriction, value: 'London', scope: 'city') }

it 'with restricted ip' do
allow_any_instance_of(ActionDispatch::Request).to receive(:remote_ip).and_return(london_ip)
get auth_request
expect(response.status).to eq(401)
expect(response.headers['Authorization']).to be_nil
expect(response.body).to eq("{\"errors\":[\"authz.access_restricted\"]}")
end

it 'with non-restricted ip' do
allow_any_instance_of(ActionDispatch::Request).to receive(:remote_ip).and_return(tokyo_ip)
get auth_request
expect(response.status).to eq(200)
end
end

context 'restricts with country' do
let!(:restriction) { create(:restriction, value: 'japan', scope: 'country') }

Expand Down
2 changes: 0 additions & 2 deletions spec/support/geoip_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ def get(ip)
when '196.245.163.202'
{
'country' => { 'names' => { 'en' => 'United Kingdom' } },
'city' => { 'names' => { 'en' => 'London' } },
'continent' => { 'names' => { 'en' => 'Europe' } }
}
when '140.227.60.114'
{
'country' => { 'names' => { 'en' => 'Japan' } },
'city' => { 'names' => { 'en' => 'Tokyo' } },
'continent' => { 'names' => { 'en' => 'Asia' } }
}
else
Expand Down

0 comments on commit 5958bff

Please sign in to comment.