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
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.
13 14 15 |
# File 'lib/auth.rb', line 13 def initialize(auth_config) @auth_config = auth_config end |
Instance Method Details
#generate_token ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/auth.rb', line 30 def generate_token url = URI(AUTH0_TOKEN_ENDPOINT) http = Net::HTTP.new(url.host, url.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE request = Net::HTTP::Post.new(url) request["content-type"] = 'application/json' request.body = token_params.to_json response = http.request(request) token = JSON.parse(response.read_body)['access_token'] Cache.write(AUTH0_CACHE_KEY, token, expires_in: 1.day) token end |
#get_token ⇒ Object
26 27 28 |
# File 'lib/auth.rb', line 26 def get_token Cache.read(AUTH0_CACHE_KEY) || generate_token end |
#token_params ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/auth.rb', line 17 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 |