class Yade::Common::Client::AuthenticationClient

authentication client

Public Class Methods

new() click to toggle source
# File lib/yade/common/client/authentication_client.rb, line 16
def initialize
  client_id = 'web_app'
  client_secret = 'changeit'

  # Basic d2ViX2FwcDpjaGFuZ2VpdA==
  # The BASE64 encoded value of your 'clientId + ":" + clientSecret'
  @authorization = "Basic #{Base64.encode64("#{client_id}:#{client_secret}")}"
end

Public Instance Methods

access_token() click to toggle source
# File lib/yade/common/client/authentication_client.rb, line 59
def access_token
  authentication = Yade::Common::Model::Authentication.new

  authentication.read

  if Time.now > Time.parse(authentication.expiration_time)
    response = refresh_token(authentication.auth_url, authentication.refresh_token)

    if response.code == 200
      authentication.access_token = response['access_token']
    else
      response = request_token(authentication.auth_url, authentication.auth_username,
                               authentication.auth_password)
      authentication.access_token = response['access_token']
      authentication.refresh_token = response['refresh_token']
      authentication.expiration_time = Time.now + response['expires_in']
    end

    authentication.write
  end

  authentication.access_token
end
login(auth_url, auth_username, auth_password) click to toggle source

login

# File lib/yade/common/client/authentication_client.rb, line 26
def login(auth_url, auth_username, auth_password)
  response = request_token(auth_url, auth_username, auth_password)

  parsed_response = response.parsed_response

  access_token = parsed_response['access_token']
  refresh_token = parsed_response['refresh_token']
  expiration_time = Time.now + parsed_response['expires_in']

  authentication = Yade::Common::Model::Authentication.new(auth_url: auth_url,
                                                           auth_username: auth_username,
                                                           auth_password: auth_password,
                                                           access_token: access_token,
                                                           refresh_token: refresh_token,
                                                           expiration_time: expiration_time)

  authentication.write

  authentication
end
logout() click to toggle source

logout

# File lib/yade/common/client/authentication_client.rb, line 48
def logout
  authentication = Yade::Common::Model::Authentication.new(auth_url: '',
                                                           auth_username: '',
                                                           auth_password: '',
                                                           access_token: '',
                                                           refresh_token: '',
                                                           expiration_time: '')

  authentication.write
end

Private Instance Methods

refresh_token(auth_url, refresh_token) click to toggle source

refresh token

# File lib/yade/common/client/authentication_client.rb, line 95
def refresh_token(auth_url, refresh_token)
  # curl -X POST -H 'Authorization: Basic d2ViX2FwcDpjaGFuZ2VpdA==' \
  #     -d "refresh_token=${...}&grant_type=refresh_token" \
  #     'http://host.com:9999/oauth/token'
  options = {
      headers: { Accept: 'application/json', Authorization: @authorization },
      body: { refresh_token: refresh_token, grant_type: 'refresh_token' }
  }

  self.class.base_uri auth_url

  self.class.post('/oauth/token', options)
end
request_token(auth_url, auth_username, auth_password) click to toggle source

request token

# File lib/yade/common/client/authentication_client.rb, line 86
def request_token(auth_url, auth_username, auth_password)
  options = { headers: { Accept: 'application/json', Authorization: @authorization } }

  self.class.base_uri auth_url

  self.class.post("/oauth/token?username=#{auth_username}&password=#{auth_password}&grant_type=password", options)
end