Class: PDND::JWTGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/pdnd-ruby-client/jwt_generator.rb

Overview

Classe responsabile della generazione di token JWT per autenticare le richieste verso l'API PDND. Firma il token con una chiave RSA (RS256) e lo invia al server OAuth2 per ottenere un access token.

Examples:

generator = PDND::JWTGenerator.new(config)
token, exp = generator.generate_token

JWT Generator collapse

JWT Generator collapse

Constructor Details

#initialize(config, env = 'produzione') ⇒ JWTGenerator

Returns a new instance of JWTGenerator.

Parameters:

  • config (Hash)

    Configurazione con chiavi come :issuer, :clientId, :privKeyPath, ecc.

  • env (String) (defaults to: 'produzione')

    Ambiente ('produzione' o 'collaudo' o 'attestazione')



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/pdnd-ruby-client/jwt_generator.rb', line 35

def initialize(config, env = 'produzione')
  @config     = config
  @env        = env
  @assertion  = ''
  @token      = ''
  @token_exp  = ''
  @debug      = false

  assign_config_values(config)
  configure_environment
end

Instance Attribute Details

#assertionObject

Returns the value of attribute assertion.



29
30
31
# File 'lib/pdnd-ruby-client/jwt_generator.rb', line 29

def assertion
  @assertion
end

#audienceObject

Returns the value of attribute audience.



29
30
31
# File 'lib/pdnd-ruby-client/jwt_generator.rb', line 29

def audience
  @audience
end

#client_idObject

Returns the value of attribute client_id.



29
30
31
# File 'lib/pdnd-ruby-client/jwt_generator.rb', line 29

def client_id
  @client_id
end

#configObject

Returns the value of attribute config.



29
30
31
# File 'lib/pdnd-ruby-client/jwt_generator.rb', line 29

def config
  @config
end

#debugObject

Returns the value of attribute debug.



29
30
31
# File 'lib/pdnd-ruby-client/jwt_generator.rb', line 29

def debug
  @debug
end

#endpointObject

Returns the value of attribute endpoint.



29
30
31
# File 'lib/pdnd-ruby-client/jwt_generator.rb', line 29

def endpoint
  @endpoint
end

#envObject

Returns the value of attribute env.



29
30
31
# File 'lib/pdnd-ruby-client/jwt_generator.rb', line 29

def env
  @env
end

#issuerObject

Returns the value of attribute issuer.



29
30
31
# File 'lib/pdnd-ruby-client/jwt_generator.rb', line 29

def issuer
  @issuer
end

#kidObject

Returns the value of attribute kid.



29
30
31
# File 'lib/pdnd-ruby-client/jwt_generator.rb', line 29

def kid
  @kid
end

#priv_keyObject

Returns the value of attribute priv_key.



29
30
31
# File 'lib/pdnd-ruby-client/jwt_generator.rb', line 29

def priv_key
  @priv_key
end

#priv_key_pathObject

Returns the value of attribute priv_key_path.



29
30
31
# File 'lib/pdnd-ruby-client/jwt_generator.rb', line 29

def priv_key_path
  @priv_key_path
end

#purpose_idObject

Returns the value of attribute purpose_id.



29
30
31
# File 'lib/pdnd-ruby-client/jwt_generator.rb', line 29

def purpose_id
  @purpose_id
end

#tokenObject

Returns the value of attribute token.



29
30
31
# File 'lib/pdnd-ruby-client/jwt_generator.rb', line 29

def token
  @token
end

#token_expObject

Returns the value of attribute token_exp.



29
30
31
# File 'lib/pdnd-ruby-client/jwt_generator.rb', line 29

def token_exp
  @token_exp
end

Instance Method Details

#generate_tokenArray<String>

Returns Token di accesso e data di scadenza formattata.

Returns:

  • (Array<String>)

    Token di accesso e data di scadenza formattata



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/pdnd-ruby-client/jwt_generator.rb', line 48

def generate_token
  private_key = load_private_key
  @assertion = JWT.encode(build_payload, private_key, 'RS256', build_header)
  debug_log('🔐 Token JWT generato', @assertion)

  response_body = post_assertion
  @token = response_body['access_token']
  @token_exp = format_expiration(response_body['expires_in'])

  debug_log('✅ Token generato', @token)
  debug_log('✅ Token scadenza', @token_exp)

  [@token, @token_exp]
end