module Reivt::Auth

Convience module for handling our authentication actions and talking to

Auth0

@author [brwnrclse]

Constants

AUTH0_ID
AUTH_CALLBACK_URL
AUTH_CLIENT_ID
AUTH_STORE_ACCESS_TOKEN
AUTH_URL
VERIFIER

Public Class Methods

auth_code_url() click to toggle source

Provides the user with a means to obtain an authorization code for

accessing rev's api by opening a browser to our Auth0 login page

@return [nil]

# File lib/reivt/auth.rb, line 36
def self.auth_code_url
  verifier_challenge = Sysrandom.urlsafe_base64(
    Digest::SHA256.new.update(VERIFIER).digest.to_i
  )

  auth_code_url = AUTH_URL +
                  '/authorize?response_type=code&scope=openid%20profile' \
                  '&client_id=' + AUTH_CLIENT_ID +
                  '&redirect_uri=' + AUTH_CALLBACK_URL +
                  '&code_challenge=' + verifier_challenge +
                  '&code_challenge_method=S256'

  auth_code_url
end
auth_token(auth_code) click to toggle source

Exchanges the auth code obtained for a token used to access rev's api

@param auth_code [String] The auth code obtained from logging in

@return [String] The auth token used for accessing rev's api

# File lib/reivt/auth.rb, line 57
def self.auth_token(auth_code)
  auth_token_uri = URI.parse('https://vaemoi.auth0.com/oauth/token')
  body = {
    grant_type: 'authorization_code',
    client_id: AUTH_CLIENT_ID,
    code_verifier: VERIFIER,
    code: auth_code,
    redirect_uri: AUTH_CALLBACK_URL
  }
  http = Net::HTTP.new(auth_token_uri.host, auth_token_uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  req = Net::HTTP::Post.new(auth_token_uri)
  req.content_type = 'application/json'
  req.body = body.to_json

  res = http.request(req)
  token = {}
  token[:access_token] = JSON.parse(res.body)['access_token']
  token[:auth0_id] = JSON.parse(res.body)['id_token']
  token[:expires] = Time.now.to_i + JSON.parse(res.body)['expires'].to_i

  token
end
logged_in() click to toggle source

Checks if the user has an Authentication token for accessing the API

@return [Bool] true if token found

raises an exception otherwise
# File lib/reivt/auth.rb, line 86
def self.logged_in
  if AUTH_STORE_ACCESS_TOKEN.nil? || AUTH_STORE_ACCESS_TOKEN.empty?
    raise Reivt::LoginException
  end

  true
end