Class: Msf::WebServices::Authentication::Strategies::ApiToken
- Inherits:
-
Warden::Strategies::Base
- Object
- Warden::Strategies::Base
- Msf::WebServices::Authentication::Strategies::ApiToken
- Defined in:
- lib/msf/core/web_services/authentication/strategies/api_token.rb
Direct Known Subclasses
Constant Summary collapse
- AUTHORIZATION =
'HTTP_AUTHORIZATION'- AUTHORIZATION_SCHEME =
'Bearer'- AUTHORIZATION_PATTERN =
/\A#{AUTHORIZATION_SCHEME}\s+(?<token>\S.*)\z/.freeze
Instance Method Summary collapse
-
#auth_from_db(token) ⇒ Object
Authenticates the user associated with the API token from the DB.
-
#auth_from_env(token) ⇒ Object
Authenticates the API token from an environment variable.
-
#authenticate! ⇒ Object
Authenticate the request.
-
#bearer_token ⇒ String?
The token carried by an Authorization header using the Bearer scheme.
-
#blank_token?(token) ⇒ Boolean
True if the token is missing or contains no usable characters.
-
#valid? ⇒ Boolean
Check if request contains valid data and should be authenticated.
-
#validate_user(user) ⇒ Hash
Validates the user associated with the API token.
Instance Method Details
#auth_from_db(token) ⇒ Object
Authenticates the user associated with the API token from the DB
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/msf/core/web_services/authentication/strategies/api_token.rb', line 38 def auth_from_db(token) db_manager = env['msf.db_manager'] # Reached when the application was never able to establish a database # connection. Reject rather than raising a 500 from a nil db_manager. throw(:warden, message: 'Authentication is not configured.', code: 401) if db_manager.nil? throw(:warden, message: 'Invalid API token.', code: 401) if blank_token?(token) user = db_manager.users(persistence_token: token).first validation_data = validate_user(user) if validation_data[:valid] success!(user) else throw(:warden, message: validation_data[:message], code: validation_data[:code]) end end |
#auth_from_env(token) ⇒ Object
Authenticates the API token from an environment variable
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/msf/core/web_services/authentication/strategies/api_token.rb', line 67 def auth_from_env(token) expected_token = request.env['msf.api_token'] # Compared in constant time to avoid leaking the token a byte at a time. if !blank_token?(token) && !blank_token?(expected_token) && Rack::Utils.secure_compare(token, expected_token) success!(message: "Successful auth from token") else throw(:warden, message: 'Invalid API token.', code: 401) end end |
#authenticate! ⇒ Object
Authenticate the request. Authentication is always enforced: there is no unauthenticated bootstrap, the first user is created out of band by msfdb.
18 19 20 21 22 23 |
# File 'lib/msf/core/web_services/authentication/strategies/api_token.rb', line 18 def authenticate! token = bearer_token throw(:warden, message: 'Invalid API token.', code: 401) if blank_token?(token) request.env['msf.api_token'].nil? ? auth_from_db(token) : auth_from_env(token) end |
#bearer_token ⇒ String?
The token carried by an Authorization header using the Bearer scheme. Tokens are only accepted here, never from the query string, so that they stay out of access logs.
30 31 32 33 34 35 |
# File 'lib/msf/core/web_services/authentication/strategies/api_token.rb', line 30 def bearer_token = request.env[AUTHORIZATION] return unless .is_a?(String) AUTHORIZATION_PATTERN.match()&.[](:token) end |
#blank_token?(token) ⇒ Boolean
Returns true if the token is missing or contains no usable characters.
79 80 81 |
# File 'lib/msf/core/web_services/authentication/strategies/api_token.rb', line 79 def blank_token?(token) !token.is_a?(String) || token.strip.empty? end |
#valid? ⇒ Boolean
Check if request contains valid data and should be authenticated.
12 13 14 |
# File 'lib/msf/core/web_services/authentication/strategies/api_token.rb', line 12 def valid? !bearer_token.nil? end |
#validate_user(user) ⇒ Hash
Validates the user associated with the API token.
62 63 64 |
# File 'lib/msf/core/web_services/authentication/strategies/api_token.rb', line 62 def validate_user(user) !user.nil? ? {valid: true, code: 0, message: nil} : {valid: false, code: 401, message: "Invalid API token."} end |