require 'public_suffix' module RestPack module Web class Context attr_accessor :domain, :application, :channel, :configurations, :services, :user, :user_id, :request def initialize(env) restpack = env[:restpack] if restpack @request = restpack[:request] @domain = restpack[:domain] @application = restpack[:application] @channel = restpack[:channel] @configurations = restpack[:configurations] @services = restpack[:services] @user = restpack[:user] @user_id = @user[:id] if @user end end def is_authenticated? !@user.nil? end def get_service(name) @services.find { |s| s[:name] == name.to_s } end def home_domain @application.home_domain || "www.#{root_domain}" end def auth_domain @application.auth_domain || "auth.#{root_domain}" end def root_domain PublicSuffix.parse(@domain.host).domain end def logout_url(next_url = nil) #TODO: GJ: whitelist the next_url next_url ||= "http://#{home_domain}/" "http://#{auth_domain}/auth/logout?next=#{next_url}" end def login_url(provider = :twitter, next_url = nil) next_url ||= "http://#{home_domain}/" "http://#{auth_domain}/auth/#{provider}?next=#{next_url}" end def debug_info #TODO: GJ: move to a mixin user_debug_info = "" if is_authenticated? image_debug_info = "" image_debug_info = " * **image** : #{@user[:image]} ![Image](#{@user[:image]}" unless @user[:image].nil? user_debug_info = %{ * **id** : #{@user[:id]} * **name** : #{@user[:name]} * **nickname** : #{@user[:nickname]} * **location** : #{@user[:location]} * **description** : #{@user[:description]} #{image_debug_info} } end %{ ### RestPack Context: #### User: * **is authenticated**: #{is_authenticated?} #{user_debug_info} #### Channel: * **id**: #{@channel.id} * **name**: #{@channel.name} * **applications**: #{@channel.applications.map { |a| a.name }.join(', ')} #### Application: * **id**: #{@application.id} * **name**: #{@application.name} * **domains**: #{@application.domains.map { |a| a.host }.join(', ')} #### Domain: * **id**: #{@domain.id} * **host**: #{@domain.host} #### Configuration: * **keys**: #{@configurations.map { |c| c.key }.join(', ')} #### Authentication: * **logout**: #{logout_url} * **twitter oauth**: #{login_url(:twitter)} * **google oauth**: #{login_url(:google_oauth2)} } end end end end