Class: Tempest::Session
- Inherits:
-
Object
- Object
- Tempest::Session
- Defined in:
- lib/tempest/session.rb
Constant Summary collapse
- EXPIRY_LEEWAY_SECONDS =
30
Instance Attribute Summary collapse
-
#access_jwt ⇒ Object
readonly
Returns the value of attribute access_jwt.
-
#did ⇒ Object
readonly
Returns the value of attribute did.
-
#handle ⇒ Object
readonly
Returns the value of attribute handle.
-
#identifier ⇒ Object
Returns the value of attribute identifier.
-
#on_change ⇒ Object
Returns the value of attribute on_change.
-
#pds_host ⇒ Object
readonly
Returns the value of attribute pds_host.
-
#refresh_jwt ⇒ Object
readonly
Returns the value of attribute refresh_jwt.
Class Method Summary collapse
Instance Method Summary collapse
- #access_expired? ⇒ Boolean
-
#initialize(access_jwt:, refresh_jwt:, did:, handle:, pds_host:, identifier: nil) ⇒ Session
constructor
A new instance of Session.
- #refresh! ⇒ Object
Constructor Details
#initialize(access_jwt:, refresh_jwt:, did:, handle:, pds_host:, identifier: nil) ⇒ Session
Returns a new instance of Session.
42 43 44 45 46 47 48 49 |
# File 'lib/tempest/session.rb', line 42 def initialize(access_jwt:, refresh_jwt:, did:, handle:, pds_host:, identifier: nil) @access_jwt = access_jwt @refresh_jwt = refresh_jwt @did = did @handle = handle @pds_host = pds_host @identifier = identifier end |
Instance Attribute Details
#access_jwt ⇒ Object (readonly)
Returns the value of attribute access_jwt.
11 12 13 |
# File 'lib/tempest/session.rb', line 11 def access_jwt @access_jwt end |
#did ⇒ Object (readonly)
Returns the value of attribute did.
11 12 13 |
# File 'lib/tempest/session.rb', line 11 def did @did end |
#handle ⇒ Object (readonly)
Returns the value of attribute handle.
11 12 13 |
# File 'lib/tempest/session.rb', line 11 def handle @handle end |
#identifier ⇒ Object
Returns the value of attribute identifier.
12 13 14 |
# File 'lib/tempest/session.rb', line 12 def identifier @identifier end |
#on_change ⇒ Object
Returns the value of attribute on_change.
12 13 14 |
# File 'lib/tempest/session.rb', line 12 def on_change @on_change end |
#pds_host ⇒ Object (readonly)
Returns the value of attribute pds_host.
11 12 13 |
# File 'lib/tempest/session.rb', line 11 def pds_host @pds_host end |
#refresh_jwt ⇒ Object (readonly)
Returns the value of attribute refresh_jwt.
11 12 13 |
# File 'lib/tempest/session.rb', line 11 def refresh_jwt @refresh_jwt end |
Class Method Details
.create(config, auth_factor_token: nil) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/tempest/session.rb', line 14 def self.create(config, auth_factor_token: nil) url = "#{config.pds_host}/xrpc/com.atproto.server.createSession" body = { identifier: config.identifier, password: config.app_password } body[:authFactorToken] = auth_factor_token if auth_factor_token response = Tempest::HTTP.post_json(url, body: body) unless response.ok? details = response.body.is_a?(Hash) ? response.body : {} raise AuthenticationError.new( "createSession failed (#{response.status}): #{details["message"] || response.body.inspect}", code: details["error"], ) end from_payload(response.body, pds_host: config.pds_host) end |
.from_payload(payload, pds_host:) ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/tempest/session.rb', line 32 def self.from_payload(payload, pds_host:) new( access_jwt: payload.fetch("accessJwt"), refresh_jwt: payload.fetch("refreshJwt"), did: payload.fetch("did"), handle: payload.fetch("handle"), pds_host: pds_host, ) end |
Instance Method Details
#access_expired? ⇒ Boolean
51 52 53 54 55 |
# File 'lib/tempest/session.rb', line 51 def access_expired? exp = jwt_exp(@access_jwt) return true if exp.nil? Time.now.to_i + EXPIRY_LEEWAY_SECONDS >= exp end |
#refresh! ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/tempest/session.rb', line 57 def refresh! url = "#{@pds_host}/xrpc/com.atproto.server.refreshSession" response = Tempest::HTTP.post_json( url, headers: { "Authorization" => "Bearer #{@refresh_jwt}" }, ) unless response.ok? details = response.body.is_a?(Hash) ? response.body : {} raise AuthenticationError.new( "refreshSession failed (#{response.status}): #{details["message"] || response.body.inspect}", code: details["error"], ) end @access_jwt = response.body.fetch("accessJwt") @refresh_jwt = response.body.fetch("refreshJwt") @did = response.body.fetch("did") @handle = response.body.fetch("handle") @on_change&.call(self) self end |