An unofficial, mostly Bitwarden-compatible API server written in Ruby (Sinatra and ActiveRecord)
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Allow overriding /attachments path with RUBYWARDEN_ATTACHMENTS_URL

Also stop storing the attachment URL in the database, it's not used
by anything and may change later depending on this variable

+30 -22
+2 -1
README.md
··· 82 82 If you are not deploying Rubywarden on its own hostname or want to alter the 83 83 paths for any reason, you can override them with environment variables: 84 84 85 + - `RUBYWARDEN_ATTACHMENTS_URL` for the attachments URL - defaults to `/attachments` 85 86 - `RUBYWARDEN_BASE_URL` for the API base - defaults to `/api` 86 87 - `RUBYWARDEN_IDENTITY_BASE_URL` for the identity API base - defaults to 87 88 `/identity` ··· 91 92 on a subdirectory called `/notbitwarden`, you would set the environment 92 93 variables in your startup script: 93 94 94 - sudo -u _rubywarden env RUBYWARDEN_ENV=production RUBYWARDEN_BASE_URL=/notbitwarden/api RUBYWARDEN_IDENTITY_BASE_URL=/notbitwarden/identity RUBYWARDEN_ICONS_URL=/notbitwarden/icons bundle exec rackup -p 4567 config.ru 95 + sudo -u _rubywarden env RUBYWARDEN_ENV=production RUBYWARDEN_BASE_URL=/notbitwarden/api RUBYWARDEN_IDENTITY_BASE_URL=/notbitwarden/identity RUBYWARDEN_ICONS_URL=/notbitwarden/icons RUBYWARDEN_ATTACHMENTS_URL=/notbitwarden/attachments bundle exec rackup -p 4567 config.ru 95 96 96 97 Then you can configure the Bitwarden clients with a single server URL of 97 98 `https://example.com/notbitwarden`.
+5
db/migrate/20190419171915_drop_attachments_url.rb
··· 1 + class DropAttachmentsUrl < ActiveRecord::Migration[5.1] 2 + def change 3 + remove_column "attachments", "url" 4 + end 5 + end
+9 -13
lib/attachment.rb
··· 16 16 17 17 class Attachment < DBModel 18 18 self.table_name = "attachments" 19 - attr_accessor :context 20 19 21 20 before_create :generate_uuid_primary_key 22 - before_create :generate_url 23 21 24 - belongs_to :cipher, foreign_key: :cipher_uuid, inverse_of: :attachments 22 + belongs_to :cipher, 23 + foreign_key: :cipher_uuid, 24 + inverse_of: :attachments 25 25 26 - def self.build_from_params(params, context) 27 - attachment = new filename: params[:filename], 28 - size: params[:size], 29 - file: params[:file] 30 - attachment.context = context 31 - attachment 26 + def self.build_from_params(params) 27 + Attachment.new(filename: params[:filename], size: params[:size], 28 + file: params[:file]) 32 29 end 33 30 34 31 def to_hash ··· 42 39 } 43 40 end 44 41 45 - private 46 - 47 - def generate_url 48 - self.url = context.url("/attachments/#{self.cipher_uuid}/#{self.id}") 42 + def url 43 + "#{::ATTACHMENTS_URL}/#{self.cipher_uuid}/#{self.id}" 49 44 end 50 45 46 + private 51 47 def human_file_size 52 48 ActiveSupport::NumberHelper.number_to_human_size(self.size) 53 49 end
+13 -8
lib/routes/attachments.rb
··· 38 38 attachment_params = { filename: filename, 39 39 size: file.size, 40 40 file: file.read } 41 - attachment = cipher.attachments.build_from_params(attachment_params, self) 41 + attachment = cipher.attachments.build_from_params(attachment_params) 42 42 43 43 Attachment.transaction do 44 44 if !attachment.save ··· 50 50 end 51 51 52 52 delete "/ciphers/:uuid/attachment/:attachment_id" do 53 - delete_attachment uuid: params[:uuid], attachment_uuid: params[:attachment_id] 53 + delete_attachment uuid: params[:uuid], 54 + attachment_uuid: params[:attachment_id] 54 55 end 55 56 56 57 post "/ciphers/:uuid/attachment/:attachment_id/delete" do 57 - delete_attachment uuid: params[:uuid], attachment_uuid: params[:attachment_id] 58 + delete_attachment uuid: params[:uuid], 59 + attachment_uuid: params[:attachment_id] 58 60 end 59 61 end # BASE_URL 60 62 61 - app.get "/attachments/:uuid/:attachment_id" do 62 - a = Attachment.find_by_uuid_and_cipher_uuid(params[:attachment_id], params[:uuid]) 63 - attachment(a.filename) 64 - response.write(a.file) 65 - end 63 + app.namespace ATTACHMENTS_URL do 64 + get "/:uuid/:attachment_id" do 65 + a = Attachment.find_by_uuid_and_cipher_uuid(params[:attachment_id], 66 + params[:uuid]) 67 + attachment(a.filename) 68 + response.write(a.file) 69 + end 70 + end # ATTACHMENTS_URL 66 71 end # registered app 67 72 end 68 73 end
+1
lib/rubywarden.rb
··· 41 41 BASE_URL = (ENV["RUBYWARDEN_BASE_URL"] || "/api") 42 42 IDENTITY_BASE_URL = (ENV["RUBYWARDEN_IDENTITY_BASE_URL"] || "/identity") 43 43 ICONS_URL = (ENV["RUBYWARDEN_ICONS_URL"] || "/icons") 44 + ATTACHMENTS_URL = (ENV["RUBYWARDEN_ATTACHMENTS_URL"] || "/attachments") 44 45 45 46 # whether to allow new users 46 47 if !defined?(ALLOW_SIGNUPS)