Class: LcpRuby::Authentication::Provider

Inherits:
Struct
  • Object
show all
Defined in:
lib/lcp_ruby/authentication/provider.rb

Overview

Value object representing one entry under ‘auth.providers` in auth.yml. Built by ProviderRegistry; consumed by OmniAuthBuilder, UserResolver, RoleMapper, and the login view.

Constant Summary collapse

VALID_TYPES =
%i[oidc devise].freeze
VALID_ROLE_SOURCES =
%i[yaml host db].freeze
DEFAULTS =
{
  scopes: %w[openid profile email].freeze,
  pkce: true,
  response_type: "code",
  response_mode: "query",
  role_source: :yaml,
  provisioning: { auto_create: true, update_on_login: [], reject_if_host_missing: false }.freeze,
  logout: { mode: :local }.freeze
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#audienceObject

Returns the value of attribute audience

Returns:

  • (Object)

    the current value of audience



8
9
10
# File 'lib/lcp_ruby/authentication/provider.rb', line 8

def audience
  @audience
end

#button_styleObject

Returns the value of attribute button_style

Returns:

  • (Object)

    the current value of button_style



8
9
10
# File 'lib/lcp_ruby/authentication/provider.rb', line 8

def button_style
  @button_style
end

#claim_mappingsObject

Returns the value of attribute claim_mappings

Returns:

  • (Object)

    the current value of claim_mappings



8
9
10
# File 'lib/lcp_ruby/authentication/provider.rb', line 8

def claim_mappings
  @claim_mappings
end

#client_idObject

Returns the value of attribute client_id

Returns:

  • (Object)

    the current value of client_id



8
9
10
# File 'lib/lcp_ruby/authentication/provider.rb', line 8

def client_id
  @client_id
end

#client_secretObject

Returns the value of attribute client_secret

Returns:

  • (Object)

    the current value of client_secret



8
9
10
# File 'lib/lcp_ruby/authentication/provider.rb', line 8

def client_secret
  @client_secret
end

#default_roleObject

Returns the value of attribute default_role

Returns:

  • (Object)

    the current value of default_role



8
9
10
# File 'lib/lcp_ruby/authentication/provider.rb', line 8

def default_role
  @default_role
end

#discovery_urlObject

Returns the value of attribute discovery_url

Returns:

  • (Object)

    the current value of discovery_url



8
9
10
# File 'lib/lcp_ruby/authentication/provider.rb', line 8

def discovery_url
  @discovery_url
end

#display_nameObject

Returns the value of attribute display_name

Returns:

  • (Object)

    the current value of display_name



8
9
10
# File 'lib/lcp_ruby/authentication/provider.rb', line 8

def display_name
  @display_name
end

#iconObject

Returns the value of attribute icon

Returns:

  • (Object)

    the current value of icon



8
9
10
# File 'lib/lcp_ruby/authentication/provider.rb', line 8

def icon
  @icon
end

#logoutObject

Returns the value of attribute logout

Returns:

  • (Object)

    the current value of logout



8
9
10
# File 'lib/lcp_ruby/authentication/provider.rb', line 8

def logout
  @logout
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



8
9
10
# File 'lib/lcp_ruby/authentication/provider.rb', line 8

def name
  @name
end

#pkceObject

Returns the value of attribute pkce

Returns:

  • (Object)

    the current value of pkce



8
9
10
# File 'lib/lcp_ruby/authentication/provider.rb', line 8

def pkce
  @pkce
end

#provisioningObject

Returns the value of attribute provisioning

Returns:

  • (Object)

    the current value of provisioning



8
9
10
# File 'lib/lcp_ruby/authentication/provider.rb', line 8

def provisioning
  @provisioning
end

#redirect_uriObject

Returns the value of attribute redirect_uri

Returns:

  • (Object)

    the current value of redirect_uri



8
9
10
# File 'lib/lcp_ruby/authentication/provider.rb', line 8

def redirect_uri
  @redirect_uri
end

#response_modeObject

Returns the value of attribute response_mode

Returns:

  • (Object)

    the current value of response_mode



8
9
10
# File 'lib/lcp_ruby/authentication/provider.rb', line 8

def response_mode
  @response_mode
end

#response_typeObject

Returns the value of attribute response_type

Returns:

  • (Object)

    the current value of response_type



8
9
10
# File 'lib/lcp_ruby/authentication/provider.rb', line 8

def response_type
  @response_type
end

#role_mappingObject

Returns the value of attribute role_mapping

Returns:

  • (Object)

    the current value of role_mapping



8
9
10
# File 'lib/lcp_ruby/authentication/provider.rb', line 8

def role_mapping
  @role_mapping
end

#role_sourceObject

Returns the value of attribute role_source

Returns:

  • (Object)

    the current value of role_source



8
9
10
# File 'lib/lcp_ruby/authentication/provider.rb', line 8

def role_source
  @role_source
end

#scopesObject

Returns the value of attribute scopes

Returns:

  • (Object)

    the current value of scopes



8
9
10
# File 'lib/lcp_ruby/authentication/provider.rb', line 8

def scopes
  @scopes
end

#typeObject

Returns the value of attribute type

Returns:

  • (Object)

    the current value of type



8
9
10
# File 'lib/lcp_ruby/authentication/provider.rb', line 8

def type
  @type
end

Class Method Details

.from_hash(raw) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/lcp_ruby/authentication/provider.rb', line 49

def self.from_hash(raw)
  h = raw.transform_keys(&:to_sym)
  instance = new(
    name:                h.fetch(:name).to_s.freeze,
    type:                whitelist(h.fetch(:type).to_sym, VALID_TYPES, "type"),
    display_name:        (h[:display_name] || h.fetch(:name).to_s.titleize).freeze,
    icon:                h[:icon]&.freeze,
    button_style:        (h[:button_style] || "secondary").freeze,
    discovery_url:       h[:discovery_url]&.freeze,
    client_id:           h[:client_id]&.freeze,
    client_secret:       h[:client_secret]&.freeze,
    scopes:              Array(h[:scopes].presence || DEFAULTS[:scopes]).map { |s| s.to_s.freeze }.freeze,
    pkce:                h.fetch(:pkce, DEFAULTS[:pkce]),
    response_type:       (h[:response_type] || DEFAULTS[:response_type]).to_s.freeze,
    response_mode:       (h[:response_mode] || DEFAULTS[:response_mode]).to_s.freeze,
    audience:            h[:audience]&.freeze,
    redirect_uri:        h[:redirect_uri]&.freeze,
    claim_mappings:      symbolize_claim_mappings(h[:claim_mappings] || {}).freeze,
    role_source:         whitelist((h[:role_source] || DEFAULTS[:role_source]).to_sym, VALID_ROLE_SOURCES, "role_source"),
    role_mapping:        (h[:role_mapping] || {}).freeze,
    default_role:        h[:default_role]&.freeze,
    provisioning:        DEFAULTS[:provisioning].merge((h[:provisioning] || {}).transform_keys(&:to_sym)).freeze,
    logout:              symbolize_logout(h[:logout]).freeze
  )
  instance.freeze
end

.symbolize_claim_mappings(mappings) ⇒ Object



84
85
86
# File 'lib/lcp_ruby/authentication/provider.rb', line 84

def self.symbolize_claim_mappings(mappings)
  mappings.transform_keys(&:to_sym).transform_values(&:to_s)
end

.symbolize_logout(raw) ⇒ Object



88
89
90
91
# File 'lib/lcp_ruby/authentication/provider.rb', line 88

def self.symbolize_logout(raw)
  merged = DEFAULTS[:logout].merge((raw || {}).transform_keys(&:to_sym))
  merged.merge(mode: merged[:mode].to_sym)
end

.whitelist(value, allowed, field) ⇒ Object

Raises:

  • (ArgumentError)


93
94
95
96
97
# File 'lib/lcp_ruby/authentication/provider.rb', line 93

def self.whitelist(value, allowed, field)
  return value if allowed.include?(value)

  raise ArgumentError, "#{field} must be one of #{allowed.inspect} (got #{value.inspect})"
end

Instance Method Details

#devise?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/lcp_ruby/authentication/provider.rb', line 103

def devise?
  type == :devise
end

#oidc?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/lcp_ruby/authentication/provider.rb', line 99

def oidc?
  type == :oidc
end

#with(**overrides) ⇒ Object

Returns a new frozen Provider with the given attribute overrides. Used by tests so they don’t mutate the registry-held singleton.



78
79
80
81
82
# File 'lib/lcp_ruby/authentication/provider.rb', line 78

def with(**overrides)
  attrs = members.each_with_object({}) { |m, h| h[m] = self[m] }
  attrs.merge!(overrides)
  self.class.new(**attrs).freeze
end