omniauth-applicaster 1.1.1 → 1.2.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
  SHA1:
3
- metadata.gz: 4d0a3317532707cf06cf526f9fd1415f42192319
4
- data.tar.gz: 5d82821d7a9f287c4323992430277ce271eba424
3
+ metadata.gz: c557d14bae9ae9c5e9f67d0008d5375ac694ccd1
4
+ data.tar.gz: a434435c67e5391ed860c91f7e654d110fc62755
5
5
  SHA512:
6
- metadata.gz: bb5b8fbcdad31b8183625d25720d07645eacc6bc17a67147ad9d7238665e3dab674a9d25166a428525bdfb19e529624d3b8cc5e7fc5bf58529c472acc589e08b
7
- data.tar.gz: 044fd3eade0aba820f4ef97b91bf1b2b5dd1492d04f81e267b8c859dc531a4360f38d87eaa4c72559cc08684fb65933a42d101cfa25f75db6e60d9baaa3ab6f3
6
+ metadata.gz: 4e1b369b1647364acf5c804a51c926abb9cd811ba6704e9580dc3c54aedac3a6bec9ec6072812adab7c2ee103680ab44f2a36e323a624474d2a1c35e3b977634
7
+ data.tar.gz: 5dffb8080ed50ced4bc38a107752ac7c0d09635a7e6d9593af295b054047cec3d1859329bd77d5c94a734629142a6d95bc9213bfa4ded7ca2c232ffbdbbfbbfc
data/README.md CHANGED
@@ -13,29 +13,27 @@ gem 'omniauth-applicaster'
13
13
 
14
14
  ## Usage
15
15
 
16
- ### Configuration
17
-
18
- The OAuth client ID and client secret are read from the environment variables
19
- `ACCOUNTS_CLIENT_ID` and `ACCOUNTS_CLIENT_SECRET` respectivly.
16
+ ### Omniauth strategy in Rails
20
17
 
