Class: Masks::Configuration

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Attributes, ActiveModel::Model, ActiveModel::Serializers::JSON, ActiveModel::Validations
Defined in:
lib/masks/configuration.rb

Overview

Container for masks configuration data.

Instance Method Summary collapse

Instance Method Details

#access(name) ⇒ Hash

Returns configuration data for the given access name.

Parameters:

  • name (String)

Returns:

  • (Hash)

Raises:



186
187
188
189
190
191
# File 'lib/masks/configuration.rb', line 186

def access(name)
  defaults = Masks::Access.defaults(name)
  raise Masks::Error::UnknownAccess, name unless defaults

  defaults.deep_merge(access_types&.fetch(name, {}))
end

#access_typesHash

Returns configuration data for all access types.

This does not include defaults created with Masks::Access.access.

Returns:

  • (Hash)


198
199
200
201
202
203
204
# File 'lib/masks/configuration.rb', line 198

def access_types
  data
    .fetch(:access, {})
    .to_h do |name, opts|
      [name, opts.merge(name:, cls: opts[:cls]&.constantize)]
    end
end

#datObject

Returns all configuration data as RecursiveOpenStruct.

Returns:

  • RecursiveOpenStruct



31
32
33
# File 'lib/masks/configuration.rb', line 31

def dat
  RecursiveOpenStruct.new(data)
end

#issuerString

Returns a string to use as the “issuer” for various secrets—TOTP, JWT, etc.

Returns:

  • (String)


37
38
39
# File 'lib/masks/configuration.rb', line 37

def issuer
  data.fetch(:name, "masks").parameterize
end

#lifetimesHash

A hash of expiration times for various resources.

These values are used to override expiration times for things like password reset emails and other time-based authentication methods.

Returns:

  • (Hash)


116
117
118
119
120
# File 'lib/masks/configuration.rb', line 116

def lifetimes
  (super || dat.lifetimes&.to_h || {}).transform_values do |seconds|
    seconds.to_i.seconds
  end
end

#mask(type) ⇒ Hash

Returns configuration data for a given mask type.

Parameters:

  • type (String)

Returns:

  • (Hash)

Raises:



159
160
161
162
163
# File 'lib/masks/configuration.rb', line 159

def mask(type)
  config = data.dig(:types, type.to_sym)
  raise Masks::Error::InvalidConfiguration, type unless config
  config
end

#masksArray<Masks::Mask>

Returns all configured masks.

Returns:



168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/masks/configuration.rb', line 168

def masks
  @masks ||=
    begin
      masks = super || data.fetch(:masks, [])
      masks.map do |opts|
        case opts
        when Masks::Mask
          opts
        when Hash
          Masks::Mask.new(opts.merge(config: self))
        end
      end
    end
end

#model(name) ⇒ Class

Returns a model Class.

Returns:

  • (Class)


151
152
153
# File 'lib/masks/configuration.rb', line 151

def model(name)
  models[name]&.constantize
end

#modelsHash{Symbol => String}

A hash of default models the app relies on.

This makes it easy to provide a substitute for key models while still relying on the base active record implementation.

Returns:

  • (Hash{Symbol => String})


128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/masks/configuration.rb', line 128

def models
  {
    actor: "Masks::Rails::Actor",
    scope: "Masks::Rails::Scope",
    role: "Masks::Rails::Role",
    email: "Masks::Rails::Email",
    recovery: "Masks::Rails::Recovery",
    device: "Masks::Rails::Device",
    key: "Masks::Rails::Key",
    openid_client: "Masks::Rails::OpenID::Client",
    openid_access_token: "Masks::Rails::OpenID::AccessToken",
    openid_id_token: "Masks::Rails::OpenID::IdToken",
    openid_authorization: "Masks::Rails::OpenID::Authorization",
    session_json: "Masks::SessionResource",
    request: "Masks::Sessions::Request",
    inline: "Masks::Sessions::Inline",
    access: "Masks::Sessions::Access"
  }.merge(super || data.fetch(:models, {}))
end

#openidString

Returns a string to use as the “issuer” for various secrets—TOTP, JWT, etc.

Returns:

  • (String)


73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/masks/configuration.rb', line 73

def openid
  {
    scopes: %w[openid profile email address phone],
    subject_types: %w[nickname email pairwise],
    response_types: %w[code token id_token],
    grant_types: %w[
      client_credentials
      authorization_code
      implicit
      refresh_token
    ],
    pairwise_salt: "masks"
  }.merge(super || data.fetch(:openid, {}))
end

#signups=(value) ⇒ Object

Setter for whether or not sigups are allowed.

Parameters:

  • value (Boolean)


209
210
211
# File 'lib/masks/configuration.rb', line 209

def signups=(value)
  data[:signups] = value
end

#signups?Boolean

Whether or not sigups are allowed.

Returns:

  • (Boolean)


216
217
218
# File 'lib/masks/configuration.rb', line 216

def signups?
  data[:signups]
end

A hash of links—urls to various places on the frontend.

These default to generated rails routes, but can be overridden where necessary.

Returns:

  • (Hash)


94
95
96
97
98
99
100
101
102
# File 'lib/masks/configuration.rb', line 94

def site_links
  {
    root: rails_url.try(:root_path) || "/",
    recover: rails_url.recover_path,
    login: rails_url.session_path,
    after_login: rails_url.session_path,
    after_logout: rails_url.session_path
  }.merge(super || data.fetch(:links, {}))
end

#site_logoString

Returns the site logo, used for visual identification of the site.

Returns:

  • (String)


49
50
51
# File 'lib/masks/configuration.rb', line 49

def 
  super || data.fetch(:logo, nil)
end

#site_nameString

Returns the site name, used for referring to the site (e.g. in the logo).

Returns:

  • (String)


55
56
57
# File 'lib/masks/configuration.rb', line 55

def site_name
  super || data.fetch(:name, "masks")
end

#site_titleString

Returns the site title, used primarily in meta tags.

Returns:

  • (String)


61
62
63
# File 'lib/masks/configuration.rb', line 61

def site_title
  super || data.fetch(:title, "")
end

#site_urlString

Returns the canonical url for the site.

Returns:

  • (String)


67
68
69
# File 'lib/masks/configuration.rb', line 67

def site_url
  super || data.fetch(:url, nil)
end

#themeString

Returns a site theme to use on the frontend.

Returns:

  • (String)


43
44
45
# File 'lib/masks/configuration.rb', line 43

def theme
  super || data.fetch(:theme, "dark")
end

#versionString

Returns the current global session key.

Returns:

  • (String)


106
107
108
# File 'lib/masks/configuration.rb', line 106

def version
  super || "v1"
end