Module: FellowshipOneAPI::OAuth::OAuthAuthentication

Includes:
FellowshipOneAPI::OAuth
Defined in:
lib/f1api/oauth/oauth_authentication.rb

Overview

Implements the pure OAuth method of authentication. This allows the Fellowship One API to manage the authentication process.

Instance Attribute Summary collapse

Attributes included from FellowshipOneAPI::OAuth

#authenticated_user_uri, #oauth_access_token, #oauth_consumer, #oauth_consumer_key, #oauth_consumer_secret

Instance Method Summary collapse

Methods included from FellowshipOneAPI::OAuth

#load_consumer_config

Instance Attribute Details

#oauth_authorize_urlObject (readonly) Also known as: authorize_url

The OAuth authorization URI



11
12
13
# File 'lib/f1api/oauth/oauth_authentication.rb', line 11

def oauth_authorize_url
  @oauth_authorize_url
end

#oauth_requestObject (readonly)

The OAuth request object



8
9
10
# File 'lib/f1api/oauth/oauth_authentication.rb', line 8

def oauth_request
  @oauth_request
end

Instance Method Details

#authenticate!(type = :portal) ⇒ Object Also known as: authorize!

Gets a new request token and return the authenticated URI

type

Can be :portal or :weblink based on which credentials you want to authenticate against



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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/f1api/oauth/oauth_authentication.rb', line 16

def authenticate!(type = :portal)
  load_consumer_config(type) if oauth_consumer.nil?

  @oauth_request = oauth_consumer.get_request_token
  @oauth_authorize_url = oauth_request.authorize_url

  @oauth_consumer.instance_eval do
    # The token request reponse is scoped only in the token_request method, but I need to get access to the response
    # headers so that I can pull back the Content-Location header and get the authenticated user URI
    def token_request(http_method, path, token = nil, request_options = {}, *arguments)
      @tr_response = request(http_method, path, token, request_options, *arguments)
      case @tr_response.code.to_i

      when (200..299)
        if block_given?
          yield @tr_response.body
        else
          # symbolize keys
          # TODO this could be considered unexpected behavior; symbols or not?
          # TODO this also drops subsequent values from multi-valued keys
          CGI.parse(@tr_response.body).inject({}) do |h,(k,v)|
            h[k.strip.to_sym] = v.first
            h[k.strip]        = v.first
            h
          end
        end
      when (300..399)
        # this is a redirect
        @tr_response.error!
      when (400..499)
        raise OAuth::Unauthorized, @tr_response
      else
        @tr_response.error!
      end
    end
    
    # The HTTP response from token_request
    def token_request_response
      @tr_response
    end
  end
    
  oauth_authorize_url
end

#get_access_tokenObject

After a the user has been authenticated then we use the access token to access protected resources in the API. Since the authentication has taken place, we now know about the user that authenticated and have a URI to the record of that user.

The URI for the authenticated user is returned.



67
68
69
70
# File 'lib/f1api/oauth/oauth_authentication.rb', line 67

def get_access_token
  @oauth_access_token = oauth_request.get_access_token
  @authenticated_user_uri = oauth_consumer.token_request_response["Content-Location"]
end