Class: CognitoIdp::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/cognito_idp/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id:, domain:, client_secret: nil, adapter: Faraday.default_adapter, stubs: nil) ⇒ Client

Returns a new instance of Client.



10
11
12
13
14
15
16
# File 'lib/cognito_idp/client.rb', line 10

def initialize(client_id:, domain:, client_secret: nil, adapter: Faraday.default_adapter, stubs: nil)
  @adapter = adapter
  @client_id = client_id
  @client_secret = client_secret
  @domain = domain
  @stubs = stubs
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



8
9
10
# File 'lib/cognito_idp/client.rb', line 8

def adapter
  @adapter
end

#client_idObject (readonly)

Returns the value of attribute client_id.



8
9
10
# File 'lib/cognito_idp/client.rb', line 8

def client_id
  @client_id
end

#client_secretObject (readonly)

Returns the value of attribute client_secret.



8
9
10
# File 'lib/cognito_idp/client.rb', line 8

def client_secret
  @client_secret
end

#domainObject (readonly)

Returns the value of attribute domain.



8
9
10
# File 'lib/cognito_idp/client.rb', line 8

def domain
  @domain
end

Instance Method Details

#authorization_uri(redirect_uri:, **options) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/cognito_idp/client.rb', line 26

def authorization_uri(redirect_uri:, **options)
  AuthorizationUri.new(
    client_id: client_id,
    domain: domain,
    redirect_uri: redirect_uri,
    **options
  ).to_s
end

#get_token(grant_type:, **options) {|token| ... } ⇒ Object

Yields:

  • (token)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/cognito_idp/client.rb', line 35

def get_token(grant_type:, **options)
  params = {
    client_id: client_id,
    code: options[:code],
    code_verifier: options[:code_verifier],
    grant_type: grant_type,
    redirect_uri: options[:redirect_uri],
    refresh_token: options[:refresh_token],
    scope: options[:scope]
  }.compact
  response = connection.post("/oauth2/token", params, basic_authorization_headers)
  handle_error_response(response)

  token = Token.new(response.body)
  yield(token) if block_given?
  token
end

#get_user_info(token) {|user_info| ... } ⇒ Object

Yields:

  • (user_info)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/cognito_idp/client.rb', line 53

def (token)
  access_token = case token
  when Token
    token.access_token
  else
    token
  end
  response = connection.post("/oauth2/userInfo", nil, {"Authorization" => "Bearer #{access_token}"})
  handle_error_response(response)

   = UserInfo.new(response.body)
  yield() if block_given?
  
end

#inspectObject



18
19
20
21
22
23
24
# File 'lib/cognito_idp/client.rb', line 18

def inspect
  "#<#{self.class}:0x#{object_id.to_s(16)} " \
    "@adapter=#{adapter.inspect}, " \
    "@client_id=#{client_id.inspect}, " \
    "@client_secret=#{client_secret.nil? ? "nil" : "[REDACTED]"}, " \
    "@domain=#{domain.inspect}>"
end

#logout_uri(**options) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/cognito_idp/client.rb', line 81

def logout_uri(**options)
  LogoutUri.new(
    client_id: client_id,
    domain: domain,
    **options
  ).to_s
end

#revoke_token(token) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/cognito_idp/client.rb', line 68

def revoke_token(token)
  refresh_token = case token
  when Token
    token.refresh_token
  else
    token
  end

  params = {client_id: client_id, token: refresh_token}
  response = connection.post("/oauth2/revoke", params, basic_authorization_headers)
  handle_error_response(response)
end