summaryrefslogtreecommitdiff
path: root/users/lib/warden/strategies/secure_remote_password.rb
diff options
context:
space:
mode:
authorAzul <azul@leap.se>2012-11-12 19:42:00 +0100
committerAzul <azul@leap.se>2012-11-12 19:42:00 +0100
commita0ba5ec9c705a74b86fdb558436fb05103bf88e3 (patch)
tree22e0562166e38eb84dc20c8e7addf3089b3af13c /users/lib/warden/strategies/secure_remote_password.rb
parent05ea71016fd54a14159c72299c25efbdc2f177bc (diff)
parentfe8b49232d31681667badaaeff7aa4d0a40445ea (diff)
Merge branch 'develop' into feature-client-side-validations
Conflicts: Gemfile.lock
Diffstat (limited to 'users/lib/warden/strategies/secure_remote_password.rb')
-rw-r--r--users/lib/warden/strategies/secure_remote_password.rb57
1 files changed, 57 insertions, 0 deletions
diff --git a/users/lib/warden/strategies/secure_remote_password.rb b/users/lib/warden/strategies/secure_remote_password.rb
new file mode 100644
index 0000000..8266e2d
--- /dev/null
+++ b/users/lib/warden/strategies/secure_remote_password.rb
@@ -0,0 +1,57 @@
+module Warden
+ module Strategies
+ class SecureRemotePassword < Warden::Strategies::Base
+
+ def valid?
+ handshake? || authentication?
+ end
+
+ def authenticate!
+ if authentication?
+ validate!
+ else # handshake
+ initialize!
+ end
+ end
+
+ protected
+
+ def handshake?
+ params['A'] && params['login']
+ end
+
+ def authentication?
+ params['client_auth'] && session[:handshake]
+ end
+
+ def validate!
+ user = session[:handshake].authenticate(params['client_auth'].hex)
+ user ? success!(user) : fail!(:password => "Could not log in")
+ end
+
+ def initialize!
+ user = User.find_by_param(id)
+ session[:handshake] = user.initialize_auth(params['A'].hex)
+ custom! json_response(session[:handshake])
+ rescue RECORD_NOT_FOUND
+ fail! :login => "User not found!"
+ end
+
+ def json_response(object)
+ [ 200,
+ {"Content-Type" => "application/json; charset=utf-8"},
+ [object.to_json]
+ ]
+ end
+
+ def id
+ params["id"] || params["login"]
+ end
+ end
+ end
+ Warden::Strategies.add :secure_remote_password,
+ Warden::Strategies::SecureRemotePassword
+
+end
+
+