Skip to content

Commit

Permalink
add YARD docs for all files in lib/ and app/
Browse files Browse the repository at this point in the history
  • Loading branch information
mftaff committed Dec 18, 2017
1 parent ec668b5 commit afa55c9
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 5 deletions.
5 changes: 5 additions & 0 deletions app/controllers/passwordless/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
# frozen_string_literal: true

# Namespace for classes and modules that handle the MVC for the passwordless Gem.
# This is the main module for this gem.
module Passwordless
# Base Controller Class. All controllers inherit from here.
class ApplicationController < ::ApplicationController
# Always returns true. Used to check if <Some>Controller inherits from ApplicationController.
# @return [boolean]
def passwordless_controller?
true
end
Expand Down
19 changes: 18 additions & 1 deletion app/controllers/passwordless/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,24 @@
require 'bcrypt'

module Passwordless
# Controller to manage RESTful methods for Sessions.
class SessionsController < ApplicationController
include ControllerHelpers

helper_method :authenticatable_resource

# Controller#Method for Route: get '/sign_in'.
# Assigns an email_field and new Session to be used by new view.
# renders sessions/new.html.erb.
def new
@email_field = email_field
@session = Session.new
end

# Controller#Method for Route: post '/sign_in'.
# Saves a new Session. Sends out a Mailer
# renders sessions/create.html.erb.
# @see Mailer#magic_link Mailer#magic_link
def create
authenticatable = find_authenticatable

Expand All @@ -29,6 +37,11 @@ def create
render
end

# Controller#Method for Route: get '/sign_in/:token'.
# Uses a token to sign user in.
# will try to redirect to either some reset path or the root_path.
# @see ControllerHelpers#sign_in
# @see ControllerHelpers#reset_passwordless_redirect_location!
def show
# Make it "slow" on purpose to make brute-force attacks more of a hassle
BCrypt::Password.create(params[:token])
Expand All @@ -45,7 +58,11 @@ def show
redirect_to main_app.root_path
end
end


# Controller#Method for Route: match '/sign_out', via: %i[get delete].
# Signs user out.
# redirects to root_path
# @see ControllerHelpers#sign_out
def destroy
sign_out authenticatable_class
redirect_to main_app.root_path
Expand Down
7 changes: 5 additions & 2 deletions app/mailers/passwordless/mailer.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# frozen_string_literal: true

module Passwordless
# The mailer responsible for sending Passwordless' mails
# The mailer responsible for sending Passwordless' mails.
class Mailer < ActionMailer::Base
default from: Passwordless.default_from_address


# Sends a magic link (secret token) email to allow sign in with
# link straight from email.
# @param session [Object] the rails session Object.
def magic_link(session)
@session = session

Expand Down
1 change: 1 addition & 0 deletions app/models/passwordless/application_record.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

module Passwordless
# Classic Rails class to abstractify and insulate ActiveRecord:Base.
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/passwordless/session.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

module Passwordless
# The session responsible holding the connection between the record trying to
# The session responsible for holding the connection between the record trying to
# log in and the unique tokens.
class Session < ApplicationRecord
belongs_to :authenticatable,
Expand Down
26 changes: 26 additions & 0 deletions lib/passwordless/controller_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
# frozen_string_literal: true

module Passwordless
# Some helpers for controllers that are connected to passswordless Models.
# These helpers give the ability to sign_in/out of a session, authenticate a
# session, and save/reset the passwordless redirect path.
module ControllerHelpers
# Authenticate a model using cookies. Will create a key from the model name
# and check if there is a value in cookies for that key.
# @param authenticatable_class [Object] any Model connected to passwordless.
# ( e.g - User or Admin ).
# @return [Object, nil] an instance of Model found by id stored in
# cookies.encrypted at key: key_from_model_name,
# or nil if no value corresponds to that key.
def authenticate_by_cookie(authenticatable_class)
key = cookie_name(authenticatable_class)
authenticatable_id = cookies.encrypted[key]
Expand All @@ -10,22 +20,38 @@ def authenticate_by_cookie(authenticatable_class)
authenticatable_class.find_by(id: authenticatable_id)
end

# Signs in user by assigning her id to a permanent cookie.
# @param authenticatable [Object] instance of Model to sign in
# ( e.g - @user when @user = User.find(id: some_id) ).
# @return [Object] the Object that is passed in.
def sign_in(authenticatable)
key = cookie_name(authenticatable.class)
cookies.encrypted.permanent[key] = { value: authenticatable.id }
authenticatable
end

# Signs out user by deleting her id from permanent cookies.
# @param (see #authenticate_by_cookie)
# @return [Integer, nil] the id that was deleted from cookies, or nil
# if no value found for provided key.
def sign_out(authenticatable_class)
key = cookie_name(authenticatable_class)
cookies.encrypted.permanent[key] = { value: nil }
cookies.delete(key)
end

# saves request.original_url as the redirect location for a passwordless Model.
# @param (see #authenticate_by_cookie)
# @return [String] the redirect url that was just saved.
def save_passwordless_redirect_location!(authenticatable_class)
session[session_key(authenticatable_class)] = request.original_url
end

# resets the redirect_location to root_path by deleting the redirect_url from
# session.
# @param (see #authenticate_by_cookie)
# @return [String, nil] the redirect url that was just deleted, or nil if no url
# found for given Model.
def reset_passwordless_redirect_location!(authenticatable_class)
session.delete session_key(authenticatable_class)
end
Expand Down
1 change: 1 addition & 0 deletions lib/passwordless/engine.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

module Passwordless
# Engine that runs the passwordless gem.
class Engine < ::Rails::Engine
isolate_namespace Passwordless

Expand Down
7 changes: 6 additions & 1 deletion lib/passwordless/model_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
# frozen_string_literal: true

module Passwordless
# Some helpers for models that can sign in passswordlessly
# Some helpers for models that can sign in passswordlessly. These helpers
# are used to hook up SomeModel with passwordless_sessions.
module ModelHelpers
# Adds passwordless sign_in for SomeModel (examples: User, Admin).
# Creates relationship - has_many :passwordless_sessions
# Allows - Call SomeModel.passwordless_email_field to return email used.
# @param field [string] email submitted by user.
def passwordless_with(field)
has_many :passwordless_sessions, class_name: 'Passwordless::Session'
define_singleton_method(:passwordless_email_field) { field }
Expand Down
5 changes: 5 additions & 0 deletions lib/passwordless/router_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
# frozen_string_literal: true

module Passwordless
# Some helpers for generating passwordless routes.
module RouterHelpers
# Generates passwordless routes for a Model that is connected to Passwordless.
# @param resource [Symbol] the pluralized symbol of your Model (e.g - :users).
# @param at [string] Optional - provide custom path (i.e controller) for routes (e.g "users").
# @param as [string] Optional - provide custom url for routes (e.g "special-users").
def passwordless_for(resource, at: nil, as: nil)
mount(
Passwordless::Engine,
Expand Down
4 changes: 4 additions & 0 deletions lib/passwordless/url_safe_base_64_generator.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# frozen_string_literal: true

module Passwordless
# Generates secure random numbers for sessions/cookies, etc.
class UrlSafeBase64Generator
# Generates a url safe base64 secure random number. :-)
# @param _session [Object] some Object that contains this class.
# @return [string] secure 32 byte base64 string.
def call(_session)
SecureRandom.urlsafe_base64(32)
end
Expand Down
1 change: 1 addition & 0 deletions lib/passwordless/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

module Passwordless
# The current version of Passwordless.
VERSION = '0.4.1'
end

0 comments on commit afa55c9

Please sign in to comment.