aboutsummaryrefslogtreecommitdiffstats
path: root/app/api/concerns/twitter_oauth_echo_authentication.rb
blob: f0cf53aee1f16775663fa89bf6bc93de068b14b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
require "open-uri"

module TwitterOauthEchoAuthentication
  def authenticate_with_twitter_oauth_echo
    twitter_provider = "https://api.twitter.com/1.1/account/verify_credentials.json"

    provider = headers["X-Auth-Service-Provider"]
    credentials = headers["X-Verify-Credentials-Authorization"]
    unless provider == twitter_provider && credentials
      raise Aclog::Exceptions::OAuthEchoError, "X-Auth-Service-Provider is invalid"
    end

    json = open(twitter_provider, "Authorization" => credentials) {|res|
      Yajl::Parser.parse(res.read)
    }

    json["id"]
  rescue Aclog::Exceptions::OAuthEchoError
    raise $!
  rescue OpenURI::HTTPError
    if $!.message.include?("401")
      raise Aclog::Exceptions::OAuthEchoUnauthorized, $!
    else
      raise $!
    end
  end
end