Class: RazorRisk::Cassini::Applications::SecurableMicroservice

Inherits:
Microservice
  • Object
show all
Includes:
RazorRisk::Core::Diagnostics::Logger
Defined in:
lib/razor_risk/cassini/applications/securable_microservice.rb

Direct Known Subclasses

SecuredMicroservice

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Microservice

define_catch_all_handlers, full_description, init_base_service, run!

Class Method Details

.init_service(host: to_nil(host_not_given_ = true), port: to_nil(port_not_given_ = true), authentication_scheme: to_nil(authentication_scheme_not_given_ = true), jwt_encoding_algorithm: to_nil(jwt_encoding_algorithm_not_given_ = true), secrets: to_nil(secrets_not_given_ = true), **options) ⇒ Object

This must be invoked prior to activation of the application

Signature

  • Parameters:

    - host

    [String] The host to which the application binds. May be: nil, in which case no binding is performed and the Sinatra defaults are observed; +'0.0.0.0', in which case the server will listen on all available interfaces; a specific IP addres, in which case the server will listen only on that address. It is recommend that a specific address is used. Required

    - port

    [Integer] The port on which to activate. Required

    - authentication_scheme

    [String, Symbol] The authentication scheme to be used. Must be one of RECOGNISED_AUTHENTICATION_SCHEMES. Required

    - jwt_encoding_algorithm

    [String] The algorithm that will be used for encoding the JWT in the :jwt authentication scheme. Must be one of RECOGNISED_ALGORITHMS. Required only if authentication_scheme is :jwt

    - secrets

    [Hash] An association of secrets for algorithms. Must contain at least secrets for the algorithms specified for jwt_encoding_algorithm.

Raises:

  • (ArgumentError)


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/razor_risk/cassini/applications/securable_microservice.rb', line 82

def self.init_service \
    host: to_nil(host_not_given_ = true),
    port: to_nil(port_not_given_ = true),
    authentication_scheme: to_nil(authentication_scheme_not_given_ = true),
    jwt_encoding_algorithm: to_nil(jwt_encoding_algorithm_not_given_ = true),
    secrets: to_nil(secrets_not_given_ = true),
    **options

    trace ParamNames[ :host, :port, :authentication_scheme, :jwt_encoding_algorithm, :secrets ], host, port, authentication_scheme, jwt_encoding_algorithm, secrets

    raise ArgumentError, 'missing keyword: host' if host_not_given_
    check_parameter host, 'host', type: ::String, allow_nil: true

    raise ArgumentError, 'missing keyword: port' if port_not_given_
    check_parameter port, 'port', type: Integer

    raise ArgumentError, 'missing keyword: authentication_scheme' if authentication_scheme_not_given_
    # check_parameter cannot types types then convert (by block) then
    # check values in one operation, so instead convert to Symbol only
    # if String
    authentication_scheme = authentication_scheme.to_sym if ::String === authentication_scheme
    check_parameter authentication_scheme, 'authentication_scheme', types: [ ::String, ::Symbol ], values: RECOGNISED_AUTHENTICATION_SCHEMES

    if :jwt == authentication_scheme

        if severity_logged? :debug4

            log :debug4, "jwt_encoding_algorithm (#{jwt_encoding_algorithm.class})=#{jwt_encoding_algorithm}"
        end

        raise ArgumentError, 'missing keyword: jwt_encoding_algorithm' if jwt_encoding_algorithm_not_given_
        check_parameter jwt_encoding_algorithm, 'jwt_encoding_algorithm', type: ::String, values: RECOGNISED_ALGORITHMS
    end

    init_service_ \
        authentication_scheme,
        jwt_encoding_algorithm,
        secrets,
        options

    # invoke superclass
    init_base_service host: host, port: port, **options
end

.secret(algorithm) ⇒ Object



126
127
128
129
130
131
132
133
134
# File 'lib/razor_risk/cassini/applications/securable_microservice.rb', line 126

def self.secret algorithm

    trace ParamNames[:algorithm], algorithm

    return nil if algorithm.nil?
    return nil unless settings.respond_to? :secrets

    settings.secrets[algorithm.downcase]
end

Instance Method Details

#secret(algorithm) ⇒ Object



136
137
138
139
140
141
# File 'lib/razor_risk/cassini/applications/securable_microservice.rb', line 136

def secret algorithm

    trace ParamNames[:algorithm], algorithm

    self.class.secret algorithm
end