Sha256: 39397a05d4a94db4fce67849fdd8fe26eb1de4a40fa20d30158deeecadd2c5dc

Contents?: true

Size: 1.26 KB

Versions: 2

Compression:

Stored size: 1.26 KB

Contents

# Usage:
#
# class ApplicationController
#   include SimpleTokenAuth::AuthenticateWithToken
# end
#
# class UserController < ApplicationController
#   prepend_before_action :authenticate_user_from_token!
# end
#
module SimpleTokenAuth
  module AuthenticateWithToken
    def method_missing(method, *args, &block)
      if m = method.to_s.match(/authenticate_(.+)_from_token!/)
        send :authenticate_from_token!, m[1]
      else
        super
      end
    end

    private

    def authenticate_from_token!(scope_name)
      scope_class = scope_name.camelize.constantize
      authenticate_or_request_with_http_token do |token, options|
        return false if token.blank?

        scope, token  = *find_scope(scope_class, token)
        authenticated = false

        if scope
          api_key = scope.api_key
          authenticated = api_key && !api_key.expired? && compare_token(api_key.access_token, token)
        end

        after_authenticated(scope, self) if authenticated

        authenticated
      end
    end

    def after_authenticated(*args)
      SimpleTokenAuth.after_authenticated(*args)
    end

    def find_scope(*args)
      SimpleTokenAuth.find_scope(*args)
    end

    def compare_token(a, b)
      SimpleTokenAuth.compare_token(a, b)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
simple_token_auth-0.0.4 lib/simple_token_auth/authenticate_with_token.rb
simple_token_auth-0.0.3 lib/simple_token_auth/authenticate_with_token.rb