Module: Gigya::ControllerUtils

Defined in:
lib/gigya/controller_utils.rb

Overview

Helper/controller mixins

Constant Summary collapse

GIGYA_SESSION_PARAM =
:gigya_token
GIGYA_QUERY_PARAM =
:gigya_token
:gigya_token
@@gigya_jwt_refresh_time =
nil
@@gigya_refresh_time_decay =
true

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.gigya_jwt_refresh_timeObject


14
15
16
# File 'lib/gigya/controller_utils.rb', line 14

def self.gigya_jwt_refresh_time
  @@gigya_jwt_refresh_time
end

.gigya_jwt_refresh_time=(val) ⇒ Object


9
10
11
# File 'lib/gigya/controller_utils.rb', line 9

def self.gigya_jwt_refresh_time=(val)
  @@gigya_jwt_refresh_time = val
end

.gigya_refresh_time_decayObject


22
23
24
# File 'lib/gigya/controller_utils.rb', line 22

def self.gigya_refresh_time_decay
  @@gigya_refresh_time_decay
end

.gigya_refresh_time_decay=(val) ⇒ Object


18
19
20
# File 'lib/gigya/controller_utils.rb', line 18

def self.gigya_refresh_time_decay=(val)
  @@gigya_refresh_time_decay = val
end

Instance Method Details

#gigya_jwt_tokenObject

Obtain the token from the standard places


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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# 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
      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
    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_refreshObject


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/gigya/controller_utils.rb', line 94

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
    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) ⇒ Object


127
128
129
130
131
132
133
134
135
136
# File 'lib/gigya/controller_utils.rb', line 127

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_identifierObject


155
156
157
158
159
160
# File 'lib/gigya/controller_utils.rb', line 155

def gigya_user_identifier
  @gigya_user_identifier ||= begin
    interpret_jwt_token
    @gigya_jwt_info["sub"]
  end
end

#gigya_user_informationObject


150
151
152
153
# File 'lib/gigya/controller_utils.rb', line 150

def gigya_user_information
  interpret_jwt_token
  @gigya_jwt_info
end

#gigya_user_requiredObject


26
27
28
29
30
31
32
# 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) ⇒ Object


78
79
80
81
82
83
84
85
86
87
88
# File 'lib/gigya/controller_utils.rb', line 78

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?Boolean

Returns:

  • (Boolean)

138
139
140
# File 'lib/gigya/controller_utils.rb', line 138

def needs_token_refresh?
  needs_token_refresh_for_time?
end

#needs_token_refresh_for_time?Boolean

Returns:

  • (Boolean)

142
143
144
145
146
147
148
# File 'lib/gigya/controller_utils.rb', line 142

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_refreshObject


90
91
92
# File 'lib/gigya/controller_utils.rb', line 90

def perform_token_refresh
  gigya_perform_token_refresh
end