github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/tests/system/lib/bitwarden/cipher.rb (about)

     1  class Bitwarden
     2    module Cipher
     3      def self.doctype
     4        "com.bitwarden.ciphers"
     5      end
     6  
     7      # Bitwarden CLI doesn't let us remove a cipher from an organization, or
     8      # move a cipher from one organization to another. But, we can use the
     9      # update method manually.
    10      def self.update(inst, item_id, data)
    11        opts = {
    12          accept: "application/json",
    13          content_type: "application/json",
    14          authorization: "Bearer #{inst.token_for doctype}"
    15        }
    16        key = get_symmetric_key(inst)
    17        data[:name] = Bitwarden::Organization.encrypt_name(key, data[:name])
    18        data[:notes] = Bitwarden::Organization.encrypt_name(key, data[:notes] || "")
    19        data[:card].each do |field, value|
    20          data[:card][field] = Bitwarden::Organization.encrypt_name(key, value)
    21        end if data[:card]
    22        data[:organizationId] = nil
    23        data[:collectionsIds] = nil
    24        body = JSON.generate data
    25        res = inst.client["/bitwarden/api/ciphers/#{item_id}"].put body, opts
    26        JSON.parse(res.body)
    27      end
    28  
    29      def self.get_symmetric_key(inst)
    30        opts = {
    31          accept: "application/json",
    32          authorization: "Bearer #{inst.token_for 'com.bitwarden.profiles'}"
    33        }
    34        res = inst.client["/bitwarden/api/accounts/profile"].get opts
    35        body = JSON.parse(res.body)
    36        Bitwarden::Organization.decrypt_symmetric_key(inst, body["Key"])
    37      end
    38    end
    39  end