Class: Ksef::Sessions
- Inherits:
-
Object
- Object
- Ksef::Sessions
- Defined in:
- lib/ksef/sessions.rb
Overview
Manages the KSeF interactive session lifecycle:
1. `POST /auth/challenge` → challenge + timestamp
2. encrypt the integration token with the KSeF public RSA key
3. `POST /auth/ksef-token` → authentication operation token
4. poll `GET /auth/{ref}` → wait for status = success
5. `POST /auth/token/redeem` → access + refresh tokens
6. on close: `DELETE /auth/sessions/current`
The most common entry point is #with_interactive, which sets up the session, yields it to the caller’s block, and tears it down on exit.
Constant Summary collapse
- AUTH_SUCCESS_STATUS =
200- AUTH_PENDING_STATUS =
100- DEFAULT_POLL_INTERVAL =
1.0- DEFAULT_POLL_TIMEOUT =
60
Instance Method Summary collapse
-
#initialize(client) ⇒ Sessions
constructor
A new instance of Sessions.
-
#open(poll_interval: DEFAULT_POLL_INTERVAL, poll_timeout: DEFAULT_POLL_TIMEOUT) ⇒ Ksef::Session
Acquires a new authenticated session.
-
#public_key_for_token_encryption ⇒ Object
Fetches the freshest public-key certificate suitable for token encryption.
-
#terminate(session) ⇒ Object
Closes the session by calling ‘DELETE /auth/sessions/current`.
-
#with_interactive(**opts) {|Ksef::Session| ... } ⇒ Object
Opens a session, yields it, and ensures it is terminated.
Constructor Details
#initialize(client) ⇒ Sessions
Returns a new instance of Sessions.
21 22 23 |
# File 'lib/ksef/sessions.rb', line 21 def initialize(client) @client = client end |
Instance Method Details
#open(poll_interval: DEFAULT_POLL_INTERVAL, poll_timeout: DEFAULT_POLL_TIMEOUT) ⇒ Ksef::Session
Acquires a new authenticated session. Callers can then attach it via Client#current_session= if they prefer manual management.
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/ksef/sessions.rb', line 46 def open(poll_interval: DEFAULT_POLL_INTERVAL, poll_timeout: DEFAULT_POLL_TIMEOUT) credentials = @client.credentials case credentials when Credentials::Token open_with_token(credentials, poll_interval: poll_interval, poll_timeout: poll_timeout) else raise NotImplementedError, "Only Ksef::Credentials::Token is supported in v#{Ksef::VERSION}" end end |
#public_key_for_token_encryption ⇒ Object
Fetches the freshest public-key certificate suitable for token encryption. Cached for the lifetime of the Sessions instance.
72 73 74 |
# File 'lib/ksef/sessions.rb', line 72 def public_key_for_token_encryption @public_key_for_token_encryption ||= fetch_public_key end |
#terminate(session) ⇒ Object
Closes the session by calling ‘DELETE /auth/sessions/current`.
58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/ksef/sessions.rb', line 58 def terminate(session) return if session.terminated? @client.connection.request( :delete, "/auth/sessions/current", bearer_token: session.access_token ) session.mark_terminated! session end |
#with_interactive(**opts) {|Ksef::Session| ... } ⇒ Object
Opens a session, yields it, and ensures it is terminated.
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/ksef/sessions.rb', line 29 def with_interactive(**opts) session = open(**opts) @client.current_session = session begin yield session ensure terminate(session) unless session.terminated? @client.current_session = nil end end |