Class: HTTPX::Plugins::OAuth::OAuthSession

Inherits:
Object
  • Object
show all
Defined in:
lib/httpx/plugins/oauth.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(issuer:, client_id:, client_secret:, access_token: nil, refresh_token: nil, scope: nil, token_endpoint: nil, response_type: nil, grant_type: nil, token_endpoint_auth_method: nil) ⇒ OAuthSession

Returns a new instance of OAuthSession.

Raises:



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/httpx/plugins/oauth.rb', line 21

def initialize(
  issuer:,
  client_id:,
  client_secret:,
  access_token: nil,
  refresh_token: nil,
  scope: nil,
  token_endpoint: nil,
  response_type: nil,
  grant_type: nil,
  token_endpoint_auth_method: nil
)
  @issuer = URI(issuer)
  @client_id = client_id
  @client_secret = client_secret
  @token_endpoint = URI(token_endpoint) if token_endpoint
  @response_type = response_type
  @scope = case scope
           when String
             scope.split
           when Array
             scope
  end
  @access_token = access_token
  @refresh_token = refresh_token
  @token_endpoint_auth_method = String(token_endpoint_auth_method) if token_endpoint_auth_method
  @grant_type = grant_type || (@refresh_token ? "refresh_token" : "client_credentials")

  unless @token_endpoint_auth_method.nil? || SUPPORTED_AUTH_METHODS.include?(@token_endpoint_auth_method)
    raise Error, "#{@token_endpoint_auth_method} is not a supported auth method"
  end

  return if SUPPORTED_GRANT_TYPES.include?(@grant_type)

  raise Error, "#{@grant_type} is not a supported grant type"
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



19
20
21
# File 'lib/httpx/plugins/oauth.rb', line 19

def access_token
  @access_token
end

#client_idObject (readonly)

Returns the value of attribute client_id.



19
20
21
# File 'lib/httpx/plugins/oauth.rb', line 19

def client_id
  @client_id
end

#client_secretObject (readonly)

Returns the value of attribute client_secret.



19
20
21
# File 'lib/httpx/plugins/oauth.rb', line 19

def client_secret
  @client_secret
end

#grant_typeObject (readonly)

Returns the value of attribute grant_type.



19
20
21
# File 'lib/httpx/plugins/oauth.rb', line 19

def grant_type
  @grant_type
end

#refresh_tokenObject (readonly)

Returns the value of attribute refresh_token.



19
20
21
# File 'lib/httpx/plugins/oauth.rb', line 19

def refresh_token
  @refresh_token
end

#scopeObject (readonly)

Returns the value of attribute scope.



19
20
21
# File 'lib/httpx/plugins/oauth.rb', line 19

def scope
  @scope
end

Instance Method Details

#load(http) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/httpx/plugins/oauth.rb', line 66

def load(http)
  return if @grant_type && @scope

   = http.get("#{@issuer}/.well-known/oauth-authorization-server").raise_for_status.json

  @token_endpoint = ["token_endpoint"]
  @scope = ["scopes_supported"]
  @grant_type = Array(["grant_types_supported"]).find { |gr| SUPPORTED_GRANT_TYPES.include?(gr) }
  @token_endpoint_auth_method = Array(["token_endpoint_auth_methods_supported"]).find do |am|
    SUPPORTED_AUTH_METHODS.include?(am)
  end
  nil
end

#merge(other) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/httpx/plugins/oauth.rb', line 80

def merge(other)
  obj = dup

  case other
  when OAuthSession
    other.instance_variables.each do |ivar|
      val = other.instance_variable_get(ivar)
      next unless val

      obj.instance_variable_set(ivar, val)
    end
  when Hash
    other.each do |k, v|
      obj.instance_variable_set(:"@#{k}", v) if obj.instance_variable_defined?(:"@#{k}")
    end
  end
  obj
end

#token_endpointObject



58
59
60
# File 'lib/httpx/plugins/oauth.rb', line 58

def token_endpoint
  @token_endpoint || "#{@issuer}/token"
end

#token_endpoint_auth_methodObject



62
63
64
# File 'lib/httpx/plugins/oauth.rb', line 62

def token_endpoint_auth_method
  @token_endpoint_auth_method || "client_secret_basic"
end