Class: Spree::RefreshToken
- Inherits:
-
Object
- Object
- Spree::RefreshToken
- Defined in:
- app/models/spree/refresh_token.rb
Class Method Summary collapse
-
.cleanup_expired! ⇒ Object
Clean up expired tokens.
-
.create_for(user, audience:, request_env: {}) ⇒ Spree::RefreshToken
Create a refresh token for a user.
- .default_expiry ⇒ Object
-
.revoke_all_for(user) ⇒ Object
Revoke all refresh tokens for a user (e.g., on password change).
Instance Method Summary collapse
- #expired? ⇒ Boolean
-
#last_active_at ⇒ ActiveSupport::TimeWithZone
Each refresh replaces the row, so its creation is the session's last use.
-
#rotate!(request_env: {}) ⇒ Object
Rotate: destroy this token and create a new one.
Class Method Details
.cleanup_expired! ⇒ Object
Clean up expired tokens
81 82 83 |
# File 'app/models/spree/refresh_token.rb', line 81 def self.cleanup_expired! expired.delete_all end |
.create_for(user, audience:, request_env: {}) ⇒ Spree::RefreshToken
Create a refresh token for a user.
65 66 67 68 69 70 71 72 73 |
# File 'app/models/spree/refresh_token.rb', line 65 def self.create_for(user, audience:, request_env: {}) create!( user: user, audience: audience, expires_at: default_expiry.from_now, ip_address: request_env[:ip_address], user_agent: request_env[:user_agent] ) end |
.default_expiry ⇒ Object
85 86 87 88 89 |
# File 'app/models/spree/refresh_token.rb', line 85 def self.default_expiry Spree::Api::Config[:refresh_token_expiry].seconds rescue StandardError 30.days end |
.revoke_all_for(user) ⇒ Object
Revoke all refresh tokens for a user (e.g., on password change)
76 77 78 |
# File 'app/models/spree/refresh_token.rb', line 76 def self.revoke_all_for(user) where(user: user).delete_all end |
Instance Method Details
#expired? ⇒ Boolean
29 30 31 |
# File 'app/models/spree/refresh_token.rb', line 29 def expired? expires_at <= Time.current end |
#last_active_at ⇒ ActiveSupport::TimeWithZone
Each refresh replaces the row, so its creation is the session's last use.
35 36 37 |
# File 'app/models/spree/refresh_token.rb', line 35 def last_active_at created_at end |
#rotate!(request_env: {}) ⇒ Object
Rotate: destroy this token and create a new one. Returns the new token.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'app/models/spree/refresh_token.rb', line 41 def rotate!(request_env: {}) new_token = nil transaction do new_token = self.class.create!( user: user, audience: audience, expires_at: self.class.default_expiry.from_now, ip_address: request_env[:ip_address] || ip_address, user_agent: request_env[:user_agent] || user_agent ) destroy! end new_token end |