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.



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

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

Returns the value of attribute adapter.



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

def adapter
  @adapter
end

#client_idObject

Returns the value of attribute client_id.



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

def client_id
  @client_id
end

#client_secretObject

Returns the value of attribute client_secret.



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

def client_secret
  @client_secret
end

#domainObject

Returns the value of attribute domain.



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

def domain
  @domain
end

Instance Method Details

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



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

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)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/cognito_idp/client.rb', line 26

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)
  return unless response.success?

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

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

Yields:

  • (user_info)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/cognito_idp/client.rb', line 44

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

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

#logout_uri(**options) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/cognito_idp/client.rb', line 59

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