authentic-jwt 0.0.2

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
+ SHA1:
3
+ metadata.gz: c9553910f0f78a9ff6cd893059e887bf5455a3b3
4
+ data.tar.gz: d6b336cabdb1df56705a9f879838938313ba1e4a
5
+ SHA512:
6
+ metadata.gz: 5cc805a9829594ea77750b7a1b97cfa8e3bc3a4479ad5dc7c83ff63d38c19ccde34c22be37dcc5943e53418db397f63ec17faa08e9fa71e8dd5986b53da20a9f
7
+ data.tar.gz: 6c5eb72edca91f1adfe558f9cef58eec3e9293af0a4035ed9ffb9473259028364a6029a3b2be38bc04faa0721a23e49703055cab9e12b3f9cc1904d2b8bf87fd
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /Gemfile.lock
7
+ /jars/*.jar
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,49 @@
1
+ Style/StringLiterals:
2
+ EnforcedStyle: double_quotes
3
+ Style/WordArray:
4
+ Enabled: false
5
+ Metrics/LineLength:
6
+ Enabled: false
7
+ Style/TrailingCommaInLiteral:
8
+ Enabled: false
9
+ Style/TrailingCommaInArguments:
10
+ Enabled: false
11
+ Style/EmptyLinesAroundClassBody:
12
+ Enabled: false
13
+ Style/EmptyLinesAroundModuleBody:
14
+ Enabled: false
15
+ Style/EmptyLinesAroundBlockBody:
16
+ Enabled: false
17
+ Style/CaseIndentation:
18
+ Enabled: false
19
+ Style/Documentation:
20
+ Enabled: false
21
+ Style/MultilineOperationIndentation:
22
+ EnforcedStyle: indented
23
+ Style/RegexpLiteral:
24
+ Enabled: false
25
+ Style/IfUnlessModifier:
26
+ Enabled: false
27
+ Style/TrivialAccessors:
28
+ AllowPredicates: true
29
+ Style/BracesAroundHashParameters:
30
+ Enabled: false
31
+ Style/IndentHash:
32
+ Enabled: false
33
+ Style/NumericLiterals:
34
+ Enabled: false
35
+ AllCops:
36
+ TargetRubyVersion: 2.2
37
+ Exclude:
38
+ - 'bin/**/*'
39
+ - 'vendor/**/*'
40
+ Style/ExtraSpacing:
41
+ Enabled: false
42
+ Style/DotPosition:
43
+ Enabled: false
44
+ Style/SpaceInsideBlockBraces:
45
+ Enabled: false
46
+ Style/SpaceInsideHashLiteralBraces:
47
+ Enabled: false
48
+ Style/AlignHash:
49
+ Enabled: false
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.3
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ sudo: false
2
+ language: ruby
3
+ matrix:
4
+ include:
5
+ - rvm: jruby-9.1.5.0
6
+ jdk: oraclejdk8
7
+ before_install: gem install bundler
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/README.md ADDED
@@ -0,0 +1,25 @@
1
+ # AuthenticJwt
2
+
3
+ TODO
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem "authentic-jwt"
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ TODO: Write usage instructions here
16
+
17
+ ## Development
18
+
19
+ 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.
20
+
21
+ 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).
22
+
23
+ ## Contributing
24
+
25
+ Bug reports and pull requests are welcome on GitHub at https://github.com/mytours/authentic-jwt.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
7
+
8
+ Dir.glob("./lib/tasks/**/*.rake").each { |r| import r }
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "authentic_jwt/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "authentic-jwt"
8
+ spec.version = AuthenticJwt::VERSION
9
+ spec.authors = ["Authentic Limited"]
10
+ spec.email = ["rubygems@kotiri.com"]
11
+
12
+ spec.summary = "Client authentication for Authentic Apps"
13
+ spec.description = spec.summary
14
+ spec.homepage = "https://github.com/mytours/authentic-jwt"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "jwt"
22
+ spec.add_dependency "multi_json"
23
+
24
+ spec.add_development_dependency "awesome_print"
25
+ spec.add_development_dependency "bundler"
26
+ spec.add_development_dependency "pry"
27
+ spec.add_development_dependency "rake"
28
+ spec.add_development_dependency "rspec"
29
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "authentic-jwt"
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
data/bin/setup ADDED
@@ -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,3 @@
1
+ require "authentic_jwt/version"
2
+ require "authentic_jwt/errors"
3
+ require "authentic_jwt/role"
@@ -0,0 +1,4 @@
1
+ module AuthenticJwt
2
+ class Unauthorized < RuntimeError; end
3
+ class Forbidden < RuntimeError; end
4
+ end
@@ -0,0 +1,13 @@
1
+ module AuthenticJwt
2
+ module Grape
3
+ module AuthMethods
4
+ attr_accessor :jwt_payload
5
+
6
+ def jwt_user_id
7
+ return unless jwt_payload
8
+ return unless jwt_payload["id"]
9
+ jwt_payload["id"].to_i
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,14 @@
1
+ require "grape"
2
+
3
+ module AuthenticJwt
4
+ module Grape
5
+ module Extension
6
+ def oauth2(value)
7
+ route_setting(:oauth2, scope: value)
8
+ value
9
+ end
10
+
11
+ ::Grape::API.extend self
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,103 @@
1
+ require "authentic_jwt/grape/extension"
2
+ require "authentic_jwt/grape/auth_methods"
3
+ require "openssl"
4
+ require "jwt"
5
+
6
+ module AuthenticJwt
7
+ module Grape
8
+ class Middleware < ::Grape::Middleware::Base
9
+ def before
10
+ return unless scope
11
+
12
+ raise Unauthorized, "JWT public key not present" unless public_key
13
+
14
+ raise Unauthorized, "Authorization header not present" unless authorization_header
15
+
16
+ raise Unauthorized, "Bearer token not present" unless bearer_token
17
+
18
+ raise Unauthorized, "JWT payload not present" unless jwt_payload
19
+
20
+ context.extend(AuthMethods)
21
+ context.jwt_payload = jwt_payload
22
+
23
+ return unless account_id
24
+
25
+ raise Forbidden, "Account has no role" unless account_role
26
+
27
+ raise Forbidden, "Account role is too low" unless acceptable_roles.include?(account_role)
28
+ end
29
+
30
+ protected
31
+
32
+ PUBLIC_KEY_ENV_VAR = "AUTHENTIC_AUTH_PUBLIC_KEY".freeze
33
+ ACCOUNT_ID_ENV_VAR = "AUTHENTIC_AUTH_ACCOUNT_ID".freeze
34
+ BEARER_PATTERN = /Bearer (.+)/
35
+
36
+ def context
37
+ env["api.endpoint"]
38
+ end
39
+
40
+ def authorization_header
41
+ return if env["HTTP_AUTHORIZATION"].to_s.empty?
42
+ env["HTTP_AUTHORIZATION"]
43
+ end
44
+
45
+ def route_setting
46
+ context.route_setting(:oauth2)
47
+ end
48
+
49
+ def scope
50
+ return unless route_setting
51
+ route_setting.fetch(:scope, nil)
52
+ end
53
+
54
+ def bearer_token
55
+ return unless authorization_header
56
+ if authorization_header =~ BEARER_PATTERN
57
+ result = Regexp.last_match(1)
58
+ unless result.to_s.empty?
59
+ result
60
+ end
61
+ end
62
+ end
63
+
64
+ def public_key
65
+ result = ENV[PUBLIC_KEY_ENV_VAR].to_s
66
+ return if result.empty?
67
+ OpenSSL::PKey::RSA.new(result)
68
+ end
69
+
70
+ def jwt_payload
71
+ return unless bearer_token
72
+ return unless public_key
73
+ payload, header = JWT.decode(bearer_token, public_key, true, algorithm: "RS512")
74
+ payload
75
+ end
76
+
77
+ def account_id
78
+ result = ENV[ACCOUNT_ID_ENV_VAR].to_s
79
+ return if result.empty?
80
+ result.to_i
81
+ end
82
+
83
+ def account_payload
84
+ return unless jwt_payload
85
+ jwt_payload["accounts"].detect { |account| account["id"] == account_id }
86
+ end
87
+
88
+ def account_role
89
+ return unless account_payload
90
+ account_payload["role"]
91
+ end
92
+
93
+ def acceptable_roles
94
+ return [] unless scope
95
+ case scope
96
+ when "read" then AuthenticJwt::Role.read
97
+ when "write" then AuthenticJwt::Role.write
98
+ else raise ArgumentError
99
+ end
100
+ end
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,34 @@
1
+ module AuthenticJwt
2
+ module Role
3
+ def self.roles
4
+ MAPPING.keys
5
+ end
6
+
7
+ def self.mapping
8
+ MAPPING
9
+ end
10
+
11
+ def self.read
12
+ READ + WRITE
13
+ end
14
+
15
+ def self.write
16
+ WRITE
17
+ end
18
+
19
+ protected
20
+
21
+ READ = ["subscriber"].freeze
22
+ WRITE = ["contributor", "author", "editor", "partner", "admin", "internal"].freeze
23
+
24
+ MAPPING = {
25
+ "subscriber" => 10,
26
+ "contributor" => 20,
27
+ "author" => 30,
28
+ "editor" => 40,
29
+ "partner" => 70,
30
+ "admin" => 80,
31
+ "internal" => 90
32
+ }.freeze
33
+ end
34
+ end
@@ -0,0 +1,3 @@
1
+ module AuthenticJwt
2
+ VERSION = "0.0.2".freeze
3
+ end
metadata ADDED
@@ -0,0 +1,159 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: authentic-jwt
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Authentic Limited
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-01-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jwt
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: multi_json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
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: awesome_print
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: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Client authentication for Authentic Apps
112
+ email:
113
+ - rubygems@kotiri.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".rubocop.yml"
121
+ - ".ruby-version"
122
+ - ".travis.yml"
123
+ - Gemfile
124
+ - README.md
125
+ - Rakefile
126
+ - authentic-jwt.gemspec
127
+ - bin/console
128
+ - bin/setup
129
+ - lib/authentic-jwt.rb
130
+ - lib/authentic_jwt/errors.rb
131
+ - lib/authentic_jwt/grape/auth_methods.rb
132
+ - lib/authentic_jwt/grape/extension.rb
133
+ - lib/authentic_jwt/grape/middleware.rb
134
+ - lib/authentic_jwt/role.rb
135
+ - lib/authentic_jwt/version.rb
136
+ homepage: https://github.com/mytours/authentic-jwt
137
+ licenses: []
138
+ metadata: {}
139
+ post_install_message:
140
+ rdoc_options: []
141
+ require_paths:
142
+ - lib
143
+ required_ruby_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ required_rubygems_version: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ requirements: []
154
+ rubyforge_project:
155
+ rubygems_version: 2.6.8
156
+ signing_key:
157
+ specification_version: 4
158
+ summary: Client authentication for Authentic Apps
159
+ test_files: []