Module: Jive::OauthToken::InstanceMethods
- Defined in:
- lib/jive/oauth_token/instance_methods.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#access_token_expired? ⇒ Boolean
Return whether access token is expired.
-
#refresh_access_token ⇒ Object
Fetches a refreshed access token.
-
#valid_access_token ⇒ Object
Gets a valid access token from this object - Will refresh existing token if invalid.
Class Method Details
.included(base) ⇒ Object
4 5 6 |
# File 'lib/jive/oauth_token/instance_methods.rb', line 4 def self.included(base) base.table_name = "jive_oauth_tokens" end |
Instance Method Details
#access_token_expired? ⇒ Boolean
Return whether access token is expired
48 49 50 51 52 53 |
# File 'lib/jive/oauth_token/instance_methods.rb', line 48 def access_token_expired? return false if self.expires_at.nil? # Expiration date less than now == expired self.expires_at < Time.now end |
#refresh_access_token ⇒ Object
Fetches a refreshed access token
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/jive/oauth_token/instance_methods.rb', line 17 def refresh_access_token require "open-uri" require "net/http" require "openssl" require "base64" uri = URI.parse("#{self.add_on.jive_url}/oauth2/token") http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true request = Net::HTTP::Post.new(uri.request_uri) request.basic_auth self.add_on.client_id, self.add_on.client_secret request.set_form_data({ "refresh_token" => "#{self.refresh_token}", "grant_type" => "refresh_token", }) response = http.request(request) json_body = JSON.parse(response.body) if (response.code.to_i != 200) raise RuntimeError, json_body["error"].to_s.upcase end self.access_token = json_body["access_token"] self.expires_in = json_body["expires_in"] self.expires_at = json_body["expires_in"].to_i.seconds.from_now self.save end |
#valid_access_token ⇒ Object
Gets a valid access token from this object - Will refresh existing token if invalid
9 10 11 12 13 14 |
# File 'lib/jive/oauth_token/instance_methods.rb', line 9 def valid_access_token # The token we have stored is expired - fetch a new one using the refresh token self.refresh_access_token if self.access_token_expired? self.access_token end |