lib/restpack_web/context.rb in restpack_web-0.2.21 vs lib/restpack_web/context.rb in restpack_web-0.4.1
- old
+ new
@@ -1,119 +1,70 @@
-require 'public_suffix'
+module RestPack::Web
+ class Context
+ attr_accessor :domain, :application, :user, :account
-module RestPack
- module Web
- class Context
- attr_accessor :request, :domain, :application, :channel, :configurations, :user, :user_id
+ def initialize(env)
+ restpack = env['restpack']
- def initialize(env)
- restpack = env[:restpack]
-
- if restpack
- @request = restpack[:request]
- @domain = restpack[:domain]
- @application = restpack[:application]
- @channel = restpack[:channel]
- @configurations = restpack[:configurations]
- @user = restpack[:user]
- @user_id = @user[:id] if @user
- end
+ if restpack
+ @domain = restpack[:domain]
+ @application = restpack[:application]
+ @user = restpack[:user]
+ @account = restpack[:account]
end
+ end
- def is_authenticated?
- !@user.nil?
- end
+ def authenticated?
+ !@user.nil?
+ end
- def get_configuration(key)
- @configurations.find { |c| c.key == key }
- end
+ def user_id
+ authenticated? ? @user[:id] : nil
+ end
- def services
- get_configuration('services').value
- end
+ def account_id
+ authenticated? ? @account[:id] : nil
+ end
- def get_service(name)
- services.find { |s| s[:name] == name }
- end
+ def application_id
+ @application[:id]
+ end
- def get_service_domain(name)
- get_service(name)[:domain]
- end
+ def domain_id
+ @domain[:id]
+ end
- def home_domain
- @application.home_domain || "www.#{root_domain}"
- end
+ def home_domain
+ "www.#{@domain[:identifier]}"
+ end
- def auth_domain
- @application.auth_domain || "auth.#{root_domain}"
- end
+ def auth_domain
+ "auth.#{@domain[:identifier]}"
+ end
- def root_domain
- PublicSuffix.parse(@domain.host).domain
- end
+ def logout_url(next_url = nil)
+ next_url ||= "http://#{home_domain}/"
+ auth_url('/auth/logout', next_url)
+ 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)
+ auth_url("/auth/#{provider}", 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"
+ 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]} 
- }
- end
+ def auth_url(path, next_url = nil)
+ #TODO: GJ: whitelist the next_url?
+ #TODO: GJ: URI encode next_url?
+ port = RestPack::Web.config.authentication_port
- %{
-### RestPack Context:
+ port_part = port ? ":#{port}" : ""
+ next_part = next_url ? "?next=#{next_url}" : ""
-#### 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(', ')}
- * **services**: #{services.map { |s| s[:name] + ': ' + s[:domain] }.join(', ')}
-
-#### Authentication:
-
- * **logout**: #{logout_url}
- * **twitter oauth**: #{login_url(:twitter)}
- * **google oauth**: #{login_url(:google_oauth2)}
- }
- end
+ "http://#{auth_domain}#{port_part}#{path}#{next_part}"
end
end
end