flimper_poncho 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b1ea0837fd9b1a91ba66b8f955f58384d52b2b90910a64f449f09551dc9316fd
4
- data.tar.gz: 49bfe02a089d6d3f838d1e5e72fb0ba4ba4a781a63dd1e6778cfe705a6efee77
3
+ metadata.gz: 7fc01415394547ff040e49b1a262b2fc437dfb5176fc941e066b2a0a2328a050
4
+ data.tar.gz: 8fb8ee3817ad0e76afb7b0b374da4ccfb37fe8cbbaba867d9d019e711ac2bd0f
5
5
  SHA512:
6
- metadata.gz: 8dd3f92abc84aadaec6a61dd3a1b5adf25a3877ffa85badb1299ea3f569d1a635878969dda99968e0f1337853dfdd13bf88538c693e0d31439421420ceee3899
7
- data.tar.gz: d7fafbba43dacdccb320298b76baa9384f7d7a36a8b286c13ab58afb13f37452d5712d2e6e965bd309c832ec81bd0e88ebd5aaaf5b56c8b6dcd0fb0e3c1dc06c
6
+ metadata.gz: 88f9c8adae9140491fea28c79cd41d52e7ef48d361ca2e916b0469548c451898f3f2e78ae3fdea54715d6eedc5d1b464b5a37bd4f7cb155af47f0f3cbbd458ed
7
+ data.tar.gz: 2757b8e1044c7ab2f752e101cd58f8cad1fff4167d9fe6d7afcc2f3b80e5f9311f4a65a667b81b708c641997b671c0203aa710491652c18d077063cfbf80ace1
data/Gemfile.lock CHANGED
@@ -1,39 +1,25 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- flimper_poncho (0.1.0)
5
- active_interaction (~> 3.7)
4
+ flimper_poncho (0.1.1)
6
5
  faraday (~> 0.15.4)
7
6
  jwt (~> 2.1)
8
7
 
9
8
  GEM
10
9
  remote: https://rubygems.org/
11
10
  specs:
12
- active_interaction (3.7.1)
13
- activemodel (>= 4, < 7)
14
- activemodel (5.2.3)
15
- activesupport (= 5.2.3)
16
- activesupport (5.2.3)
17
- concurrent-ruby (~> 1.0, >= 1.0.2)
18
- i18n (>= 0.7, < 2)
19
- minitest (~> 5.1)
20
- tzinfo (~> 1.1)
21
11
  addressable (2.6.0)
22
12
  public_suffix (>= 2.0.2, < 4.0)
23
13
  ast (2.4.0)
24
14
  byebug (11.0.1)
25
- concurrent-ruby (1.1.5)
26
15
  crack (0.4.3)
27
16
  safe_yaml (~> 1.0.0)
28
17
  diff-lcs (1.3)
29
18
  faraday (0.15.4)
30
19
  multipart-post (>= 1.2, < 3)
31
20
  hashdiff (0.3.8)
32
- i18n (1.6.0)
33
- concurrent-ruby (~> 1.0)
34
21
  jaro_winkler (1.5.2)
35
22
  jwt (2.1.0)
36
- minitest (5.11.3)
37
23
  multipart-post (2.0.0)
38
24
  parallel (1.17.0)
39
25
  parser (2.6.2.0)
@@ -65,10 +51,8 @@ GEM
65
51
  unicode-display_width (>= 1.4.0, < 1.6)
66
52
  ruby-progressbar (1.10.0)
67
53
  safe_yaml (1.0.5)
68
- thread_safe (0.3.6)
69
- tzinfo (1.2.5)
70
- thread_safe (~> 0.1)
71
54
  unicode-display_width (1.5.0)
55
+ vcr (4.0.0)
72
56
  webmock (3.5.1)
73
57
  addressable (>= 2.3.6)
74
58
  crack (>= 0.3.2)
@@ -84,6 +68,7 @@ DEPENDENCIES
84
68
  rake (~> 12.3)
85
69
  rspec (~> 3.8)
86
70
  rubocop (~> 0.66.0)
71
+ vcr (~> 4.0)
87
72
  webmock (~> 3.5.1)
88
73
 
89
74
  BUNDLED WITH
data/README.md CHANGED
@@ -28,9 +28,21 @@ Or install it yourself as:
28
28
 
29
29
  ## Usage
30
30
 
31
- - Create a user session:
31
+ - Issue an ApiClient jwt:
32
32
 
33
- `FlimperPoncho::UserSession::Create.run!(email: 'test@flimper.com', password: 'password')`
33
+ `Jwts::Issuers::ForApiClients.new(name: 'FLIMPER-BACK', authorization_token: 'FLIMPER-BACK-AUTHORIZATION-TOKEN').issue!`
34
+
35
+ - Issue a User jwt:
36
+
37
+ `Jwts::Issuers::ForUsers.new(email: 'test@test.com', password: 'password').issue!`
38
+
39
+ - Check ApiClient permission:
40
+
41
+ `Permissions::Checkers::ForApiClients.new(jwt_token: 'valid_jwt', name: 'FLIMPER-BACK', resource_code: 'TWITTER_API_BACKEND', level: 'READ').can_perform_action?`
42
+
43
+ - Check User permission:
44
+
45
+ `Permissions::Checkers::ForUsers.new(jwt_token: 'valid_jwt', email: 'email-1@gmail.com', resource_code: 'application-1', level: 'READ').can_perform_action?`
34
46
 
