flexmls_api 0.7.3 → 0.7.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.7.3
1
+ 0.7.5
@@ -87,19 +87,32 @@ module FlexmlsApi
87
87
 
88
88
  # Representation of a session with the api using oauth2
89
89
  class OAuthSession
90
- attr_accessor :access_token, :expires_in, :scope, :refresh_token, :refresh_timeout
90
+ SESSION_ATTRIBUTES = [:access_token, :expires_in, :scope, :refresh_token, :refresh_timeout, :start_time]
91
+ attr_accessor *SESSION_ATTRIBUTES
91
92
  def initialize(options={})
92
93
  @access_token = options["access_token"]
93
94
  @expires_in = options["expires_in"]
94
95
  @scope = options["scope"]
95
96
  @refresh_token = options["refresh_token"]
96
- @start_time = DateTime.now
97
- @refresh_timeout = 3600
97
+ @start_time = options.fetch("start_time", DateTime.now)
98
+ @refresh_timeout = options.fetch("refresh_timeout",3600)
99
+ if @start_time.is_a? String
100
+ @start_time = DateTime.parse(@start_time)
101
+ end
98
102
  end
99
103
  # Is the user session token expired?
100
104
  def expired?
101
105
  @start_time + Rational(@expires_in - @refresh_timeout, 86400) < DateTime.now
102
106
  end
107
+
108
+ def to_json(*a)
109
+ hash = {}
110
+ SESSION_ATTRIBUTES.each do |k|
111
+ value = self.send(k)
112
+ hash[k.to_s] = value unless value.nil?
113
+ end
114
+ hash.to_json(*a)
115
+ end
103
116
  end
104
117
 
105
118
  #=OAuth2 configuration provider for applications
@@ -17,6 +17,7 @@ module FlexmlsApi
17
17
  else
18
18
  raise ClientError, "Unsupported grant type [#{provider.grant_type}]"
19
19
  end
20
+ FlexmlsApi.logger.debug("[oauth2] setup #{granter.class.name}")
20
21
  granter
21
22
  end
22
23
 
@@ -37,11 +38,12 @@ module FlexmlsApi
37
38
  protected
38
39
 
39
40
  def create_session(token_params)
40
- FlexmlsApi.logger.debug("Authenticating to #{provider.access_uri}")
41
+ FlexmlsApi.logger.debug("[oauth2] create_session to #{provider.access_uri} params #{token_params}")
41
42
  uri = URI.parse(provider.access_uri)
42
43
  request_path = "#{uri.path}"
43
44
  response = oauth_access_connection("#{uri.scheme}://#{uri.host}").post(request_path, "#{token_params}").body
44
45
  response.expires_in = provider.session_timeout if response.expires_in.nil?
46
+ FlexmlsApi.logger.debug("[oauth2] New session created #{response}")
45
47
  response
46
48
  end
47
49
 
@@ -11,7 +11,7 @@ module FlexmlsApi
11
11
  end
12
12
  def authenticate
13
13
  if(provider.code.nil?)
14
- FlexmlsApi.logger.debug("Redirecting to provider to get the authorization code")
14
+ FlexmlsApi.logger.debug("[oauth2] No authoriztion code present. Redirecting to #{authorization_url}.")
15
15
  provider.redirect(authorization_url)
16
16
  end
17
17
  if needs_refreshing?
@@ -22,11 +22,12 @@ module FlexmlsApi
22
22
  end
23
23
 
24
24
  def refresh()
25
+ FlexmlsApi.logger.debug("[oauth2] Refresh oauth session.")
25
26
  refresher = GrantTypeRefresh.new(client,provider,session)
26
27
  refresher.params = {"redirect_uri" => @provider.redirect_uri}
27
28
  refresher.authenticate
28
29
  rescue ClientError => e
29
- FlexmlsApi.logger.info("Refreshing token failed, the library will try and authenticate from scratch: #{e.message}")
30
+ FlexmlsApi.logger.info("[oauth2] Refreshing token failed, the library will try and authenticate from scratch: #{e.message}")
30
31
  nil
31
32
  end
32
33
 
@@ -21,7 +21,7 @@ module FlexmlsApi
21
21
  def refresh()
22
22
  GrantTypeRefresh.new(client,provider,session).authenticate
23
23
  rescue ClientError => e
24
- FlexmlsApi.logger.info("Refreshing token failed, the library will try and authenticate from scratch: #{e.message}")
24
+ FlexmlsApi.logger.info("[oauth2] Refreshing token failed, the library will try and authenticate from scratch: #{e.message}")
25
25
  nil
