class DecodeProjectAuthenticationTokenCommand
Authentication implementation mostly copied and slightly adapted from paweljw.github.io/2017/07/rails-5.1-api-app-part-4-authentication-and-authorization/ Big thanks!
Attributes
headers[R]
Public Class Methods
new(headers)
click to toggle source
# File natural-backend/app/commands/decode_project_authentication_token_command.rb, line 10 def initialize(headers) @headers = headers @project = nil end
Private Instance Methods
authentication_header()
click to toggle source
# File natural-backend/app/commands/decode_project_authentication_token_command.rb, line 35 def authentication_header headers['Authentication'] end
decoded_id()
click to toggle source
# File natural-backend/app/commands/decode_project_authentication_token_command.rb, line 48 def decoded_id token_contents['project_id'] end
project()
click to toggle source
# File natural-backend/app/commands/decode_project_authentication_token_command.rb, line 20 def project @project ||= Project.find_by(id: decoded_id) @project || errors.add(:token, "Token invalid") && nil end
run()
click to toggle source
# File natural-backend/app/commands/decode_project_authentication_token_command.rb, line 15 def run return unless token_present? @result = project if project end
token()
click to toggle source
# File natural-backend/app/commands/decode_project_authentication_token_command.rb, line 29 def token return authentication_header.split(' ').last if authentication_header.present? errors.add(:token, "Token missing") nil end
token_contents()
click to toggle source
# File natural-backend/app/commands/decode_project_authentication_token_command.rb, line 39 def token_contents @token_contents ||= begin decoded = JwtService.decode(token) Rails.logger.info(decoded) errors.add(:token, "Token expired") unless decoded decoded end end
token_present?()
click to toggle source
# File natural-backend/app/commands/decode_project_authentication_token_command.rb, line 25 def token_present? token.present? && token_contents.present? end