jive-oauth_token 0.0.3 → 0.0.4

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: b01ca7c7dc01b55786783953b6aabe24bda64d5b
4
- data.tar.gz: bd41455a447c753d549b374adc56e3d08748abf0
3
+ metadata.gz: d320aeb657a6840fa9edebb56bb5f888d3fe12f0
4
+ data.tar.gz: fea289cac5c6a5aa5350304b00790c8df5a3c747
5
5
  SHA512:
6
- metadata.gz: a66c6663e8ec95c9549a02d3ab0f41823a382d59c2e2acd782bcf19104853a29c2b6350b2276681bee0e0f3e5ef0182db3e75fef50fb76575b2807c3c9cfa1fb
7
- data.tar.gz: f5868d1a9be01b70db72481c7efcc984021cab559f83fd49585f694d0c87b1fdfb66b96823949aa9394e79dcb5b90a9a32482e1612bdd17bcb2b9ccb7c5fbb72
6
+ metadata.gz: 963c20cc924ee303214ebd625ce02497962d45bb655cdcba3a2a010c6cda4a528dbe383dd891ad592240416e4f2fc6f77813b6c74391386625da36a80c53a5e7
7
+ data.tar.gz: 2307ccbf1f6d123635b793b94cafe0ac62daf134efb191a8b3c39f7283bc1e687a9ae2f618d5a1bce0a411376e607548920c80ad88656886756fbb763dbbfc3e
@@ -18,6 +18,7 @@ Gem::Specification.new do |spec|
18
18
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
19
  spec.require_paths = ["lib"]
20
20
 
21
+ spec.add_dependency "jive-add_on", ">= 0.0.2"
21
22
  spec.add_dependency "activerecord", [">= 3.0", "< 5.0"]
22
23
 
23
24
  if RUBY_PLATFORM == 'java'
@@ -1,6 +1,8 @@
1
1
  class AddJiveOauthTokens010Migration < ActiveRecord::Migration
2
2
  def self.up
3
3
  create_table :jive_oauth_tokens do |t|
4
+ t.integer :jive_add_on_id
5
+
4
6
  t.integer :owner_id
5
7
  t.string :owner_type
6
8
 
@@ -2,6 +2,9 @@ require "active_support"
2
2
  require "active_support/dependencies"
3
3
  require "active_record"
4
4
 
5
+ require "jive/add_on"
6
+ require "jive/oauth_token/add_on/class_methods"
7
+
5
8
  require "jive/oauth_token/version"
6
9
  require "jive/oauth_token/compatibility"
7
10
 
@@ -14,4 +17,7 @@ module Jive
14
17
  end
15
18
 
16
19
  Jive::OauthToken.send :include, Jive::OauthToken::InstanceMethods
17
- Jive::OauthToken.send :extend, Jive::OauthToken::ClassMethods
20
+ Jive::OauthToken.send :extend, Jive::OauthToken::ClassMethods
21
+
22
+ # We want add-ons to know they can now associate with OAuthTokens
23
+ Jive::AddOn.send :include, Jive::OauthToken::AddOn::ClassMethods
@@ -0,0 +1,13 @@
1
+ module Jive
2
+ class OauthToken < ActiveRecord::Base
3
+ module AddOn
4
+ module ClassMethods
5
+ def self.included(base)
6
+ base.class_eval do
7
+ has_many :oauth_tokens, :class_name => "Jive::OauthToken", :as => :owner
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -3,6 +3,8 @@ module Jive
3
3
  module ClassMethods
4
4
  def self.extended(base)
5
5
  base.belongs_to :owner, :polymorphic => true
6
+ base.belongs_to :add_on, :class_name => "Jive::AddOn", :foreign_key => :jive_add_on_id
7
+ base.validates :add_on, presence: true
6
8
  end
7
9
  end
8
10
  end
@@ -4,6 +4,53 @@ module Jive
4
4
  def self.included(base)
5
5
  base.table_name = "jive_oauth_tokens"
6
6
  end
7
+
8
+ # Gets a valid access token from this object - Will refresh existing token if invalid
9
+ def valid_access_token
10
+ # The token we have stored is expired - fetch a new one using the refresh token
11
+ self.refresh_access_token if self.access_token_expired?
12
+
13
+ self.access_token
14
+ end
15
+
16
+ # Fetches a refreshed access token
17
+ def refresh_access_token
18
+ require "open-uri"
19
+ require "net/http"
20
+ require "openssl"
21
+ require "base64"
22
+
23
+ uri = URI.parse("#{self.add_on.jive_url}/oauth2/token")
24
+ http = Net::HTTP.new(uri.host, uri.port)
25
+ http.use_ssl = true
26
+
27
+ request = Net::HTTP::Post.new(uri.request_uri)
28
+ request.basic_auth self.add_on.client_id, self.add_on.client_secret
29
+ request.set_form_data({
30
+ "refresh_token" => "#{self.refresh_token}",
31
+ "grant_type" => "refresh_token",
32
+ })
33
+
34
+ response = http.request(request)
35
+ json_body = JSON.parse(response.body)
36
+
37
+ if (response.code.to_i != 200)
38
+ raise RuntimeError, json_body["error"].to_s.upcase
39
+ end
40
+
41
+ self.access_token = json_body["access_token"]
42
+ self.expires_in = json_body["expires_in"]
43
+ self.expires_at = json_body["expires_in"].to_i.seconds.from_now
44
+ self.save
45
+ end
46
+
47
+ # Return whether access token is expired
48
+ def access_token_expired?
49
+ return false if self.expires_at.nil?
50
+
51
+ # Expiration date less than now == expired
52
+ self.expires_at < Time.now
53
+ end
7
54
  end
8
55
  end
9
56
  end
@@ -2,6 +2,6 @@ require "active_record"
2
2
 
3
3
  module Jive
4
4
  class OauthToken < ActiveRecord::Base
5
- VERSION = "0.0.3"
5
+ VERSION = "0.0.4"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jive-oauth_token
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Butch Marshall
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-02-13 00:00:00.000000000 Z
11
+ date: 2016-02-21 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jive-add_on
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.0.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.0.2
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: activerecord
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -150,6 +164,7 @@ files:
150
164
  - lib/generators/jive/oauth_token/oauth_token_generator.rb
151
165
  - lib/generators/jive/oauth_token/templates/migration_0.1.0.rb
152
166
  - lib/jive/oauth_token.rb
167
+ - lib/jive/oauth_token/add_on/class_methods.rb
153
168
  - lib/jive/oauth_token/class_methods.rb
154
169
  - lib/jive/oauth_token/compatibility.rb
155
170
  - lib/jive/oauth_token/instance_methods.rb