Class: Credentials

Inherits:
Object
  • Object
show all
Defined in:
lib/Credentials.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Credentials

Returns a new instance of Credentials.



18
19
20
21
# File 'lib/Credentials.rb', line 18

def initialize(args)
	args.keys.each { |name| instance_variable_set "@" + name.to_s, args[name] }	
	@token_details = nil
end

Instance Attribute Details

#client_keyObject

Returns the value of attribute client_key.



12
13
14
# File 'lib/Credentials.rb', line 12

def client_key
  @client_key
end

#client_secretObject

Returns the value of attribute client_secret.



13
14
15
# File 'lib/Credentials.rb', line 13

def client_secret
  @client_secret
end

#credential_typeObject

Returns the value of attribute credential_type.



11
12
13
# File 'lib/Credentials.rb', line 11

def credential_type
  @credential_type
end

#passwordObject

Returns the value of attribute password.



15
16
17
# File 'lib/Credentials.rb', line 15

def password
  @password
end

#token_detailsObject

Returns the value of attribute token_details.



16
17
18
# File 'lib/Credentials.rb', line 16

def token_details
  @token_details
end

#user_nameObject

Returns the value of attribute user_name.



14
15
16
# File 'lib/Credentials.rb', line 14

def user_name
  @user_name
end

Instance Method Details

#get_access_tokenObject

Get Access Token Using OAuth



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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/Credentials.rb', line 35

def get_access_token
	
	if (!token_has_expired())
		return @token_details["access_token"]
	end
	
	# Determine OAuth Flow
	case @credential_type
		when OAuthGrantType::PASSWORD
			oauth_data = {
				:grant_type => @credential_type, 
				:client_id => @client_key, 
				:client_secret => @client_secret, 
				:username => @user_name, 
				:password => @password }
		when OAuthGrantType::CLIENT_CREDENTIALS
			oauth_data = {
				:grant_type => @credential_type, 
				:client_id => @client_key, 
				:client_secret => @client_secret }
		else
			puts 'Current OAuth flow only supports Resource Owner and Client Credentials'				
	end

	# define endpoint
	uri = get_uri '/oauth2/token'

	# define HTTPS connection
	https = Net::HTTP.new(uri.host, uri.port)
	https.use_ssl = true
	https.verify_mode = OpenSSL::SSL::VERIFY_NONE
	
	# define Request
	req = Net::HTTP::Post.new uri.request_uri
	req['Api-Key'] = @client_key
	req.set_form_data oauth_data
	
	res = https.request req
	data = res.body
	result = JSON.parse(data)
	if !res.is_a?(Net::HTTPSuccess)
		raise res.code + ": " + result['error_description']	
	end

	@token_details = result
	expiry = token_details["expires_in"].to_i
	@token_details["sdk_expire_time"] = Time.now + expiry

	return result['access_token']
end

#get_uri(path) ⇒ Object



23
24
25
# File 'lib/Credentials.rb', line 23

def get_uri(path)
	return URI.parse "#{Api_Host::AUTH_BASE_URL}#{path}"
end

#token_has_expiredObject



27
28
29
30
31
32
# File 'lib/Credentials.rb', line 27

def token_has_expired()
	if (!token_details.nil?) && (@token_details.has_key?("sdk_expire_time"))
		return @token_details["sdk_expire_time"] < (Time.now - 300)
	end
	return true
end