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
-
#AuthorisationOnly_from_credentials(username, **options) ⇒ Object
Constructs an Authentication-only authorisation token from the given
username. -
#Basic_from_credentials(username, password, domain = nil, **options) ⇒ Object
Constructs a Basic Authentication authorisation token from the given
username,password, and, optionally,domain. -
#credentials_from_AuthorisationOnly(auth, **options) ⇒ Object
Returns a credentials array containing only username.
-
#credentials_from_Basic(auth, **options) ⇒ Object
Returns a credentials array - [ username, password, domain ] - if present.
-
#credentials_from_JWT(auth, jwt_secret, **options) ⇒ Arrary<::String>
Returns a credentials array from a JSON Web Token.
-
#JWT_from_credentials(session_id, user_id, user_name, jwt_algorithm, jwt_secret, **options) ⇒ ::String
Constructs a JWT authorisation token for a user session.
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, ** trace ParamNames[ :username, :options ], username, 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 benil:domain[ ::String ] - the domain. May benil:options[ ::Hash ] - options
-
Options:
:no_chomp[ boolean ] Prevents the result being chomped
Return
A string of the form 'Basic :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, ** trace ParamNames[ :username, :password, :domain, :options ], username, password, domain, 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 [: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] - causesnilto be returned if not matched; otherwise an emptyArrayis 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, ** trace ParamNames[ :auth, :options ], auth, username = nil if auth =~ /^RazorRisk\.(?:Cassini|Razor)\.Auth(?:ori[sz]ation)Only /i && !$'.empty? username = $'.unpack('m')[0] [ username, nil, nil ] else [: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] - causesnilto be returned if not matched; otherwise an emptyArrayis 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, ** trace ParamNames[ :auth, :options ], auth, 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 [:nil] ? nil : [] end |
#credentials_from_JWT(auth, jwt_secret, **options) ⇒ Arrary<::String>
Returns a credentials array from a JSON Web Token.
- Parameters:
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, ** trace( ParamNames[ :auth, :jwt_secret, :options ], auth, jwt_secret, ) 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 [: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.
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, ** trace( ParamNames[ :session_id, :jwt_algorithm, :jwt_secret, :options ], session_id, jwt_algorithm, jwt_secret, ) 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 |