Class: JiveRails::OauthController

Inherits:
ApplicationController show all
Defined in:
app/controllers/jive_rails/oauth_controller.rb

Instance Method Summary collapse

Instance Method Details

#authorizeUrlObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/controllers/jive_rails/oauth_controller.rb', line 47

def authorizeUrl
	authorization = CGI.parse((request.headers["Authorization"] || "").gsub(/^JiveEXTN\s/,''))
	client_id = authorization["client_id"].first
	jive_url = authorization["jive_url"].first

	viewerId = params[:viewerID]
	jiveTenantID = params[:jiveTenantID]
	callback = params[:callback]
	extra_auth_params = {}
	context = {}

	originJiveTenantID = request.headers["X-Tenant-Id"] || ""
	targetJiveTenantID = (authorization["tenant_id"] || []).first

	app = JiveRails::AddOn.where(:client_id => client_id, :tenant_id => targetJiveTenantID).first
	client_secret = app.client_secret

	if !params[:context].to_s.empty?
		context = ::JSON.parse(params[:context].to_s)
	end
	
	# encode the origin jiveTenantID in the context
	if !originJiveTenantID.empty?
		context[:originJiveTenantID] = originJiveTenantID
	end

	# encode the jiveTenantID in the context
	if !jiveTenantID.empty?
		context[:jiveTenantID] = jiveTenantID
	end

	if !params[:extraAuthParams].to_s.empty?
		extra_auth_params = ::JSON.parse(URI.unescape(params[:extraAuthParams].to_s))
	end

	args = {
		:clientOAuth2CallbackUrl => oauth_oauth2Callback_url,
		:oauth2ConsumerKey => client_id,
		:oauth2ConsumerSecret => client_secret,
		:originServerAuthorizationUrl => "#{jive_url}/oauth2/authorize",
		:originServerTokenRequestUrl => "#{jive_url}/oauth2/token",
		:oauth2CallbackExtraParams => nil,
	}

	response = Jive::OAuth2.build_authorize_url_response_map(args, callback, {
		:viewerID => viewerId,
		:context => context
	}, extra_auth_params)

	respond_to do |format|
		format.json { render :json => response }
	end
end

#oauth2CallbackObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/controllers/jive_rails/oauth_controller.rb', line 10

def oauth2Callback
	code = params[:code]
	@state = params[:state]

	# Decode state
	@state = ::Base64.decode64 @state
	# No idea why JSON.parse doesn't work here...
	@state = ActiveSupport::JSON.decode @state
	@state = ActiveSupport::JSON.decode @state

	jive_tenant_id = (@state["context"].is_a?(Hash) && !@state["context"]["jiveTenantID"].to_s.empty?) ? @state["context"]["jiveTenantID"] : nil
	viewer_id = @state["viewerID"] || nil
	client_id = @state["client_id"] || nil
	origin_jive_tenant_id = (@state["context"].is_a?(Hash) && !@state["context"]["originJiveTenantID"].to_s.empty?) ? @state["context"]["originJiveTenantID"] : nil

	app = JiveRails::AddOn.where(:client_id => client_id, :tenant_id => jive_tenant_id).first
	jive_url = app.jive_url

	args = {
		:jiveTenantID => jive_tenant_id,
		:originJiveTenantID => origin_jive_tenant_id,
		:clientId => client_id,
		:clientOAuth2CallbackUrl => oauth_oauth2Callback_url,
		:oauth2ConsumerKey => client_id,
		:oauth2ConsumerSecret => app.client_secret,
		:originServerTokenRequestUrl => "#{jive_url}/oauth2/token",
	}
	post_object = Jive::OAuth2.build_oauth2_callback_object(args, code)

	@result = Jive::OAuth2.get_oauth2_token(args, post_object);
	@jive_redirect_url = "#{@state["jiveRedirectUrl"]}?ticket=#{viewer_id}"

	respond_to do |format|
		format.html
	end
end