Class: Zaikio::OAuthClient::ClientConfiguration

Inherits:
Object
  • Object
show all
Defined in:
lib/zaikio/oauth_client/client_configuration.rb

Defined Under Namespace

Classes: OrganizationConnection

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_name) ⇒ ClientConfiguration

Returns a new instance of ClientConfiguration.



7
8
9
10
# File 'lib/zaikio/oauth_client/client_configuration.rb', line 7

def initialize(client_name)
  @default_scopes = []
  @client_name = client_name
end

Instance Attribute Details

#client_idObject

Returns the value of attribute client_id.



5
6
7
# File 'lib/zaikio/oauth_client/client_configuration.rb', line 5

def client_id
  @client_id
end

#client_nameObject (readonly)

Returns the value of attribute client_name.



4
5
6
# File 'lib/zaikio/oauth_client/client_configuration.rb', line 4

def client_name
  @client_name
end

#client_secretObject

Returns the value of attribute client_secret.



5
6
7
# File 'lib/zaikio/oauth_client/client_configuration.rb', line 5

def client_secret
  @client_secret
end

#default_scopesObject

Returns the value of attribute default_scopes.



5
6
7
# File 'lib/zaikio/oauth_client/client_configuration.rb', line 5

def default_scopes
  @default_scopes
end

#org_configObject (readonly)

Returns the value of attribute org_config.



4
5
6
# File 'lib/zaikio/oauth_client/client_configuration.rb', line 4

def org_config
  @org_config
end

Instance Method Details

#default_scopes_for(type = "Person") ⇒ Object



36
37
38
# File 'lib/zaikio/oauth_client/client_configuration.rb', line 36

def default_scopes_for(type = "Person")
  type == "Organization" ? org_config.default_scopes : default_scopes
end

#oauth_clientObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/zaikio/oauth_client/client_configuration.rb', line 17

def oauth_client
  @oauth_client ||= OAuth2::Client.new(
    client_id,
    client_secret,
    authorize_url: "oauth/authorize",
    token_url: "oauth/access_token",
    connection_opts: { headers: { Accept: "application/json" } },
    site: Zaikio::OAuthClient.configuration.host
  )

  @oauth_client.options[:auth_scheme] = Zaikio::OAuthClient.oauth_scheme

  @oauth_client
end

#register_organization_connection {|@org_config| ... } ⇒ Object

Yields:



12
13
14
15
# File 'lib/zaikio/oauth_client/client_configuration.rb', line 12

def register_organization_connection
  @org_config ||= OrganizationConnection.new
  yield(@org_config)
end

#scopes_for_auth(_id = nil) ⇒ Object



32
33
34
# File 'lib/zaikio/oauth_client/client_configuration.rb', line 32

def scopes_for_auth(_id = nil)
  default_scopes
end

#token_by_client_credentials(bearer_id: nil, bearer_type: "Person", scopes: []) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/zaikio/oauth_client/client_configuration.rb', line 40

def token_by_client_credentials(bearer_id: nil, bearer_type: "Person", scopes: [])
  plain_scopes = Zaikio::OAuthClient.get_plain_scopes(scopes)
  scopes_with_prefix = plain_scopes.map do |scope|
    "#{bearer_type[0..2]}/#{bearer_id}.#{scope}"
  end

  Zaikio::OAuthClient.with_oauth_scheme(:basic_auth) do
    oauth_client.client_credentials.get_token(scope: scopes_with_prefix.join(","))
  end
end