Module: RazorRisk::Cassini::Authorisation::HeaderHelpers

Includes:
Pantheios, RazorRisk::Core::Diagnostics::Logger, Xqsr3::Quality::ParameterChecking
Included in:
Sinatra::Helpers::CheckAuthHelper
Defined in:
lib/razor_risk/cassini/authorisation/header_helpers.rb

Defined Under Namespace

Modules: Authorisation_HeaderHelpers_Constants_

Instance Method Summary collapse

Instance Method Details

#AuthorisationOnly_from_credentials(username, **options) ⇒ Object

Constructs an Authentication-only authorisation token from the given username

Signature

  • Parameters:

    • :username [ ::String ] - the username
    • :options [ ::Hash ] - options
  • Options:



147
148
149
150
151
152
153
154
# File 'lib/razor_risk/cassini/authorisation/header_helpers.rb', line 147

def AuthorisationOnly_from_credentials username, **options

    trace ParamNames[ :username, :options ], username, options

    check_parameter username, 'username', type: ::String

    'RazorRisk.Cassini.AuthorisationOnly ' + [ username ].pack('m')
end

#Basic_from_credentials(username, password, domain = nil, **options) ⇒ Object

Constructs a Basic Authentication authorisation token from the given username, password, and, optionally, domain.

Signature

  • Parameters:

    • :username [ ::String ] - the username
    • :password [ ::String ] - the password. May be nil
    • :domain [ ::String ] - the domain. May be nil
    • :options [ ::Hash ] - options
  • Options:

    • :no_chomp [ boolean ] Prevents the result being chomped

Return

A string of the form 'Basic '. Note that the string will NOT contain a trailing new-line sequence unless the option :no_chomp is specified



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/razor_risk/cassini/authorisation/header_helpers.rb', line 69

def Basic_from_credentials username, password, domain = nil, **options

    trace ParamNames[ :username, :password, :domain, :options ], username, password, domain, options

    check_parameter username, 'username', type: ::String

    if (domain || '').empty?

        credentials =   "#{username}:#{password}"
    else

        credentials =   "#{domain}\\#{username}:#{password}"
    end

    r   =   'Basic ' + [ credentials ].pack('m')

    r   =   r.chomp unless options[:no_chomp]

    r
end

#credentials_from_AuthorisationOnly(auth, **options) ⇒ Object

Returns a credentials array containing only username

Signature

  • Parameters:

    • auth [ ::String ] - the authorisation token, in the format "RazorRisk.Razor.AuthorisationOnly "
  • Options:

    • :nil [Boolean] - causes nil to be returned if not matched; otherwise an empty Array is returned


167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/razor_risk/cassini/authorisation/header_helpers.rb', line 167

def credentials_from_AuthorisationOnly auth, **options

    trace ParamNames[ :auth, :options ], auth, options

    username = nil

    if auth =~ /^RazorRisk\.(?:Cassini|Razor)\.Auth(?:ori[sz]ation)Only /i && !$'.empty?

        username = $'.unpack('m')[0]

        [ username, nil, nil ]
    else

        options[:nil] ? nil : []
    end
end

#credentials_from_Basic(auth, **options) ⇒ Object

Returns a credentials array - [ username, password, domain ] - if present

Signature

  • Parameters:

    • auth [ ::String ] - the authorisation token, in the format "Basic "
  • Options:

    • :nil [Boolean] - causes nil to be returned if not matched; otherwise an empty Array is returned


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/razor_risk/cassini/authorisation/header_helpers.rb', line 102

def credentials_from_Basic auth, **options

    trace ParamNames[ :auth, :options ], auth, options

    check_parameter auth, 'auth', type: ::String

    if auth !~ /^Basic /i

        log :failure, 'authorisation token is not Basic'
    else

        uid = $'.unpack('m')[0]

        if uid !~ /^([^:]+):(.*)$/

            log :failure, 'Basic authorisation token is not well-formed'
        else

            username    =   $1
            password    =   $2
            domain      =   nil

            if username =~ /\\/

                domain, username = $`, $'
            end

            return [ username, password, domain ]
        end
    end

    options[:nil] ? nil : []
end

#credentials_from_JWT(auth, jwt_secret, **options) ⇒ Arrary<::String>

Returns a credentials array from a JSON Web Token.

  • Parameters:

Parameters:

  • auth (::String) —

    The authorisation token, in the format "Bearer ", where is a JWT.

  • jwt_secret (::String) —

    The secret used for JWT encryption.

  • options (::Hash) —

    The options hash.

Options Hash (**options):

  • nil (Boolean) —

    Causes nil to be returned if not matched; otherwise an empty Array is returned.

Returns:

  • (Arrary<::String>) —

    A credentials array, [session_id], if present.



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/razor_risk/cassini/authorisation/header_helpers.rb', line 228

def credentials_from_JWT auth, jwt_secret, **options

    trace(
        ParamNames[ :auth, :jwt_secret, :options ],
        auth, jwt_secret, options
    )

    check_parameter auth, 'auth', type: ::String
    check_parameter jwt_secret, 'jwt_secret', type: ::String

    if auth !~ /^Bearer /i

        log :failure, 'authorisation token is not Bearer'
    else

        jwt = $'.strip

        payload, header = JWT.decode jwt, jwt_secret, true

        if payload && header

            session_id = payload['razorrisk.razor.session_id']
            user_id    = payload['razorrisk.razor.user_id']
            user_name  = payload['razorrisk.razor.user_name']

            return [
                session_id,
                user_id,
                user_name
            ] if session_id
        end
    end

    options[:nil] ? nil : []
end

#JWT_from_credentials(session_id, user_id, user_name, jwt_algorithm, jwt_secret, **options) ⇒ ::String

Constructs a JWT authorisation token for a user session.

Parameters:

  • session_id (::String) —

    The session ID.

  • user_id (::String) —

    The user ID.

  • user_name (::String) —

    The user's long name.

  • jwt_algorithm (::String) —

    the JWT algorithm

  • jwt_secret (::String) —

    the JWT secret

  • options (::Hash) —

    options

Returns:

  • (::String) —

    The created JSON Web Token.



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/razor_risk/cassini/authorisation/header_helpers.rb', line 195

def JWT_from_credentials session_id, user_id, user_name, jwt_algorithm, jwt_secret, **options

    trace(
        ParamNames[ :session_id, :jwt_algorithm, :jwt_secret, :options ],
        session_id, jwt_algorithm, jwt_secret, options
    )

    check_parameter session_id, 'session_id', type: ::String
    check_parameter jwt_secret, 'jwt_secret', type: ::String

    payload = {
        'razorrisk.razor.user_id'    => user_id,
        'razorrisk.razor.user_name'  => user_name,
        'razorrisk.razor.session_id' => session_id,
    }

    JWT.encode payload, jwt_secret, jwt_algorithm
end