zaikio-oauth_client 0.3.5 → 0.4.0

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: 18ff86a2a9302a3e1b9b1d26713b63c95d0c88835760d0fd10be57167154d5ec
4
- data.tar.gz: 1cefd5a0abb861d6a9d2dbdb45c80d9f6ba592b250be72527be9bbaacc924a39
3
+ metadata.gz: a0941f7172aa5d44ab3548647ddb08818ba3f49b3d16b6362c4763056da0ce81
4
+ data.tar.gz: d8354f0e4f1e4a29c3340f9db82e6b257f8cd0af611aa856da5c7db35e6d9101
5
5
  SHA512:
6
- metadata.gz: 9d4623a440c5ecb8d989df4757f77fd890d15272d2a8e1af077ec0da77292f488c24f239926072c517803cba22ef750a8f14c66f9f5e765e076136081813f8d6
7
- data.tar.gz: 1e77a452cc6fa4a2b40be38ede7c5115d36e0e113a682e165c7c7a0ca57c758077644a50d9bbd034275d0ba002344ebd1b5e9b9702f2618712b4801a00498900
6
+ metadata.gz: 8239d18c9d17d868190adb4d6430241684fb6f8b9333c3b56e28161f9bfe2127a1a6284165559244e507dec7bef4904d4497c100d964057f5fde051e16e754fc
7
+ data.tar.gz: ba022530c742210508279d61937a3d0f330ecea230682e2d54373dc2479cf1016d95428df33429bafa03dafe8b1cb461f6a3ae5c41c1ffc6ddc8b9eef33257cd
data/README.md CHANGED
@@ -26,6 +26,8 @@ This will create the tables:
26
26
 
27
27
  ### 2. Mount routes
28
28
 
29
+ Add this to `config/routes.rb`:
30
+
29
31
  ```rb
30
32
  mount Zaikio::OAuthClient::Engine => "/zaikio"
31
33
  ```
@@ -35,7 +37,7 @@ mount Zaikio::OAuthClient::Engine => "/zaikio"
35
37
  ```rb
36
38
  # config/initializers/zaikio_oauth_client.rb
37
39
  Zaikio::OAuthClient.configure do |config|
38
- config.environment = :test
40
+ config.environment = :sandbox
39
41
 
40
42
  config.register_client :warehouse do |warehouse|
41
43
  warehouse.client_id = "52022d7a-7ba2-41ed-8890-97d88e6472f6"
@@ -65,6 +67,28 @@ Zaikio::OAuthClient.configure do |config|
65
67
  end
66
68
  ```
67
69
 
70
+
71
+ ### 4. Clean up outdated access tokens (recommended)
72
+
73
+ To avoid keeping all expired oath and refresh tokens in your database, we recommend to implement their scheduled deletion. We recommend therefore to use a schedule gems such as [sidekiq](https://github.com/mperham/sidekiq) and [sidekiq-scheduler](https://github.com/moove-it/sidekiq-scheduler).
74
+
75
+ Simply add the following to your Gemfile:
76
+
77
+ ```rb
78
+ gem "sidekiq"
79
+ gem "sidekiq-scheduler"
80
+ ```
81
+ Then run `bundle install`.
82
+
83
+ Configure sidekiq scheduler in `config/sidekiq.yml`:
84
+ ```yaml
85
+ :schedule:
86
+ cleanup_acces_tokens_job:
87
+ cron: '0 3 * * *' # This will delete all expired tokens every day at 3am.
88
+ class: 'Zaikio::CleanupAccessTokensJob'
89
+ ```
90
+
91
+
68
92
  ## Usage
69
93
 
70
94
  ### OAuth Flow
@@ -95,6 +119,24 @@ You can then use `Current.user` anywhere.
95
119
 
96
120
  For **logout** use: `zaikio_oauth_client.session_path, method: :delete` or build your own controller for deleting the cookie.
97
121
 
122
+ #### Multiple clients
123
+
124
+ When performing requests against directory APIs, it is important to always provide the correct client in order to use the client credentials flow correctly. Otherwise always the first client will be used. It is recommended to specify an `around_action`:
125
+
126
+ ```rb
127
+ class ApplicationController < ActionController::Base
128
+ around_action :with_client
129
+
130
+ private
131
+
132
+ def with_client
133
+ Zaikio::OAuthClient.with_client Current.client_name do
134
+ yield
135
+ end
136
+ end
137
+ end
138
+ ```
139
+
98
140
  #### Redirecting
99
141
 
100
142
  The `zaikio_oauth_client.new_session_path` which was used for the first initiation of the OAuth flow, accepts an optional parameter `origin` which will then be used to redirect the user at the end of a completed & successful OAuth flow.
@@ -0,0 +1,7 @@
1
+ module Zaikio
2
+ class CleanupAccessTokensJob < ApplicationJob
3
+ def perform
4
+ Zaikio::AccessToken.with_invalid_refresh_token.delete_all
5
+ end
6
+ end
7
+ end
@@ -28,10 +28,14 @@ module Zaikio
28
28
  where("expires_at > :now", now: Time.current)
29
29
  .where.not(id: Zaikio::JWTAuth.blacklisted_token_ids)
30
30
  }
