Class: Scaled::Auth::OAuthClientCredentials

Inherits:
Object
  • Object
show all
Defined in:
lib/scaled/auth/oauth_client_credentials.rb

Overview

OAuth Client Credentials authentication strategy. Стратегія OAuth Client Credentials для автентифікації.

Constant Summary collapse

DEFAULT_TOKEN_URL =
"https://api.tailscale.com/api/v2/oauth/token"
REFRESH_SAFETY_WINDOW =
30

Instance Method Summary collapse

Constructor Details

#initialize(client_id:, client_secret:, **options) ⇒ void

Note: token refresh is automatic when expiry approaches. Нотатка: токен оновлюється автоматично при наближенні до завершення дії.

Parameters:

  • client_id (String)

    OAuth client identifier

  • client_secret (String)

    OAuth client secret

  • options (Hash)

    optional settings (:scopes, :token_url, :open_timeout, :read_timeout, :now)



21
22
23
24
25
26
27
28
29
30
# File 'lib/scaled/auth/oauth_client_credentials.rb', line 21

def initialize(client_id:, client_secret:, **options)
  @client_id = normalize_required(client_id, "client_id")
  @client_secret = normalize_required(client_secret, "client_secret")
  @scopes = Array(options.fetch(:scopes, [])).map(&:to_s).map(&:strip).reject(&:empty?)
  @token_url = options.fetch(:token_url, DEFAULT_TOKEN_URL)
  @open_timeout = options.fetch(:open_timeout, 5)
  @read_timeout = options.fetch(:read_timeout, 30)
  @now = options.fetch(:now, -> { Time.now })
  @token_data = nil
end

Instance Method Details

#apply(headers) ⇒ Hash{String => String}

Note: mutates and returns the same hash for compatibility with HTTP layer. Нотатка: змінює і повертає той самий hash для сумісності з HTTP-шаром.

Parameters:

  • headers (Hash{String => String})

    mutable request headers

Returns:

  • (Hash{String => String})

    same hash with Bearer authorization



36
37
38
39
# File 'lib/scaled/auth/oauth_client_credentials.rb', line 36

def apply(headers)
  headers["Authorization"] = "Bearer #{access_token}"
  headers
end