1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
58
59
60
61
62
63
64
65
66
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
class SrpTest < RackTest
setup do
@testcode = InviteCode.new
@testcode.save!
end
teardown do
if @user
cleanup_user
end
Warden.test_reset!
end
# this test wraps the api and implements the interface the ruby-srp client.
def handshake(login, aa)
post api_url("sessions.json"),
:login => login,
'A' => aa,
:format => :json
response = JSON.parse(last_response.body)
if response['errors']
raise RECORD_NOT_FOUND.new(response['errors'])
else
return response['B']
end
end
def validate(m)
put api_url("sessions/#{@login}.json"),
:client_auth => m,
:format => :json
return JSON.parse(last_response.body)
end
protected
attr_reader :server_auth
def register_user(login = "integration_test", password = 'srp, verify me!', invite_code = @testcode.invite_code)
cleanup_user(login)
post api_url('users.json'),
user_params(login: login, password: password, invite_code: invite_code)
assert(@user = User.find_by_login(login), 'user should have been created: %s' % last_response_errors)
@login = login
@password = password
end
def update_user(params = {})
put api_url("users/#{@user.id}.json"),
user_params(params),
auth_headers
end
def authenticate(params = nil)
@server_auth = srp(params).authenticate(self)
end
def auth_headers
return {} if @server_auth.nil?
{
"HTTP_AUTHORIZATION" => encoded_token
}
end
def encoded_token
ActionController::HttpAuthentication::Token.encode_credentials(server_auth["token"])
end
def logout(params=nil, headers=nil)
delete api_url("logout.json"),
params || {format: :json},
headers || auth_headers
end
def cleanup_user(login = nil)
login ||= @user.login
Identity.by_address.key(login + '@' + APP_CONFIG[:domain]).each do |identity|
identity.destroy
end
if user = User.find_by_login(login)
user.destroy
end
end
def user_params(params)
if params.keys.include?(:password)
srp_process_password(params)
end
return { user: params, format: :json }
end
def srp_process_password(params)
params.reverse_merge! login: @login, salt: @salt
@srp = SRP::Client.new params[:login], password: params.delete(:password)
@salt = srp.salt.to_s(16)
params.merge! :password_verifier => srp.verifier.to_s(16),
:password_salt => @salt
end
def srp(params = nil)
if params.nil?
@srp
else
params.reverse_merge! password: @password
SRP::Client.new(params.delete(:login) || @login, params)
end
end
def last_response_errors
JSON.parse(last_response.body)['errors']
rescue
""
end
def api_url(path)
"http://api.lvh.me:3000/#{api_version}/#{path}"
end
def api_version
2
end
end
|