social_web-well_known 0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2916f12849cd44cd8a9eee7e5085d5c39e2e13e6d27deea343140ce72897bfd4
4
+ data.tar.gz: ba1b499ab666b37499ab87da2c0cbd3add82cadd13b378c9850031d81ad1c13d
5
+ SHA512:
6
+ metadata.gz: 1bfc7e23acc16b89d8a5cbed4861d542ddad9d367d62d79e1b0fa3c2c361cd98d3d03377a6cd294fdfc7a52ee65e919a4e3e2c4edf01e3cfdbd5e6821af2e7e2
7
+ data.tar.gz: 56a8cdd9c9d876a70a409bcabbdb5f57459bf8b790f5a8a8c1d631caa9d71f75dce92dab46852407d19e03c4a626a35b0eda50ac13bdd80c01148a51a15433fc
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Shane Cavanaugh
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/lib/well_known.rb ADDED
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/setup'
4
+
5
+ require 'roda'
6
+
7
+ require 'well_known/exceptions'
8
+ require 'well_known/configuration'
9
+ require 'well_known/hooks'
10
+
11
+ module WellKnown
12
+ def self.configure
13
+ yield config
14
+ end
15
+
16
+ def self.config
17
+ @configuration ||= Configuration.new
18
+ end
19
+
20
+ class Routes < ::Roda
21
+ plugin :halt
22
+ plugin :json
23
+ plugin :json_parser
24
+ plugin :middleware, env_var: 'social_web.well_known'
25
+
26
+ route do |r|
27
+ r.on('.well-known') do
28
+ r.get 'webfinger' do
29
+ Hooks.run('well_known.webfinger.before_get', r)
30
+
31
+ resource_param = r.params['resource']
32
+ r.halt 400 if resource_param.nil?
33
+ if WellKnown.config.webfinger_resource[:subject] != resource_param
34
+ r.halt 404
35
+ end
36
+
37
+ response.status = 200
38
+ response['Content-Type'] = 'application/jrd+json'
39
+ WellKnown.config.webfinger_resource
40
+ ensure
41
+ Hooks.run('well_known.webfinger.after_get', response)
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WellKnown
4
+ class Configuration
5
+ attr_accessor :webfinger_resource
6
+
7
+ def initialize
8
+ @webfinger_resource = {}
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WellKnown
4
+ class Error < StandardError; end
5
+ end
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WellKnown
4
+ HOOKS = %w[
5
+ well_known.webfinger.before_get
6
+ well_known.webfinger.after_get
7
+ ].freeze
8
+
9
+ module Hooks
10
+ class FailedHook < Error
11
+ MESSAGE_FORMAT = <<~TXT
12
+ %<hook_name>s: %<hook_source_location>s
13
+
14
+ %<exception_class>s: %<exception_message>s
15
+ TXT
16
+ attr_accessor :hook_name, :hook_source_location, :wrapped_exception
17
+
18
+ def initialize(hook_name, hook_source_location, wrapped_exception)
19
+ @hook_name = hook_name
20
+ @hook_source_location = hook_source_location
21
+ @wrapped_exception = wrapped_exception
22
+ super(format_message)
23
+ end
24
+
25
+ def cause
26
+ wrapped_exception || super
27
+ end
28
+
29
+ private
30
+
31
+ def format_message
32
+ format(
33
+ MESSAGE_FORMAT,
34
+ hook_name: hook_name,
35
+ exception_class: wrapped_exception.class,
36
+ exception_message: wrapped_exception.message,
37
+ hook_source_location: hook_source_location.join(':')
38
+ )
39
+ end
40
+ end
41
+
42
+ def self.register(name, &blk)
43
+ Registry.instance.register(name, &blk)
44
+ end
45
+
46
+ def self.run(name, *args, **kwargs)
47
+ hooks = Registry.instance[name]
48
+ hooks&.each do |hook|
49
+ hook.call(*args, **kwargs)
50
+ rescue StandardError => e
51
+ raise FailedHook.new(name, hook.source_location, e)
52
+ end
53
+ end
54
+
55
+ class Registry
56
+ attr_accessor :hooks
57
+
58
+ def self.instance
59
+ @instance ||= new
60
+ end
61
+
62
+ def initialize
63
+ @hooks = {}
64
+ end
65
+
66
+ def [](name)
67
+ @hooks[name]
68
+ end
69
+
70
+ def register(name, &action)
71
+ @hooks[name] ||= []
72
+ @hooks[name] << action.to_proc
73
+ end
74
+ end
75
+ end
76
+ end
data/spec/helper.rb ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Helper
4
+ def app
5
+ builder = Rack::Builder.new
6
+ builder.run WellKnown::Routes
7
+ end
8
+
9
+ def configure
10
+ mock_config = instance_double('WellKnown::Configuration')
11
+ allow(WellKnown).to receive(:config).and_return(mock_config)
12
+ yield mock_config
13
+ end
14
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'simplecov'
4
+ SimpleCov.add_filter(/spec/)
5
+ SimpleCov.start
6
+
7
+ require 'well_known'
8
+ require 'rack/test'
9
+ require 'helper'
10
+
11
+ RSpec.configure do |config|
12
+ config.include Rack::Test::Methods, type: :route
13
+ config.include Helper
14
+
15
+ config.expect_with :rspec do |expectations|
16
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
17
+ end
18
+
19
+ config.mock_with :rspec do |mocks|
20
+ mocks.verify_partial_doubles = true
21
+ end
22
+
23
+ config.shared_context_metadata_behavior = :apply_to_host_groups
24
+
25
+ config.example_status_persistence_file_path = 'tmp/examples.txt'
26
+
27
+ config.disable_monkey_patching!
28
+
29
+ config.warnings = false
30
+ config.default_formatter = 'doc' if config.files_to_run.one?
31
+
32
+ # Run specs in random order to surface order dependencies. If you find an
33
+ # order dependency and want to debug it, you can fix the order by providing
34
+ # the seed, which is printed after each run.
35
+ # --seed 1234
36
+ config.order = :random
37
+
38
+ # Seed global randomization in this process using the `--seed` CLI option.
39
+ # Setting this allows you to use `--seed` to deterministically reproduce
40
+ # test failures related to randomization by passing the same `--seed` value
41
+ # as the one that triggered the failure.
42
+ Kernel.srand config.seed
43
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ module WellKnown
6
+ class Routes
7
+ RSpec.describe '/.well-known', type: :route do
8
+ context '/.well-known/webfinger' do
9
+ context 'GET /.well-known/webfinger' do
10
+ context 'resource is absent or empty' do
11
+ it 'returns a 400' do
12
+ get '/.well-known/webfinger'
13
+ expect(last_response.status).to eq(400)
14
+ end
15
+ end
16
+
17
+ context 'resource is not found' do
18
+ it 'returns a 404' do
19
+ get '/.well-known/webfinger', { resource: '' }
20
+ expect(last_response.status).to eq(404)
21
+ end
22
+ end
23
+
24
+ context 'resource is found' do
25
+ it 'returns the resource' do
26
+ subject = 'acct:beep@boop.com'
27
+ configure do |config|
28
+ allow(config).
29
+ to receive(:webfinger_resource).
30
+ and_return(subject: subject)
31
+ end
32
+
33
+ get '/.well-known/webfinger', { resource: subject }
34
+ expect(last_response.status).to eq(200)
35
+ expect(last_response.content_type).to eq('application/jrd+json')
36
+ end
37
+ end
38
+
39
+ it 'runs hooks before and after the request' do
40
+ expect(Hooks).
41
+ to receive(:run).
42
+ ordered.
43
+ with(
44
+ 'well_known.webfinger.before_get',
45
+ kind_of(Rack::Request)
46
+ )
47
+ expect(Hooks).
48
+ to receive(:run).
49
+ ordered.
50
+ with(
51
+ 'well_known.webfinger.after_get',
52
+ kind_of(Roda::RodaResponse)
53
+ )
54
+ get '/.well-known/webfinger', '{}'
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: social_web-well_known
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Shane Cavanaugh
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-08-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: roda
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rack-test
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.1'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.1'
83
+ description: Ednpoints for Well-Known URIs
84
+ email:
85
+ - shane@shanecav.net
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - LICENSE.txt
91
+ - lib/well_known.rb
92
+ - lib/well_known/configuration.rb
93
+ - lib/well_known/exceptions.rb
94
+ - lib/well_known/hooks.rb
95
+ - spec/helper.rb
96
+ - spec/spec_helper.rb
97
+ - spec/well_known_spec.rb
98
+ homepage: https://github.com/social_web/webmention
99
+ licenses:
100
+ - MIT
101
+ metadata:
102
+ homepage_uri: https://github.com/social_web/webmention
103
+ source_code_uri: https://github.com/social_web/webmention
104
+ changelog_uri: https://github.com/social_web/well_known/tree/master/CHANGELOG.md
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: 1.9.2
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubygems_version: 3.0.4
121
+ signing_key:
122
+ specification_version: 4
123
+ summary: Ednpoints for Well-Known URIs
124
+ test_files: []