cinch-authentication-hotfix 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: dddb75e97c1f0d5c51c45f3e6ea1c529cfa91220
4
+ data.tar.gz: af15487020dce9c076edb60d65d2928b704e3ce7
5
+ SHA512:
6
+ metadata.gz: 95a31e0f096db0439d2d083c47db35a39cdf29b0427a897ce116c5d57180ea8804c8d3105db0104c5caadad457f9fc21332666274eeb491b7cddb097880102ed
7
+ data.tar.gz: f1d0b93d95557c3a8e913d947f5ebd9a9ddb20cd11cdcba2b124e0d067a2b7ef13ad8e41ecdb5995fa9781e31091e47e3501fce8f0b7af384493074c2ceb24c0
@@ -0,0 +1,14 @@
1
+ module Cinch
2
+ class Configuration
3
+ class Authentication < Configuration
4
+ KnownOptions = [:strategy, :level, :channel, :logged_in, :registration,
5
+ :fetch_user]
6
+
7
+ def self.default_config
8
+ {
9
+ :logged_in => {}
10
+ }
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,154 @@
1
+ require 'cinch/configuration/authentication'
2
+
3
+ module Cinch
4
+ module Extensions
5
+ module Authentication
6
+ module ClassMethods
7
+ # Public: Enables authentication for the full plugin.
8
+ def enable_authentication
9
+ hook :pre, :for => [:match], :method => :authenticated?
10
+ end
11
+ end
12
+
13
+ def self.included(base)
14
+ base.extend ClassMethods
15
+ end
16
+
17
+ # Public: Checks if the user is authorized to run the command.
18
+ #
19
+ # m - The Cinch::Message.
20
+ # levels - The level(s) of authentication Symbol(s) the user must have
21
+ # (default: nil).
22
+ #
23
+ # Examples
24
+ #
25
+ # # The :channel_status strategy
26
+ # authenticated? m, :h # => true for :q, :a, :o and :h
27
+ # authenticated? m, :o # => true for :q, :a and :o
28
+ #
29
+ # # The :user_list and :user_login strategy
30
+ # authenticated? m, [:admins, :users]
31
+ # authenticated? m, :admins
32
+ #
33
+ # Returns a Boolean.
34
+ def authenticated?(m, levels = nil)
35
+ strategy = config[:authentication_strategy] ||
36
+ bot.config.authentication.strategy
37
+ levels = levels || config[:authentication_level] ||
38
+ bot.config.authentication.level
39
+
40
+ case strategy
41
+ when :channel_status then return channel_status_strategy m, levels
42
+ when :list then return list_strategy m, levels
43
+ when :login then return login_strategy m, levels
44
+ end
45
+
46
+ raise StandardError, 'You have not configured an authentication ' +
47
+ 'strategy.'
48
+ end
49
+
50
+ # Internal: Checks if the user is an operator on the channel.
51
+ #
52
+ # m - The Cinch::Message.
53
+ # level - The level Symbol.
54
+ def channel_status_strategy(m, level)
55
+ if config.has_key? :authentication_channel
56
+ channel = Channel config[:authentication_channel]
57
+ elsif bot.config.authentication.channel
58
+ channel = Channel bot.config.authentication.channel
59
+ else
60
+ channel = m.channel
61
+ end
62
+
63
+ if level.nil?
64
+ raise StandardError, "You haven't configured an authentication level."
65
+ end
66
+
67
+ bot.loggers.debug level.inspect
68
+
69
+ user_modes = channel.users[m.user]
70
+ modes = { q: 'founder', a: 'admin', o: 'operator',
71
+ h: 'half-operator', v: 'voice' }
72
+
73
+ modes.keys.take(modes.keys.find_index(level) + 1).each do |mode|
74
+ return true if user_modes.include? mode.to_s
75
+ end
76
+
77
+ m.user.notice "This command requires at least #{modes[level]} status " +
78
+ "on #{channel}."
79
+
80
+ return false
81
+ rescue => e
82
+ m.user.notice 'Something went wrong.'
83
+ raise
84
+ end
85
+
86
+ # Internal: Checks if the user sending the message is on the user list.
87
+ #
88
+ # m - The Cinch::Message.
89
+ # levels - The level Symbol(s).
90
+ #
91
+ # Returns a Boolean.
92
+ def list_strategy(m, levels)
93
+ m.user.refresh
94
+
95
+ unless m.user.authed?
96
+ m.user.notice "This command requires you to be authenticated."
97
+ return false
98
+ end
99
+
100
+ user_list = Array(levels).each_with_object [] do |level, list|
101
+ list.concat(config[level] || bot.config.authentication.send(level))
102
+ end
103
+
104
+ if user_list.nil?
105
+ bot.loggers.debug "You have not configured any user lists."
106
+ end
107
+
108
+ unless user_list.include? m.user.authname
109
+ m.user.notice "You are not authorized to run this command."
110
+ return false
111
+ end
112
+
113
+ return true
114
+ end
115
+
116
+ # Internal: Checks if the user sending the message has logged in.
117
+ #
118
+ # m- The Cinch::Message.
119
+ # levels - The level Symbol(s).
120
+ def login_strategy(m, levels)
121
+ if current_user(m).nil?
122
+ m.user.notice "You are not authorized to run this command. Please " +
123
+ "log in using !login [<username>] <password>."
124
+ return false
125
+ end
126
+
127
+ levels = Array(levels)
128
+
129
+ levels.each do |level|
130
+ level_lambda = config[level] || bot.config.authentication.send(level)
131
+
132
+ return true if level_lambda.call current_user(m)
133
+ end
134
+
135
+ # The previous check will fail if no level is given.
136
+ return true if levels.nil?
137
+
138
+ m.user.notice 'You are not authorized to run this command.'
139
+ return false
140
+ end
141
+
142
+ # Public: Returns the nickname of the currently logged in user.
143
+ #
144
+ # Returns a String.
145
+ # Returns nil when a user is not logged in.
146
+ def current_user(m)
147
+ bot.config.authentication.logged_in[m.user]
148
+ end
149
+ end
150
+ end
151
+ end
152
+
153
+ require 'cinch/plugins/user_login'
154
+ require 'cinch/plugins/user_list'
@@ -0,0 +1,41 @@
1
+ require 'cinch/extensions/authentication'
2
+
3
+ module Cinch
4
+ module Plugins
5
+ class UserList
6
+ include Cinch::Plugin
7
+ include Cinch::Extensions::Authentication
8
+
9
+ set :plugin_name, 'userlists'
10
+ set :help, <<-USAGE.gsub(/^ {6}/, '')
11
+ Allows you to add/delete users from user lists used for authentication.
12
+ Usage:
13
+ - !add_to_<level> <nickname>: Adds a user to the <level> list.
14
+ - !delete_from_<level> <nickname>: Deletes a user from the <level> list.
15
+ - !show_<level>_list: Shows the <level> list.
16
+ USAGE
17
+
18
+ match /add_to_(\S+) (\S+)/s, :method => :add
19
+ match /delete_from_(\S+) (\S)/s, :method => :delete
20
+ match /show_(\S+)_list/s, :method => :show
21
+
22
+ enable_authentication
23
+
24
+ def add(m, level, nickname)
25
+ if bot.config.send(level) << nickname
26
+ m.user.notice "#{nickname} has been added to the #{level} list."
27
+ end
28
+ end
29
+
30
+ def delete(m, level, nickname)
31
+ if bot.config.send(level).delete nickname
32
+ m.user.notice "#{nickname} has been deleted from the #{level} list."
33
+ end
34
+ end
35
+
36
+ def show(m, level)
37
+ m.user.notice bot.config.send(level).join ', '
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,82 @@
1
+ require 'cinch/extensions/authentication'
2
+
3
+ module Cinch
4
+ module Plugins
5
+ class UserLogin
6
+ include Cinch::Plugin
7
+ include Cinch::Extensions::Authentication
8
+
9
+ set :plugin_name, 'account'
10
+ set :help, <<-USAGE.gsub(/^ {6}/, '')
11
+ Allows you to login to run commands that require authorization.
12
+ Usage:
13
+ - !register [<nickname>] <password>: Registration (note: <nickname> defaults to your current nickname if ommited).
14
+ - !login [<nickname>] <password>: Logging in (note: <nickname> defaults to your current nickname if ommited).
15
+ - !logout: Logout.
16
+ USAGE
17
+
18
+ match /register (?:(\S+) )?(\S+)/s, :method => :register
19
+ match /login (?:(\S+) )?(\S+)/s, :method => :login
20
+ match /logout/s, :method => :logout
21
+
22
+ # Admin-only commands. Perhaps in a later version.
23
+ #match /add_to_(\S+) (\S+)/s => :method => :add_to
24
+ #match /remove_from_(\S+) (\S+)/s, :method => :remove_from
25
+
26
+ def register(m, nickname, password)
27
+ registration = config[:registration] ||
28
+ bot.config.authentication.registration
29
+
30
+ if registration.nil?
31
+ raise StandardError, 'You have not configured a registration lambda.'
32
+ elsif registration.call(nickname || m.user.nick, password)
33
+ m.user.notice "You have been registered as #{nickname||m.user.nick}."
34
+ login m, nickname, password
35
+ else
36
+ m.user.notice "An account with nickname #{nickname || m.user.nick} " +
37
+ 'already exists.'
38
+ end
39
+ rescue => e
40
+ m.user.notice 'Something went wrong.'
41
+ raise
42
+ end
43
+
44
+ def login(m, nickname, password)
45
+ fetch_user = config[:fetch_user] || bot.config.authentication.fetch_user
46
+
47
+ if fetch_user.nil?
48
+ raise StandardError, 'You have not configured an fetch_user lambda.'
49
+ end
50
+
51
+ user = fetch_user.call(nickname || m.user.nick)
52
+
53
+ if user.nil?
54
+ m.user.notice 'You have not registered or mistyped your nickname.'
55
+ return
56
+ end
57
+
58
+ unless user.respond_to? :authenticate
59
+ raise StandardError, "Please implement #{user.class}#authenticate."
60
+ end
61
+
62
+ if user.authenticate(password)
63
+ bot.config.authentication.logged_in[m.user] = user
64
+ m.user.notice "You have been logged in as #{nickname || m.user.nick}."
65
+ else
66
+ m.user.notice 'Unknown username/password combination.'
67
+ end
68
+ rescue => e
69
+ m.user.notice 'Something went wrong.'
70
+ raise
71
+ end
72
+
73
+ def logout(m)
74
+ if bot.config.authentication.logged_in.delete m.user
75
+ m.user.notice 'You have been logged out.'
76
+ else
77
+ m.user.notice 'You are not logged in.'
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cinch-authentication-hotfix
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.4
5
+ platform: ruby
6
+ authors:
7
+ - Sirgue
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-04-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: cinch
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ description: An authentication system with multiple strategies for the Cinch IRC framework.
28
+ It's just an hotfix.
29
+ email:
30
+ - poubelle@sirgue.fr
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - lib/cinch/configuration/authentication.rb
36
+ - lib/cinch/extensions/authentication.rb
37
+ - lib/cinch/plugins/user_list.rb
38
+ - lib/cinch/plugins/user_login.rb
39
+ homepage: https://github.com/Sirgue/cinch-authentication
40
+ licenses: []
41
+ metadata: {}
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubyforge_project:
58
+ rubygems_version: 2.6.11
59
+ signing_key:
60
+ specification_version: 4
61
+ summary: Authentication for Cinch.
62
+ test_files: []