Module: Oauth2Token

Defined in:
lib/oauth2_token.rb,
lib/oauth2_token/version.rb

Constant Summary collapse

VERSION =
"0.1.4"

Class Method Summary collapse

Class Method Details

.get_token(client_id, client_secret, options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/oauth2_token.rb', line 9

def get_token(client_id, client_secret, options={})
  begin
    endpoint = get_endpoint(options['uri'], "create")
    jwt = create_jwt(client_id, client_secret, endpoint, options['realm'])
    body = {
      "grant_type"            => "client_credentials",
      "scope"                 => options['scope'],
      "realm"                 => options['realm'],
      "client_assertion_type" => "urn:ietf:params:oauth:client-assertion-type:jwt-bearer",
      "client_assertion"      => jwt
    }
    response = wrap(HTTParty.post(endpoint, http_options.merge(:body => body)))
    response['access_token']
  rescue Exception => e
    raise e, "unable to fetch token for #{client_id}"
  end
end

.is_token_active?(token, options = {}) ⇒ Boolean Also known as: is_token_valid?

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/oauth2_token.rb', line 27

def is_token_active?(token, options={})
  begin
    endpoint = get_endpoint(options['uri'], "validate")
    body = {
      "token" => token,
      "realm" => options['realm']
    }
    response = wrap(HTTParty.post(endpoint, http_options.merge(:body => body)))
    response['active']
  rescue Exception => e
    raise "Unable to validate token. #{e.backtrace}"
  end
end