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.



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

def ipRestrictOnOff
  @ipRestrictOnOff
end

#linkhubObject

Returns the value of attribute linkhub.



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

def linkhub
  @linkhub
end

#scopesObject

Returns the value of attribute scopes.



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

def scopes
  @scopes
end

#token_tableObject

Returns the value of attribute token_table.



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

def token_table
  @token_table
end

#useLocalTimeYNObject

Returns the value of attribute useLocalTimeYN.



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

def useLocalTimeYN
  @useLocalTimeYN
end

#useStaticIPObject

Returns the value of attribute useStaticIP.



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

def useStaticIP
  @useStaticIP
end

Class Method Details

.instance(linkID, secretKey) ⇒ Object



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

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



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

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

#getServiceURLObject



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

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



72
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
# File 'lib/barocert.rb', line 72

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



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

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



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

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



143
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
# File 'lib/barocert.rb', line 143

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



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

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

#setIpRestrictOnOff(value) ⇒ Object



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

def setIpRestrictOnOff(value)
  @ipRestrictOnOff = value
end

#setServiceURL(value) ⇒ Object



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

def setServiceURL(value)
  @_ServiceURL = value
end

#setUseLocalTimeYN(value) ⇒ Object



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

def setUseLocalTimeYN(value)
  @useLocalTimeYN = value
end

#setUseStaticIP(value) ⇒ Object



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

def setUseStaticIP(value)
  @useStaticIP = value
end