Module: RestfulApiAuthentication

Defined in:
lib/restful_api_authentication/checker.rb,
lib/restful_api_authentication.rb,
lib/restful_api_authentication/railtie.rb,
lib/restful_api_authentication/version.rb,
lib/generators/restful_api_authentication/install/install_generator.rb

Overview

Copyright © 2012 David Kiger

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Defined Under Namespace

Modules: Generators Classes: Checker, Railtie

Constant Summary collapse

VERSION =
"0.3.0"

Instance Method Summary collapse

Instance Method Details

#authenticated?Boolean

This method should be used as a Rails before_filter in any controller in which one wants to ensure requests have valid client authentication headers.

If the request is not authenticated, it will use the rails respond_with method to send a 401 Unauthorized response.

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/restful_api_authentication.rb', line 37

def authenticated?
  checker = RestfulApiAuthentication::Checker.new(request.headers, request.fullpath)
  if checker.authorized?
    return true
  else
    if checker.verbose_errors
      respond_with(checker.errors, :status => 401, :location => nil)
    else
      respond_with(["not authorized"], :status => 401, :location => nil)
    end
  end
end

#authenticated_master?Boolean

This method should be used as a Rails before_filter in any controller in which one wants to ensure requests have valid client authentication headers and are considered master applications.

In order to be authenticated, not only do the headers need to be valid but the is_master flag must be true in the associated RestClient model.

Master accounts can be used for anything you like but are typically reserved for admin specific requests that should only be performed by a limited number of clients.

Returns:

  • (Boolean)


55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/restful_api_authentication.rb', line 55

def authenticated_master?
  checker = RestfulApiAuthentication::Checker.new(request.headers, request.fullpath)
  if checker.authorized?({:require_master => true})
    return true
  else
    if checker.verbose_errors
      respond_with(checker.errors, :status => 401, :location => nil)
    else
      respond_with(["not authorized"], :status => 401, :location => nil)
    end
  end
end