devise-radius-authenticatable 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,126 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
|
3
|
+
module Devise
|
4
|
+
module RadiusAuthenticatable
|
5
|
+
# The Devise::RadiusAuthenticatable::TestHelpers module provides a very simple stub
|
6
|
+
# server through the RadiusServer class. It modifies the Radiustar::Request.new
|
7
|
+
# method to create a request in the stub server that can be used to check that the
|
8
|
+
# proper information is passed to the radius server. It also modifies the
|
9
|
+
# Radiustar::Request#authenticate method to perform authentication against the stub
|
10
|
+
# server.
|
11
|
+
#
|
12
|
+
# The RadiusServer class offers a simple interface for creating users prior to your
|
13
|
+
# tests. The create_radius_user method allows for the creation of a radius user
|
14
|
+
# within the stub server. The radius_server method provides the RadiusServer instance
|
15
|
+
# that test assertions can be performed against.
|
16
|
+
#
|
17
|
+
# The stub server is a singleton class to provide easy access. This means that it
|
18
|
+
# needs to have all state cleared out between tests. The clear_radius_users and
|
19
|
+
# clear_radius_request methods offer an easy way to clear the user and request info
|
20
|
+
# out of the server between tests.
|
21
|
+
module TestHelpers
|
22
|
+
def radius_server
|
23
|
+
RadiusServer.instance
|
24
|
+
end
|
25
|
+
|
26
|
+
def create_radius_user(username, password, attributes = {})
|
27
|
+
RadiusServer.instance.add_user(username, password, attributes)
|
28
|
+
end
|
29
|
+
|
30
|
+
def clear_radius_users
|
31
|
+
RadiusServer.instance.clear_users
|
32
|
+
end
|
33
|
+
|
34
|
+
def clear_radius_request
|
35
|
+
RadiusServer.instance.clear_request
|
36
|
+
end
|
37
|
+
|
38
|
+
# Stub RadiusServer that allows testing of radius authentication without a real
|
39
|
+
# server.
|
40
|
+
class RadiusServer
|
41
|
+
include Singleton
|
42
|
+
|
43
|
+
attr_reader :url, :options
|
44
|
+
|
45
|
+
def initialize
|
46
|
+
clear_users
|
47
|
+
clear_request
|
48
|
+
end
|
49
|
+
|
50
|
+
# Stores the information about the radius request that would have been sent to
|
51
|
+
# the radius server. This information can be queried to determine that the
|
52
|
+
# proper information is being sent.
|
53
|
+
def create_request(url, options)
|
54
|
+
@url = url
|
55
|
+
@options = options
|
56
|
+
end
|
57
|
+
|
58
|
+
# Clear the request information that is stored.
|
59
|
+
def clear_request
|
60
|
+
@url = nil
|
61
|
+
@options = nil
|
62
|
+
end
|
63
|
+
|
64
|
+
# Add a user to the radius server to use for authentication purposes. A couple
|
65
|
+
# of default attributes will be returned in the auth response if no attributes
|
66
|
+
# are supplied when creating the user.
|
67
|
+
def add_user(username, password, attributes = {})
|
68
|
+
@users[username] = {}
|
69
|
+
@users[username][:password] = password
|
70
|
+
if attributes.empty?
|
71
|
+
@users[username][:attributes] = {
|
72
|
+
'User-Name' => username,
|
73
|
+
'Filter-Id' => 60
|
74
|
+
}
|
75
|
+
else
|
76
|
+
@users[username][:attributes] = attributes
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
# Clear the users that have been configured for the radius server.
|
81
|
+
def clear_users
|
82
|
+
@users = {}
|
83
|
+
end
|
84
|
+
|
85
|
+
# Accessor to retrieve the attributes configured for the specified user.
|
86
|
+
def attributes(username)
|
87
|
+
@users[username][:attributes]
|
88
|
+
end
|
89
|
+
|
90
|
+
# Called to perform authentication using the specified username and password. If
|
91
|
+
# the authentication is successful, an Access-Accept is returned along with the
|
92
|
+
# radius attributes configured for the user. If authentication fails, an
|
93
|
+
# Access-Reject is returned.
|
94
|
+
def authenticate(username, password)
|
95
|
+
if @users[username] && @users[username][:password] == password
|
96
|
+
{ :code => 'Access-Accept' }.merge(@users[username][:attributes])
|
97
|
+
else
|
98
|
+
{ :code => 'Access-Reject' }
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
def self.included(mod)
|
104
|
+
Radiustar::Request.class_eval do
|
105
|
+
def initialize(url, options = {})
|
106
|
+
Devise::RadiusAuthenticatable::TestHelpers::RadiusServer.instance.
|
107
|
+
create_request(url, options)
|
108
|
+
end
|
109
|
+
|
110
|
+
def authenticate(username, password, secret)
|
111
|
+
Devise::RadiusAuthenticatable::TestHelpers::RadiusServer.instance.
|
112
|
+
authenticate(username, password)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
if mod.respond_to?(:after)
|
117
|
+
mod.after(:each) do
|
118
|
+
Devise::RadiusAuthenticatable::TestHelpers::RadiusServer.instance.
|
119
|
+
clear_request
|
120
|
+
Devise::RadiusAuthenticatable::TestHelpers::RadiusServer.instance.clear_users
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: devise-radius-authenticatable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-07-
|
12
|
+
date: 2012-07-23 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: devise
|
@@ -225,6 +225,7 @@ files:
|
|
225
225
|
- lib/devise-radius-authenticatable.rb
|
226
226
|
- lib/devise/models/radius_authenticatable.rb
|
227
227
|
- lib/devise/radius_authenticatable.rb
|
228
|
+
- lib/devise/radius_authenticatable/test_helpers.rb
|
228
229
|
- lib/devise/radius_authenticatable/version.rb
|
229
230
|
- lib/devise/strategies/radius_authenticatable.rb
|
230
231
|
- lib/generators/devise_radius_authenticatable/install_generator.rb
|
@@ -294,7 +295,6 @@ files:
|
|
294
295
|
- spec/spec_helper.rb
|
295
296
|
- spec/support/devise_helpers.rb
|
296
297
|
- spec/support/generator_helpers.rb
|
297
|
-
- spec/support/radius_helpers.rb
|
298
298
|
homepage: http://github.com/cbascom/devise-radius-authenticatable
|
299
299
|
licenses: []
|
300
300
|
post_install_message:
|
@@ -386,4 +386,3 @@ test_files:
|
|
386
386
|
- spec/spec_helper.rb
|
387
387
|
- spec/support/devise_helpers.rb
|
388
388
|
- spec/support/generator_helpers.rb
|
389
|
-
- spec/support/radius_helpers.rb
|
@@ -1,84 +0,0 @@
|
|
1
|
-
class Radiustar::Request
|
2
|
-
def initialize(url, options = {})
|
3
|
-
RadiusServer.instance.create_request(url, options)
|
4
|
-
end
|
5
|
-
|
6
|
-
def authenticate(username, password, secret)
|
7
|
-
RadiusServer.instance.authenticate(username, password)
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
class RadiusServer
|
12
|
-
attr_reader :url, :options
|
13
|
-
|
14
|
-
def self.instance
|
15
|
-
@@server ||= new
|
16
|
-
end
|
17
|
-
|
18
|
-
def initialize
|
19
|
-
clear_users
|
20
|
-
clear_request
|
21
|
-
end
|
22
|
-
|
23
|
-
def create_request(url, options)
|
24
|
-
@url = url
|
25
|
-
@options = options
|
26
|
-
end
|
27
|
-
|
28
|
-
def clear_request
|
29
|
-
@url = nil
|
30
|
-
@options = nil
|
31
|
-
end
|
32
|
-
|
33
|
-
def add_user(username, password, attributes = {})
|
34
|
-
@users[username] = {}
|
35
|
-
@users[username][:password] = password
|
36
|
-
if attributes.empty?
|
37
|
-
@users[username][:attributes] = {
|
38
|
-
'User-Name' => username,
|
39
|
-
'Filter-Id' => 60
|
40
|
-
}
|
41
|
-
else
|
42
|
-
@users[username][:attributes] = attributes
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
def clear_users
|
47
|
-
@users = {}
|
48
|
-
end
|
49
|
-
|
50
|
-
def attributes(username)
|
51
|
-
@users[username][:attributes]
|
52
|
-
end
|
53
|
-
|
54
|
-
def authenticate(username, password)
|
55
|
-
if @users[username] && @users[username][:password] == password
|
56
|
-
{ :code => 'Access-Accept' }.merge(@users[username][:attributes])
|
57
|
-
else
|
58
|
-
{ :code => 'Access-Reject' }
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
module RadiusHelpers
|
64
|
-
def radius_server
|
65
|
-
RadiusServer.instance
|
66
|
-
end
|
67
|
-
|
68
|
-
def create_radius_user(username, password, attributes = {})
|
69
|
-
RadiusServer.instance.add_user(username, password, attributes)
|
70
|
-
end
|
71
|
-
|
72
|
-
def clear_radius_users
|
73
|
-
RadiusServer.instance.clear_users
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
RSpec::configure do |c|
|
78
|
-
c.include RadiusHelpers
|
79
|
-
|
80
|
-
c.after(:each) do
|
81
|
-
RadiusServer.instance.clear_request
|
82
|
-
RadiusServer.instance.clear_users
|
83
|
-
end
|
84
|
-
end
|