summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorValery Sizov <valery@gitlab.com>2017-08-01 16:04:35 +0300
committerValery Sizov <valery@gitlab.com>2017-08-04 12:22:10 +0300
commit26d15a89dd4602b1d80d65b29fd48e79696dfb2c (patch)
treef068f19cf4c64407f2867ab888fe512fef87d34f /lib
parentdbadf5a57660bea6edf38597af17a32094d5fe1f (diff)
downloadgitlab-ce-26d15a89dd4602b1d80d65b29fd48e79696dfb2c.tar.gz
Fix Mattermost integrationmattermost_fixes
Diffstat (limited to 'lib')
-rw-r--r--lib/mattermost/session.rb26
1 files changed, 20 insertions, 6 deletions
diff --git a/lib/mattermost/session.rb b/lib/mattermost/session.rb
index 688a79c0441..ef08bd46e17 100644
--- a/lib/mattermost/session.rb
+++ b/lib/mattermost/session.rb
@@ -36,11 +36,12 @@ module Mattermost
def with_session
with_lease do
- raise Mattermost::NoSessionError unless create
+ create
begin
yield self
- rescue Errno::ECONNREFUSED
+ rescue Errno::ECONNREFUSED => e
+ Rails.logger.error(e.message + "\n" + e.backtrace.join("\n"))
raise Mattermost::NoSessionError
ensure
destroy
@@ -85,10 +86,12 @@ module Mattermost
private
def create
- return unless oauth_uri
- return unless token_uri
+ raise Mattermost::NoSessionError unless oauth_uri
+ raise Mattermost::NoSessionError unless token_uri
@token = request_token
+ raise Mattermost::NoSessionError unless @token
+
@headers = {
Authorization: "Bearer #{@token}"
}
@@ -106,11 +109,16 @@ module Mattermost
@oauth_uri = nil
response = get("/api/v3/oauth/gitlab/login", follow_redirects: false)
- return unless 300 <= response.code && response.code < 400
+ return unless (300...400) === response.code
redirect_uri = response.headers['location']
return unless redirect_uri
+ oauth_cookie = parse_cookie(response)
+ @headers = {
+ Cookie: oauth_cookie.to_cookie_string
+ }
+
@oauth_uri = URI.parse(redirect_uri)
end
@@ -124,7 +132,7 @@ module Mattermost
def request_token
response = get(token_uri, follow_redirects: false)
- if 200 <= response.code && response.code < 400
+ if (200...400) === response.code
response.headers['token']
end
end
@@ -156,5 +164,11 @@ module Mattermost
rescue Errno::ECONNREFUSED => e
raise Mattermost::ConnectionError.new(e.message)
end
+
+ def parse_cookie(response)
+ cookie_hash = CookieHash.new
+ response.get_fields('Set-Cookie').each { |c| cookie_hash.add_cookies(c) }
+ cookie_hash
+ end
end
end