35
47
  ## Development
36
48
 
@@ -25,9 +25,9 @@ Gem::Specification.new do |spec|
25
25
  spec.add_development_dependency 'rake', '~> 12.3'
26
26
  spec.add_development_dependency 'rspec', '~> 3.8'
27
27
  spec.add_development_dependency 'rubocop', '~> 0.66.0'
28
+ spec.add_development_dependency 'vcr', '~> 4.0'
28
29
  spec.add_development_dependency 'webmock', '~> 3.5.1'
29
30
 
30
- spec.add_dependency 'active_interaction', '~> 3.7'
31
31
  spec.add_dependency 'faraday', '~> 0.15.4'
32
32
  spec.add_dependency 'jwt', '~> 2.1'
33
33
  end
@@ -0,0 +1,20 @@
1
+ require 'flimper_poncho/jwts/issuers/template'
2
+
3
+ module Jwts
4
+ module Issuers
5
+ class ForApiClients < Jwts::Issuers::Template
6
+ def initialize(name:, authorization_token:)
7
+ @name = name
8
+ @authorization_token = authorization_token
9
+ end
10
+
11
+ def poncho_api_endpoint
12
+ "#{ENV.fetch('PONCHO_API_URL')}/api_clients/jwt_tokens.json"
13
+ end
14
+
15
+ def security_data
16
+ { name: @name, authorization_token: @authorization_token }.to_json
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,20 @@
1
+ require 'flimper_poncho/jwts/issuers/template'
2
+
3
+ module Jwts
4
+ module Issuers
5
+ class ForUsers < Jwts::Issuers::Template
6
+ def initialize(email:, password:)
7
+ @email = email
8
+ @password = password
9
+ end
10
+
11
+ def poncho_api_endpoint
12
+ "#{ENV.fetch('PONCHO_API_URL')}/users/jwt_tokens.json"
13
+ end
14
+
15
+ def security_data
16
+ { email: @email, password: @password }.to_json
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,30 @@
1
+ require 'faraday'
2
+ require 'json'
3
+
4
+ module Jwts
5
+ module Issuers
6
+ class Template
7
+ def issue!
8
+ poncho_response = Faraday.new.post do |request|
9
+ request.url(poncho_api_endpoint)
10
+ request.headers['Content-Type'] = 'application/json'
11
+ request.body = security_data
12
+ end
13
+
14
+ return unless poncho_response.success?
15
+
16
+ JSON.parse(poncho_response.body).dig('jwt_token')
17
+ end
18
+
19
+ private
20
+
21
+ def poncho_api_endpoint
22
+ raise NotImplementedError
23
+ end
24
+
25
+ def security_data
26
+ raise NotImplementedError
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,20 @@
1
+ require 'flimper_poncho/permissions/checkers/template'
2
+
3
+ module Permissions
4
+ module Checkers
5
+ class ForApiClients < Permissions::Checkers::Template
6
+ attr_reader :jwt_token
7
+
8
+ def initialize(jwt_token:, resource_code:, level:, name:)
9
+ @jwt_token = jwt_token
10
+ @resource_code = resource_code
11
+ @level = level
12
+ @name = name
13
+ end
14
+
15
+ def permission_query
16
+ { 'name' => @name, 'resource_code' => @resource_code, 'level' => @level }
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,20 @@
1
+ require 'flimper_poncho/permissions/checkers/template'
2
+
3
+ module Permissions
4
+ module Checkers
5
+ class ForUsers < Permissions::Checkers::Template
6
+ attr_reader :jwt_token
7
+
8
+ def initialize(jwt_token:, resource_code:, level:, email:)
9
+ @jwt_token = jwt_token
10
+ @resource_code = resource_code
11
+ @level = level
12
+ @email = email
13
+ end
14
+
15
+ def permission_query
16
+ { 'email' => @email, 'resource_code' => @resource_code, 'level' => @level }
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,31 @@
1
+ require 'jwt'
2
+
3
+ module Permissions
4
+ module Checkers
5
+ class Template
6
+ def can_perform_action?
7
+ permissions.include?(permission_query) || permissions.include?(permission_query.merge('level' => 'ADMIN'))
8
+ rescue JWT::DecodeError
9
+ false
10
+ end
11
+
12
+ private
13
+
14
+ def permissions
15
+ decoded_jwt.dig('permissions') || []
16
+ end
17
+
18
+ def decoded_jwt
19
+ JWT.decode(jwt_token, ENV.fetch('PONCHO_API_SIGNATURE_KEY'), true, algorithm: 'HS256')[0]
20
+ end
21
+
22
+ def permission_query
23
+ raise NotImplementedError
24
+ end
25
+
26
+ def jwt_token
27
+ raise NotImplementedError
28
+ end
29
+ end
30
+ end
31
+ end
@@ -1,3 +1,3 @@
1
1
  module FlimperPoncho
