serge 0.1.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7d123b881b2747c9be15783a3acc8f79af45aacf0e9823708c504bdd83877c04
4
- data.tar.gz: aef408116087ad1eb5427f777766c4ac44880dece234772416455784641ffdc6
3
+ metadata.gz: 1ddf846378e4991b3739c64cb37257ad0d584212e963a14feb991ac2922219c2
4
+ data.tar.gz: db98af509011dcf2955601d39cc691e9723e701432bbf8ac36c1c842a343ecf5
5
5
  SHA512:
6
- metadata.gz: 22b4f55ebbb5b38a9eef5fe699ff17421e9bfa56eb208a6dfc0a9f698364c2f17e48a1a68619f11168cdc1c0a7d27ab2bf1fd32da003898b94e62b6a92c7c625
7
- data.tar.gz: 5a497cf2a756d26ac3d1165d77eaeab1c6385e323daa5751446f909626706c2e48bf41fbb6415f94162f15f9de72d8128a581031cea9ecbefc7729666c0b6c92
6
+ metadata.gz: b5b9bb3afa094e3383a676be914c7e716079f4aca68c883e7c04028bf78712b5eb2bff8f69b0731653ec9f5c1f3c4a863ff29760f6e9f7494d75130d874a66c5
7
+ data.tar.gz: f7479060ea773d8bf07e6e40c2a2871a046fbf7eee4a70be288ba61ba850f96d4efb007a80dcf182b352cf7606878feea730854fc88c0e4a88fbf3e2bbcaeb53
data/README.md CHANGED
@@ -1,28 +1,67 @@
1
1
  # Serge
2
2
 
3
- TODO: Delete this and the text below, and describe your gem
4
-
5
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/serge`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ A Ruby library to handle HTTP signatures for inbox deliveries on the Fediverse.
6
4
 
7
5
  ## Installation
8
6
 
9
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
-
11
7
  Install the gem and add to the application's Gemfile by executing:
12
8
 
13
9
  ```bash
14
- bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
10
+ bundle add serge
15
11
  ```
16
12
 
17
13
  If bundler is not being used to manage dependencies, install the gem by executing:
18
14
 
19
15
  ```bash
