summaryrefslogtreecommitdiff
path: root/users/app
diff options
context:
space:
mode:
Diffstat (limited to 'users/app')
-rw-r--r--users/app/controllers/sessions_controller.rb11
-rw-r--r--users/app/models/user.rb36
2 files changed, 38 insertions, 9 deletions
diff --git a/users/app/controllers/sessions_controller.rb b/users/app/controllers/sessions_controller.rb
index e68d798..284c0e2 100644
--- a/users/app/controllers/sessions_controller.rb
+++ b/users/app/controllers/sessions_controller.rb
@@ -8,16 +8,20 @@ class SessionsController < ApplicationController
def create
@user = User.find_by_param(params[:login])
session[:handshake] = @user.initialize_auth(params['A'].hex)
- render :json => { :B => session[:handshake].bb.to_s(16) }
+ User.current = @user #?
+ render :json => session[:handshake]
rescue RECORD_NOT_FOUND
render :json => {:errors => {:login => ["unknown user"]}}
end
def update
+ # TODO: validate the id belongs to the session
@user = User.find_by_param(params[:id])
- @server_auth = @user.authenticate!(params[:client_auth].hex, session.delete(:handshake))
+ @srp_session = session.delete(:handshake)
+ @srp_session.authenticate!(params[:client_auth].hex)
session[:user_id] = @user.id
- render :json => {:M2 => @server_auth}
+ User.current = @user #?
+ render :json => @srp_session
rescue WRONG_PASSWORD
session[:handshake] = nil
render :json => {:errors => {"password" => ["wrong password"]}}
@@ -25,6 +29,7 @@ class SessionsController < ApplicationController
def destroy
session[:user_id] = nil
+ User.current = nil #?
redirect_to root_path
end
end
diff --git a/users/app/models/user.rb b/users/app/models/user.rb
index fa64f42..1afb9db 100644
--- a/users/app/models/user.rb
+++ b/users/app/models/user.rb
@@ -1,14 +1,23 @@
class User < CouchRest::Model::Base
- include SRP::Authentication
-
property :login, String, :accessible => true
property :email, String, :accessible => true
property :password_verifier, String, :accessible => true
property :password_salt, String, :accessible => true
- validates :login, :password_salt, :password_verifier, :presence => true
- validates :login, :uniqueness => true
+ validates :login, :password_salt, :password_verifier,
+ :presence => true
+
+ validates :login,
+ :uniqueness => true
+
+ validates :login,
+ :format => { :with => /\A[A-Za-z\d_]+\z/,
+ :message => "Only letters, digits and _ allowed" }
+
+ validates :password_salt, :password_verifier,
+ :format => { :with => /\A[\dA-Fa-f]+\z/,
+ :message => "Only hex numbers allowed" }
timestamps!
@@ -24,8 +33,8 @@ class User < CouchRest::Model::Base
# valid set of attributes for testing
def valid_attributes_hash
{ :login => "me",
- :password_verifier => "1234",
- :password_salt => "4321" }
+ :password_verifier => "1234ABC",
+ :password_salt => "4321AB" }
end
end
@@ -38,6 +47,10 @@ class User < CouchRest::Model::Base
super(options.merge(:only => ['login', 'password_salt']))
end
+ def initialize_auth(aa)
+ return SRP::Session.new(self, aa)
+ end
+
def salt
password_salt.hex
end
@@ -46,4 +59,15 @@ class User < CouchRest::Model::Base
password_verifier.hex
end
+ def username
+ login
+ end
+
+ def self.current
+ Thread.current[:user]
+ end
+ def self.current=(user)
+ Thread.current[:user] = user
+ end
+
end