Class: BarocertService

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

Overview

Barocert API BaseService class

Constant Summary collapse

ServiceID_REAL =
"BAROCERT"
ServiceURL =
"https://barocert.linkhub.co.kr"
ServiceURL_Static =
"https://static-barocert.linkhub.co.kr"
APIVersion =
"2.1"
BOUNDARY =
"==BAROCERT_RUBY_SDK=="

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#ipRestrictOnOffObject

Returns the value of attribute ipRestrictOnOff.



17
18
19
# File 'lib/barocert.rb', line 17

def ipRestrictOnOff
  @ipRestrictOnOff
end

#linkhubObject

Returns the value of attribute linkhub.



17
18
19
# File 'lib/barocert.rb', line 17

def linkhub
  @linkhub
end

#scopesObject

Returns the value of attribute scopes.



17
18
19
# File 'lib/barocert.rb', line 17

def scopes
  @scopes
end

#token_tableObject

Returns the value of attribute token_table.



17
18
19
# File 'lib/barocert.rb', line 17

def token_table
  @token_table
end

#useLocalTimeYNObject

Returns the value of attribute useLocalTimeYN.



17
18
19
# File 'lib/barocert.rb', line 17

def useLocalTimeYN
  @useLocalTimeYN
end

#useStaticIPObject

Returns the value of attribute useStaticIP.



17
18
19
# File 'lib/barocert.rb', line 17

def useStaticIP
  @useStaticIP
end

Class Method Details

.instance(linkID, secretKey) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/barocert.rb', line 21

def instance(linkID, secretKey)
	@instance ||= new
	@instance.token_table = {}
	@instance.linkhub = Linkhub.instance(linkID, secretKey)
	@instance.scopes = ["partner"]
	@instance.ipRestrictOnOff = false
	@instance.useStaticIP = false
	@instance.useLocalTimeYN = true
	return @instance
end

Instance Method Details

#addScope(scopeValue) ⇒ Object

add Service Scope array



36
37
38
# File 'lib/barocert.rb', line 36

def addScope(scopeValue)
	@scopes.push(scopeValue)
end

#getServiceURLObject



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/barocert.rb', line 60

def getServiceURL()
	if @_ServiceURL.nil? || @_ServiceURL == ""
		if @useStaticIP
			return ServiceURL_Static
		else
			return ServiceURL
		end
	else
		return @_ServiceURL
	end
end

#getSession_TokenObject

Get Session Token by checking token-cached hash or token Request



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/barocert.rb', line 73

def getSession_Token()
	targetToken = nil
	refresh = false

	# check already cached CorpNum's SessionToken
	if @token_table.key?(@linkhub._linkID)
		targetToken = @token_table[@linkhub._linkID]
	end

	if targetToken.nil?
		refresh = true
	else
		# Token's expireTime must use parse() because time format is hh:mm:ss.SSSZ
		expireTime = DateTime.parse(targetToken['expiration'])
		serverUTCTime = DateTime.strptime(@linkhub.getTime(@useStaticIP, false, @useLocalTimeYN))
		refresh = expireTime < serverUTCTime
	end

	if refresh
		begin
			# getSessionToken from Linkhub
			targetToken = @linkhub.getSessionToken(ServiceID_REAL, "" , @scopes, @ipRestrictOnOff ? "" : "*", @useStaticIP, false, @useLocalTimeYN)

		rescue LinkhubException => le
			raise BarocertException.new(le.code, le.message)
		end
		# append token to cache hash
		@token_table[@linkhub._linkID] = targetToken
	end

	targetToken['session_token']
end

#gzip_parse(target) ⇒ Object

end of getSession_Token



108
109
110
111
112
# File 'lib/barocert.rb', line 108

def gzip_parse (target)
	sio = StringIO.new(target)
	gz = Zlib::GzipReader.new(sio)
	gz.read()
end

#httpget(url) ⇒ Object

Barocert API http Get Request Func



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/barocert.rb', line 115

def httpget(url)
	headers = {
			"Accept-Encoding" => "gzip,deflate",
	}

	headers["Authorization"] = "Bearer " + getSession_Token()

	uri = URI(getServiceURL() + url)
	request = Net::HTTP.new(uri.host, 443)
	request.use_ssl = true

	Net::HTTP::Get.new(uri)

	res = request.get(uri.request_uri, headers)

	if res.code == "200"
		if res.header['Content-Encoding'].eql?('gzip')
			JSON.parse(gzip_parse(res.body))
		else
			JSON.parse(res.body)
		end
	else
		raise BarocertException.new(JSON.parse(res.body)["code"], JSON.parse(res.body)["message"])
	end
end

#httppost(url, postData = nil) ⇒ Object

Request HTTP Post



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/barocert.rb', line 144

def httppost(url,  postData=nil)

	headers = {
		"Accept-Encoding" => "gzip,deflate",
		"x-bc-version" => APIVersion,
	}

	date = @linkhub.getTime(@useStaticIP, false)

	hmacTarget = "POST\n"
	if postData != nil
		hmacTarget += Base64.strict_encode64(Digest::SHA256.digest(postData)) + "\n"
	end
	hmacTarget += date + "\n"
	hmacTarget += url + "\n"

	key = Base64.decode64(@linkhub._secretKey)

	data = hmacTarget
	digest = OpenSSL::Digest.new("sha256")
	hmac = Base64.strict_encode64(OpenSSL::HMAC.digest(digest, key, data))

	headers["x-bc-auth"] = hmac
	headers["x-bc-date"] = date

	headers["x-bc-encryptionmode"] = "GCM"

	headers["Content-Type"] = "application/json; charset=utf8"

	headers["Authorization"] = "Bearer " + getSession_Token()


	uri = URI(getServiceURL() + url)

	https = Net::HTTP.new(uri.host, 443)
	https.use_ssl = true
	Net::HTTP::Post.new(uri)

	res = https.post(uri.request_uri, postData, headers)

	if res.code == "200"
		if res.header['Content-Encoding'].eql?('gzip')
			JSON.parse(gzip_parse(res.body))
		else
			JSON.parse(res.body)
		end
	else
		raise BarocertException.new(JSON.parse(res.body)["code"], JSON.parse(res.body)["message"])
	end
end

#setAuthURL(value) ⇒ Object



56
57
58
# File 'lib/barocert.rb', line 56

def setAuthURL(value)
	@linkhub.setServiceURL(value)
end

#setIpRestrictOnOff(value) ⇒ Object



40
41
42
# File 'lib/barocert.rb', line 40

def setIpRestrictOnOff(value)
	@ipRestrictOnOff = value
end

#setServiceURL(value) ⇒ Object



52
53
54
# File 'lib/barocert.rb', line 52

def setServiceURL(value)
	@_ServiceURL = value
end

#setUseLocalTimeYN(value) ⇒ Object



48
49
50
# File 'lib/barocert.rb', line 48

def setUseLocalTimeYN(value)
	@useLocalTimeYN = value
end

#setUseStaticIP(value) ⇒ Object



44
45
46
# File 'lib/barocert.rb', line 44

def setUseStaticIP(value)
	@useStaticIP = value
end

#sha256Base64urlFile(target) ⇒ Object

end of httppost



197
198
199
200
# File 'lib/barocert.rb', line 197

def sha256Base64urlFile(target)
	hashed = Digest::SHA256.digest(target)
	return Base64.urlsafe_encode64(hashed, padding:false)
end