Class: Agora::AgoraDynamicKey2::AccessToken
- Inherits:
-
Object
- Object
- Agora::AgoraDynamicKey2::AccessToken
- Defined in:
- lib/agora/dynamic_key2/access_token.rb
Constant Summary collapse
- VERSION =
'007'.freeze
- VERSION_LENGTH =
3- SERVICES =
{ ServiceRtc::SERVICE_TYPE => ServiceRtc, ServiceRtm::SERVICE_TYPE => ServiceRtm, ServiceFpa::SERVICE_TYPE => ServiceFpa, ServiceChat::SERVICE_TYPE => ServiceChat, ServiceApaas::SERVICE_TYPE => ServiceApaas }.freeze
Instance Attribute Summary collapse
-
#app_cert ⇒ Object
Returns the value of attribute app_cert.
-
#app_id ⇒ Object
Returns the value of attribute app_id.
-
#expire ⇒ Object
Returns the value of attribute expire.
-
#issue_ts ⇒ Object
Returns the value of attribute issue_ts.
-
#salt ⇒ Object
Returns the value of attribute salt.
-
#services ⇒ Object
Returns the value of attribute services.
Instance Method Summary collapse
- #add_service(service) ⇒ Object
- #build ⇒ Object
- #fetch_sign ⇒ Object
- #fetch_version ⇒ Object
-
#initialize(app_id = '', app_cert = '', expire = 900) ⇒ AccessToken
constructor
A new instance of AccessToken.
- #parse(token) ⇒ Object
- #uuid?(str) ⇒ Boolean
Constructor Details
#initialize(app_id = '', app_cert = '', expire = 900) ⇒ AccessToken
Returns a new instance of AccessToken.
157 158 159 160 161 162 163 164 |
# File 'lib/agora/dynamic_key2/access_token.rb', line 157 def initialize(app_id = '', app_cert = '', expire = 900) @app_id = app_id @app_cert = app_cert @expire = expire @issue_ts = Time.now.to_i @salt = rand(1...99_999_999) @services = {} end |
Instance Attribute Details
#app_cert ⇒ Object
Returns the value of attribute app_cert.
147 148 149 |
# File 'lib/agora/dynamic_key2/access_token.rb', line 147 def app_cert @app_cert end |
#app_id ⇒ Object
Returns the value of attribute app_id.
147 148 149 |
# File 'lib/agora/dynamic_key2/access_token.rb', line 147 def app_id @app_id end |
#expire ⇒ Object
Returns the value of attribute expire.
147 148 149 |
# File 'lib/agora/dynamic_key2/access_token.rb', line 147 def expire @expire end |
#issue_ts ⇒ Object
Returns the value of attribute issue_ts.
147 148 149 |
# File 'lib/agora/dynamic_key2/access_token.rb', line 147 def issue_ts @issue_ts end |
#salt ⇒ Object
Returns the value of attribute salt.
147 148 149 |
# File 'lib/agora/dynamic_key2/access_token.rb', line 147 def salt @salt end |
#services ⇒ Object
Returns the value of attribute services.
147 148 149 |
# File 'lib/agora/dynamic_key2/access_token.rb', line 147 def services @services end |
Instance Method Details
#add_service(service) ⇒ Object
166 167 168 |
# File 'lib/agora/dynamic_key2/access_token.rb', line 166 def add_service(service) @services[service.type] = service end |
#build ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/agora/dynamic_key2/access_token.rb', line 170 def build return '' if !uuid?(@app_id) || !uuid?(@app_cert) signing = fetch_sign data = Util.pack_string(@app_id) + Util.pack_uint32(@issue_ts) + Util.pack_uint32(@expire) \ + Util.pack_uint32(@salt) + Util.pack_uint16(@services.size) @services.each do |_, service| data += service.pack end signature = OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), signing, data) fetch_version + Base64.strict_encode64(Zlib::Deflate.deflate(Util.pack_string(signature) + data)) end |
#fetch_sign ⇒ Object
185 186 187 188 |
# File 'lib/agora/dynamic_key2/access_token.rb', line 185 def fetch_sign sign = OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), Util.pack_uint32(@issue_ts), @app_cert) OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), Util.pack_uint32(@salt), sign) end |
#fetch_version ⇒ Object
190 191 192 |
# File 'lib/agora/dynamic_key2/access_token.rb', line 190 def fetch_version VERSION end |
#parse(token) ⇒ Object
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/agora/dynamic_key2/access_token.rb', line 200 def parse(token) return false if token[0..VERSION_LENGTH - 1] != fetch_version data = Zlib::Inflate.inflate(Base64.strict_decode64(token[VERSION_LENGTH..-1])) signature, data = Util.unpack_string(data) @app_id, data = Util.unpack_string(data) @issue_ts, data = Util.unpack_uint32(data) @expire, data = Util.unpack_uint32(data) @salt, data = Util.unpack_uint32(data) service_num, data = Util.unpack_uint16(data) (1..service_num).each do service_type, data = Util.unpack_uint16(data) service = SERVICES[service_type].new _, data = service.unpack(data) @services[service_type] = service end true end |
#uuid?(str) ⇒ Boolean
194 195 196 197 198 |
# File 'lib/agora/dynamic_key2/access_token.rb', line 194 def uuid?(str) return false if str.length != 32 true end |