Class: Inferno::DSL::OAuthCredentials
- Inherits:
-
Object
- Object
- Inferno::DSL::OAuthCredentials
- Includes:
- Entities::Attributes
- Defined in:
- lib/inferno/dsl/oauth_credentials.rb
Overview
OAuthCredentials provide a user with a single input which allows a fhir client to use a bearer token and automatically refresh the token when it expires.
Constant Summary collapse
- ATTRIBUTES =
[ :access_token, :refresh_token, :token_url, :client_id, :client_secret, :token_retrieval_time, :expires_in, :name ].freeze
Instance Attribute Summary collapse
- #access_token ⇒ Object
-
#client ⇒ Object
Returns the value of attribute client.
- #client_id ⇒ Object
- #client_secret ⇒ Object
- #expires_in ⇒ Object
- #name ⇒ Object
- #refresh_token ⇒ Object
- #token_retrieval_time ⇒ Object
- #token_url ⇒ Object
Instance Method Summary collapse
- #able_to_refresh? ⇒ Boolean
- #add_to_client(client) ⇒ Object
- #confidential_client? ⇒ Boolean
-
#initialize(raw_attributes_hash) ⇒ OAuthCredentials
constructor
A new instance of OAuthCredentials.
- #need_to_refresh? ⇒ Boolean
- #oauth2_refresh_headers ⇒ Object
- #oauth2_refresh_params ⇒ Object
- #to_hash ⇒ Object
- #to_s ⇒ Object
- #update_from_response_body(request) ⇒ Object
Methods included from Entities::Attributes
Constructor Details
#initialize(raw_attributes_hash) ⇒ OAuthCredentials
Returns a new instance of OAuthCredentials.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/inferno/dsl/oauth_credentials.rb', line 34 def initialize(raw_attributes_hash) attributes_hash = raw_attributes_hash.symbolize_keys invalid_keys = attributes_hash.keys - ATTRIBUTES raise Exceptions::UnknownAttributeException.new(invalid_keys, self.class) if invalid_keys.present? attributes_hash.each do |name, value| value = DateTime.parse(value) if name == :token_retrieval_time && value.is_a?(String) instance_variable_set(:"@#{name}", value) end self.token_retrieval_time = DateTime.now if access_token.present? && token_retrieval_time.blank? end |
Instance Attribute Details
#access_token ⇒ Object
|
# File 'lib/inferno/dsl/oauth_credentials.rb', line 24
|
#client ⇒ Object
Returns the value of attribute client.
22 23 24 |
# File 'lib/inferno/dsl/oauth_credentials.rb', line 22 def client @client end |
#client_id ⇒ Object
|
# File 'lib/inferno/dsl/oauth_credentials.rb', line 24
|
#client_secret ⇒ Object
|
# File 'lib/inferno/dsl/oauth_credentials.rb', line 24
|
#expires_in ⇒ Object
|
# File 'lib/inferno/dsl/oauth_credentials.rb', line 24
|
#name ⇒ Object
|
# File 'lib/inferno/dsl/oauth_credentials.rb', line 24
|
#refresh_token ⇒ Object
|
# File 'lib/inferno/dsl/oauth_credentials.rb', line 24
|
#token_retrieval_time ⇒ Object
|
# File 'lib/inferno/dsl/oauth_credentials.rb', line 24
|
#token_url ⇒ Object
|
# File 'lib/inferno/dsl/oauth_credentials.rb', line 24
|
Instance Method Details
#able_to_refresh? ⇒ Boolean
87 88 89 |
# File 'lib/inferno/dsl/oauth_credentials.rb', line 87 def able_to_refresh? refresh_token.present? && token_url.present? end |
#add_to_client(client) ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'lib/inferno/dsl/oauth_credentials.rb', line 68 def add_to_client(client) client.oauth_credentials = self self.client = client return unless access_token.present? client.set_bearer_token(access_token) end |
#confidential_client? ⇒ Boolean
92 93 94 |
# File 'lib/inferno/dsl/oauth_credentials.rb', line 92 def confidential_client? client_id.present? && client_secret.present? end |
#need_to_refresh? ⇒ Boolean
78 79 80 81 82 83 84 |
# File 'lib/inferno/dsl/oauth_credentials.rb', line 78 def need_to_refresh? return false if access_token.blank? || refresh_token.blank? return true if expires_in.blank? token_retrieval_time.to_i + expires_in.to_i - DateTime.now.to_i < 60 end |
#oauth2_refresh_headers ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/inferno/dsl/oauth_credentials.rb', line 105 def oauth2_refresh_headers base_headers = { 'Content-Type' => 'application/x-www-form-urlencoded' } return base_headers unless confidential_client? credentials = "#{client_id}:#{client_secret}" base_headers.merge( 'Authorization' => "Basic #{Base64.strict_encode64(credentials)}" ) end |
#oauth2_refresh_params ⇒ Object
97 98 99 100 101 102 |
# File 'lib/inferno/dsl/oauth_credentials.rb', line 97 def oauth2_refresh_params { 'grant_type' => 'refresh_token', 'refresh_token' => refresh_token } end |
#to_hash ⇒ Object
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/inferno/dsl/oauth_credentials.rb', line 51 def to_hash self.class::ATTRIBUTES.each_with_object({}) do |attribute, hash| value = send(attribute) next if value.nil? value = token_retrieval_time.iso8601 if attribute == :token_retrieval_time hash[attribute] = value end end |
#to_s ⇒ Object
63 64 65 |
# File 'lib/inferno/dsl/oauth_credentials.rb', line 63 def to_s JSON.generate(to_hash) end |
#update_from_response_body(request) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/inferno/dsl/oauth_credentials.rb', line 118 def update_from_response_body(request) token_response_body = JSON.parse(request.response_body) expires_in = token_response_body['expires_in'].is_a?(Numeric) ? token_response_body['expires_in'] : nil self.access_token = token_response_body['access_token'] self.refresh_token = token_response_body['refresh_token'] if token_response_body['refresh_token'].present? self.expires_in = expires_in self.token_retrieval_time = DateTime.now add_to_client(client) self end |