Class: RestClientWrapper::Authenticator::Oauth

Inherits:
Object
  • Object
show all
Includes:
RestClientWrapper::Auth
Defined in:
lib/rest_client_wrapper/authenticators/oauth.rb

Overview

Oauth

Constant Summary collapse

@@api_client =

rubocop:disable Style/ClassVars

{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site:, token_url_path:, client_id:, client_secret:) ⇒ Oauth

Returns a new instance of Oauth.



39
40
41
42
# File 'lib/rest_client_wrapper/authenticators/oauth.rb', line 39

def initialize(site:, token_url_path:, client_id:, client_secret:)
  @client_id = client_id
  @@api_client[client_id] = { lock: Mutex.new, settings: { site: site, token_url_path: token_url_path, client_secret: client_secret }, access_token: nil, refresh_token: nil }
end

Instance Attribute Details

#client_idObject (readonly)

Returns the value of attribute client_id.



35
36
37
# File 'lib/rest_client_wrapper/authenticators/oauth.rb', line 35

def client_id
  @client_id
end

Class Method Details

.authenticate(client_id:, access_token: nil) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rest_client_wrapper/authenticators/oauth.rb', line 60

def self.authenticate(client_id:, access_token: nil)
  # Ensure that other threads aren't checking and updating the token at the same time
  @@api_client[client_id][:lock].synchronize do
    # Return access_token from @@api_client when the current_token is different to what's in @@api_client as it's already been refreshed
    return @@api_client[client_id][:access_token] if !access_token.nil? && !@@api_client[client_id][:access_token].nil? && @@api_client[client_id][:access_token].casecmp(access_token).nonzero?

    payload = {
      grant_type:    GrantType::CLIENT_CREDENTIALS,
      client_id:     client_id,
      client_secret: @@api_client&.[](client_id)&.[](:settings)&.[](:client_secret)
    }
    url = "#{ @@api_client&.[](client_id)&.[](:settings)&.[](:site) }#{ @@api_client&.[](client_id)&.[](:settings)&.[](:token_url_path) }"

    response = ::RestClient::Request.execute({ method: :post, url: url, payload: payload })

    if Http.ok?(response.code)
      content_type = MIME::Types[response&.headers&.[](:content_type)].first
      raise StandardError "Unable to retreive token, response was in a unexpected format" unless content_type == "application/json"

      token_payload = JSON.parse(response.body)
      @@api_client[client_id][:access_token] = token_payload["access_token"]
      @@api_client[client_id][:refresh_token] = token_payload["refresh_token"]
    end
  end
end

Instance Method Details

#access_tokenObject



48
49
50
# File 'lib/rest_client_wrapper/authenticators/oauth.rb', line 48

def access_token
  return @@api_client&.[](@client_id)&.[](:access_token)
end

#generate_authObject



52
53
54
55
56
57
58
# File 'lib/rest_client_wrapper/authenticators/oauth.rb', line 52

def generate_auth
  Authenticator::Oauth.authenticate({ client_id: @client_id }) if @@api_client&.[](@client_id)&.[](:access_token).nil?
  access_token = @@api_client&.[](@client_id)&.[](:access_token)
  raise StandardError "Unable to authenticate #{ @client_id }" if @@api_client&.[](@client_id)&.[](:access_token).nil?

  return { Authorization: "Bearer #{ access_token }" }
end

#tokensObject



44
45
46
# File 'lib/rest_client_wrapper/authenticators/oauth.rb', line 44

def tokens
  return @@api_client
end