Class: Auth
- Inherits:
-
Object
- Object
- Auth
- Defined in:
- lib/auth.rb
Constant Summary collapse
- AUTH0_TOKEN_ENDPOINT =
"https://dev-pattern.auth0.com/oauth/token".freeze
- AUTH0_DEFAULT_AUDIENCE =
"https://dev-pattern.auth0.com/api/v2/".freeze
- AUTH0_DEFAULT_GRANT_TYPE =
"client_credentials".freeze
- AUTH0_CACHE_KEY =
'AUTH0_CACHE_KEY'.freeze
- HTTP =
Net::HTTP::Persistent.new(name: 'scraper_central_auth').tap do |h| h.verify_mode = OpenSSL::SSL::VERIFY_NONE end
Instance Method Summary collapse
- #generate_token ⇒ Object
- #get_token ⇒ Object
-
#initialize(auth_config) ⇒ Auth
constructor
A new instance of Auth.
- #token_params ⇒ Object
Constructor Details
#initialize(auth_config) ⇒ Auth
Returns a new instance of Auth.
17 18 19 |
# File 'lib/auth.rb', line 17 def initialize(auth_config) @auth_config = auth_config end |
Instance Method Details
#generate_token ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/auth.rb', line 34 def generate_token url = URI(AUTH0_TOKEN_ENDPOINT) request = Net::HTTP::Post.new(url) request["content-type"] = 'application/json' request.body = token_params.to_json response = HTTP.request(url, request) token = JSON.parse(response.read_body)['access_token'] Cache.write(AUTH0_CACHE_KEY, token, expires_in: 1.day) token end |
#get_token ⇒ Object
30 31 32 |
# File 'lib/auth.rb', line 30 def get_token Cache.read(AUTH0_CACHE_KEY) || generate_token end |
#token_params ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/auth.rb', line 21 def token_params { grant_type: AUTH0_DEFAULT_GRANT_TYPE, client_id: @auth_config[:client_id], client_secret: @auth_config[:client_secret], audience: AUTH0_DEFAULT_AUDIENCE } end |