module Gigya::ControllerUtils

Helper/controller mixins

Constants

GIGYA_QUERY_PARAM
GIGYA_SESSION_PARAM

Public Class Methods

gigya_jwt_refresh_time() click to toggle source
# File lib/gigya/controller_utils.rb, line 14
def self.gigya_jwt_refresh_time
        @@gigya_jwt_refresh_time
end
gigya_jwt_refresh_time=(val) click to toggle source
# File lib/gigya/controller_utils.rb, line 9
def self.gigya_jwt_refresh_time=(val)
        @@gigya_jwt_refresh_time = val
end
gigya_refresh_time_decay() click to toggle source
# File lib/gigya/controller_utils.rb, line 22
def self.gigya_refresh_time_decay
        @@gigya_refresh_time_decay
end
gigya_refresh_time_decay=(val) click to toggle source
# File lib/gigya/controller_utils.rb, line 18
def self.gigya_refresh_time_decay=(val)
        @@gigya_refresh_time_decay = val
end

Public Instance Methods

gigya_jwt_token() click to toggle source

Obtain the token from the standard places

# File lib/gigya/controller_utils.rb, line 35
def gigya_jwt_token
        @gigya_jwt_token ||= begin
                tmp_token = nil
                token_location = nil

                begin
                        authenticate_with_http_token do |token, options|
                                tmp_token = token
                                token_location = :header
                        end
                rescue
                        # If this is being called from a helper instead of a controller, then the authenticate_with_http_token is not available.
                        # Additionally, we probably can't even use the HTTP Authorization header anyway
                end

                begin
                        if tmp_token.blank?
                                tmp_token = params[GIGYA_QUERY_PARAM] unless params[GIGYA_QUERY_PARAM].blank?
                                token_location = :param
                                if tmp_token.blank?
                                        tmp_token = cookies[GIGYA_COOKIE_PARAM]
                                        token_location = :cookie
                                end
                        end
                rescue
                        # Some lightweight controllers don't do cookies
                end

                begin
                        if tmp_token.blank?
                                tmp_token = session[GIGYA_SESSION_PARAM]
                                token_location = :session
                        end
                rescue
                        # Some lightweight controllers don't do sessions
                end

                token_location = nil if tmp_token.blank?

                @gigya_token_location = token_location

                tmp_token
        end
end
gigya_perform_token_refresh() click to toggle source
# File lib/gigya/controller_utils.rb, line 96
def gigya_perform_token_refresh
        info = gigya_user_information

        fields = info.keys - ["iss", "apiKey", "iat", "exp", "sub"]
        if @@gigya_refresh_time_decay
                # Refresh only until the original token expires
                # Note that this is slightly leaky
                expiration = (Time.at(info["exp"]) - Time.now).to_i
        else
                # Keep refreshing with the same time period
                expiration = info["exp"] - info["iat"]
        end
        expiration_time = Time.now + expiration
        result = Gigya::Connection.shared_connection.api_get("accounts", "getJWT", {:targetUID => gigya_user_identifier, :fields => fields.join(","), :expiration => expiration})
        token = result["id_token"]

        raise "Unable to refresh token" if token.blank?

        case @gigya_token_location
                when :header
                        headers["X-Set-Authorization-Token"] = token
                        headers["X-Set-Authorization-Token-Expiration"] = expiration_time.to_i.to_s
                when :cookie
                        cookies[GIGYA_COOKIE_PARAM] = token
                when :session
                        session[GIGYA_SESSION_PARAM] = token
                when :param
                        # FIXME - don't know what to do here.
        end
        @gigya_jwt_token = token
        interpret_jwt_token(true) # Force reinterpretation of token
end
gigya_save_jwt(destination = :cookie) click to toggle source
# File lib/gigya/controller_utils.rb, line 129
def gigya_save_jwt(destination = :cookie)
        interpret_jwt_token
        if destination == :cookie
                cookies[GIGYA_COOKIE_PARAM] = gigya_jwt_token
        elsif destination == :session
                cookies[GIGYA_SESSION_PARAM] = gigya_jwt_token
        else
                raise "Invalid Gigya JWT destination"
        end
end
gigya_user_identifier() click to toggle source
# File lib/gigya/controller_utils.rb, line 157
def gigya_user_identifier
        @gigya_user_identifier ||= begin
                interpret_jwt_token
                @gigya_jwt_info["sub"]
        end
end
gigya_user_information() click to toggle source
# File lib/gigya/controller_utils.rb, line 152
def gigya_user_information
        interpret_jwt_token
        @gigya_jwt_info
end
gigya_user_required() click to toggle source
# File lib/gigya/controller_utils.rb, line 26
def gigya_user_required
        begin
                render(:json => {:error => "Invalid login"}, :status => 401) if gigya_user_identifier.blank?
        rescue
                render(:json => {:error => "#{$!.message}"}, :status => 401)
        end
end
interpret_jwt_token(force = false) click to toggle source
# File lib/gigya/controller_utils.rb, line 80
def interpret_jwt_token(force = false)
        if @gigya_jwt_info.nil?
                @gigya_jwt_info = Gigya::Connection.shared_connection.validate_jwt(gigya_jwt_token)

                perform_token_refresh if needs_token_refresh?
        elsif force
                @gigya_jwt_info = Gigya::Connection.shared_connection.validate_jwt(gigya_jwt_token)
        end

        @gigya_jwt_info
end
needs_token_refresh?() click to toggle source
# File lib/gigya/controller_utils.rb, line 140
def needs_token_refresh?
        needs_token_refresh_for_time?
end
needs_token_refresh_for_time?() click to toggle source
# File lib/gigya/controller_utils.rb, line 144
def needs_token_refresh_for_time?
        return false if @@gigya_jwt_refresh_time.nil?

        issue_time = Time.at(@gigya_jwt_info["iat"].to_i)

        return issue_time + @@gigya_jwt_refresh_time < Time.now
end
perform_token_refresh() click to toggle source
# File lib/gigya/controller_utils.rb, line 92
def perform_token_refresh
        gigya_perform_token_refresh
end