Sha256: 34e3067cd00801ee06246b4cfb4381c9185e73c9f742840f15df5f20a9d98251

Contents?: true

Size: 1.24 KB

Versions: 8

Compression:

Stored size: 1.24 KB

Contents

require 'cow_auth/exceptions'

module CowAuth
  module TokenAuth
    module AuthenticateRequest
      extend ActiveSupport::Concern

    private

      SID_KEY = 'sid='
      TOKEN_KEY = 'token='
      AUTHORIZATION_REGEX = /^(Token|Bearer)\s*/
      AUTHORIZATION_DELIMITERS = /(?:,|;|\t+)/

      def authenticate_user
        sid, auth_token = extract_credentials(request.authorization)
        if sid.present? && auth_token.present?
          user = authentication_class.find_by(sid: sid)
          @current_user = user.try(:authenticate_with_token, auth_token) ? user : nil
          return true if @current_user.present?
        end
        raise CowAuth::NotAuthenticatedError.new('User not authenticated.')
      end

      def extract_credentials(authorization_header)
        return nil if authorization_header.blank? || !(authorization_header =~ /\A#{AUTHORIZATION_REGEX}/)
        params = authorization_header.sub(AUTHORIZATION_REGEX, '').split(/\s*#{AUTHORIZATION_DELIMITERS}\s*/)
        sid = params[1].sub(SID_KEY, '') if params[1] =~ /\A#{SID_KEY}/
        auth_token = params[0].sub(TOKEN_KEY, '') if params[0] =~ /\A#{TOKEN_KEY}/
        return sid, auth_token
      end

      def current_user
        return @current_user
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
cow_auth-0.8.1 lib/cow_auth/token_auth/authenticate_request.rb
cow_auth-0.8.0 lib/cow_auth/token_auth/authenticate_request.rb
cow_auth-0.7.1 lib/cow_auth/token_auth/authenticate_request.rb
cow_auth-0.7.0 lib/cow_auth/token_auth/authenticate_request.rb
cow_auth-0.6.2 lib/cow_auth/token_auth/authenticate_request.rb
cow_auth-0.6.1 lib/cow_auth/token_auth/authenticate_request.rb
cow_auth-0.6.0 lib/cow_auth/token_auth/authenticate_request.rb
cow_auth-0.5.3 lib/cow_auth/token_auth/authenticate_request.rb