21
- The gem uses `https://accounts2.applicaster.com` as the site's endpoint by
22
- default to change this set the `ACCOUNTS_BASE_URL` environment variable. This is
23
- useful for example when running a local version of the accounts service
18
+ See [Omniauth](https://github.com/intridea/omniauth) for setting up omniauth.
24
19
 
25
- ### Omniauth strategy
20
+ ```ruby
21
+ # config/initializers/applicaster.rb
26
22
 
27
- See [Omniauth](https://github.com/intridea/omniauth) for setting up omniauth.
23
+ Applicaster::Accounts.configure do |config|
24
+ config.client_id = "my-service-uid"
25
+ config.client_secret = "my-service-secret"
28
26
 
29
- In Rails, you will need something along the lines of:
27
+ #use local accounts service with Pow when in development
28
+ config.base_url = "http://accounts2.dev/" if Rails.env.development?
29
+ end
30
+ ```
30
31
 
31
32
  ```ruby
32
- ENV["ACCOUNTS_CLIENT_ID"] = "my-service-uid"
33
- ENV["ACCOUNTS_CLIENT_SECRET"] = "my-service-secret"
33
+ # config/initializers/omniauth.rb
34
34
 
35
35
  Rails.application.config.middleware.use OmniAuth::Builder do
36
- provider :applicaster,
37
- ENV["ACCOUNTS_CLIENT_ID"],
38
- ENV["ACCOUNTS_CLIENT_SECRET"]
36
+ provider :applicaster
39
37
  end
40
38
  ```
41
39
 
@@ -67,6 +65,21 @@ MyApp::Application.routes.draw do
67
65
  end
68
66
  ```
69
67
 
68
+ ### Configuration
69
+
70
+ For the possible configuration options please see
71
+ [Applicaster::Accounts::Configuration](lib/applicaster/accounts/configuration.rb)
72
+
73
+ If not provided via the configuration block, the OAuth client ID and client
74
+ secret are read from the environment variables `ACCOUNTS_CLIENT_ID` and
75
+ `ACCOUNTS_CLIENT_SECRET` respectivly.
76
+
77
+ The gem uses `https://accounts2.applicaster.com` as the site's endpoint by
78
+ default to change this use the `base_url` config option or set the
79
+ `ACCOUNTS_BASE_URL` environment variable. This is useful for example when
80
+ running a local version of the accounts service
81
+
82
+
70
83
  ### Accounts SDK
71
84
 
72
85
  #### List all available accounts
@@ -83,7 +96,7 @@ end
83
96
 
84
97
  ```ruby
85
98
  user = Applicaster::Accounts.user_from_token(access_token)
86
- # user is an Applicaster::Accounts::User instnce
99
+ # user is an Applicaster::Accounts::User instance
87
100
  ```
88
101
 
89
102
  ## Contributing
@@ -6,37 +6,33 @@ module Applicaster
6
6
  class Accounts
7
7
  autoload :Account, "applicaster/accounts/account"
8
8
  autoload :User, "applicaster/accounts/user"
9
+ autoload :Configuration, "applicaster/accounts/configuration"
9
10
 
10
11
  RETRYABLE_STATUS_CODES = [500, 503, 502]
11
- FARADAY_TIMEOUT = 0.5
12
-
13
- attr_accessor :client_id
14
- attr_accessor :client_secret
15
12
 
16
13
  class << self
17
- def default_site
18
- "https://accounts2.applicaster.com"
19
- end
20
-
21
- def site
22
- URI.parse(ENV["ACCOUNTS_BASE_URL"] || default_site)
23
- end
24
-
25
14
  def connection(options = {})
26
- Faraday.new(url: site, request: { timeout: FARADAY_TIMEOUT } ) do |conn|
15
+ conn_opts = {
16
+ url: config.base_url,
17
+ request: { timeout: config.timeout }
18
+ }
19
+
20
+ Faraday.new(conn_opts) do |conn|
27
21
  if options[:token]
28
22
  conn.request :oauth2, options[:token]
29
23
  end
30
24
 
31
25
  conn.request :json
32
26
  conn.request :retry,
27
+ max: config.retries,
33
28
  interval: 0.05,
34
29
  backoff_factor: 2,
35
30
  exceptions: [Faraday::ClientError, Faraday::TimeoutError],
36
31
  methods: [],
37
32
  retry_if: -> (env, exception) {
38
33
  env[:method] == :get &&
39
- RETRYABLE_STATUS_CODES.include?(env[:status])
34
+ (exception.is_a?(Faraday::TimeoutError) ||
35
+ RETRYABLE_STATUS_CODES.include?(env[:status]))
40
36
  }
41
37
 
42
38
 
@@ -59,14 +55,26 @@ module Applicaster
59
55
  if e.response[:status] == 401
60
56
  nil
61
57
  else
62
- raise e
58
+ raise
63
59
  end
64
60
  end
65
- end
66
61
 
67
- def initialize(client_id = nil, client_secret = nil)
68
- @client_id = client_id || ENV["ACCOUNTS_CLIENT_ID"]
69
- @client_secret = client_secret || ENV["ACCOUNTS_CLIENT_SECRET"]
62
+ def config
63
+ @config ||= Configuration.new
64
+ end
65
+
66
+ def configure
67
+ yield config
68
+ end
69
+
70
+ def oauth_client(config = config)
71
+ ::OAuth2::Client.new(
72
+ config.client_id,
73
+ config.client_secret,
74
+ site: config.base_url,
75
+ authorize_url: "/oauth/authorize",
76
+ )
77
+ end
70
78
  end
71
79
 
72
80
  def user_data_from_omniauth(omniauth_credentials)
@@ -87,24 +95,9 @@ module Applicaster
87
95
  protected
88
96
 
89
97
  def client_credentials_token
90
- @client_credentials_token ||= client.client_credentials.get_token
91
- end
92
-
93
- def client
94
- @client ||= ::OAuth2::Client.new(
95
- client_id,
96
- client_secret,
97
- site: Applicaster::Accounts.site,
98
- authorize_url: "/oauth/authorize",
99
- )
100
- end
101
-
102
- def access_token(omniauth_credentials)
103
- @access_token ||= OAuth2::AccessToken.new(
104
- client,
105
- omniauth_credentials["token"],
106
- omniauth_credentials.except("token", "expires"),
107
- )
98
+ @client_credentials_token ||= self.class.oauth_client
99
+ .client_credentials
100
+ .get_token
108
101
  end
109
102
  end
110
103
  end
@@ -0,0 +1,32 @@
1
+ module Applicaster
2
+ class Accounts
3
+ class Configuration
4
+ include Virtus.model
5
+
6
+ # The base URL of the accounts service
7
+ attribute :base_url, String,
8
+ default: :default_base_url
9
+
10
+ # OAuth2 provider client ID
11
+ attribute :client_id, String,
12
+ default: proc { ENV["ACCOUNTS_CLIENT_ID"] }
13
+
14
+ # OAuth2 provider client secret
15
+ attribute :client_secret, String,
16
+ default: proc { ENV["ACCOUNTS_CLIENT_SECRET"] }
17
+
18
+ # Number of times to retry safe requests
19
+ attribute :retries, Integer,
20
+ default: 2
21
+
22
+ # Number of seconds before a request will be timed out
23
+ attribute :timeout, Float,
24
+ default: 1
25
+
26
+
27
+ def default_base_url
28
+ ENV["ACCOUNTS_BASE_URL"] || "https://accounts2.applicaster.com/"
29
+ end
30
+ end
31
+ end
32
+ end
@@ -1,5 +1,5 @@
1
- module Omniauth
1
+ module OmniAuth
2
2
  module Applicaster
3
- VERSION = "1.1.1"
3
+ VERSION = "1.2.0"
4
4
  end
5
5
  end
@@ -24,12 +24,7 @@ module OmniAuth
24
24
  end
25
25
 
26
26
  def client
27
- ::OAuth2::Client.new(
28
- options.client_id,
29
- options.client_secret,
30
- site: ::Applicaster::Accounts.site,
31
- authorize_url: "/oauth/authorize",
32
- )
27
+ ::Applicaster::Accounts.oauth_client
33
28
  end
34
29
  end
35
30
  end
@@ -5,7 +5,7 @@ require 'omniauth-applicaster/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "omniauth-applicaster"
8
- spec.version = Omniauth::Applicaster::VERSION
8
+ spec.version = OmniAuth::Applicaster::VERSION
9
9
  spec.authors = ["Neer Friedman"]
10
10
  spec.email = ["neerfri@gmail.com"]
11
11
  spec.summary = %q{Omniauth strategy for http://accounts.applicaster.com}
@@ -0,0 +1,39 @@
1
+ RSpec.describe Applicaster::Accounts::Configuration do
2
+ let(:config) { described_class.new }
3
+
4
+ specify "defaults" do
5
+ expect(config.attributes).to eq({
6
+ base_url: "https://accounts2.applicaster.com/",
7
+ client_id: nil,
8
+ client_secret: nil,
9
+ retries: 2,
10
+ timeout: 1.0,
11
+ })
12
+ end
13
+
14
+ describe "#base_url" do
15
+ it "defaults to env var ACCOUNTS_BASE_URL" do
16
+ base_url = "http://example.com"
17
+
18
+ with_env_var("ACCOUNTS_BASE_URL", base_url) do
19
+ expect(config.base_url).to eq(base_url)
20
+ end
21
+ end
22
+ end
23
+
24
+ describe "#client_id" do
25
+ it "defaults to env var ACCOUNTS_CLIENT_ID" do
26
+ with_env_var("ACCOUNTS_CLIENT_ID", "test-client-id") do
27
+ expect(config.client_id).to eq("test-client-id")
28
+ end
29
+ end
30
+ end
31
+
32
+ describe "#client_secret" do
33
+ it "defaults to env var ACCOUNTS_CLIENT_SECRET" do
34
+ with_env_var("ACCOUNTS_CLIENT_SECRET", "test-client-secret") do
35
+ expect(config.client_secret).to eq("test-client-secret")
36
+ end
37
+ end
38
+ end
39
+ end
@@ -7,32 +7,6 @@ RSpec.describe Applicaster::Accounts do
7
7
  end
8
8
  end
9
9
 
10
- describe ".site" do
11
- it "returns a URI object" do
12
- expect(return_value).to be_kind_of(URI)
13
- end
14
-
15
- it "returns https://accounts2.applicaster.com" do
16
- expect(return_value.to_s).to eq("https://accounts2.applicaster.com")
17
- end
18
-
19
- context "when ACCOUNTS_BASE_URL is set" do
20
- around do |example|
21
- with_base_url("http://example.com") do
22
- example.run
23
- end
24
- end
25
-
26
- it "returns http://example.com" do
27
- expect(return_value.to_s).to eq("http://example.com")
28
- end
29
- end
30
-
31
- def return_value
32
- Applicaster::Accounts.site
33
- end
34
- end
35
-
36
10
  describe ".connection" do
37
11
  let(:remote_url) { "https://accounts2.applicaster.com/test.json" }
38
12
  let(:request_stub) { stub_request(:get, remote_url) }
@@ -63,22 +37,29 @@ RSpec.describe Applicaster::Accounts do
63
37
  end
64
38
 
65
39
  context "when server is not responding" do
66
- around do |example|
67
- with_base_url("http://localhost:6969") do
68
- WebMock.allow_net_connect!
69
- example.run
70
- WebMock.disable_net_connect!
71
- end
72
- end
40
+ let(:timeout) { 0.1 }
41
+ let(:retries) { 1 }
42
+ let(:max_exec_time) { 1 + timeout + (timeout + 0.05) * retries }
73
43
 
74
44
  before do
75
45
  @server = TCPServer.new(6969)
46
+
47
+ Applicaster::Accounts.configure do |config|
48
+ config.base_url = "http://localhost:6969"
49
+ config.timeout = timeout
50
+ config.retries = retries
51
+ end
76
52
  end
77
53
 
78
- it "times out after 0.5 second with 2 retries" do
79
- expect {
80
- connection.get("/test.json") rescue nil
81
- }.to change { Time.now }.by(a_value < 1.5)
54
+ it "times out after 0.1 second with 1 retry" do
55
+ disable_webmock do
56
+ expect {
57
+ begin
58
+ connection.get("/test.json")
59
+ rescue Faraday::TimeoutError
60
+ end
61
+ }.to change { Time.now }.by(a_value < max_exec_time)
62
+ end
82
63
  end
83
64
  end
84
65
 
@@ -111,17 +92,27 @@ RSpec.describe Applicaster::Accounts do
111
92
  end
112
93
  end
113
94
 
114
- describe "#initialize" do
115
- it "accepts client_id and client_secret" do
116
- service = Applicaster::Accounts.new("my_client_id", "my_client_secret")
95
+ describe ".config" do
96
+ it "returns an Applicaster::Accounts::Configuration" do
97
+ expect(config).to be_kind_of(Applicaster::Accounts::Configuration)
98
+ end
117
99
 
118
- expect(service.client_id).to eq("my_client_id")
119
- expect(service.client_secret).to eq("my_client_secret")
100
+ def config
101
+ Applicaster::Accounts.config
120
102
  end
103
+ end
121
104
 
122
- it "takes default values from ENV vars" do
123
- expect(accounts_service.client_id).to eq("client_id")
124
- expect(accounts_service.client_secret).to eq("client_secret")
105
+ describe ".configure" do
106
+ it "yields with Applicaster::Accounts.config" do
107
+ expect { |b| configure(&b) }.to yield_with_args(config)
108
+ end
109
+
110
+ def configure(&block)
111
+ Applicaster::Accounts.configure(&block)
112
+ end
113
+
114
+ def config
115
+ Applicaster::Accounts.config
125
116
  end
126
117
  end
127
118
 
@@ -160,10 +151,4 @@ RSpec.describe Applicaster::Accounts do
160
151
  ]
161
152
  end
162
153
  end
163
-
164
- def with_base_url(url)
165
- value_bofre, ENV["ACCOUNTS_BASE_URL"] = ENV["ACCOUNTS_BASE_URL"], url
166
- yield
167
- ENV["ACCOUNTS_BASE_URL"] = value_bofre
168
- end
169
154
  end
@@ -0,0 +1,13 @@
1
+ module EnvVariableHelper
2
+ RSpec.configure do |config|
3
+ config.include self
4
+ end
5
+
6
+ def with_env_var(name, value)
7
+ value_bofre, ENV[name] = ENV[name], value
8
+ yield
9
+ ENV[name] = value_bofre
10
+ end
11
+ end
12
+
13
+
@@ -0,0 +1,9 @@
1
+ RSpec.configure do |config|
2
+ config.before do
3
+ Applicaster::Accounts.instance_variable_set(:@config, nil)
4
+ Applicaster::Accounts.configure do |config|
5
+ config.client_id = "client-id"
6
+ config.client_secret = "client-secret"
7
+ end
8
+ end
9
+ end
@@ -3,8 +3,14 @@ module WebmockStubsHelper
3
3
  config.include self
4
4
  end
5
5
 
6
+ def disable_webmock
7
+ WebMock.allow_net_connect!
8
+ yield
9
+ WebMock.disable_net_connect!
10
+ end
11
+
6
12
  def stub_client_credentials_request
7
- stub_request(:post, "https://client_id:client_secret@#{accounts_host}/oauth/token")
13
+ stub_request(:post, "https://client-id:client-secret@#{accounts_host}/oauth/token")
8
14
  .with(:body => {"grant_type"=>"client_credentials"})
9
15
  .to_return(successful_json_response(access_token: "client-credentials-token"))
10
16
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: omniauth-applicaster
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Neer Friedman
@@ -137,6 +137,7 @@ files:
137
137
  - Rakefile
138
138
  - lib/applicaster/accounts.rb
139
139
  - lib/applicaster/accounts/account.rb
140
+ - lib/applicaster/accounts/configuration.rb
140
141
  - lib/applicaster/accounts/user.rb
141
142
  - lib/applicaster/auth_helpers.rb
142
143
  - lib/applicaster/sessions_controller_mixin.rb
@@ -145,11 +146,13 @@ files:
145
146
  - lib/omniauth/strategies/applicaster.rb
146
147
  - omniauth-applicaster.gemspec
147
148
  - spec/lib/applicaster/accounts/account_spec.rb
149
+ - spec/lib/applicaster/accounts/configuration_spec.rb
148
150
  - spec/lib/applicaster/accounts_spec.rb
149
151
  - spec/lib/applicaster/auth_helpers_spec.rb
150
152
  - spec/spec_helper.rb
151
153
  - spec/support/dummy_controller.rb
152
- - spec/support/setup_env_vars.rb
154
+ - spec/support/env_variable_helper.rb
155
+ - spec/support/set_test_configuration.rb
153
156
  - spec/support/webmock_stubs_helper.rb
154
157
  homepage: ''
155
158
  licenses:
@@ -177,9 +180,11 @@ specification_version: 4
177
180
  summary: Omniauth strategy for http://accounts.applicaster.com
178
181
  test_files:
179
182
  - spec/lib/applicaster/accounts/account_spec.rb
183
+ - spec/lib/applicaster/accounts/configuration_spec.rb
180
184
  - spec/lib/applicaster/accounts_spec.rb
181
185
  - spec/lib/applicaster/auth_helpers_spec.rb
182
186
  - spec/spec_helper.rb
183
187
  - spec/support/dummy_controller.rb
184
- - spec/support/setup_env_vars.rb
188
+ - spec/support/env_variable_helper.rb
189
+ - spec/support/set_test_configuration.rb
185
190
  - spec/support/webmock_stubs_helper.rb
@@ -1,2 +0,0 @@
1
- ENV["ACCOUNTS_CLIENT_ID"] = "client_id"
2
- ENV["ACCOUNTS_CLIENT_SECRET"] = "client_secret"