31
+ scope :with_invalid_refresh_token, lambda {
32
+ where("created_at <= ?", Time.current - Zaikio::AccessToken.refresh_token_valid_for)
33
+ }
31
34
  scope :valid_refresh, lambda {
32
35
  where("expires_at <= :now AND created_at > :created_at_max",
33
36
  now: Time.current,
34
37
  created_at_max: Time.current - refresh_token_valid_for)
38
+ .where("refresh_token IS NOT NULL")
35
39
  .where.not(id: Zaikio::JWTAuth.blacklisted_token_ids)
36
40
  }
37
41
  scope :by_bearer, lambda { |bearer_type: "Person", bearer_id:, scopes: []|
@@ -7,14 +7,17 @@ require "zaikio/oauth_client/authenticatable"
7
7
  module Zaikio
8
8
  module OAuthClient
9
9
  class << self
10
- attr_accessor :configuration
11
10
  attr_reader :client_name
12
11
 
13
12
  def configure
14
- self.configuration ||= Configuration.new
13
+ @configuration ||= Configuration.new
15
14
  yield(configuration)
16
15
  end
17
16
 
17
+ def configuration
18
+ @configuration ||= Configuration.new
19
+ end
20
+
18
21
  def for(client_name = nil)
19
22
  client_config_for(client_name).oauth_client
20
23
  end
@@ -31,10 +34,11 @@ module Zaikio
31
34
  end
32
35
 
33
36
  def with_client(client_name)
37
+ original_client_name = @client_name || nil
34
38
  @client_name = client_name
35
39
  yield
36
40
  ensure
37
- @client_name = nil
41
+ @client_name = original_client_name
38
42
  end
39
43
 
40
44
  def with_auth(options_or_access_token, &block)
@@ -5,11 +5,11 @@ module Zaikio
5
5
  module OAuthClient
6
6
  class Configuration
7
7
  HOSTS = {
8
- development: "http://directory.zaikio.test",
9
- test: "http://directory.zaikio.test",
10
- staging: "https://directory.staging.zaikio.com",
11
- sandbox: "https://directory.sandbox.zaikio.com",
12
- production: "https://directory.zaikio.com"
8
+ development: "http://hub.zaikio.test",
9
+ test: "http://hub.zaikio.test",
10
+ staging: "https://hub.staging.zaikio.com",
11
+ sandbox: "https://hub.sandbox.zaikio.com",
12
+ production: "https://hub.zaikio.com"
13
13
  }.freeze
14
14
 
15
15
  attr_accessor :host
@@ -1,5 +1,5 @@
1
1
  module Zaikio
2
2
  module OAuthClient
3
- VERSION = "0.3.5".freeze
3
+ VERSION = "0.4.0".freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zaikio-oauth_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.5
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zaikio GmbH
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-28 00:00:00.000000000 Z
11
+ date: 2020-08-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -42,16 +42,22 @@ dependencies:
42
42
  name: zaikio-jwt_auth
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: 0.2.1
48
+ - - "<"
49
+ - !ruby/object:Gem::Version
50
+ version: 0.4.0
48
51
  type: :runtime
49
52
  prerelease: false
50
53
  version_requirements: !ruby/object:Gem::Requirement
51
54
  requirements:
52
- - - "~>"
55
+ - - ">="
53
56
  - !ruby/object:Gem::Version
54
57
  version: 0.2.1
58
+ - - "<"
59
+ - !ruby/object:Gem::Version
60
+ version: 0.4.0
55
61
  - !ruby/object:Gem::Dependency
56
62
  name: pg
57
63
  requirement: !ruby/object:Gem::Requirement
@@ -98,6 +104,7 @@ files:
98
104
  - app/controllers/zaikio/oauth_client/sessions_controller.rb
99
105
  - app/helpers/zaikio/application_helper.rb
100
106
  - app/jobs/zaikio/application_job.rb
107
+ - app/jobs/zaikio/cleanup_access_tokens_job.rb
101
108
  - app/models/zaikio/access_token.rb
102
109
  - config/initializers/inflections.rb
103
110
  - config/locales/en.yml
@@ -112,7 +119,7 @@ files:
112
119
  - lib/zaikio/oauth_client/engine.rb
113
120
  - lib/zaikio/oauth_client/test_helper.rb
114
121
  - lib/zaikio/oauth_client/version.rb
115
- homepage: https://crispymtn.com
122
+ homepage: https://www.zaikio.com
116
123
  licenses:
117
124
  - MIT
118
125
  metadata: {}
@@ -131,7 +138,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
131
138
  - !ruby/object:Gem::Version
132
139
  version: '0'
133
140
  requirements: []
134
- rubygems_version: 3.1.2
141
+ rubygems_version: 3.0.3
135
142
  signing_key:
136
143
  specification_version: 4
137
144
  summary: Zaikio Platform Connectivity