2
- VERSION = '0.1.0'.freeze
2
+ VERSION = '0.1.1'.freeze
3
3
  end
@@ -1,5 +1,8 @@
1
1
  require 'flimper_poncho/version'
2
- require 'flimper_poncho/user_session/create'
2
+ require 'flimper_poncho/jwts/issuers/for_api_clients'
3
+ require 'flimper_poncho/jwts/issuers/for_users'
4
+ require 'flimper_poncho/permissions/checkers/for_api_clients'
5
+ require 'flimper_poncho/permissions/checkers/for_users'
3
6
 
4
7
  module FlimperPoncho
5
8
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flimper_poncho
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pablo Gonzaga
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-04-01 00:00:00.000000000 Z
11
+ date: 2019-04-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -81,33 +81,33 @@ dependencies:
81
81
  - !ruby/object:Gem::Version
82
82
  version: 0.66.0
83
83
  - !ruby/object:Gem::Dependency
84
- name: webmock
84
+ name: vcr
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - "~>"
88
88
  - !ruby/object:Gem::Version
89
- version: 3.5.1
89
+ version: '4.0'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
- version: 3.5.1
96
+ version: '4.0'
97
97
  - !ruby/object:Gem::Dependency
98
- name: active_interaction
98
+ name: webmock
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
101
  - - "~>"
102
102
  - !ruby/object:Gem::Version
103
- version: '3.7'
104
- type: :runtime
103
+ version: 3.5.1
104
+ type: :development
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
108
  - - "~>"
109
109
  - !ruby/object:Gem::Version
110
- version: '3.7'
110
+ version: 3.5.1
111
111
  - !ruby/object:Gem::Dependency
112
112
  name: faraday
113
113
  requirement: !ruby/object:Gem::Requirement
@@ -157,8 +157,12 @@ files:
157
157
  - bin/setup
158
158
  - flimper_poncho.gemspec
159
159
  - lib/flimper_poncho.rb
160
- - lib/flimper_poncho/entities/user_session.rb
161
- - lib/flimper_poncho/user_session/create.rb
160
+ - lib/flimper_poncho/jwts/issuers/for_api_clients.rb
161
+ - lib/flimper_poncho/jwts/issuers/for_users.rb
162
+ - lib/flimper_poncho/jwts/issuers/template.rb
163
+ - lib/flimper_poncho/permissions/checkers/for_api_clients.rb
164
+ - lib/flimper_poncho/permissions/checkers/for_users.rb
165
+ - lib/flimper_poncho/permissions/checkers/template.rb
162
166
  - lib/flimper_poncho/version.rb
163
167
  homepage: https://github.com/flimperapp/poncho_gem
164
168
  licenses:
@@ -1,26 +0,0 @@
1
- require 'jwt'
2
-
3
- module FlimperPoncho
4
- module Entities
5
- class UserSession
6
- attr_reader :email, :authorization_token, :permissions
7
-
8
- def initialize(jwt:)
9
- @jwt = jwt
10
- @authorization_token = jwt_decode.dig('authorization_token')
11
- @email = jwt_decode.dig('email')
12
-
13
- @permissions = jwt_decode.dig('permissions').map do |permission|
14
- { email: permission.dig('email'), resource_code: permission.dig('resource_code'),
15
- level: permission.dig('level') }
16
- end
17
- end
18
-
19
- private
20
-
21
- def jwt_decode
22
- @jwt_decode ||= JWT.decode(@jwt, ENV.fetch('PONCHO_API_SIGNATURE_KEY'), true, algorithm: 'HS256')[0]
23
- end
24
- end
25
- end
26
- end
@@ -1,39 +0,0 @@
1
- require 'active_interaction'
2
- require 'faraday'
3
- require 'json'
4
- require 'flimper_poncho/entities/user_session'
5
-
6
- module FlimperPoncho
7
- module UserSession
8
- class Create < ActiveInteraction::Base
9
- string :email
10
- string :password
11
-
12
- validates :email, :password, presence: true
13
-
14
- def execute
15
- poncho_response = faraday_connection.post do |request|
16
- request.url(poncho_api_url)
17
- request.headers['Content-Type'] = 'application/json'
18
- request.body = { email: email, password: password }.to_json
19
- end
20
-
21
- return unless poncho_response.success?
22
-
23
- FlimperPoncho::Entities::UserSession.new(jwt: JSON.parse(poncho_response.body).dig('jwt_token'))
24
- rescue KeyError => error
25
- errors.add(:poncho_api_url, error.message)
26
- end
27
-
28
- private
29
-
30
- def poncho_api_url
31
- @poncho_api_url ||= "#{ENV.fetch('PONCHO_API_URL')}/user_sessions"
32
- end
33
-
34
- def faraday_connection
35
- @faraday_connection ||= Faraday.new
36
- end
37
- end
38
- end
39
- end