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'
- TOKEN_QUERY_PARAM =
'token'
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.
-
#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
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/msf/core/web_services/authentication/strategies/api_token.rb', line 34 def auth_from_db(token) db_manager = env['msf.db_manager'] 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
57 58 59 60 61 62 63 |
# File 'lib/msf/core/web_services/authentication/strategies/api_token.rb', line 57 def auth_from_env(token) if token == request.env['msf.api_token'] success!(message: "Successful auth from token") else throw(:warden, message: 'Invalid API token.', code: 401) end end |
#authenticate! ⇒ Object
Authenticate the request.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/msf/core/web_services/authentication/strategies/api_token.rb', line 17 def authenticate! auth_initialized = request.env['msf.auth_initialized'] = request.env[AUTHORIZATION] if !auth_initialized success!({message: "Initialize authentication by creating an initial user account."}) else if .is_a?(String) && .start_with?(AUTHORIZATION_SCHEME) token = .sub(/^#{AUTHORIZATION_SCHEME}\s+/, '') else token = params[TOKEN_QUERY_PARAM] end request.env['msf.api_token'].nil? ? auth_from_db(token) : auth_from_env(token) end end |
#valid? ⇒ Boolean
Check if request contains valid data and should be authenticated.
10 11 12 13 14 |
# File 'lib/msf/core/web_services/authentication/strategies/api_token.rb', line 10 def valid? auth_initialized = request.env['msf.auth_initialized'] = request.env[AUTHORIZATION] !auth_initialized || (.is_a?(String) && .start_with?(AUTHORIZATION_SCHEME)) || !params[TOKEN_QUERY_PARAM].nil? end |
#validate_user(user) ⇒ Hash
Validates the user associated with the API token.
52 53 54 |
# File 'lib/msf/core/web_services/authentication/strategies/api_token.rb', line 52 def validate_user(user) !user.nil? ? {valid: true, code: 0, message: nil} : {valid: false, code: 401, message: "Invalid API token."} end |