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_COOKIE_PARAM =
:gigya_token
- @@gigya_jwt_refresh_time =
nil
- @@gigya_refresh_time_decay =
true
Class Method Summary collapse
- .gigya_jwt_refresh_time ⇒ Object
- .gigya_jwt_refresh_time=(val) ⇒ Object
- .gigya_refresh_time_decay ⇒ Object
- .gigya_refresh_time_decay=(val) ⇒ Object
Instance Method Summary collapse
-
#gigya_jwt_token ⇒ Object
Obtain the token from the standard places.
- #gigya_perform_token_refresh ⇒ Object
- #gigya_save_jwt(destination = :cookie) ⇒ Object
- #gigya_user_identifier ⇒ Object
- #gigya_user_information ⇒ Object
- #gigya_user_required ⇒ Object
- #interpret_jwt_token(force = false) ⇒ Object
- #needs_token_refresh? ⇒ Boolean
- #needs_token_refresh_for_time? ⇒ Boolean
- #perform_token_refresh ⇒ Object
Class Method Details
.gigya_jwt_refresh_time ⇒ Object
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_decay ⇒ Object
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_token ⇒ Object
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 77 78 |
# 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, | 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 = [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 ⇒ Object
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 126 127 |
# 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 [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
129 130 131 132 133 134 135 136 137 138 |
# File 'lib/gigya/controller_utils.rb', line 129 def gigya_save_jwt(destination = :cookie) interpret_jwt_token if destination == :cookie [GIGYA_COOKIE_PARAM] = gigya_jwt_token elsif destination == :session [GIGYA_SESSION_PARAM] = gigya_jwt_token else raise "Invalid Gigya JWT destination" end end |
#gigya_user_identifier ⇒ Object
157 158 159 160 161 162 |
# 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 ⇒ Object
152 153 154 155 |
# File 'lib/gigya/controller_utils.rb', line 152 def gigya_user_information interpret_jwt_token @gigya_jwt_info end |
#gigya_user_required ⇒ Object
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 => "#{$!.}"}, :status => 401) end end |
#interpret_jwt_token(force = false) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 |
# 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? ⇒ Boolean
140 141 142 |
# File 'lib/gigya/controller_utils.rb', line 140 def needs_token_refresh? needs_token_refresh_for_time? end |
#needs_token_refresh_for_time? ⇒ Boolean
144 145 146 147 148 149 150 |
# 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 ⇒ Object
92 93 94 |
# File 'lib/gigya/controller_utils.rb', line 92 def perform_token_refresh gigya_perform_token_refresh end |