Module: MicroAuth::AuthenticationHelper

Defined in:
app/helpers/micro_auth/authentication_helper.rb

Instance Method Summary collapse

Instance Method Details

#authenticated_user_object(token) ⇒ Hash{Symbol => Object}

Provides a hash of user data based on what data the provided token is scoped to access. For instance, :email will only be included when a pii scoped token is presented.

Parameters:

  • token (MicroAuth::Token)

    A Token instance to specify the scoped access level.

Returns:

  • (Hash{Symbol => Object})

    A user data hash.



30
31
32
33
34
35
# File 'app/helpers/micro_auth/authentication_helper.rb', line 30

def authenticated_user_object(token)
  fields = [:id, :created_at, :is_global_moderator, :is_global_admin, :username, :website, :twitter, :staff,
            :developer, :discord]
  fields << :email if token.scope.include? 'pii'
  fields.to_h { |f| [f, token.user.send(f)] }
end

#construct_redirect(redirect_uri, **params) ⇒ String

Builds a redirect URI, adding specified parameters as necessary.

Parameters:

  • redirect_uri (String)

    The base redirect URI to start from

  • params (Hash{Symbol => #to_s})

    A hash of parameters to add as query parameters to the URI

Returns:

  • (String)

    The final URI.



18
19
20
21
22
23
# File 'app/helpers/micro_auth/authentication_helper.rb', line 18

def construct_redirect(redirect_uri, **params)
  uri = URI(redirect_uri)
  query = URI.decode_www_form(uri.query || '').to_h.merge(params)
  uri.query = URI.encode_www_form(query.to_a)
  uri.to_s
end

#valid_auth_scopesHash{String => String}

Contains all valid scopes for authentication requests. A ‘nil` description indicates a scope that is not displayed to the user during authentication requests.

Returns:

  • (Hash{String => String})

    A hash of valid authentication scopes.



6
7
8
9
10
11
# File 'app/helpers/micro_auth/authentication_helper.rb', line 6

def valid_auth_scopes
  {
    'perpetual' => nil,
    'pii' => 'Access to personal information (including email address)'
  }
end