Class: Yade::Common::Client::AuthenticationClient

Inherits:
Object
  • Object
show all
Includes:
HTTParty, Yade::Common::Config
Defined in:
lib/yade/common/client/authentication_client.rb

Overview

authentication client

Instance Method Summary collapse

Constructor Details

#initializeAuthenticationClient

Returns a new instance of AuthenticationClient.



16
17
18
19
20
21
22
23
# 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

Instance Method Details

#access_tokenObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# 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) ⇒ Object

login



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/yade/common/client/authentication_client.rb', line 26

def (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

#logoutObject

logout



48
49
50
51
52
53
54
55
56
57
# 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