Skip to main content

Ruby

Use the passbolt gem to log in to Passbolt and read, write and share secrets from Ruby.

Installation​

The gem is on RubyGems as switch-case-passbolt-sdk. It needs Ruby 3.0 or later and the ffi gem, and holds the native library of Linux (x86_64, aarch64), macOS and Windows.

gem install switch-case-passbolt-sdk
Gemfile
gem "switch-case-passbolt-sdk", require: "passbolt"

You load it with require "passbolt".

Initialize​

Log in with Passbolt::Client.login. Its calls block: the UniFFI generator of Ruby has no async support yet, so the gem has no async client.

require "passbolt"

Passbolt::Client.login(
"https://passbolt.example.com", File.read("private.asc"), ENV.fetch("PASSBOLT_PASSPHRASE")
) do |client|
client.list_resource_views("github").each do |view|
puts "#{view.id} #{view.name}"
end
end

With a block, login logs out at the end. Without one it returns the client: call client.logout when you are done. A failed logout raises its Passbolt::Error, unless the block is already leaving on an exception.

Login options​

login takes these keywords after the URL, the key and the passphrase. Every keyword is optional.

KeywordDescription
server_public_keyThe armored server key pinned with Passbolt.fetch_server_key.
metadata_key_fingerprintThe pinned fingerprint of the shared metadata key.
mfa_totp_tokenThe base32 TOTP secret, for an account with TOTP.
timeout_secondsSeconds a request may take. Default: 60; 0 waits forever.
tls_skip_verifySkip the certificate check. Default: false.
proxyThe proxy URL.
client_certificate_pem, client_key_pemThe PEM certificate and key, for mTLS.

Pin the server key​

server_key = Passbolt.fetch_server_key("https://passbolt.example.com")
puts "Check this fingerprint with your administrator: #{server_key.fingerprint}"

client = Passbolt::Client.login(
"https://passbolt.example.com", private_key, passphrase,
server_public_key: server_key.armored_key,
mfa_totp_token: nil, # the base32 TOTP secret, for an account with TOTP
metadata_key_fingerprint: nil,
timeout_seconds: nil, # 60 by default, 0 waits forever
tls_skip_verify: false,
proxy: nil,
client_certificate_pem: nil # and client_key_pem:, for mTLS
)

Read resources​

The calls return objects with readers: Passbolt::ResourceView, Passbolt::Resource, Passbolt::Folder, Passbolt::User… A ResourceView holds the fields every resource type shares, decrypted; the others, custom fields and icon among them, are the JSON objects metadata_json and secret_json.

Read a password​

view = client.get_resource_view(resource_id)
puts view.password

Create and share resources​

create_resource takes a Passbolt::NewResource, update_resource a Passbolt::ResourceChanges. The fields you leave out keep their default. Passbolt::PermissionType holds READ, UPDATE, OWNER and REVOKE.

Create, update, share and move a resource​

created = client.create_resource(Passbolt::NewResource.new(name: "GitHub", username: "ada", password: "…"))
client.update_resource(created.id, Passbolt::ResourceChanges.new(password: "new"))
client.share_resource(created.id, user_ids: [user_id], permission_type: Passbolt::PermissionType::UPDATE)

folder = client.create_folder("Ops")
client.move_resource(created.id, folder.id)

Generate a password​

Generate passwords from character sets, passphrases of words of the EFF list, and PIN codes, as the browser extension does. Start from the password policies of the organization or from settings of your own. The keywords are the fields of the settings; those you leave out keep the defaults of Passbolt.

Generate secrets and rate them​

policies = client.get_password_policies # the organization's: generator and settings
secret = Passbolt.generate_secret(policies)

password = Passbolt.generate_password(length: 24, mask_emoji: true)
passphrase = Passbolt.generate_passphrase(words: 6, word_case: Passbolt::WordCase::CAMELCASE)
pin_code = Passbolt.generate_pin_code(6)

Passbolt.secret_strength(password) == Passbolt::Strength::VERY_STRONG # as the extension shows it
Passbolt.secret_entropy(password) # in bits

Handle errors​

Every error of the SDK is a Passbolt::Error: TransportError, AuthError, CryptoError, ApiError (with the HTTP status), NotFoundError, ConfigError or InvalidArgumentError.

Catch a crypto error and an API error​

begin
Passbolt.get_key_fingerprint("not a key")
rescue Passbolt::CryptoError => e
puts "not a key: #{e.message}"
rescue Passbolt::ApiError => e
puts "the server answered #{e.status}: #{e.reason}"
rescue Passbolt::Error => e
puts "failed: #{e.message}"
end

Method names​

The methods are those of the overview, in snake_case. Flags and optional arguments are keywords; the others are positional:

  • list_resources(search = nil, favorites_only: false, owned_by_me: false)
  • list_users(search = nil, group_id: nil, admin_only: false)
  • share_folder(id, permission_type:, user_ids: [], group_ids: [], with_content: false)

The generated bindings stay available as PassboltFfi.

  • Overview: the concepts shared by every language.
  • API reference: every method, function and record.
  • Upgrading: the breaking changes since the 0.1.1 packages.