Module: JunglePath::API::Helpers::AuthOld

Defined in:
lib/jungle_path/api/helpers/auth_old.rb

Instance Method Summary collapse

Instance Method Details

#authenticate(no_cache = false) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/jungle_path/api/helpers/auth_old.rb', line 5

def authenticate no_cache=false
	user_name = request.env['REMOTE_USER']
	password = request.env['REMOTE_PASSWORD']
	valid, authentication_messages = basic_authentication(user_name, password, no_cache)
	unless valid
		valid, authentication_messages = basic_authentication(user_name, password, true)
		halt 401, authentication_messages.join("\n") unless valid
	end
	request.body.rewind
end

#authenticate_key(key_string, no_cache = false) ⇒ Object



59
60
61
62
63
# File 'lib/jungle_path/api/helpers/auth_old.rb', line 59

def authenticate_key key_string, no_cache=false
	key = get_key(key_string, no_cache)
	set_current_key(key)
	valid = (key and key.valid?)
end

#authenticate_user(user_name, password, no_cache = false) ⇒ Object



65
66
67
68
69
# File 'lib/jungle_path/api/helpers/auth_old.rb', line 65

def authenticate_user user_name, password, no_cache=false
	user = get_user(user_name, password, no_cache)
	set_current_user(user)
	valid = (user and user.is_valid)
end

#basic_authentication(user_name, password, no_cache = false) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/jungle_path/api/helpers/auth_old.rb', line 16

def basic_authentication user_name, password, no_cache=false
	authentication_messages = []
	if user_name and user_name.start_with?("sk_")
		valid = authenticate_key(user_name, no_cache)
		if valid
			authentication_messages << "key is valid: #{current_key.to_h}."
			user = get_user_from_key(current_key, no_cache)
			unless user
				authentication_messages << "User not found for current_key."
				valid = false
			end
			set_current_user user
		else
			authentication_messages << "key #{user_name} is not valid."
			set_current_user nil
		end
	else
		valid = authenticate_user(user_name, password, no_cache)
		if valid
			authentication_messages << "User is valid: #{current_user.to_h}."
			key = get_default_key(current_user.id, no_cache)
			unless key
				authentication_messages << "Default key not found for current_user."
				valid = false
			end
			set_current_key(key)
		else
			authentication_messages << "User #{user_name} is not valid."
			set_current_key( nil)
		end
	end
	messages = authentication_messages.join("\n    ")
	if valid
		roles = get_roles(no_cache)
		set_current_roles roles
		set_current_auth
		set_current_query_filters(no_cache)
	else
		set_current_roles nil
	end
	[valid, authentication_messages]
end

#current_authObject



222
223
224
# File 'lib/jungle_path/api/helpers/auth_old.rb', line 222

def current_auth
	@current_auth
end

#current_keyObject



210
211
212
# File 'lib/jungle_path/api/helpers/auth_old.rb', line 210

def current_key
	@current_key
end

#current_query_filtersObject



226
227
228
# File 'lib/jungle_path/api/helpers/auth_old.rb', line 226

def current_query_filters
	@current_query_filters
end

#current_roleObject



218
219
220
# File 'lib/jungle_path/api/helpers/auth_old.rb', line 218

def current_role
	@current_role
end

#current_rolesObject



214
215
216
# File 'lib/jungle_path/api/helpers/auth_old.rb', line 214

def current_roles
	@current_roles
end

#current_userObject



206
207
208
# File 'lib/jungle_path/api/helpers/auth_old.rb', line 206

def current_user
	@current_user
end

#get_any_user(user_name, password, no_cache = false) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/jungle_path/api/helpers/auth_old.rb', line 98

def get_any_user user_name, password, no_cache=false
	cache_key = "#{user_name}.#{password}"
	user = cache.get(cache_key)
	puts "user: #{user}."
	if user == nil or no_cache
		hash = SQL::AnyUser.by_user_name(db, user_name)
		puts "hash: #{hash}."
		user = Schema::User.new(hash, false) if hash
		user.is_valid = valid_user?(user, password) if user
		cache.set cache_key, user if user
	end
	user
end

#get_default_key(user_id, no_cache = false) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/jungle_path/api/helpers/auth_old.rb', line 128

