Class: Fortifier::AuthUserApi
- Inherits:
-
Object
- Object
- Fortifier::AuthUserApi
- Defined in:
- app/models/fortifier/auth_user_api.rb
Instance Method Summary collapse
- #auth_users_by_uuids(uuids) ⇒ Object
- #authenticate(login, pw, user_agent = "IE", request_ip = "127.0.0.1", account = {}) ⇒ Object
- #authenticate_batch_sso(account_uuid, token, user_agent = "IE", request_ip = "127.0.0.1") ⇒ Object
- #authenticate_on_demand_sso(login_token, user_agent = "IE", request_ip = "127.0.0.1") ⇒ Object
- #authenticate_uuid(uuid, pw, user_agent = "IE", request_ip = "127.0.0.1", account = {}) ⇒ Object
- #batch_update(user_info) ⇒ Object
- #change_password(params) ⇒ Object
- #create(params) ⇒ Object
- #create_password_reset_token(email) ⇒ Object
- #delete(params) ⇒ Object
- #find_auth_user(field, param) ⇒ Object
- #find_auth_user_emails(array_of_uuids) ⇒ Object
- #link(params) ⇒ Object
- #reset_password(params) ⇒ Object
- #search_for_auth_users(params) ⇒ Object
- #unlink(params) ⇒ Object
- #update(params) ⇒ Object
- #validate(params) ⇒ Object
Instance Method Details
#auth_users_by_uuids(uuids) ⇒ Object
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
# File 'app/models/fortifier/auth_user_api.rb', line 344 def auth_users_by_uuids(uuids) return [] unless uuids.present? formatted_uuids = uuids.map { |uuid| "'#{uuid}'" }.join(',') sql = <<-SQL SELECT fortifier_auth_users.*, latest_secrets.last_password_change FROM `fortifier_auth_users` LEFT OUTER JOIN ( SELECT auth_user_id, MAX(created_at) AS last_password_change FROM fortifier_secrets GROUP BY auth_user_id) latest_secrets ON fortifier_auth_users.id = latest_secrets.auth_user_id WHERE `fortifier_auth_users`.`uuid` IN (#{formatted_uuids}) SQL result = ActiveRecord::Base.connection.execute sql auth_users = [].tap do | arr | result.each(as: :hash) { | row | arr << row } end { auth_users: auth_users } end |
#authenticate(login, pw, user_agent = "IE", request_ip = "127.0.0.1", account = {}) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'app/models/fortifier/auth_user_api.rb', line 6 def authenticate(login, pw, user_agent="IE", request_ip="127.0.0.1", account={}) results = AuthenticationSteps.stepper(auth_type: "standard_auth", login: login, secret: pw, user_agent: user_agent, remote_addr: request_ip, account: account) auth_user = results[:auth_user] uuid = auth_user ? auth_user.uuid : nil failed_logins = auth_user ? auth_user.consecutive_failed_logins : nil error_msg = results[:auth_msg] if failed_logins==0 && !error_msg #successful authentication { uuid: uuid, status: true } else { uuid: uuid, status: false, errors: [error_msg], consecutive_failed_logins: failed_logins } end end |
#authenticate_batch_sso(account_uuid, token, user_agent = "IE", request_ip = "127.0.0.1") ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'app/models/fortifier/auth_user_api.rb', line 34 def authenticate_batch_sso(account_uuid, token, user_agent="IE", request_ip="127.0.0.1") results = Fortifier::AuthenticationSteps.stepper(auth_type: "batch_sso_auth", account_uuid: account_uuid, token: token, user_agent: user_agent, remote_addr: request_ip) auth_user = results[:auth_user] if auth_user { uuid: results[:auth_user].uuid, status: true } else { status: false, errors: [results[:auth_msg]] } end end |
#authenticate_on_demand_sso(login_token, user_agent = "IE", request_ip = "127.0.0.1") ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'app/models/fortifier/auth_user_api.rb', line 51 def authenticate_on_demand_sso(login_token, user_agent="IE", request_ip="127.0.0.1") results = Fortifier::AuthenticationSteps.stepper(auth_type: "on_demand_sso_auth", token: login_token, user_agent: user_agent, remote_addr: request_ip) auth_user = results[:auth_user] if auth_user { uuid: results[:auth_user].uuid, status: true } else { status: false, errors: [results[:auth_msg]] } end end |
#authenticate_uuid(uuid, pw, user_agent = "IE", request_ip = "127.0.0.1", account = {}) ⇒ Object
29 30 31 32 |
# File 'app/models/fortifier/auth_user_api.rb', line 29 def authenticate_uuid(uuid, pw, user_agent="IE", request_ip="127.0.0.1", account={}) login = AuthUser.where(uuid: uuid).pluck(:login).first authenticate(login, pw, user_agent, request_ip, account) end |
#batch_update(user_info) ⇒ Object
102 103 104 105 106 |
# File 'app/models/fortifier/auth_user_api.rb', line 102 def batch_update(user_info) updater = BatchUpdater.new(user_info) updater.perform_updates updater.user_status end |
#change_password(params) ⇒ Object
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'app/models/fortifier/auth_user_api.rb', line 191 def change_password(params) return {status: false, errors: [:uuid_not_provided]} if params[:uuid].blank? pw = params[:password] pwc = params[:password_confirmation] auth_user = Fortifier::AuthUser.where("uuid = ?", params[:uuid]).first correct_current_pw = auth_user.authenticated?(params[:current]).present? unless auth_user.blank? return {status: false, errors: [:bad_current_password]} if !correct_current_pw return {status: false, errors: [:auth_user_not_found]} if !auth_user new_secret = Secret.new(auth_user: auth_user, secret: pw, secret_confirmation: pwc) if new_secret.valid? new_secret.save { status: true } else { status: false, errors: new_secret.errors. } end end |
#create(params) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'app/models/fortifier/auth_user_api.rb', line 108 def create(params) auth_user = AuthUser.new() auth_user.login = params[:login] auth_user.email = params[:email] auth_user.name = params[:name] auth_user.note = params[:note] auth_user.app_uuids = params[:app_uuids] auth_user.account_uuids = params[:account_uuids] auth_user.search_keywords = params[:search_keywords] sso_user = params[:sso_user] skip_validation = params[:skip_validation] secret = sso_user ? Secret.make_sso_token : params[:password] secret_confirmation = sso_user ? secret : params[:password_confirmation] enc_type = sso_user ? Secret::SSO_TOKEN : nil new_secret = Secret.new(secret_value: (params[:crypted_password] if skip_validation), salt: (params[:salt] if skip_validation), secret: (secret if !skip_validation), secret_confirmation: (secret_confirmation if !skip_validation), enc_type: (skip_validation ? Secret::SHA : enc_type), skip_validation: (true if skip_validation)) valid_auth_user = auth_user.valid? valid_secret = new_secret.valid? if valid_auth_user && valid_secret auth_user.save auth_user.secrets << new_secret { uuid: auth_user.uuid, token: (auth_user.current_secret.secret_value if sso_user), status: true } else { status: false, errors: auth_user.errors., secret_errors: new_secret.errors. } end end |
#create_password_reset_token(email) ⇒ Object
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'app/models/fortifier/auth_user_api.rb', line 233 def create_password_reset_token(email) return {status: false, errors: [:email_not_provided]} if email.blank? auth_user = Fortifier::AuthUser.where("email = ?", email).first return {status: false, errors: [:auth_user_not_found]} if !auth_user pw = Secret.make_token new_secret = Secret.new(auth_user: auth_user, secret: pw, secret_confirmation: pw, reset_token: true) if new_secret.valid? new_secret.save { status: true, token: pw } else { status: false, errors: new_secret.errors. } end end |
#delete(params) ⇒ Object
290 291 292 293 294 295 296 297 298 299 300 301 302 |
# File 'app/models/fortifier/auth_user_api.rb', line 290 def delete(params) return {status: false, errors: [:uuid_not_provided]} if params[:uuid].blank? auth_user = AuthUser.where(uuid: params[:uuid]).first return {status: false, errors: [:auth_user_not_found]} if auth_user.blank? auth_user.deleted = 1 result = auth_user.save result ? {status: true} : {status: false, errors: (result.errors. if result)} end |
#find_auth_user(field, param) ⇒ Object
304 305 306 307 308 309 310 311 312 313 314 |
# File 'app/models/fortifier/auth_user_api.rb', line 304 def find_auth_user(field, param) field = field.to_s || '' case field when 'uuid' then auth_user = AuthUser.where("uuid = ?", param).first when 'login', 'email' then auth_user = AuthUser.where("deleted = 0 and (login = ? OR email = ?)", param, param).first when 'token' then auth_user = AuthUser.joins(:secrets).where("secret_value = ? AND (expired IS NULL OR expired = false)", param).first end auth_user.blank? ? {} : auth_user.public_attribute_hash end |
#find_auth_user_emails(array_of_uuids) ⇒ Object
316 317 318 319 320 321 322 323 324 325 326 |
# File 'app/models/fortifier/auth_user_api.rb', line 316 def find_auth_user_emails(array_of_uuids) return {errors: [:no_uuids_provided]} if array_of_uuids.blank? au_uuids_and_emails = {} AuthUser.where(uuid: array_of_uuids).inject(au_uuids_and_emails) do |hash, auth_user| hash[auth_user.uuid] = auth_user.email hash end {uuids_with_emails: au_uuids_and_emails} end |
#link(params) ⇒ Object
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'app/models/fortifier/auth_user_api.rb', line 252 def link(params) return {status: false, errors: [:uuid_not_provided]} if params[:uuid].blank? app_uuids = params[:app_uuids] || [] account_uuids = params[:account_uuids] || [] search_keywords = params[:search_keywords] || [] return {status: false, errors: [:app_and_account_uuids_not_provided]} if (app_uuids.blank? && account_uuids.blank?) auth_user = AuthUser.where(uuid: params[:uuid]).first return {status: false, errors: [:auth_user_not_found]} if auth_user.blank? auth_user.app_uuids.concat(app_uuids) auth_user.account_uuids.concat(account_uuids) auth_user.search_keywords.concat(search_keywords) result = auth_user.save result ? {status: true} : {status: false, errors: (result.errors. if result)} end |
#reset_password(params) ⇒ Object
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'app/models/fortifier/auth_user_api.rb', line 213 def reset_password(params) return {status: false, errors: [:uuid_not_provided]} if params[:uuid].blank? pw = params[:password] pwc = params[:password_confirmation] auth_user = Fortifier::AuthUser.where("uuid = ?", params[:uuid]).first return {status: false, errors: [:auth_user_not_found]} if !auth_user new_secret = Secret.new(auth_user: auth_user, secret: pw, secret_confirmation: pwc) if new_secret.valid? new_secret.save { status: true } else { status: false, errors: new_secret.errors. } end end |
#search_for_auth_users(params) ⇒ Object
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 |
# File 'app/models/fortifier/auth_user_api.rb', line 328 def search_for_auth_users(params) act_rel = Fortifier::AuthUser.paged_and_sorted_search(params) hash = { # the following are will_paginate methods total_pages: act_rel.total_pages, current_page: act_rel.current_page, per_page: act_rel.per_page, total_entries: act_rel.total_entries } hash[:auth_users] = act_rel.map do |au| au.public_attribute_hash end hash end |
#unlink(params) ⇒ Object
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'app/models/fortifier/auth_user_api.rb', line 271 def unlink(params) return {status: false, errors: [:uuid_not_provided]} if params[:uuid].blank? app_uuids = params[:app_uuids] || [] account_uuids = params[:account_uuids] || [] search_keywords = params[:search_keywords] || [] return {status: false, errors: [:app_and_account_uuids_not_provided]} if (app_uuids.blank? && account_uuids.blank?) auth_user = AuthUser.where(uuid: params[:uuid]).first return {status: false, errors: [:auth_user_not_found]} if auth_user.blank? auth_user.app_uuids -= app_uuids auth_user.account_uuids -= account_uuids auth_user.search_keywords -= search_keywords result = auth_user.save result ? {status: true} : {status: false, errors: (result.errors. if result)} end |
#update(params) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'app/models/fortifier/auth_user_api.rb', line 148 def update(params) # TODO: (DK) change user pw by way of email reset rather than admin reset? # could clean some of this code up auth_user = Fortifier::AuthUser.where("uuid = ?", params[:uuid]).first return {status: false, errors: [:auth_user_not_found]} if auth_user.blank? auth_user.login = params[:login] if params[:login] auth_user.email = params[:email] if params[:email] auth_user.name = params[:name] if params[:name] auth_user.note = params[:note] if params[:note] auth_user.app_uuids = params[:app_uuids] auth_user.account_uuids = params[:account_uuids] auth_user.search_keywords = params[:search_keywords] sso_user = params[:sso_user] if params[:password] || sso_user secret = sso_user ? Secret.make_sso_token : params[:password] secret_confirmation = sso_user ? secret : params[:password_confirmation] enc_type = sso_user ? Secret::SSO_TOKEN : nil end if secret.present? && secret_confirmation.present? new_secret = Secret.new(secret: secret, secret_confirmation: secret_confirmation) end valid_auth_user = auth_user.valid? valid_secret = new_secret ? new_secret.valid? : true if valid_auth_user && valid_secret auth_user.consecutive_failed_logins = 0 auth_user.save auth_user.secrets << new_secret if new_secret { uuid: auth_user.uuid, token: (auth_user.current_secret.secret_value if sso_user), status: true } else { status: false, errors: auth_user.errors., secret_errors: (new_secret.errors. if new_secret) } end end |
#validate(params) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'app/models/fortifier/auth_user_api.rb', line 67 def validate(params) uuid,login,email,note = params[:uuid],params[:login],params[:email],params[:note] enc_type = params[:sso_user].present? ? Secret::SSO_TOKEN : nil validation_type = params[:validation_type] auth_user = (validation_type=='create') ? AuthUser.new() : AuthUser.where("uuid = ?", uuid).first if params[:sso_user] secret, secret_confirmation, enc_type = Secret.make_sso_token, secret, Secret::SSO_TOKEN else secret, secret_confirmation, enc_type = params[:password], params[:password_confirmation], nil end auth_user.assign_attributes(login:login, email:email, note:note) new_secret = Secret.new(auth_user: auth_user, secret: secret, secret_confirmation: secret_confirmation, enc_type: enc_type) auth_user.valid? new_secret.valid? if validation_type=='update' && (secret.blank? && secret_confirmation.blank?) secret_errors = [] else secret_errors = new_secret.errors. end errors = auth_user.errors. secret_errors = secret_errors validation_status = (errors.blank? && secret_errors.blank?) ? true : false { status: validation_status, errors: errors, secret_errors: secret_errors } end |