Class: Momento::CredentialProvider

Inherits:
Object
  • Object
show all
Defined in:
lib/momento/auth/credential_provider.rb

Overview

Contains the information required for a Momento client to connect to and authenticate with Momento services.

Defined Under Namespace

Classes: AuthTokenData

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



7
8
9
# File 'lib/momento/auth/credential_provider.rb', line 7

def api_key
  @api_key
end

#cache_endpointObject (readonly)

Returns the value of attribute cache_endpoint.



7
8
9
# File 'lib/momento/auth/credential_provider.rb', line 7

def cache_endpoint
  @cache_endpoint
end

#control_endpointObject (readonly)

Returns the value of attribute control_endpoint.



7
8
9
# File 'lib/momento/auth/credential_provider.rb', line 7

def control_endpoint
  @control_endpoint
end

Class Method Details

.from_api_key_v2(api_key:, endpoint:) ⇒ Momento::CredentialProvider

Creates a CredentialProvider from a v2 API key string and endpoint.

Parameters:

  • api_key (String)

    the v2 API key

  • endpoint (String)

    the Momento service endpoint

Returns:

Raises:



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/momento/auth/credential_provider.rb', line 47

def self.from_api_key_v2(api_key:, endpoint:)
  raise Momento::Error::InvalidArgumentError, 'API key cannot be empty' if api_key.nil? || api_key.empty?
  raise Momento::Error::InvalidArgumentError, 'Endpoint cannot be empty' if endpoint.nil? || endpoint.empty?

  unless v2_api_key?(api_key)
    raise Momento::Error::InvalidArgumentError,
      'Received an invalid v2 API key. Are you using the correct key? \
      Or did you mean to use `from_string()` with a legacy key instead?'
  end

  allocate.tap do |instance|
    instance.send(:initialize_from_v2, api_key, endpoint)
  end
end

.from_disposable_token(token) ⇒ Momento::CredentialProvider

Creates a CredentialProvider from a Momento disposable token

Parameters:

  • token (String)

    the Momento disposable token

Returns:

Raises:



36
37
38
39
40
# File 'lib/momento/auth/credential_provider.rb', line 36

def self.from_disposable_token(token)
  raise Momento::Error::InvalidArgumentError, 'Auth token string cannot be empty' if token.empty?

  new(token)
end

.from_env_var(env_var_name) ⇒ Momento::CredentialProvider

Deprecated.

Please use #from_env_var_v2 instead.

Creates a CredentialProvider from a Momento API key loaded from an environment variable.

Parameters:

  • env_var_name (String)

    the environment variable containing the API key

Returns:

Raises:



14
15
16
17
18
19
# File 'lib/momento/auth/credential_provider.rb', line 14

def self.from_env_var(env_var_name)
  api_key = ENV.fetch(env_var_name) {
    raise Momento::Error::InvalidArgumentError, "Env var #{env_var_name} must be set"
  }
  new(api_key)
end

.from_env_var_v2(api_key_env_var: "MOMENTO_API_KEY", endpoint_env_var: "MOMENTO_ENDPOINT") ⇒ Momento::CredentialProvider

Creates a CredentialProvider from a v2 API key and endpoint loaded from environment variables MOMENTO_API_KEY and MOMENTO_ENDPOINT by default.

Parameters:

  • api_key_env_var (String) (defaults to: "MOMENTO_API_KEY")

    optionally provide alternate environment variable containing the v2 API key

  • endpoint_env_var (String) (defaults to: "MOMENTO_ENDPOINT")

    optionally provide alternate environment variable containing the endpoint

Returns:

Raises:



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/momento/auth/credential_provider.rb', line 68

def self.from_env_var_v2(api_key_env_var: "MOMENTO_API_KEY", endpoint_env_var: "MOMENTO_ENDPOINT")
  api_key = ENV.fetch(api_key_env_var) {
    raise Momento::Error::InvalidArgumentError, "Env var #{api_key_env_var} must be set"
  }
  endpoint = ENV.fetch(endpoint_env_var) {
    raise Momento::Error::InvalidArgumentError, "Env var #{endpoint_env_var} must be set"
  }
  unless v2_api_key?(api_key)
    raise Momento::Error::InvalidArgumentError,
      'Received an invalid v2 API key. Are you using the correct key? \
      Or did you mean to use `from_env_var()` with a legacy key instead?'
  end
  allocate.tap do |instance|
    instance.send(:initialize_from_v2, api_key, endpoint)
  end
end

.from_string(api_key) ⇒ Momento::CredentialProvider

Deprecated.

Please use #from_api_key_v2 or #from_disposable_token instead.

Creates a CredentialProvider from a Momento API key

Parameters:

  • api_key (String)

    the Momento API key

Returns:

Raises:



26
27
28
29
30
# File 'lib/momento/auth/credential_provider.rb', line 26

def self.from_string(api_key)
  raise Momento::Error::InvalidArgumentError, 'Auth token string cannot be empty' if api_key.empty?

  new(api_key)
end