lowauth 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b8b42c570e3ffc13a1765f475059e3ccbfba90e4fb96fbe89b788fea22db29c9
4
+ data.tar.gz: 5ff5860306a3ca2f118f2e867224c913babf89dbd67b099d861fd3b48a679386
5
+ SHA512:
6
+ metadata.gz: 1266a0d3b3cf64142ebda1eacc23588ac2537ecc1a86a9bafe80cc769f65155fc6874f5c2c7ec4a13ac4966e232c92d55b352b03809e1f536ebd39a4c207b6e7
7
+ data.tar.gz: '09a61a921c8be8c044aa968ec9bf4f69e21e2adc138ddd32ea21417b24de581c104feab26f9249b1f2d4983c5c017c8f3c4fcebde25f8d048dfd4306286fb3c8'
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LowAuth
4
+ # "requires :admin" in a node lets only users with that role call its actions, and "requires" any logged in user.
5
+ # Anyone else gets nothing, so the router moves on and responds with a 404, which doesn't say there's a page.
6
+ # Actions that change things, like "receive" and "post", also need the form to come from this site.
7
+ module Guard
8
+ READS = %i[render get query side_effect].freeze
9
+
10
+ def requires(role = nil)
11
+ singleton_class.prepend(guard(role))
12
+ end
13
+
14
+ def self.allowed?(event, action, role)
15
+ return false unless event && LowAuth.role?(LowAuth.current_user(event), role)
16
+
17
+ READS.include?(action) || LowAuth.same_origin?(event)
18
+ end
19
+
20
+ private
21
+
22
+ def guard(role)
23
+ Module.new do
24
+ (Low::Templates::Renderer::ACTIONS + Low::Templates::Renderer::CONTROLLER_ACTIONS).each do |action|
25
+ define_method(action) do |**kwargs|
26
+ return unless defined?(super) && Guard.allowed?(kwargs[:event], action, role)
27
+
28
+ super(**kwargs)
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LowAuth
4
+ # Stops guessing passwords: after 10 failed logins for a key, like a login name or an IP, in 15 minutes, it waits.
5
+ # It's kept in memory, so it's per process and starts again when the server does.
6
+ module LoginLimit
7
+ ATTEMPTS = 10
8
+ WINDOW = 15 * 60
9
+
10
+ @failures = Hash.new { |hash, key| hash[key] = [] }
11
+ @mutex = Mutex.new
12
+
13
+ class << self
14
+ def allowed?(key)
15
+ @mutex.synchronize { recent(key).size < ATTEMPTS }
16
+ end
17
+
18
+ def failed(key)
19
+ @mutex.synchronize { recent(key) << Time.now }
20
+ end
21
+
22
+ def clear(key)
23
+ @mutex.synchronize { @failures.delete(key) }
24
+ end
25
+
26
+ private
27
+
28
+ def recent(key)
29
+ @failures[key].select! { |time| time > Time.now - WINDOW }
30
+ @failures[key]
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LowAuth
4
+ # Logging in and out from a node, which sets or expires the session cookie with the node's response.
5
+ module NodeHelpers
6
+ def current_user(event)
7
+ LowAuth.current_user(event)
8
+ end
9
+
10
+ def login(user, event:)
11
+ headers = event.request.headers
12
+ token = Sessions.create(user_id: user[:id], ip: Array(headers['x-forwarded-for']).first,
13
+ user_agent: Array(headers['user-agent']).first)
14
+ set_cookie(LowAuth.cookie_name, token, max_age: LowAuth.session_days * 86_400, secure: LowAuth.secure_cookies)
15
+ end
16
+
17
+ def logout(event:)
18
+ token = event.cookies[LowAuth.cookie_name]
19
+ Sessions.destroy(token) if token
20
+ set_cookie(LowAuth.cookie_name, '', max_age: 0, secure: LowAuth.secure_cookies)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'argon2'
4
+
5
+ module LowAuth
6
+ # Hashes passwords with Argon2, and logs users in with them. A user with only an old hash from another site, like
7
+ # Drupal's, logs in with LowAuth.legacy_password, which rehashes their password with Argon2.
8
+ module Passwords
9
+ # Verified when there's no such user, so a login takes as long whether or not the user exists.
10
+ DECOY_HASH = Argon2::Password.create(SecureRandom.hex(16))
11
+
12
+ class << self
13
+ def digest(password)
14
+ Argon2::Password.create(password)
15
+ end
16
+
17
+ # The user a login and password belong to, or nil.
18
+ def authenticate(login, password)
19
+ user = find_user(login.to_s.strip)
20
+ return decoy(password) unless user
21
+ return unless LowAuth.can_login.call(user)
22
+ return user if user[:password_hash] && Argon2::Password.verify_password(password, user[:password_hash])
23
+
24
+ upgrade(user, password)
25
+ end
26
+
27
+ private
28
+
29
+ def find_user(login)
30
+ return if login.empty?
31
+
32
+ matches = LowAuth.login_columns.map { |column| { Sequel.function(:lower, column) => login.downcase } }
33
+ LowAuth.users.where(Sequel.|(*matches)).first
34
+ end
35
+
36
+ def upgrade(user, password)
37
+ return if user[:password_hash] || LowAuth.legacy_password.nil?
38
+ return unless LowAuth.legacy_password.call(user, password)
39
+
40
+ LowAuth.users.where(id: user[:id]).update(password_hash: digest(password))
41
+ user
42
+ end
43
+
44
+ def decoy(password)
45
+ Argon2::Password.verify_password(password, DECOY_HASH)
46
+ nil
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'digest'
4
+ require 'securerandom'
5
+
6
+ module LowAuth
7
+ # Sessions in the sessions table. The browser keeps a random token in a cookie and the table keeps only its SHA-256,
8
+ # so a leaked table can't log anyone in.
9
+ module Sessions
10
+ class << self
11
+ # A new session's token, for the cookie.
12
+ def create(user_id:, ip: nil, user_agent: nil)
13
+ token = SecureRandom.urlsafe_base64(32)
14
+ now = Time.now
15
+ table.insert(token_digest: digest(token), user_id:, ip:, user_agent:, created_at: now,
16
+ expires_at: now + (LowAuth.session_days * 86_400))
17
+ token
18
+ end
19
+
20
+ # The user a token's session belongs to, while it hasn't expired.
21
+ def user(token)
22
+ session = table.where(token_digest: digest(token)).where { expires_at > Time.now }.first
23
+ return unless session
24
+
25
+ LowAuth.users.where(id: session[:user_id]).first
26
+ end
27
+
28
+ def destroy(token)
29
+ table.where(token_digest: digest(token)).delete
30
+ end
31
+
32
+ # Logs a user out everywhere, like after they change their password.
33
+ def destroy_all(user_id:)
34
+ table.where(user_id:).delete
35
+ end
36
+
37
+ private
38
+
39
+ def table
40
+ LowAuth.database[:sessions]
41
+ end
42
+
43
+ def digest(token)
44
+ Digest::SHA256.hexdigest(token.to_s)
45
+ end
46
+ end
47
+ end
48
+ end
data/lib/lowauth.rb ADDED
@@ -0,0 +1,87 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'sequel'
4
+ require 'uri'
5
+
6
+ require_relative 'version'
7
+ require_relative 'lowauth/passwords'
8
+ require_relative 'lowauth/sessions'
9
+ require_relative 'lowauth/login_limit'
10
+ require_relative 'lowauth/guard'
11
+ require_relative 'lowauth/node_helpers'
12
+
13
+ # Logins for Raindeer: passwords, sessions in a database table and a cookie, and guards on nodes.
14
+ module LowAuth
15
+ class ConfigError < StandardError; end
16
+
17
+ class << self
18
+ # The Sequel database with the users and sessions tables.
19
+ attr_writer :database
20
+ # The users table, and the columns a user can log in with, like username or email.
21
+ attr_accessor :users_table, :login_columns
22
+ # Checks a password against a user's old hash, from a site the users came from, until they log in and it's rehashed.
23
+ attr_accessor :legacy_password
24
+ # Whether a user may log in at all, like when they aren't blocked.
25
+ attr_accessor :can_login
26
+ # Each role a node can require, and how to tell if a user has it: { admin: ->(user) { user[:role] == 'admin' } }.
27
+ attr_accessor :roles
28
+ attr_accessor :cookie_name, :session_days, :secure_cookies
29
+
30
+ def configure
31
+ yield self
32
+ end
33
+
34
+ def database
35
+ @database || raise(ConfigError, 'Set the database, e.g. LowAuth.database = DB')
36
+ end
37
+
38
+ def users
39
+ database[users_table]
40
+ end
41
+
42
+ # The logged in user of the request an event came from, or nil.
43
+ def current_user(event)
44
+ token = event.cookies[cookie_name]
45
+ return unless token
46
+
47
+ Sessions.user(token)
48
+ end
49
+
50
+ def role?(user, role)
51
+ return !user.nil? if role.nil?
52
+ return false unless user
53
+
54
+ check = roles.fetch(role) { raise ConfigError, "There's no #{role} role in LowAuth.roles" }
55
+ check.call(user)
56
+ end
57
+
58
+ # Whether a form was sent from this site, by its Origin header, which browsers send with a form and sites can't fake.
59
+ def same_origin?(event)
60
+ headers = event.request.headers
61
+ origin = Array(headers['origin']).first || Array(headers['referer']).first
62
+ return false unless origin
63
+
64
+ authority(URI(origin)) == Array(headers['host']).first
65
+ rescue URI::InvalidURIError
66
+ false
67
+ end
68
+
69
+ private
70
+
71
+ # A URI's host, and its port when it isn't the scheme's usual one, as a Host header has them.
72
+ def authority(uri)
73
+ return uri.host if uri.port == uri.default_port
74
+
75
+ "#{uri.host}:#{uri.port}"
76
+ end
77
+ end
78
+
79
+ self.users_table = :users
80
+ self.login_columns = %i[username email]
81
+ self.legacy_password = nil
82
+ self.can_login = ->(_user) { true }
83
+ self.roles = {}
84
+ self.cookie_name = 'session'
85
+ self.session_days = 30
86
+ self.secure_cookies = true
87
+ end
data/lib/version.rb ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LowAuth
4
+ VERSION = '0.1.0'
5
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lowauth
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - maedi
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: argon2
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '2.3'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '2.3'
26
+ - !ruby/object:Gem::Dependency
27
+ name: lownode
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '0.5'
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: 0.5.8
36
+ type: :runtime
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - "~>"
41
+ - !ruby/object:Gem::Version
42
+ version: '0.5'
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: 0.5.8
46
+ - !ruby/object:Gem::Dependency
47
+ name: sequel
48
+ requirement: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - "~>"
51
+ - !ruby/object:Gem::Version
52
+ version: '5.0'
53
+ type: :runtime
54
+ prerelease: false
55
+ version_requirements: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: '5.0'
60
+ description: Passwords, sessions and guards on nodes, for Raindeer and LowNode
61
+ email:
62
+ - maediprichard@gmail.com
63
+ executables: []
64
+ extensions: []
65
+ extra_rdoc_files: []
66
+ files:
67
+ - lib/lowauth.rb
68
+ - lib/lowauth/guard.rb
69
+ - lib/lowauth/login_limit.rb
70
+ - lib/lowauth/node_helpers.rb
71
+ - lib/lowauth/passwords.rb
72
+ - lib/lowauth/sessions.rb
73
+ - lib/version.rb
74
+ homepage: https://github.com/low-rb/lowauth
75
+ licenses:
76
+ - MPL-2.0
77
+ metadata:
78
+ homepage_uri: https://github.com/low-rb/lowauth
79
+ source_code_uri: https://github.com/low-rb/lowauth
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: 3.3.0
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubygems_version: 4.0.20
95
+ specification_version: 4
96
+ summary: Logins for Raindeer
97
+ test_files: []