class GoToWebinar::Auth::Client

Attributes

auth_scheme[RW]
authorize_optional_params[RW]
authorize_url[RW]
basic_auth_password[RW]
basic_auth_username[RW]
consumer_key[RW]
oauth2_client[RW]
redis[RW]
secret_key[RW]
site[RW]
token_url[RW]

Public Class Methods

new(basic_auth_username: nil, basic_auth_password: nil, consumer_key: nil, secret_key: nil) click to toggle source
# File lib/go_to_webinar/auth/client.rb, line 11
def initialize(basic_auth_username: nil, basic_auth_password: nil, consumer_key: nil, secret_key: nil)
  config = GoToWebinar::Auth.configuration
  @redis = Redis.new(url: config.redis_url)
  @basic_auth_username = config.basic_auth_username
  @basic_auth_password = config.basic_auth_password
  @consumer_key = consumer_key || config.consumer_key
  @secret_key = secret_key || config.secret_key
  @site = config.site
  @authorize_url = config.authorize_url
  @authorize_optional_params = config.authorize_optional_params
  @token_url = config.token_url
  @auth_scheme = config.auth_scheme
  @oauth2_client = new_oauth2_client
end

Public Instance Methods

access_token() click to toggle source
# File lib/go_to_webinar/auth/client.rb, line 37
def access_token
  @access_token || get_access_token_from_redis || get_new_access_token
end
get_new_access_token() click to toggle source
# File lib/go_to_webinar/auth/client.rb, line 41
def get_new_access_token
  token = oauth2_client.password.get_token(basic_auth_username, basic_auth_password)
  save_to_redis(token)
  @access_token = token
end
new_oauth2_client() click to toggle source
# File lib/go_to_webinar/auth/client.rb, line 26
def new_oauth2_client
  OAuth2::Client.new(
    consumer_key,
    secret_key,
    site: site,
    authorize_url: authorize_url,
    token_url: token_url,
    auth_scheme: auth_scheme
  )
end
refresh_access_token() click to toggle source
# File lib/go_to_webinar/auth/client.rb, line 47
def refresh_access_token
  @access_token = access_token&.refresh!
end

Private Instance Methods

authorize_url_params() click to toggle source
# File lib/go_to_webinar/auth/client.rb, line 93
def authorize_url_params
  URI.encode_www_form({ client_id: consumer_key }.merge(authorize_optional_params))
end
authorize_url_with_params() click to toggle source
# File lib/go_to_webinar/auth/client.rb, line 89
def authorize_url_with_params
  authorize_url + '?' + authorize_url_params
end
get_access_token_from_redis(redis_key: 'g2w_access_token') click to toggle source
# File lib/go_to_webinar/auth/client.rb, line 56
def get_access_token_from_redis(redis_key: 'g2w_access_token')
  # retrieve from redis
  token_json = @redis.get(redis_key)
  token_hash = JSON.parse(token_json)&.[]('token') if token_json
  if token_hash
    @access_token = OAuth2::AccessToken.from_hash(oauth2_client, token_hash)
  end

  # if we found it in redis, and it's expired, let's just refresh it
  @access_token = refresh_access_token if @access_token&.expired?

  # let's return it if we got it =)
  return @access_token if @access_token

  # if it doesn't currently exist in redis, return nil
  nil
end
save_to_redis(token, redis_key: 'g2w_access_token') click to toggle source
# File lib/go_to_webinar/auth/client.rb, line 74
def save_to_redis(token, redis_key: 'g2w_access_token')
  @redis.set(redis_key, token.to_hash.to_json)
end