puavo_authentication 0.2.0 → 0.2.1
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.
- data/app/controllers/sessions_controller.rb +8 -14
- data/app/views/sessions/new.html.erb +4 -0
- data/lib/puavo/authentication.rb +234 -67
- data/lib/puavo/organisation.rb +72 -0
- data/lib/puavo_authentication/controllers/helpers.rb +182 -67
- data/rails/init.rb +8 -0
- metadata +9 -7
- data/lib/puavo/authorization.rb +0 -20
@@ -1,26 +1,20 @@
|
|
1
1
|
class SessionsController < ApplicationController
|
2
2
|
layout 'sessions'
|
3
|
-
|
3
|
+
skip_before_filter :require_puavo_authorization, :only => [:new, :create]
|
4
|
+
skip_before_filter :require_login, :only => [:new, :create]
|
4
5
|
|
5
6
|
def new
|
6
7
|
end
|
7
8
|
|
8
9
|
def create
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
#redirect_back_or_default schools_url
|
15
|
-
redirect_back_or_default root_path
|
16
|
-
else
|
17
|
-
flash[:notice] = t('flash.session.failed')
|
18
|
-
render :action => :new
|
19
|
-
end
|
10
|
+
session[:uid] = params[:user][:uid]
|
11
|
+
session[:password_plaintext] = params[:user][:password]
|
12
|
+
session[:login_flash] = t('flash.session.login_successful')
|
13
|
+
redirect_back_or_default root_path
|
20
14
|
end
|
21
15
|
|
22
16
|
def show
|
23
|
-
@user =
|
17
|
+
@user = current_user
|
24
18
|
respond_to do |format|
|
25
19
|
format.json { render :json => @user.to_json(:methods => :managed_schools) }
|
26
20
|
end
|
@@ -29,7 +23,7 @@ class SessionsController < ApplicationController
|
|
29
23
|
def destroy
|
30
24
|
# Remove dn and plaintext password values from session
|
31
25
|
session.delete :password_plaintext
|
32
|
-
session.delete :
|
26
|
+
session.delete :uid
|
33
27
|
flash[:notice] = t('flash.session.logout_successful')
|
34
28
|
redirect_to login_path
|
35
29
|
end
|
data/lib/puavo/authentication.rb
CHANGED
@@ -1,93 +1,260 @@
|
|
1
1
|
module Puavo
|
2
|
-
|
3
|
-
|
4
|
-
|
2
|
+
mattr_accessor :available_languages
|
3
|
+
|
4
|
+
class AuthenticationError < UserError
|
5
|
+
def code
|
6
|
+
"authentication_error"
|
5
7
|
end
|
8
|
+
end
|
9
|
+
|
10
|
+
class AuthenticationFailed < AuthenticationError
|
11
|
+
def code
|
12
|
+
"bad_credentials"
|
13
|
+
end
|
14
|
+
end
|
6
15
|
|
16
|
+
class AuthorizationFailed < AuthenticationError
|
17
|
+
def code
|
18
|
+
"no_permissions"
|
19
|
+
end
|
20
|
+
end
|
7
21
|
|
8
|
-
module ClassMethods
|
9
22
|
|
10
|
-
|
11
|
-
|
12
|
-
|
23
|
+
# For User model
|
24
|
+
module AuthenticationMixin
|
25
|
+
# FIXME Observer?
|
26
|
+
def delete_dn_cache
|
27
|
+
organisation_key = LdapOrganisation.first.cn.to_s
|
28
|
+
Rails.cache.delete Puavo::Authentication.dn_cache_key organisation_key, uid
|
29
|
+
end
|
30
|
+
end
|
13
31
|
|
14
|
-
|
15
|
-
|
32
|
+
class Authentication
|
33
|
+
|
34
|
+
attr_accessor :authenticated, :authorized
|
35
|
+
|
36
|
+
def self.dn_cache_key(organisation_key, uid)
|
37
|
+
"user_dn:#{ organisation_key }:#{ uid }"
|
38
|
+
end
|
39
|
+
|
40
|
+
def initialize
|
41
|
+
@credentials = {}
|
42
|
+
end
|
43
|
+
|
44
|
+
[:dn, :organisation_key, :scope].each do |attr|
|
45
|
+
define_method attr do
|
46
|
+
@credentials[attr]
|
16
47
|
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def puavo_configuration
|
51
|
+
ActiveLdap::Base.ensure_configuration
|
52
|
+
end
|
53
|
+
|
54
|
+
def base
|
55
|
+
return current_organisation.ldap_base
|
56
|
+
end
|
17
57
|
|
18
|
-
|
19
|
-
|
20
|
-
|
58
|
+
def ldap_host
|
59
|
+
@credentials[:ldap_host] || puavo_configuration["host"]
|
60
|
+
end
|
21
61
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
62
|
+
def self.remove_connection
|
63
|
+
ActiveLdap::Base.active_connections.keys.each do |connection_name|
|
64
|
+
ActiveLdap::Base.remove_connection(connection_name)
|
65
|
+
end
|
66
|
+
end
|
27
67
|
|
28
|
-
|
29
|
-
# hood with Puavo credentials.
|
30
|
-
user = self.find(:first, :attribute => "uid", :value => login)
|
68
|
+
def configure_ldap_connection(credentials)
|
31
69
|
|
32
|
-
|
33
|
-
self.remove_connection
|
70
|
+
@credentials = credentials
|
34
71
|
|
35
|
-
|
36
|
-
|
37
|
-
|
72
|
+
if current_organisation.nil?
|
73
|
+
raise Puavo::AuthenticationError, "Bad organisation"
|
74
|
+
end
|
38
75
|
|
39
|
-
|
76
|
+
if uid = @credentials[:uid]
|
77
|
+
if uid.nil? || uid.empty?
|
78
|
+
raise AuthenticationFailed, "Cannot get dn from empty or nil uid"
|
40
79
|
end
|
41
80
|
|
42
|
-
if
|
43
|
-
|
44
|
-
|
81
|
+
if uid.match(/^service\//)
|
82
|
+
uid = uid.match(/^service\/(.*)/)[1]
|
83
|
+
user_class = ExternalService
|
84
|
+
else
|
85
|
+
user_class = User
|
45
86
|
end
|
46
87
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
LdapBase.
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
begin
|
63
|
-
admin_permissions = School.search(
|
64
|
-
:filter => "(puavoSchoolAdmin=#{user_dn})",
|
65
|
-
:scope => :one, :attributes => ["puavoId"],
|
66
|
-
:limit => 1 )
|
67
|
-
rescue ActiveLdap::AuthenticationError
|
68
|
-
logger.info "Login failed for #{ login } (#{ user_dn }): Bad password"
|
69
|
-
return false
|
88
|
+
user_dn = Rails.cache.fetch self.class.dn_cache_key(organisation_key, uid) do
|
89
|
+
# Remove previous connection
|
90
|
+
self.class.remove_connection
|
91
|
+
LdapBase.ldap_setup_connection( ldap_host,
|
92
|
+
base.to_s,
|
93
|
+
puavo_configuration["bind_dn"],
|
94
|
+
puavo_configuration["password"] )
|
95
|
+
|
96
|
+
user = user_class.find(:first, :attribute => "uid", :value => uid)
|
97
|
+
|
98
|
+
if user
|
99
|
+
user.dn.to_s
|
100
|
+
else
|
101
|
+
nil
|
102
|
+
end
|
70
103
|
end
|
104
|
+
|
105
|
+
raise AuthenticationFailed, "Cannot get dn for UID '#{ uid }'" if not user_dn
|
106
|
+
logger.debug "Found #{ dn } for #{ uid }"
|
107
|
+
@credentials[:dn] = ActiveLdap::DistinguishedName.parse user_dn
|
108
|
+
end
|
71
109
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
110
|
+
# Reset attributes on new configuration
|
111
|
+
@current_user = nil
|
112
|
+
@authenticated = false
|
113
|
+
@authorized = false
|
76
114
|
|
77
|
-
|
78
|
-
|
79
|
-
if organisation && organisation.owner.include?(user_dn)
|
80
|
-
return user_dn
|
81
|
-
end
|
115
|
+
# Remove previous connection
|
116
|
+
self.class.remove_connection
|
82
117
|
|
83
|
-
# Allow authentication always if logged in user an external service
|
84
|
-
if user_dn.rdns[1]["ou"] == "System Accounts"
|
85
|
-
return user_dn
|
86
|
-
end
|
87
118
|
|
88
|
-
|
89
|
-
|
119
|
+
|
120
|
+
logger.info "Configuring ActiveLdap to use #{ @credentials.map { |k,v| "#{ k }: #{ v }" }.join ", " }"
|
121
|
+
logger.debug "PW: #{ @credentials[:password] }" if ENV["LOG_LDAP_PASSWORD"]
|
122
|
+
# Setup new ActiveLdap connections to use user's credentials
|
123
|
+
LdapBase.ldap_setup_connection ldap_host, base.to_s, @credentials[:dn], @credentials[:password]
|
124
|
+
|
125
|
+
# Do not never ever allow anonymous connections in Puavo. Should be
|
126
|
+
# false in config/ldap.yml, but we just make sure here.
|
127
|
+
LdapBase.connection.instance_variable_set :@allow_anonymous, false
|
128
|
+
|
129
|
+
end
|
130
|
+
|
131
|
+
# Test dn&password bind to LDAP without actually configuring ActiveLdap to
|
132
|
+
# use them
|
133
|
+
def test_bind(dn, password)
|
134
|
+
ldap = Net::LDAP.new(
|
135
|
+
:host => ldap_host,
|
136
|
+
:port => 389,
|
137
|
+
:encryption => {
|
138
|
+
:method => :start_tls
|
139
|
+
},
|
140
|
+
:auth => {
|
141
|
+
:method => :simple,
|
142
|
+
:username => dn.to_s,
|
143
|
+
:password => password
|
144
|
+
})
|
145
|
+
|
146
|
+
if not ldap.bind
|
147
|
+
raise AuthenticationFailed, "Test bind failed: Bad dn or password"
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
# Authenticate configured connection to LDAP.
|
152
|
+
#
|
153
|
+
# Raises AuthenticationFailed if connection could not be made.
|
154
|
+
# Returns possible admin permissions on successful connect
|
155
|
+
def authenticate
|
156
|
+
|
157
|
+
# This is the first time when LDAP connection is used with the user's
|
158
|
+
# credentials. So this search call will initialize the connection and
|
159
|
+
# will raise ActiveLdap::AuthenticationError if user supplied a
|
160
|
+
# bad password.
|
161
|
+
begin
|
162
|
+
|
163
|
+
@admin_permissions = School.search(
|
164
|
+
:filter => "(puavoSchoolAdmin=#{ dn })",
|
165
|
+
:scope => :one, :attributes => ["puavoId"],
|
166
|
+
:limit => 1 )
|
167
|
+
|
168
|
+
AccessToken.validate @credentials if oauth_access_token?
|
169
|
+
|
170
|
+
rescue ActiveLdap::AuthenticationError
|
171
|
+
raise AuthenticationFailed, "Bad dn or password"
|
172
|
+
rescue AccessToken::Expired
|
173
|
+
raise AuthenticationFailed, "OAuth Access Token expired"
|
174
|
+
end
|
175
|
+
|
176
|
+
|
177
|
+
@authenticated = true
|
178
|
+
|
179
|
+
end
|
180
|
+
|
181
|
+
def external_service?
|
182
|
+
dn.rdns[1]["ou"] == "System Accounts"
|
183
|
+
end
|
184
|
+
|
185
|
+
def oauth_client_server?
|
186
|
+
dn.rdns.first.keys.first == "puavoOAuthClientId"
|
187
|
+
end
|
188
|
+
|
189
|
+
def oauth_access_token?
|
190
|
+
dn.rdns.first.keys.first == "puavoOAuthTokenId"
|
191
|
+
end
|
192
|
+
|
193
|
+
# User is authenticated with real password
|
194
|
+
def user_password?
|
195
|
+
return false if oauth_access_token?
|
196
|
+
current_user.classes.include? "puavoEduPerson"
|
197
|
+
end
|
198
|
+
|
199
|
+
# Authorize that user has permissions to use Puavo
|
200
|
+
def authorize
|
201
|
+
|
202
|
+
raise AuthorizationFailed, "Cannot authorize before authenticating" unless @authenticated
|
203
|
+
|
204
|
+
# Authorize school admins
|
205
|
+
if not @admin_permissions.empty?
|
206
|
+
logger.info "Authorization ok: Admin #{ dn }"
|
207
|
+
return @authorized = true
|
90
208
|
end
|
209
|
+
|
210
|
+
# Authorize External Services
|
211
|
+
if external_service?
|
212
|
+
logger.info "Authorization ok: External Service #{ dn }"
|
213
|
+
return @authorized = true
|
214
|
+
end
|
215
|
+
|
216
|
+
# Authorize OAuth Access Tokens
|
217
|
+
if oauth_access_token?
|
218
|
+
return @authorized = true
|
219
|
+
end
|
220
|
+
|
221
|
+
# Authorize organisation owners
|
222
|
+
organisation = LdapOrganisation.first
|
223
|
+
if organisation && organisation.owner && organisation.owner.include?(dn)
|
224
|
+
logger.info "Authorization ok: Organisation owner #{ dn }"
|
225
|
+
return @authorized = true
|
226
|
+
end
|
227
|
+
|
228
|
+
raise AuthorizationFailed, "Unauthorized access for #{ dn }"
|
229
|
+
end
|
230
|
+
|
231
|
+
def current_user
|
232
|
+
|
233
|
+
raise "Cannot get current user before authentication" if not @authenticated
|
234
|
+
|
235
|
+
return @current_user if @current_user
|
236
|
+
|
237
|
+
|
238
|
+
if external_service?
|
239
|
+
@current_user = ExternalService.find dn
|
240
|
+
elsif oauth_access_token?
|
241
|
+
access_token = AccessToken.find dn
|
242
|
+
@current_user = User.find access_token.puavoOAuthEduPerson
|
243
|
+
else
|
244
|
+
@current_user = User.find dn
|
245
|
+
end
|
246
|
+
|
247
|
+
raise "Failed get User object for #{ dn }" if @current_user.nil?
|
248
|
+
return @current_user
|
91
249
|
end
|
250
|
+
|
251
|
+
def current_organisation
|
252
|
+
Puavo::Organisation.find organisation_key
|
253
|
+
end
|
254
|
+
|
255
|
+
def logger
|
256
|
+
RAILS_DEFAULT_LOGGER
|
257
|
+
end
|
258
|
+
|
92
259
|
end
|
93
260
|
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
module Puavo
|
2
|
+
mattr_accessor :available_languages
|
3
|
+
|
4
|
+
class Organisation
|
5
|
+
@@configurations = YAML.load_file("#{RAILS_ROOT}/config/organisations.yml")
|
6
|
+
@@key_by_host = {}
|
7
|
+
|
8
|
+
@@configurations.each do |key, value|
|
9
|
+
@@key_by_host[ value["host"] ] = key
|
10
|
+
end
|
11
|
+
|
12
|
+
cattr_accessor :configurations, :key_by_host
|
13
|
+
attr_accessor :organisation_key
|
14
|
+
|
15
|
+
|
16
|
+
def locale
|
17
|
+
@@configurations[organisation_key]["locale"] || :en
|
18
|
+
end
|
19
|
+
|
20
|
+
def schools(user)
|
21
|
+
School.all_with_permissions user
|
22
|
+
end
|
23
|
+
|
24
|
+
def value_by_key(key)
|
25
|
+
@@configurations[organisation_key][key]
|
26
|
+
end
|
27
|
+
|
28
|
+
def method_missing(method, *args)
|
29
|
+
if @@configurations[organisation_key].has_key?(method.to_s)
|
30
|
+
@@configurations[organisation_key][method.to_s]
|
31
|
+
else
|
32
|
+
super
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
class << self
|
37
|
+
def find(key)
|
38
|
+
if self.configurations.has_key?(key)
|
39
|
+
organisation = Organisation.new
|
40
|
+
organisation.organisation_key = key
|
41
|
+
organisation
|
42
|
+
else
|
43
|
+
logger.info "Can not find configuration key: #{key}"
|
44
|
+
false
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def key_by_host(host)
|
49
|
+
@@key_by_host[host]
|
50
|
+
end
|
51
|
+
|
52
|
+
def find_by_host(host)
|
53
|
+
if @@key_by_host.has_key?(host)
|
54
|
+
organisation = Organisation.new
|
55
|
+
organisation.organisation_key = @@key_by_host[host]
|
56
|
+
organisation
|
57
|
+
else
|
58
|
+
logger.info "Can not find organisation by host: #{host}"
|
59
|
+
false
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def all
|
64
|
+
@@configurations
|
65
|
+
end
|
66
|
+
|
67
|
+
def logger
|
68
|
+
RAILS_DEFAULT_LOGGER
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -1,52 +1,166 @@
|
|
1
1
|
module PuavoAuthentication
|
2
2
|
module Controllers
|
3
3
|
module Helpers
|
4
|
+
|
5
|
+
attr_accessor :authentication
|
6
|
+
|
4
7
|
def current_user
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
else
|
9
|
-
begin
|
10
|
-
return @current_user = User.find(session[:dn]) # REST/OAuth?
|
11
|
-
rescue
|
12
|
-
logger.info "Session's user not found! User is removed from ldap server."
|
13
|
-
logger.info "session[:dn]: #{session[:dn]}"
|
14
|
-
# Delete ldap connection informations from session.
|
15
|
-
session.delete :password_plaintext
|
16
|
-
session.delete :dn
|
17
|
-
end
|
18
|
-
end
|
8
|
+
|
9
|
+
if @authentication.nil?
|
10
|
+
raise "Cannot call 'current_user' before 'setup_authentication'"
|
19
11
|
end
|
20
|
-
|
12
|
+
|
13
|
+
@authentication.current_user
|
14
|
+
|
21
15
|
end
|
22
16
|
|
23
|
-
def
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
17
|
+
def current_organisation
|
18
|
+
if @authentication.nil?
|
19
|
+
raise "Cannot call 'current_organisation' before 'setup_authentication'"
|
20
|
+
end
|
21
|
+
|
22
|
+
@authentication.current_organisation
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
# Returns user dn/uid and password for some available login mean
|
28
|
+
def acquire_credentials
|
29
|
+
|
30
|
+
# OAuth Access Token
|
31
|
+
if auth_header = request.headers["HTTP_AUTHORIZATION"]
|
32
|
+
type, data = auth_header.split
|
33
|
+
if type.downcase == "bearer"
|
34
|
+
return AccessToken.decrypt_token data
|
35
35
|
end
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
36
|
+
end
|
37
|
+
|
38
|
+
# Basic Auth
|
39
|
+
# * OAuth Client Server ID & Secrect
|
40
|
+
# * External Service UID & password
|
41
|
+
# * User UID & password
|
42
|
+
authenticate_with_http_basic do |username, password|
|
43
|
+
logger.debug "Using basic authentication with #{ username }"
|
44
|
+
|
45
|
+
# FIXME: move to Puavo::Authentication class (configure_ldap_connection)
|
46
|
+
if match = username.match(/^oauth_client_id\/(.*)\/(.*)$/)
|
47
|
+
|
48
|
+
org_key = match[1]
|
49
|
+
oauth_client_id = match[2]
|
50
|
+
|
51
|
+
@authentication.configure_ldap_connection(
|
52
|
+
:organisation_key => org_key
|
53
|
+
)
|
54
|
+
|
55
|
+
oauth_client_server = OauthClient.find(:first,
|
56
|
+
:attribute => "puavoOAuthClientId",
|
57
|
+
:value => oauth_client_id)
|
58
|
+
|
59
|
+
return {
|
60
|
+
:dn => oauth_client_server.dn,
|
61
|
+
:organisation_key => org_key,
|
62
|
+
:password => password,
|
63
|
+
:scope => oauth_client_server.puavoOAuthScope
|
64
|
+
}
|
65
|
+
|
42
66
|
end
|
67
|
+
|
68
|
+
return {
|
69
|
+
:uid => username,
|
70
|
+
:organisation_key => organisation_key_from_host,
|
71
|
+
:password => password
|
72
|
+
}
|
73
|
+
end
|
74
|
+
|
75
|
+
# Puavo Session (User UID & password)
|
76
|
+
if uid = session[:uid]
|
77
|
+
logger.debug "Using session authentication with #{ uid }"
|
78
|
+
return {
|
79
|
+
:uid => uid,
|
80
|
+
:organisation_key => organisation_key_from_host,
|
81
|
+
:password => session[:password_plaintext]
|
82
|
+
}
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
# Before filter
|
88
|
+
# Setup authentication object with default credentials from
|
89
|
+
# config/ldap.yml
|
90
|
+
def setup_authentication
|
91
|
+
|
92
|
+
@authentication = Puavo::Authentication.new
|
93
|
+
|
94
|
+
end
|
95
|
+
|
96
|
+
|
97
|
+
def perform_login(credentials)
|
98
|
+
|
99
|
+
if credentials.nil?
|
100
|
+
raise Puavo::AuthenticationFailed, "No credentials supplied"
|
101
|
+
end
|
102
|
+
|
103
|
+
# Configure ActiveLdap to use the credentials
|
104
|
+
@authentication.configure_ldap_connection credentials
|
105
|
+
|
106
|
+
# Authenticate above credentials
|
107
|
+
@authentication.authenticate
|
108
|
+
|
109
|
+
# Set locale from user's organisation
|
110
|
+
I18n.locale = current_organisation.locale
|
111
|
+
|
112
|
+
return true
|
113
|
+
end
|
114
|
+
|
115
|
+
# Before filter
|
116
|
+
# Require user login credentials
|
117
|
+
def require_login
|
118
|
+
|
119
|
+
begin
|
120
|
+
perform_login(acquire_credentials)
|
121
|
+
rescue Puavo::AuthenticationError => e
|
122
|
+
logger.info "Login failed for: #{ e }"
|
123
|
+
show_authentication_error e.code, t('flash.session.failed')
|
124
|
+
return false
|
125
|
+
end
|
126
|
+
|
127
|
+
if session[:login_flash]
|
128
|
+
flash[:notice] = session[:login_flash]
|
129
|
+
session.delete :login_flash
|
130
|
+
end
|
131
|
+
|
132
|
+
return true
|
133
|
+
end
|
134
|
+
|
135
|
+
# Before filter
|
136
|
+
# Require Puavo access rights
|
137
|
+
def require_puavo_authorization
|
138
|
+
|
139
|
+
# Unauthorized always when not authenticated
|
140
|
+
return false unless @authentication
|
141
|
+
|
142
|
+
begin
|
143
|
+
@authentication.authorize
|
144
|
+
rescue Puavo::AuthorizationFailed => e
|
145
|
+
logger.info "Authorization failed: #{ e }"
|
146
|
+
show_authentication_error "unauthorized", t('flash.session.failed')
|
147
|
+
return false
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
def show_authentication_error(code, message)
|
152
|
+
session.delete :password_plaintext
|
153
|
+
session.delete :uid
|
154
|
+
if request.format == Mime::JSON
|
155
|
+
render(:json => {
|
156
|
+
:error => code,
|
157
|
+
:message => message,
|
158
|
+
}.to_json,
|
159
|
+
:status => 401)
|
43
160
|
else
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
redirect_to login_path
|
48
|
-
return false
|
49
|
-
end
|
161
|
+
store_location
|
162
|
+
flash[:notice] = message
|
163
|
+
redirect_to login_path
|
50
164
|
end
|
51
165
|
end
|
52
166
|
|
@@ -59,43 +173,44 @@ module PuavoAuthentication
|
|
59
173
|
session[:return_to] = nil
|
60
174
|
end
|
61
175
|
|
62
|
-
def
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
unless session[:organisation].nil?
|
67
|
-
host = session[:organisation].ldap_host
|
68
|
-
base = session[:organisation].ldap_base
|
69
|
-
end
|
70
|
-
if session[:dn]
|
71
|
-
dn = session[:dn]
|
72
|
-
password = session[:password_plaintext]
|
73
|
-
logger.debug "Using user's credentials for LDAP connection"
|
74
|
-
else
|
75
|
-
logger.debug "Using Puavo credentials for LDAP connection"
|
76
|
-
dn = default_ldap_configuration["bind_dn"]
|
77
|
-
password = default_ldap_configuration["password"]
|
176
|
+
def organisation_key_from_host(host=nil)
|
177
|
+
organisation_key = Puavo::Organisation.key_by_host(request.host)
|
178
|
+
unless organisation_key
|
179
|
+
organisation_key = Puavo::Organisation.key_by_host("*")
|
78
180
|
end
|
79
|
-
|
80
|
-
logger.debug "host: #{host}"
|
81
|
-
logger.debug "base: #{base}"
|
82
|
-
logger.debug "dn: #{dn}"
|
83
|
-
LdapBase.ldap_setup_connection(host, base, dn, password)
|
181
|
+
return organisation_key
|
84
182
|
end
|
85
183
|
|
86
|
-
|
87
|
-
|
88
|
-
|
184
|
+
|
185
|
+
def set_organisation_to_session
|
186
|
+
session[:organisation] = current_organisation if current_organisation
|
187
|
+
end
|
188
|
+
|
189
|
+
def set_initial_locale
|
190
|
+
# Default to English
|
191
|
+
I18n.locale = "en"
|
192
|
+
|
193
|
+
# TODO: set from user agent
|
194
|
+
|
195
|
+
# Set from hostname if it is a known organisation
|
196
|
+
if organisation = Puavo::Organisation.find_by_host(request.host)
|
197
|
+
I18n.locale = organisation.locale
|
89
198
|
end
|
199
|
+
|
90
200
|
end
|
91
201
|
|
92
|
-
def
|
93
|
-
Puavo::
|
202
|
+
def remove_ldap_connection
|
203
|
+
Puavo::Authentication.remove_connection
|
94
204
|
end
|
95
205
|
|
96
|
-
def
|
97
|
-
|
206
|
+
def theme
|
207
|
+
if current_organisation
|
208
|
+
theme = current_organisation.value_by_key('theme')
|
209
|
+
end
|
210
|
+
|
211
|
+
return theme || "breathe"
|
98
212
|
end
|
213
|
+
|
99
214
|
end
|
100
215
|
end
|
101
216
|
end
|
data/rails/init.rb
CHANGED
@@ -7,7 +7,15 @@ end
|
|
7
7
|
|
8
8
|
require 'puavo/authentication'
|
9
9
|
require 'puavo/connection'
|
10
|
+
require 'puavo/organisation'
|
10
11
|
|
11
12
|
require 'puavo_authentication/controllers/helpers'
|
12
13
|
|
13
14
|
ActionController::Base.send :include, PuavoAuthentication::Controllers::Helpers
|
15
|
+
|
16
|
+
begin
|
17
|
+
Puavo::OAUTH_CONFIG = YAML.load_file("#{ RAILS_ROOT }/config/oauth.yml")
|
18
|
+
rescue Errno::ENOENT => e
|
19
|
+
Puavo::OAUTH_CONFIG = nil
|
20
|
+
puts "WARNING: " + e.to_s
|
21
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: puavo_authentication
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
5
|
-
prerelease:
|
4
|
+
hash: 21
|
5
|
+
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 2
|
9
|
-
-
|
10
|
-
version: 0.2.
|
9
|
+
- 1
|
10
|
+
version: 0.2.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Jouni Korhonen
|
@@ -15,7 +15,8 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-
|
18
|
+
date: 2012-10-16 00:00:00 +02:00
|
19
|
+
default_executable:
|
19
20
|
dependencies: []
|
20
21
|
|
21
22
|
description: Authentication solution for Puavo applications
|
@@ -36,13 +37,14 @@ files:
|
|
36
37
|
- app/views/sessions/new.html.erb
|
37
38
|
- init.rb
|
38
39
|
- lib/puavo/authentication.rb
|
39
|
-
- lib/puavo/authorization.rb
|
40
40
|
- lib/puavo/connection.rb
|
41
|
+
- lib/puavo/organisation.rb
|
41
42
|
- lib/puavo_authentication.rb
|
42
43
|
- lib/puavo_authentication/controllers/helpers.rb
|
43
44
|
- lib/tasks/puavo_ldap_auth.rake
|
44
45
|
- lib/user_error.rb
|
45
46
|
- rails/init.rb
|
47
|
+
has_rdoc: true
|
46
48
|
homepage: http://github.com/opinsys/puavo_authentication
|
47
49
|
licenses: []
|
48
50
|
|
@@ -72,7 +74,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
72
74
|
requirements: []
|
73
75
|
|
74
76
|
rubyforge_project:
|
75
|
-
rubygems_version: 1.
|
77
|
+
rubygems_version: 1.3.7
|
76
78
|
signing_key:
|
77
79
|
specification_version: 3
|
78
80
|
summary: Authentication solution for Puavo applications
|
data/lib/puavo/authorization.rb
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
module Puavo
|
2
|
-
module Authorization
|
3
|
-
def self.current_user
|
4
|
-
Thread.current["current_user"]
|
5
|
-
end
|
6
|
-
|
7
|
-
def self.current_user=(user)
|
8
|
-
Thread.current["current_user"] = user
|
9
|
-
# Update owners list
|
10
|
-
Thread.current["owners"] = LdapOrganisation.current.owner
|
11
|
-
end
|
12
|
-
|
13
|
-
def self.organisation_owner?
|
14
|
-
if Puavo::Authorization.current_user && Thread.current["owners"]
|
15
|
-
return Thread.current["owners"].include?(Puavo::Authorization.current_user.dn)
|
16
|
-
end
|
17
|
-
return false
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|