cinch-authentication 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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 channel[: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> defauts to your current nickname if ommited).
14
+ - !login [<nickname>] <password>: Logging in (note: <nickname> defaults to your current nickname if ommited).
15
+ - !logout: Logout. Nickname defaults to the current nickname.
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,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cinch-authentication
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Paul Brickfeld
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: cinch
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 2.0.3
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 2.0.3
30
+ description: An authentication system with multiple strategies for the Cinch IRC framework.
31
+ email:
32
+ - paulbrickfeld@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - lib/cinch/configuration/authentication.rb
38
+ - lib/cinch/extensions/authentication.rb
39
+ - lib/cinch/plugins/user_list.rb
40
+ - lib/cinch/plugins/user_login.rb
41
+ homepage: https://github.com/britishtea/cinch-authentication
42
+ licenses: []
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubyforge_project: ! '[none]'
61
+ rubygems_version: 1.8.23
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: Authentication for Cinch.
65
+ test_files: []
66
+ has_rdoc: