Class: Code0::Identities::Provider::Oidc

Inherits:
BaseOauth
  • Object
show all
Defined in:
lib/code0/identities/provider/oidc.rb,
sig/code0/identities/provider/oidc.rbs

Instance Attribute Summary

Attributes inherited from BaseOauth

#config_loader

Instance Method Summary collapse

Methods inherited from BaseOauth

#access_token, #check_response, #initialize, #load_identity

Constructor Details

This class inherits a constructor from Code0::Identities::Provider::BaseOauth

Instance Method Details

#authorization_urlString

Returns:

  • (String)


33
34
35
36
37
# File 'lib/code0/identities/provider/oidc.rb', line 33

def authorization_url
  config[:authorization_url]
    .gsub("{client_id}", config[:client_id])
    .gsub("{redirect_uri}", config[:redirect_uri])
end

#configObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/code0/identities/provider/oidc.rb', line 50

def config
  config = super

  # rubocop:disable Layout/LineLength
  config[:provider_name] ||= :oidc
  config[:attribute_statements] ||= {}
  config[:attribute_statements][:identifier] ||= %w[sub id identifier]
  config[:attribute_statements][:username] ||= %w[username name login]
  config[:attribute_statements][:email] ||= %w[email mail]
  config[:attribute_statements][:firstname] ||= %w[first_name firstname firstName givenname given_name givenName]
  config[:attribute_statements][:lastname] ||= %w[last_name lastname lastName family_name familyName familyname]
  # rubocop:enable Layout/LineLength

  config
end

#create_identity(response) ⇒ Identity

Parameters:

  • response: (Net::HTTPResponse)

Returns:



39
40
41
42
43
44
45
46
47
48
# File 'lib/code0/identities/provider/oidc.rb', line 39

def create_identity(response, *)
  body = response.parsed_response

  Identity.new(config[:provider_name],
               find_attribute(body, config[:attribute_statements][:identifier]),
               find_attribute(body, config[:attribute_statements][:username]),
               find_attribute(body, config[:attribute_statements][:email]),
               find_attribute(body, config[:attribute_statements][:firstname]),
               find_attribute(body, config[:attribute_statements][:lastname]))
end

#find_attribute(attributes, attribute_statements) ⇒ Object



66
67
68
69
70
71
# File 'lib/code0/identities/provider/oidc.rb', line 66

def find_attribute(attributes, attribute_statements)
  attribute_statements.each do |statement|
    return attributes[statement] unless attributes[statement].nil?
  end
  nil
end

#token_payload(code) ⇒ { code: String, grant_type: "authorization_code", redirect_uri: String, client_id: String, client_secret: String }

Parameters:

  • code: (String)

Returns:

  • ({ code: String, grant_type: "authorization_code", redirect_uri: String, client_id: String, client_secret: String })


21
22
23
24
25
26
27
# File 'lib/code0/identities/provider/oidc.rb', line 21

def token_payload(code)
  { code: code,
    grant_type: "authorization_code",
    redirect_uri: config[:redirect_uri],
    client_id: config[:client_id],
    client_secret: config[:client_secret] }
end

#token_urlString

Returns:

  • (String)


17
18
19
# File 'lib/code0/identities/provider/oidc.rb', line 17

def token_url
  config[:token_url]
end

#user_details_urlString

Returns:

  • (String)


29
30
31
# File 'lib/code0/identities/provider/oidc.rb', line 29

def user_details_url
  config[:user_details_url]
end

#validate_config!Object



7
8
9
10
11
12
13
14
15
# File 'lib/code0/identities/provider/oidc.rb', line 7

def validate_config!
  required_keys = %i[client_id client_secret redirect_uri token_url user_details_url authorization_url]

  missing_keys = required_keys - config.keys
  invalid_keys = config.keys - required_keys - %i[provider_name attribute_statements]

  raise MissingConfigurationError, "Missing: #{missing_keys.inspect}" if missing_keys.any?
  raise InvalidConfigurationError, "Invalid: #{invalid_keys.inspect}" if invalid_keys.any?
end