Class: Hull::Paywall::Request
- Inherits:
-
Object
- Object
- Hull::Paywall::Request
- Defined in:
- lib/hull/paywall.rb
Instance Attribute Summary collapse
-
#authorized_contents ⇒ Object
readonly
Returns the value of attribute authorized_contents.
-
#user_id ⇒ Object
readonly
Returns the value of attribute user_id.
Instance Method Summary collapse
- #check_authorization_for(key) ⇒ Object
- #check_signature(sig, val) ⇒ Object
- #fetch_authorized_contents ⇒ Object
-
#initialize(env, secret) ⇒ Request
constructor
A new instance of Request.
- #set_cookie(headers) ⇒ Object
- #sign(val) ⇒ Object
Constructor Details
#initialize(env, secret) ⇒ Request
Returns a new instance of Request.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/hull/paywall.rb', line 12 def initialize env, secret @env = env @secret = secret @request = Rack::Request.new(env) @user_id = Hull.authenticate_user(env) @cookie_name = "_hull_p#{Hull.app_id}" = @request.[@cookie_name] if = Base64.decode64() rescue nil sig, val = JSON.parse() rescue [] @authorized_contents = check_signature(sig, val) ? val : [] end @authorized_contents ||= [] end |
Instance Attribute Details
#authorized_contents ⇒ Object (readonly)
Returns the value of attribute authorized_contents.
10 11 12 |
# File 'lib/hull/paywall.rb', line 10 def @authorized_contents end |
#user_id ⇒ Object (readonly)
Returns the value of attribute user_id.
10 11 12 |
# File 'lib/hull/paywall.rb', line 10 def user_id @user_id end |
Instance Method Details
#check_authorization_for(key) ⇒ Object
50 51 52 53 |
# File 'lib/hull/paywall.rb', line 50 def key return false unless @user_id @authorized_contents.include?(key) or .include?(key) end |
#check_signature(sig, val) ⇒ Object
31 32 33 34 35 |
# File 'lib/hull/paywall.rb', line 31 def check_signature sig, val return false unless @user_id return false if sig.nil? || val.nil? sig == sign(val.to_json) end |
#fetch_authorized_contents ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/hull/paywall.rb', line 37 def return [] if @user_id.nil? badges = Hull.get("#{user_id}/badges") || [] @authorized_contents = badges.map do |badge| if badge['data'] && badge['data']['transactions'] badge['data']['transactions'].map do |k,t| t['permalink'] end end end.compact.flatten.uniq.sort @authorized_contents end |
#set_cookie(headers) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/hull/paywall.rb', line 55 def headers if !@user_id.nil? && @authorized_contents.length > 0 = Base64.encode64([sign(@authorized_contents.to_json), @authorized_contents].to_json) Rack::Utils.(headers, @cookie_name, { :value => , :path => "/" }) else Rack::Utils.(headers, @cookie_name, { :path => "/" }) end end |
#sign(val) ⇒ Object
27 28 29 |
# File 'lib/hull/paywall.rb', line 27 def sign val OpenSSL::HMAC.hexdigest(OpenSSL::Digest::Digest.new('sha1'), @secret, val) end |