lib/rest-firebase.rb in rest-firebase-0.9.2 vs lib/rest-firebase.rb in rest-firebase-0.9.3
- old
+ new
@@ -1,25 +1,24 @@
require 'rest-core'
# https://www.firebase.com/docs/security/custom-login.html
# https://www.firebase.com/docs/rest-api.html
-RestFirebase = RC::Builder.client(:d, :secret, :auth, :iat) do
+RestFirebase = RC::Builder.client(:d, :secret, :auth, :auth_ttl, :iat) do
use RC::Timeout , 10
use RC::DefaultSite , 'https://SampleChat.firebaseIO-demo.com/'
use RC::DefaultHeaders, {'Accept' => 'application/json',
'Content-Type' => 'application/json'}
use RC::DefaultQuery , nil
use RC::FollowRedirect, 1
use RC::CommonLogger , nil
- use RC::Cache , nil, 600 do
- use RC::ErrorHandler, lambda{ |env| RestFirebase::Error.call(env) }
- use RC::ErrorDetectorHttp
- use RC::JsonResponse, true
- end
+ use RC::ErrorHandler , lambda{ |env| RestFirebase::Error.call(env) }
+ use RC::ErrorDetectorHttp
+ use RC::JsonResponse , true
+ use RC::Cache , nil, 600
end
class RestFirebase::Error < RestCore::Error
include RestCore
class ServerError < RestFirebase::Error; end
@@ -89,25 +88,27 @@
def generate_auth opts={}
raise RestFirebase::Error::ClientError.new(
"Please set your secret") unless secret
+ self.iat = nil
header = {:typ => 'JWT', :alg => 'HS256'}
claims = {:v => 0, :iat => iat, :d => d}.merge(opts)
# http://tools.ietf.org/html/draft-ietf-jose-json-web-signature-26
input = [header, claims].map{ |d| base64url(Json.encode(d)) }.join('.')
# http://tools.ietf.org/html/draft-ietf-oauth-json-web-token-20
"#{input}.#{base64url(Hmac.sha256(secret, input))}"
end
private
def base64url str; [str].pack('m').tr('+/', '-_'); end
- def default_query; {:auth => auth}; end
- def default_auth ; generate_auth ; end
- def default_iat ; Time.now.to_i ; end
+ def default_query ; {:auth => auth}; end
+ def default_auth ; generate_auth ; end
+ def default_auth_ttl; 82800 ; end
+ def default_iat ; Time.now.to_i ; end
def check_auth
- self.auth = nil if iat && Time.now.to_i - iat > 82800
+ self.auth = nil if auth_ttl && Time.now.to_i - iat > auth_ttl
end
end
class RestFirebase
include RestFirebase::Client