An unofficial, mostly Bitwarden-compatible API server written in Ruby (Sinatra and ActiveRecord)
1module Rubywarden
2 module Test
3 class Factory
4 USER_EMAIL = "user@example.com"
5 USER_PASSWORD = "p4ssw0rd"
6
7 def self.create_user email: USER_EMAIL, password: USER_PASSWORD
8 u = User.new
9 u.email = email
10 u.kdf_type = Bitwarden::KDF::TYPE_IDS[User::DEFAULT_KDF_TYPE]
11 u.kdf_iterations = Bitwarden::KDF::DEFAULT_ITERATIONS[User::DEFAULT_KDF_TYPE]
12 u.password_hash = Bitwarden.hashPassword(password, email,
13 Bitwarden::KDF::TYPES[u.kdf_type], u.kdf_iterations)
14 u.password_hint = "it's like password but not"
15 u.key = Bitwarden.makeEncKey(Bitwarden.makeKey(password, email,
16 Bitwarden::KDF::TYPES[u.kdf_type], u.kdf_iterations))
17 u.save
18 u
19 end
20
21 def self.login_user email: USER_EMAIL, password: USER_PASSWORD
22 post "/identity/connect/token", {
23 :grant_type => "password",
24 :username => email,
25 :password => Bitwarden.hashPassword(password, email,
26 User::DEFAULT_KDF_TYPE,
27 Bitwarden::KDF::DEFAULT_ITERATIONS[User::DEFAULT_KDF_TYPE]),
28 :scope => "api offline_access",
29 :client_id => "browser",
30 :deviceType => 3,
31 :deviceIdentifier => SecureRandom.uuid,
32 :deviceName => "firefox",
33 :devicePushToken => ""
34 }
35 last_response.status.must_equal 200
36
37 last_json_response["access_token"]
38 end
39 end
40 end
41end