20
- gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
16
+ gem install serge
21
17
  ```
22
18
 
23
19
  ## Usage
24
20
 
25
- TODO: Write usage instructions here
21
+ There are 2 HTTP signature formats (draft-cavage and [RFC 9421](https://www.rfc-editor.org/info/rfc9421/)) in use on the Fediverse. `InboxDelivery` handles both formats with one interface.
22
+
23
+ For incoming messages, inside of a Rails controller method:
24
+
25
+ ```ruby
26
+ begin
27
+ inbox_delivery = Serge::InboxDelivery.new(request)
28
+ key_id = inbox_delivery.key_id
29
+ # public_key = YOUR_CODE_TO_RETRIEVE_PUBLIC_KEY_FROM_KEY_ID
30
+
31
+ # verify the signature using the PEM-encoded RSA public key
32
+ if inbox_delivery.verified?(public_key)
33
+ # signature is valid
34
+ else
35
+ # signature is not valid
36
+ end
37
+ rescue Serge::SignatureVerificationError => e
38
+ # YOUR_CODE_TO_HANDLE_EXCEPTION
39
+ end
40
+ ```
41
+
42
+ For outgoing message headers, both legacy and RFC 9421 share the same API method:
43
+
44
+ ```ruby
45
+ headers = {}
46
+ # given legacy_signature which is true or false
47
+ klass = legacy_signature ? Serge::FediSignature : Serge::MessageSignature
48
+
49
+ headers.merge!(
50
+ klass.headers(
51
+ method: method_string,
52
+ url: target_url,
53
+ body: body,
54
+ public_key_id: public_key_url,
55
+ private_key: private_key
56
+ )
57
+ )
58
+ ```
59
+
60
+ You can also set the logger in a Rails initializer like `config/initializers/serge.rb`:
61
+
62
+ ```ruby
63
+ Serge::InboxDelivery.logger = Rails.logger
64
+ ```
26
65
 
27
66
  ## Development
28
67
 
@@ -32,8 +71,8 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
32
71
 
33
72
  ## Contributing
34
73
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/serge. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/serge/blob/main/CODE_OF_CONDUCT.md).
74
+ Bug reports and pull requests are welcome on GitHub at https://github.com/herestomwiththeweather/serge. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/herestomwiththeweather/serge/blob/main/CODE_OF_CONDUCT.md).
36
75
 
37
76
  ## Code of Conduct
38
77
 
39
- Everyone interacting in the Serge project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/serge/blob/main/CODE_OF_CONDUCT.md).
78
+ Everyone interacting in the Serge project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/herestomwiththeweather/serge/blob/main/CODE_OF_CONDUCT.md).
@@ -2,6 +2,33 @@ module Serge
2
2
  class FediSignature
3
3
  ALGORITHM = 'rsa-sha256'
4
4
 
5
+ def self.headers(method:, url:, body:, public_key_id:, private_key:)
6
+ signature_headers = {}
7
+
8
+ date = Time.now.utc.httpdate
9
+ signature_headers['Date'] = date
10
+
11
+ header_names = ['(request-target)', 'host', 'date']
12
+ if body.present?
13
+ header_names << 'digest'
14
+ digest = "SHA-256=#{Digest::SHA256.base64digest(body)}"
15
+ signature_headers['Digest'] = digest
16
+ end
17
+
18
+ header_values = {
19
+ '(request-target)' => "#{method} #{url.path}",
20
+ 'host' => url.host,
21
+ 'date' => date,
22
+ 'digest' => digest,
23
+ }
24
+
25
+ signing_string = signing_string(header_names) { |name| header_values[name] }
26
+
27
+ signature = sign(signing_string, private_key)
28
+
29
+ signature_headers.merge({'Signature' => build_header(public_key_id, signature, header_names)})
30
+ end
31
+
5
32
  def self.signing_string(header_names)
6
33
  header_names.map { |name| "#{name}: #{yield(name)}" }.join("\n")
7
34
  end
@@ -67,6 +67,8 @@ module Serge
67
67
  end
68
68
 
69
69
  result
70
+ rescue OpenSSL::PKey::PKeyError => e
71
+ raise Serge::SignatureVerificationError, "Invalid public key: #{e.message}"
70
72
  end
71
73
 
72
74
  def verified?(public_key)
@@ -69,6 +69,8 @@ module Serge
69
69
  Linzer.verify!(Rack::Request.new(@request.env), key:)
70
70
  rescue Linzer::VerifyError
71
71
  false
72
+ rescue OpenSSL::PKey::PKeyError => e
73
+ raise Serge::SignatureVerificationError, "Invalid public key: #{e.message}"
72
74
  end
73
75
 
74
76
  private
@@ -0,0 +1,22 @@
1
+ module Serge
2
+ class MessageSignature
3
+ def self.headers(method:, url:, body:, public_key_id:, private_key:)
4
+ components = %w[@method @target-uri]
5
+
6
+ signature_headers = {}
7
+ if body.present?
8
+ signature_headers['Content-Digest'] = "sha-256=:#{Digest::SHA256.base64digest(body)}:"
9
+ components << 'content-digest'
10
+ end
11
+
12
+ temp_request = Net::HTTP.const_get(method.capitalize).new(URI(url), signature_headers)
13
+ key = Linzer.new_rsa_v1_5_sha256_key(private_key, public_key_id)
14
+ Linzer.sign!(temp_request, key: key, components: components)
15
+
16
+ signature_headers.merge({
17
+ 'Signature-Input' => temp_request['signature-input'],
18
+ 'Signature' => temp_request['signature']
19
+ })
20
+ end
21
+ end
22
+ end
data/lib/serge/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Serge
4
- VERSION = "0.1.0"
4
+ VERSION = "0.3.0"
5
5
  end
data/lib/serge.rb CHANGED
@@ -16,4 +16,5 @@ end
16
16
  require 'serge/error.rb'
17
17
  require 'serge/signature_verification_error.rb'
18
18
  require 'serge/fedi_signature.rb'
19
+ require 'serge/message_signature.rb'
19
20
  require 'serge/inbox_delivery.rb'
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: serge
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Brown
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2026-09-28 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: activesupport
@@ -78,6 +77,7 @@ files:
78
77
  - lib/serge/inbox_delivery.rb
79
78
  - lib/serge/inbox_delivery/http_legacy_signature.rb
80
79
  - lib/serge/inbox_delivery/http_message_signature.rb
80
+ - lib/serge/message_signature.rb
81
81
  - lib/serge/signature_verification_error.rb
82
82
  - lib/serge/version.rb
83
83
  homepage: https://github.com/herestomwiththeweather/serge
@@ -85,7 +85,6 @@ licenses:
85
85
  - MIT
86
86
  metadata:
87
87
  homepage_uri: https://github.com/herestomwiththeweather/serge
88
- post_install_message:
89
88
  rdoc_options: []
90
89
  require_paths:
91
90
  - lib
@@ -100,8 +99,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
100
99
  - !ruby/object:Gem::Version
101
100
  version: '0'
102
101
  requirements: []
103
- rubygems_version: 3.5.22
104
- signing_key:
102
+ rubygems_version: 4.0.23
105
103
  specification_version: 4
106
104
  summary: Ruby HTTP Signature for Fedi Inbox Deliveries
107
105
  test_files: []