def get_default_key user_id, no_cache=false
	cache_key = "#{user_id}.key"
	key = cache.get(cache_key)
	if key == nil or no_cache
		array = SQL::Key.default_by_user_id(db, user_id)
		hash = array.first if array
		key = Schema::Key.new(hash) if hash
		cache.set cache_key, key if key
	end
	if key
		puts "default key.key: #{key.key}."
	else
		puts "A default key was not found for user ID: #{user_id}."
	end
	key
end

#get_key(key_string, no_cache = false) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/jungle_path/api/helpers/auth_old.rb', line 116

def get_key key_string, no_cache=false
	key = cache.get(key_string)
	if key == nil or no_cache
		array = SQL::Key.by_key(db, key_string)
		hash = array.first if array
		key = Schema::Key.new(hash) if hash
		cache.set key_string, key if key
	end
	puts "key.key: #{key.key}."
	key
end

#get_query_filters(no_cache = false) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/jungle_path/api/helpers/auth_old.rb', line 156

def get_query_filters no_cache=false
	# get filters from query_filter table.
	return nil unless current_key
	cache_key = "#{current_key.id}_query_filters"
	query_filters = cache.get(cache_key)
	if query_filters == nil or no_cache
		query_filters = []
		filters = SQL::QueryFilter.by_key(db, current_key)
		filters.each do |filter|
			query_filters << Query::Filter.new(filter[:base_table_name], filter[:sub_select])
		end
		if app_defined_query_filters
			app_defined_query_filters.each do |filter|
				query_filters << filter
			end
		end
		cache.set cache_key, query_filters
	end
	query_filters
end

#get_roles(no_cache = false) ⇒ Object



145
146
147
148
149
150
151
152
153
154
# File 'lib/jungle_path/api/helpers/auth_old.rb', line 145

def get_roles no_cache=false
	return nil unless current_key
	cache_key = "#{current_key.id}_roles"
	roles = cache.get(cache_key)
	if roles == nil or no_cache
		roles = SQL::Role.by_key(db, current_key)
		cache.set cache_key, roles if roles
	end
	roles
end

#get_user(user_name, password, no_cache = false) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/jungle_path/api/helpers/auth_old.rb', line 71

def get_user user_name, password, no_cache=false
	cache_key = "#{user_name}.#{password}"
	user = cache.get(cache_key)
	#puts "user: #{user}."
	if user == nil or no_cache
		hash = SQL::User.by_user_name(db, user_name)
		puts "hash: #{hash}."
		user = Schema::User.new(hash, false) if hash
		user.is_valid = valid_user?(user, password) if user
		cache.set cache_key, user if user
	end
	user
end

#get_user_from_key(key, no_cache = false) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/jungle_path/api/helpers/auth_old.rb', line 85

def get_user_from_key key, no_cache=false
	user = nil
	if key
		cache_key = "user_by_key_#{key.key}"
		user = cache.get(cache_key)
		if user == nil or no_cache
			user = Controller::User.new(current_user, current_key, {id: key.user_id}, db).select
			cache.set cache_key, user if user
		end
	end
	user
end

#set_current_authObject



196
197
198
199
# File 'lib/jungle_path/api/helpers/auth_old.rb', line 196

def set_current_auth
	@current_auth = ::Authorization::Filter.new current_roles, configatron.application.role_permissions, configatron.application.role_restrictions
	@current_user.auth = @current_auth
end

#set_current_key(value) ⇒ Object



181
182
183
# File 'lib/jungle_path/api/helpers/auth_old.rb', line 181

def set_current_key(value)
	@current_key = value
end

#set_current_query_filters(no_cache = false) ⇒ Object



201
202
203
204
# File 'lib/jungle_path/api/helpers/auth_old.rb', line 201

def set_current_query_filters no_cache=false
	@current_query_filters = get_query_filters(no_cache)
	@current_user.query_filters = @current_query_filters
end

#set_current_roles(roles) ⇒ Object



185
186
187
188
189
190
191
192
193
194
# File 'lib/jungle_path/api/helpers/auth_old.rb', line 185

def set_current_roles roles
	@current_role = nil
	@current_roles = roles
	if roles
		roles.each do |role|
			@current_role = role[:name]
			break
		end
	end
end

#set_current_user(user) ⇒ Object



177
178
179
# File 'lib/jungle_path/api/helpers/auth_old.rb', line 177

def set_current_user(user)
	@current_user = user
end

#valid_user?(user, password) ⇒ Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/jungle_path/api/helpers/auth_old.rb', line 112

def valid_user? user, password
	valid = (user and PasswordHash.validatePassword(password, user.hash))
end