indieauth-token-verification 0.2.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ad58613dddfcf85c62a6813e0cdb57460114325227e0c8d1e574cffdfb0e702e
4
+ data.tar.gz: 6d1ae97766b6c4510668995ef0fa4aebef0a31b6a5c6dd9ecf14fc17b8857270
5
+ SHA512:
6
+ metadata.gz: cbc762772b853ebd90d0e62129130b47fda55220ccfb5e28090f4b489d7543dbb5c4b3fa64cca4190ab865a41a334bd14cca9e1ee6862ffe141f10f74e362c82
7
+ data.tar.gz: 02beaf020aa5298f048d82736f554779960ddc3861d7db042e11cca4077daf358a3495c9f7f5edc891617f8fed0fc86d2b5437b71210a31037c1b2768e48224c
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1 @@
1
+ indieauth-token-verification
@@ -0,0 +1 @@
1
+ ruby-2.5.3
@@ -0,0 +1,27 @@
1
+ # Changelog
2
+ All notable changes to this project will be documented in this file.
3
+
4
+ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
5
+ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
6
+
7
+ ## [Unreleased]
8
+
9
+ ## [0.2.0] - 2019-01-24
10
+
11
+ ### Added
12
+ - Added explicit message to IncorrectMeError.
13
+
14
+ ## [0.1.2] - 2018-05-18
15
+
16
+ ### Added
17
+ - Use `kind_of?` rather than `code` when checking response from token endpoint.
18
+
19
+ ## [0.1.1] - 2018-03-04
20
+
21
+ ### Added
22
+ - Forced use of SSL on the token endpoint.
23
+
24
+ ## [0.1.0] - 2018-02-20
25
+
26
+ ### Added
27
+ - Initial release.
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at coc+tokenverification@deeden.co.uk. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in indieauth-token-verification.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Stephen Rushe
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.
@@ -0,0 +1,83 @@
1
+ # IndieAuth::TokenVerification
2
+
3
+ Verify an IndieAuth access token against a token endpoint, ensuring that the scope required is one of those associated with the token.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'indieauth-token-verification'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install indieauth-token-verification
20
+
21
+ ## Configuration
22
+
23
+ Use of the gem **requires** two environment variables to be specified, `TOKEN_ENDPOINT`, and `DOMAIN`.
24
+
25
+ `TOKEN_ENDPOINT` specifies the token endpoint to be used to validate the access token. Failure to specify `TOKEN_ENDPOINT` will result in a `IndieAuth::TokenVerification::MissingTokenEndpointError` error being raised.
26
+
27
+ `DOMAIN` specifies the domain we expect to see in the response from the validated token. It should match that specified when the token was first generated. Failure to specify `DOMAIN` will result in a `IndieAuth::TokenVerification::MissingDomainError` error being raised.
28
+
29
+ ## Usage
30
+
31
+ ```ruby
32
+ # Verify the provided access token, with no scope requirement
33
+ IndieAuth::TokenVerification.new(access_token).verify
34
+
35
+ # Verify the provided access token, requiring a particular scope
36
+ IndieAuth::TokenVerification.new(access_token).verify("media")
37
+ ```
38
+
39
+ ## Errors
40
+
41
+ As well as `MissingTokenEndpointError` and `MissingDomainError` mentioned above, there are other errors which will be raised in certain circumstances...
42
+
43
+ * `IndieAuth::TokenVerification::AccessTokenMissingError` - when the access token is missing
44
+ * `IndieAuth::TokenVerification::ForbiddenUserError` - when the token endpoint reports an error
45
+ * `IndieAuth::TokenVerification::IncorrectMeError` - when the `me` value in the response does not match the `DOMAIN`
46
+ * `IndieAuth::TokenVerification::InsufficentScopeError` - when the scope requested is not granted by the access token
47
+
48
+ ## A more detailed usage example
49
+
50
+ The following is a more detailed example of how the gem could be used. This example comes from a project that is actually making use of the gem in production. Most of the code involves dealing with the various errors raised by the gem and ensuring that errors are sent back with the appropriate status and error codes. `send_error` (which isn't defined here) returns a JSON response with the appropriate data and halts execution.
51
+
52
+ ```ruby
53
+ def verify_token(scope = nil)
54
+ access_token = request.env['HTTP_AUTHORIZATION'] || params['access_token'] || ''
55
+ IndieAuth::TokenVerification.new(access_token).verify(scope)
56
+ rescue IndieAuth::TokenVerification::AccessTokenMissingError
57
+ send_error(status: 401, error: 'unauthorized', description: 'Access token missing or empty')
58
+ rescue IndieAuth::TokenVerification::MissingDomainError
59
+ send_error(status: 400, error: 'invalid_request', description: 'DOMAIN is not specified')
60
+ rescue IndieAuth::TokenVerification::MissingTokenEndpointError
61
+ send_error(status: 400, error: 'invalid_request', description: 'TOKEN_ENDPOINT is not specified')
62
+ rescue IndieAuth::TokenVerification::ForbiddenUserError
63
+ send_error(status: 403, error: 'forbidden', description: 'User does not have permission')
64
+ rescue IndieAuth::TokenVerification::IncorrectMeError
65
+ send_error(status: 401, error: 'insufficient_scope', description: 'The "me" value does not match the expected DOMAIN')
66
+ rescue IndieAuth::TokenVerification::InsufficentScopeError
67
+ send_error(status: 401, error: 'insufficient_scope', description: 'The scope of this token does not meet the requirements for this request')
68
+ end
69
+ ```
70
+
71
+ ## Development
72
+
73
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
74
+
75
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
76
+
77
+ ## Contributing
78
+
79
+ Bug reports and pull requests are welcome on GitHub at https://github.com/srushe/indieauth-token-verification. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
80
+
81
+ ## License
82
+
83
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "indie_auth/token_verification"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,38 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'indie_auth/token_verification/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "indieauth-token-verification"
8
+ spec.version = IndieAuth::TokenVerification::VERSION
9
+ spec.authors = ["Stephen Rushe"]
10
+ spec.email = ["steve+gemspec@deeden.co.uk"]
11
+
12
+ spec.summary = %q{Perform the access token verification portion of the IndieAuth process.}
13
+ spec.description = %q{Perform the access token verification portion of the IndieAuth process by communicationg with a token endpoint.}
14
+ spec.homepage = "https://github.com/srushe/indieauth-token-verification"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = ENV.fetch('GEMSERVER_URL', 'nohost')
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against " \
23
+ "public gem pushes."
24
+ end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_development_dependency "bundler", "~> 2.0"
34
+ spec.add_development_dependency "climate_control"
35
+ spec.add_development_dependency "pry"
36
+ spec.add_development_dependency "rake", "~> 12.3"
37
+ spec.add_development_dependency "rspec", "~> 3.0"
38
+ end
@@ -0,0 +1,61 @@
1
+ require 'indie_auth/token_verification/version'
2
+ require 'net/http'
3
+ require 'json'
4
+
5
+ module IndieAuth
6
+ class TokenVerification
7
+ class AccessTokenMissingError < StandardError; end
8
+ class ForbiddenUserError < StandardError; end
9
+ class IncorrectMeError < StandardError; end
10
+ class InsufficentScopeError < StandardError; end
11
+ class MissingDomainError < StandardError; end
12
+ class MissingTokenEndpointError < StandardError; end
13
+
14
+ attr_reader :access_token
15
+
16
+ def initialize(access_token)
17
+ @access_token = access_token.to_s.strip.sub(/\ABearer\s*/, '')
18
+ end
19
+
20
+ def verify(desired_scope=nil)
21
+ raise AccessTokenMissingError if access_token.nil? or access_token.empty?
22
+ raise MissingDomainError if ENV.fetch('DOMAIN', nil).nil?
23
+ raise MissingTokenEndpointError if ENV.fetch('TOKEN_ENDPOINT', nil).nil?
24
+
25
+ response = validate_token
26
+ raise ForbiddenUserError unless response.kind_of? Net::HTTPSuccess
27
+
28
+ response_body = JSON.parse(response.body)
29
+ if response_body.fetch('me', nil) != ENV['DOMAIN']
30
+ raise IncorrectMeError, "Expected: '#{ENV['DOMAIN']}', Received: '#{response_body.fetch('me', nil)}'"
31
+ end
32
+
33
+ return true if desired_scope.nil?
34
+ scope_included_in_response?(response_body, desired_scope)
35
+ end
36
+
37
+ private
38
+
39
+ def validate_token
40
+ uri = URI.parse(ENV['TOKEN_ENDPOINT'])
41
+ http = Net::HTTP.new(uri.host, uri.port)
42
+ http.use_ssl = true
43
+ http.get(uri.request_uri, {'Accept' => 'application/json', 'Authorization' => "Bearer #{access_token}"})
44
+ end
45
+
46
+ def scope_included_in_response?(response, desired_scope)
47
+ scopes = scopes_from(response)
48
+
49
+ return true if scopes.include?(desired_scope)
50
+ return true if desired_scope == 'post' && scopes.include?('create')
51
+ raise InsufficentScopeError
52
+ end
53
+
54
+ def scopes_from(response_body)
55
+ return @scopes if defined? @scopes
56
+ raise InsufficentScopeError unless response_body.key?('scope')
57
+
58
+ @scopes ||= response_body['scope'].split
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,5 @@
1
+ module IndieAuth
2
+ class TokenVerification
3
+ VERSION = "0.2.0"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: indieauth-token-verification
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Stephen Rushe
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-02-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: climate_control
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '12.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '12.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description: Perform the access token verification portion of the IndieAuth process
84
+ by communicationg with a token endpoint.
85
+ email:
86
+ - steve+gemspec@deeden.co.uk
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - ".ruby-gemset"
94
+ - ".ruby-version"
95
+ - CHANGELOG.md
96
+ - CODE_OF_CONDUCT.md
97
+ - Gemfile
98
+ - LICENSE.txt
99
+ - README.md
100
+ - Rakefile
101
+ - bin/console
102
+ - bin/setup
103
+ - indieauth-token-verification.gemspec
104
+ - lib/indie_auth/token_verification.rb
105
+ - lib/indie_auth/token_verification/version.rb
106
+ homepage: https://github.com/srushe/indieauth-token-verification
107
+ licenses:
108
+ - MIT
109
+ metadata:
110
+ allowed_push_host: https://rubygems.org/
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 2.7.6
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: Perform the access token verification portion of the IndieAuth process.
131
+ test_files: []