26
26
  end
27
27
 
@@ -13,7 +13,7 @@ module FlexmlsApi
13
13
  def authenticate
14
14
  new_session = nil
15
15
  unless @session.refresh_token.nil?
16
- FlexmlsApi.logger.debug("Refreshing authentication to #{provider.access_uri} using [#{session.refresh_token}]")
16
+ FlexmlsApi.logger.debug("[oauth2] Refreshing authentication to #{provider.access_uri} using [#{session.refresh_token}]")
17
17
  new_session = create_session(token_params)
18
18
  end
19
19
  new_session
@@ -10,13 +10,13 @@ module FlexmlsApi
10
10
  class Middleware < Faraday::Response::ParseJson
11
11
  def on_complete(finished_env)
12
12
  body = parse(finished_env[:body])
13
- FlexmlsApi.logger.debug("Response Body: #{body.inspect}")
13
+ FlexmlsApi.logger.debug("[oauth2] Response Body: #{body.inspect}")
14
14
  unless body.is_a?(Hash)
15
15
  raise InvalidResponse, "The server response could not be understood"
16
16
  end
17
17
  case finished_env[:status]
18
18
  when 200..299
19
- FlexmlsApi.logger.debug("Success!")
19
+ FlexmlsApi.logger.debug("[oauth2] Success!")
20
20
  session = OAuthSession.new(body)
21
21
  else
22
22
  # Handle the WWW-Authenticate Response Header Field if present. This can be returned by
@@ -25,7 +25,7 @@ module FlexmlsApi
25
25
  FlexmlsApi.logger.warn("Authentication error #{auth_header_error}") unless auth_header_error.nil?
26
26
  raise ClientError, {:message => body["error"], :code =>0, :status => finished_env[:status]}
27
27
  end
28
- FlexmlsApi.logger.debug("Session= #{session.inspect}")
28
+ FlexmlsApi.logger.debug("[oauth2] Session=#{session.inspect}")
29
29
  finished_env[:body] = session
30
30
  end
31
31
 
@@ -186,4 +186,20 @@ describe FlexmlsApi::Authentication::OAuth2Impl do
186
186
  expect{FlexmlsApi::Authentication::OAuth2Impl.load_provider(bad_example,{}).class.to_s.should eq(bad_example)}.to raise_error(ArgumentError)
187
187
  end
188
188
 
189
- end
189
+ end
190
+
191
+ describe FlexmlsApi::Authentication::OAuthSession do
192
+ it "should serialize to json" do
193
+ args = {
194
+ "access_token" => "abc",
195
+ "expires_in" => 3600,
196
+ "refresh_token" => "123",
197
+ "refresh_timeout" => 10000,
198
+ "start_time" => "2012-01-01T00:00:00+00:00"
199
+ }
200
+ session = FlexmlsApi::Authentication::OAuthSession.new(args)
201
+ session.start_time.should eq(DateTime.parse(args["start_time"]))
202
+ JSON.parse(session.to_json).should eq(args)
203
+
204
+ end
205
+ end
@@ -46,7 +46,7 @@ describe FlexmlsApi::Configuration::YamlConfig, "Yaml Config" do
46
46
  subject.endpoint.should eq("http://api.dev.flexmls.com")
47
47
  subject.oauth2_provider.should eq("FlexmlsApi::TestOAuth2Provider")
48
48
  subject.name.should eq("test_oauth")
49
- subject.client_keys.keys.should eq([:endpoint, :oauth2_provider])
49
+ subject.client_keys.keys.should =~ [:endpoint, :oauth2_provider]
50
50
  subject.oauth2_keys.keys.should =~ [:authorization_uri, :client_id, :access_uri, :client_secret, :redirect_uri]
51
51
  end
52
52
  it "should load a configured api key for production" do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flexmls_api
3
3
  version: !ruby/object:Gem::Version
4
- hash: 5
4
+ hash: 9
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 7
9
- - 3
10
- version: 0.7.3
9
+ - 5
10
+ version: 0.7.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - Brandon Hornseth
@@ -72,12 +72,11 @@ dependencies:
72
72
  requirements:
73
73
  - - ~>
74
74
  - !ruby/object:Gem::Version
75
- hash: 1
75
+ hash: 5
76
76
  segments:
77
77
  - 1
78
78
  - 5
79
- - 1
80
- version: 1.5.1
79
+ version: "1.5"
81
80
  requirement: *id004
82
81
  prerelease: false
83
82
  name: json