Skip to content

Commit de49d7f

Browse files
authored
authentication setup, App-Worker resource and webmock (#3)
* authentication setup and also added App-Worker resource * Update README.md
1 parent aa01cfd commit de49d7f

17 files changed

+284
-14
lines changed

.rubocop.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,9 @@ RSpec/FilePath:
110110

111111
Style/Documentation:
112112
Enabled: false
113+
114+
RSpec/ExampleLength:
115+
Enabled: false
116+
117+
RSpec/MultipleExpectations:
118+
Enabled: false

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ gem 'rake', '~> 12.0'
77
gem 'rspec', '~> 3.0'
88

99
group :development do
10+
gem 'pry'
1011
gem 'rubocop', require: false
1112
gem 'rubocop-rspec'
13+
gem 'webmock'
1214
end

Gemfile.lock

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,53 @@ PATH
22
remote: .
33
specs:
44
cirro-ruby-client (0.1.0)
5+
json_api_client
6+
jwt
57

68
GEM
79
remote: https://rubygems.org/
810
specs:
11+
activemodel (6.0.3.3)
12+
activesupport (= 6.0.3.3)
13+
activesupport (6.0.3.3)
14+
concurrent-ruby (~> 1.0, >= 1.0.2)
15+
i18n (>= 0.7, < 2)
16+
minitest (~> 5.1)
17+
tzinfo (~> 1.1)
18+
zeitwerk (~> 2.2, >= 2.2.2)
19+
addressable (2.7.0)
20+
public_suffix (>= 2.0.2, < 5.0)
921
ast (2.4.1)
22+
coderay (1.1.3)
23+
concurrent-ruby (1.1.7)
24+
crack (0.4.4)
1025
diff-lcs (1.4.4)
26+
faraday (1.0.1)
27+
multipart-post (>= 1.2, < 3)
28+
faraday_middleware (1.0.0)
29+
faraday (~> 1.0)
30+
hashdiff (1.0.1)
31+
i18n (1.8.5)
32+
concurrent-ruby (~> 1.0)
33+
json_api_client (1.17.1)
34+
activemodel (>= 3.2.0)
35+
activesupport (>= 3.2.0)
36+
addressable (~> 2.2)
37+
faraday (>= 0.15.2, < 1.2.0)
38+
faraday_middleware (>= 0.9.0, < 1.2.0)
39+
rack (>= 0.2)
40+
jwt (2.2.2)
41+
method_source (1.0.0)
42+
minitest (5.14.2)
43+
multipart-post (2.1.1)
1144
parallel (1.19.2)
1245
parser (2.7.2.0)
1346
ast (~> 2.4.1)
47+
pry (0.13.1)
48+
coderay (~> 1.1)
49+
method_source (~> 1.0)
50+
public_suffix (4.0.6)
51+
rack (2.2.3)
1452
rainbow (3.0.0)
1553
rake (12.3.3)
1654
regexp_parser (1.8.1)
@@ -42,17 +80,27 @@ GEM
4280
rubocop-rspec (1.43.2)
4381
rubocop (~> 0.87)
4482
ruby-progressbar (1.10.1)
83+
thread_safe (0.3.6)
84+
tzinfo (1.2.7)
85+
thread_safe (~> 0.1)
4586
unicode-display_width (1.7.0)
87+
webmock (3.9.1)
88+
addressable (>= 2.3.6)
89+
crack (>= 0.3.2)
90+
hashdiff (>= 0.4.0, < 2.0.0)
91+
zeitwerk (2.4.0)
4692

4793
PLATFORMS
4894
ruby
4995

5096
DEPENDENCIES
5197
cirro-ruby-client!
98+
pry
5299
rake (~> 12.0)
53100
rspec (~> 3.0)
54101
rubocop
55102
rubocop-rspec
103+
webmock
56104

57105
BUNDLED WITH
58106
2.1.4

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@ Or install it yourself as:
1919
$ gem install cirro-ruby-client
2020

2121

22+
## Configuration
23+
24+
You need to create an initializer file in config/initializers.
25+
26+
```ruby
27+
CirroIO::Client.configure do |c|
28+
c.app_id 'WULnc6Y0rlaTBCSiHAb0kGWKFuIxPWBXJysyZeG3Rtw'
29+
c.private_key_path './storage/cirro.pem'
30+
c.site 'https://api.staging.cirro.io'
31+
end
32+
```
33+
34+
2235
## Development
2336

2437
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.

cirro-ruby-client.gemspec

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,7 @@ Gem::Specification.new do |spec|
2424
spec.bindir = 'exe'
2525
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
2626
spec.require_paths = ['lib']
27+
28+
spec.add_runtime_dependency 'jwt'
29+
spec.add_runtime_dependency 'json_api_client'
2730
end

lib/cirro_io/client.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
1+
require 'json_api_client'
12
require 'cirro_io/client/version'
3+
require 'cirro_io/client/configuration'
4+
require 'cirro_io/client/response_debugging_middleware'
5+
require 'cirro_io/client/jwt_authentication'
6+
require 'cirro_io/client/base'
7+
require 'cirro_io/client/app_worker'
28

39
module CirroIO
410
module Client
511
class Error < StandardError; end
6-
# Your code goes here...
12+
13+
def self.configure
14+
yield configuration if block_given?
15+
Base.site = "#{configuration.site}/#{configuration.api_version}"
16+
end
17+
18+
def self.configuration
19+
@configuration ||= Configuration.new
20+
end
721
end
822
end

lib/cirro_io/client/app_worker.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module CirroIO
2+
module Client
3+
class AppWorker < Base
4+
def self.resource_name
5+
'app-workers'
6+
end
7+
end
8+
end
9+
end

lib/cirro_io/client/base.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module CirroIO
2+
module Client
3+
class Base < JsonApiClient::Resource
4+
self.route_format = :dasherized_route
5+
self.json_key_format = :dasherized_key
6+
7+
# https://github.com/JsonApiClient/json_api_client/issues/215
8+
def self.site=(url)
9+
super(url)
10+
connection.faraday.url_prefix = url
11+
end
12+
end
13+
end
14+
end
15+
16+
CirroIO::Client::Base.connection do |connection|
17+
connection.use CirroIO::Client::JwtAuthentication
18+
connection.use Faraday::Response::Logger
19+
# connection.use CirroIO::Client::ResponseDebuggingMiddleware # This middleware can be injected during debugging or while adding new specs
20+
end

lib/cirro_io/client/configuration.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ def private_key_path(value = nil)
1414

1515
@private_key_path
1616
end
17+
18+
def site(value = nil)
19+
@site = value unless value.nil?
20+
raise 'site must be defined' unless defined?(@site)
21+
22+
@site
23+
end
24+
25+
def api_version
26+
'v1'
27+
end
1728
end
1829
end
1930
end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
require 'jwt'
2+
require 'openssl'
3+
4+
module CirroIO
5+
module Client
6+
class JwtAuthentication < Faraday::Middleware
7+
def call(env)
8+
private_pem = File.read(CirroIO::Client.configuration.private_key_path)
9+
private_key = OpenSSL::PKey::RSA.new(private_pem)
10+
11+
payload = {
12+
# JWT expiration time (10 minute maximum)
13+
exp: Time.now.to_i + (10 * 60),
14+
# App client id
15+
iss: CirroIO::Client.configuration.app_id,
16+
}
17+
18+
token = JWT.encode(payload, private_key, 'RS256')
19+
20+
env[:request_headers]['Authorization'] = "Bearer #{token}"
21+
@app.call(env)
22+
end
23+
end
24+
end
25+
end

0 commit comments

Comments
 (0)