module Panoptes::Client::Authentication

Attributes

payload[R]

Public Instance Methods

authenticated?() click to toggle source
# File lib/panoptes/client/authentication.rb, line 34
def authenticated?
  !!token_contents['id']
end
authenticated_admin?() click to toggle source
# File lib/panoptes/client/authentication.rb, line 53
def authenticated_admin?
  ensure_authenticated
  token_contents.fetch('admin', false)
end
authenticated_user_display_name() click to toggle source
# File lib/panoptes/client/authentication.rb, line 43
def authenticated_user_display_name
  ensure_authenticated
  token_contents.fetch('dname', nil)
end
authenticated_user_id() click to toggle source
# File lib/panoptes/client/authentication.rb, line 48
def authenticated_user_id
  ensure_authenticated
  token_contents.fetch('id')
end
authenticated_user_login() click to toggle source
# File lib/panoptes/client/authentication.rb, line 38
def authenticated_user_login
  ensure_authenticated
  token_contents.fetch('login', nil)
end
jwt_payload() click to toggle source
# File lib/panoptes/client/authentication.rb, line 12
def jwt_payload
  raise NotLoggedIn unless @auth[:token]
  @payload = decode_token(@auth[:token])
rescue JWT::ExpiredSignature
  raise AuthenticationExpired
end
token_contents() click to toggle source
# File lib/panoptes/client/authentication.rb, line 19
def token_contents
  if payload_exists? && !payload_expired?
    # use the cached version of the payload while not expired
    payload['data']
  else
    # decode the payload from the JWT token
    jwt_payload['data']
  end
end
token_expiry() click to toggle source
# File lib/panoptes/client/authentication.rb, line 29
def token_expiry
  # always decode and fetch the expiry time from the JWT token
  Time.at(jwt_payload.fetch('exp',0)).utc
end

Private Instance Methods

decode_token(token) click to toggle source
# File lib/panoptes/client/authentication.rb, line 80
def decode_token(token)
  payload, = JWT.decode token, jwt_signing_public_key, algorithm: 'RS512'
  payload
end
ensure_authenticated() click to toggle source
# File lib/panoptes/client/authentication.rb, line 60
def ensure_authenticated
  raise NotLoggedIn unless authenticated?
end
jwt_signing_public_key() click to toggle source
# File lib/panoptes/client/authentication.rb, line 76
def jwt_signing_public_key
  @jwt_signing_public_key ||= OpenSSL::PKey::RSA.new(File.read(@public_key_path))
end
key_file_path(file_name) click to toggle source
# File lib/panoptes/client/authentication.rb, line 94
def key_file_path(file_name)
  File.expand_path(
    File.join('..', '..', '..', '..', 'data', file_name),
    __FILE__
  )
end
payload_exists?() click to toggle source
# File lib/panoptes/client/authentication.rb, line 64
def payload_exists?
  !!@payload
end
payload_expired?() click to toggle source
# File lib/panoptes/client/authentication.rb, line 72
def payload_expired?
  payload_expiry_time < Time.now.utc
end
payload_expiry_time() click to toggle source
# File lib/panoptes/client/authentication.rb, line 68
def payload_expiry_time
  @payload_expiry_time ||= Time.at(payload.fetch('exp',0)).utc
end
public_key_for_env(env) click to toggle source
# File lib/panoptes/client/authentication.rb, line 85
def public_key_for_env(env)
  case env.to_s
  when 'staging'
    key_file_path('doorkeeper-jwt-staging.pub')
  when 'production'
    key_file_path('doorkeeper-jwt-production.pub')
  end
end