trestle-omniauth 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: cad62fb2c0fa01f9dce110649b434c19fecebcd108a9df32124ce483669d4290
4
+ data.tar.gz: de520c86922822405d5972983f7c0b38142e96b95a3b2aeac551c47a48c467e2
5
+ SHA512:
6
+ metadata.gz: 5deed4dd1d798e931343fdbc363e0a6d718429dfb69bf3bdcfdc7928e7ea9bd9e7fdd0cea7dbbdb74d0602d12ca27e041d79990d4befc50cf47c72021e8010e2
7
+ data.tar.gz: 6082bd8485a6921f7d86091a7712aaa1b2892dddb150e7d80b77169471070c6af0369ce766902b6a25312a256d6426ab366f3acbc0d57d03b943d2bdb8d18f3c
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2019 Harry Brundage
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # Trestle::Omniauth
2
+
3
+ Adds stateless authentication for [Trestle](https://trestle.io/) via [`omniauth`](https://github.com/omniauth/omniauth) providers. Similar to [`trestle-auth`](https://github.com/TrestleAdmin/trestle-auth), but doesn't require extra models, and works good if you're already using an OAuth provider for authentication elsehwere.
4
+
5
+ ## Installation
6
+ Add this line to your application's Gemfile:
7
+
8
+ ```ruby
9
+ gem 'trestle-omniauth'
10
+ ```
11
+
12
+ Add whichever OmniAuth strategies you will use for authentication to your Gemfile as well:
13
+
14
+ ```ruby
15
+ gem 'omniauth-google-oauth2' # for example
16
+ ```
17
+
18
+ And then run bundler to install your new gems:
19
+ ```bash
20
+ $ bundle
21
+ ```
22
+
23
+ __Note__: You don't need to mount this gem like you might with `Trestle`. It just plugs in to the existing way that Trestle is mounted in your app.
24
+
25
+ ## Usage
26
+
27
+ `trestle-omniauth` uses Omniauth providers unadulterated. To add providers, use the `omniauth.provider` method exposed on the trestle config the same way you'd use the `OmniAuth::Builder#provider` method.
28
+
29
+ In your `config/initializers/trestle.rb`, add providers like so:
30
+
31
+ ```ruby
32
+ # config/initializers/trestle.rb
33
+ Trestle.configure do |config|
34
+ # ...
35
+ # ...
36
+ config.omniauth.provider :developer unless Rails.env.production?
37
+ config.omniauth.provider :google_oauth2, ENV['GOOGLE_CLIENT_ID'], ENV['GOOGLE_CLIENT_SECRET']
38
+ end
39
+ ```
40
+
41
+ ## License
42
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,32 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Trestle::Omniauth'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+ load 'rails/tasks/statistics.rake'
21
+
22
+ require 'bundler/gem_tasks'
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'test'
28
+ t.pattern = 'test/**/*_test.rb'
29
+ t.verbose = false
30
+ end
31
+
32
+ task default: :test
@@ -0,0 +1 @@
1
+ //= link_directory ../stylesheets/trestle/omniauth .css
@@ -0,0 +1,4 @@
1
+ // This file is left as an extension point for user customization.
2
+ //
3
+ // It will be overridden by the similarly named file within
4
+ // the app/assets folder of the Rails application.
@@ -0,0 +1,66 @@
1
+ // User-defined variables
2
+ @import "trestle/support";
3
+ @import "omniauth/defaults";
4
+
5
+ // 3rd party dependencies
6
+ @import "trestle/bootstrap";
7
+ @import "trestle/font-awesome";
8
+
9
+ html, body {
10
+ height: 100%;
11
+ }
12
+
13
+ body {
14
+ -webkit-font-smoothing: antialiased;
15
+
16
+ margin: 0;
17
+ overflow: hidden;
18
+
19
+ color: white;
20
+ background: $auth-bg;
21
+
22
+ display: flex;
23
+ }
24
+
25
+ .container {
26
+ margin: auto;
27
+ width: 320px;
28
+ }
29
+
30
+ a {
31
+ color: rgba(white, 0.75);
32
+ &:hover { color: white; }
33
+ }
34
+
35
+ .btn {
36
+ box-shadow: rgba(black, 0.1) 0 0 2px;
37
+ padding: 12px;
38
+ font-size: 16px;
39
+ margin-bottom: 12px;
40
+ }
41
+
42
+ .btn-primary {
43
+ outline: none;
44
+
45
+ color: $auth-login-btn-color;
46
+ background: $auth-login-btn-bg;
47
+ border: $auth-login-btn-border;
48
+
49
+ &:hover {
50
+ background-color: darken($auth-login-btn-bg, 2.5%);
51
+ border-color: darken($auth-login-btn-border, 5%);
52
+ }
53
+
54
+ &:focus, &:active {
55
+ background-color: darken($auth-login-btn-bg, 5%);
56
+ border-color: darken($auth-login-btn-border, 10%);
57
+ }
58
+
59
+ &:active:focus {
60
+ outline: none !important;
61
+ background-color: darken($auth-login-btn-bg, 10%);
62
+ border-color: darken($auth-login-btn-border, 15%);
63
+ }
64
+ }
65
+
66
+ @import "trestle/custom-omniauth";
@@ -0,0 +1,4 @@
1
+ $auth-bg: $theme-bg !default;
2
+ $auth-login-btn-color: $btn-primary-color !default;
3
+ $auth-login-btn-border: $btn-primary-border !default;
4
+ $auth-login-btn-bg: $btn-primary-bg !default;
@@ -0,0 +1,49 @@
1
+ @import "trestle/support";
2
+
3
+ .userbox {
4
+ margin-top: 2px;
5
+ margin-bottom: 2px;
6
+
7
+ > a {
8
+ color: $text-color;
9
+
10
+ display: block;
11
+ padding: 2px 0;
12
+
13
+ font-size: 14px;
14
+ font-weight: normal;
15
+ line-height: 40px;
16
+
17
+ &:hover, &:focus {
18
+ text-decoration: none;
19
+ }
20
+ }
21
+
22
+ .avatar {
23
+ vertical-align: top;
24
+ margin-left: 6px;
25
+ }
26
+
27
+ .name {
28
+ color: $text-color;
29
+ }
30
+ }
31
+
32
+ @include mobile {
33
+ .userbox {
34
+ position: relative;
35
+ z-index: 5;
36
+
37
+ > a, .name {
38
+ color: white;
39
+ }
40
+
41
+ .name {
42
+ display: none;
43
+ }
44
+
45
+ .avatar {
46
+ border: 1px solid rgba(white, 0.25);
47
+ }
48
+ }
49
+ }
@@ -0,0 +1,7 @@
1
+ module Trestle
2
+ module Omniauth
3
+ class ApplicationController < ActionController::Base
4
+ protect_from_forgery with: :exception
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,19 @@
1
+ class Trestle::Omniauth::SessionsController < Trestle::ApplicationController
2
+ layout "trestle/omniauth"
3
+
4
+ skip_before_action :require_authenticated_user
5
+
6
+ def new
7
+ @providers = Trestle.config.omniauth.providers.keys
8
+ end
9
+
10
+ def create
11
+ login!(request.env["omniauth.auth"])
12
+ redirect_to previous_location || instance_exec(&Trestle.config.omniauth.redirect_on_login)
13
+ end
14
+
15
+ def destroy
16
+ logout!
17
+ redirect_to instance_exec(&Trestle.config.omniauth.redirect_on_logout)
18
+ end
19
+ end
@@ -0,0 +1,6 @@
1
+ module Trestle
2
+ module Omniauth
3
+ module ApplicationHelper
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,20 @@
1
+ module Trestle
2
+ module Omniauth
3
+ module TitleHelper
4
+ def trestle_omniauth_title
5
+ if Trestle.config.omniauth.logo
6
+ image_tag(Trestle.config.omniauth.logo)
7
+ elsif Trestle.config.site_logo
8
+ image_tag(Trestle.config.site_logo)
9
+ elsif Trestle.config.site_logo_small
10
+ safe_join([
11
+ image_tag(Trestle.config.site_logo_small, alt: "", class: ""),
12
+ content_tag(:span, Trestle.config.site_title),
13
+ ], "\n")
14
+ else
15
+ Trestle.config.site_title
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,9 @@
1
+ module Trestle::Omniauth::UserHelper
2
+ def format_user_name(user)
3
+ instance_exec(user, &Trestle.config.omniauth.format_user_name)
4
+ end
5
+
6
+ def avatar_for(user)
7
+ instance_exec(user, &Trestle.config.omniauth.avatar) if Trestle.config.omniauth.avatar
8
+ end
9
+ end
@@ -0,0 +1,6 @@
1
+ module Trestle
2
+ module Omniauth
3
+ class ApplicationJob < ActiveJob::Base
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,8 @@
1
+ module Trestle
2
+ module Omniauth
3
+ class ApplicationMailer < ActionMailer::Base
4
+ default from: 'from@example.com'
5
+ layout 'mailer'
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,7 @@
1
+ module Trestle
2
+ module Omniauth
3
+ class ApplicationRecord < ActiveRecord::Base
4
+ self.abstract_class = true
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta charset='utf-8'>
5
+ <meta content='width=device-width, initial-scale=1' name='viewport'>
6
+ <meta content='IE=edge,chrome=1' http-equiv='X-UA-Compatible'>
7
+
8
+ <meta content='no-cache' name='turbolinks-cache-control'>
9
+
10
+ <%= csrf_meta_tags %>
11
+
12
+ <title><%= Trestle.config.site_title %></title>
13
+
14
+ <%= stylesheet_link_tag "trestle/omniauth", 'data-turbolinks-track': 'reload' %>
15
+ </head>
16
+
17
+ <body>
18
+ <main class="container">
19
+ <header class="auth-header">
20
+ <h1><%= trestle_omniauth_title %></h1>
21
+ </header>
22
+
23
+ <%= yield %>
24
+ </main>
25
+ </body>
26
+ </html>
@@ -0,0 +1,14 @@
1
+ <div class="userbox pull-right dropdown">
2
+ <%= link_to "#", class: 'dropdown-toggle', data: { toggle: 'dropdown' } do %>
3
+ <span class="name">
4
+ <%= format_user_name(current_user) %>
5
+ <span class="caret"></span>
6
+ </span>
7
+
8
+ <%= avatar_for(current_user) %>
9
+ <% end %>
10
+
11
+ <ul class="dropdown-menu dropdown-menu-right">
12
+ <li><%= link_to t("admin.omniauth.logout", default: "Log out"), trestle.logout_path, class: "dropdown-item" %></li>
13
+ </ul>
14
+ </div>
@@ -0,0 +1,11 @@
1
+ <%= hook("omniauth.login.heading") %>
2
+
3
+ <% if flash[:error] -%>
4
+ <div class="alert alert-danger">
5
+ <p><%= flash[:error] %></p>
6
+ </div>
7
+ <% end -%>
8
+
9
+ <% @providers.each do |name| %>
10
+ <a class="btn btn-primary" href="/auth/<%= name %>">Login with <%= name.to_s.titleize %></a>
11
+ <% end %>
@@ -0,0 +1,9 @@
1
+ Trestle.configure do |config|
2
+ config.hook("stylesheets") do
3
+ stylesheet_link_tag("trestle/omniauth/userbox")
4
+ end
5
+
6
+ config.hook("view.header") do
7
+ render "trestle/omniauth/userbox"
8
+ end
9
+ end
data/config/routes.rb ADDED
@@ -0,0 +1,9 @@
1
+ # Trestle::Omniauth::Engine doesn't actually get mounted, and instead the routes are appended to the normal Trestle engine's routes.
2
+ Trestle::Engine.routes.draw do
3
+ controller "trestle/omniauth/sessions" do
4
+ get "login" => :new, as: :login
5
+ get "logout" => :destroy, as: :logout
6
+ post "/auth/:provider/callback" => :create
7
+ get "/auth/:provider/callback" => :create
8
+ end
9
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :trestle_omniauth do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,15 @@
1
+ require "trestle"
2
+
3
+ module Trestle
4
+ module Omniauth
5
+ extend ActiveSupport::Autoload
6
+
7
+ autoload :Configuration
8
+ autoload :LateOmniauthBuilder
9
+ autoload :ControllerMethods
10
+ end
11
+
12
+ Configuration.option :omniauth, Omniauth::Configuration.new
13
+ end
14
+
15
+ require "trestle/omniauth/engine"
@@ -0,0 +1,35 @@
1
+ module Trestle
2
+ module Omniauth
3
+ class Configuration
4
+ extend ActiveSupport::Autoload
5
+ include Configurable
6
+
7
+ option :avatar, ->(user) {
8
+ avatar { gravatar(user["info"]["email"]) }
9
+ }, evaluate: false
10
+
11
+ option :format_user_name, ->(user) {
12
+ if user["info"]["first_name"] && user["info"]["last_name"]
13
+ safe_join([user["info"]["first_name"], content_tag(:strong, user["info"]["last_name"])], " ")
14
+ elsif user["info"]["name"]
15
+ content_tag(:strong, user["info"]["name"])
16
+ elsif user["info"]["email"]
17
+ content_tag(:strong, user["info"]["email"])
18
+ else
19
+ "UID #{user[:uid]}"
20
+ end
21
+ }, evaluate: false
22
+
23
+ option :redirect_on_login, -> { Trestle.config.path }, evaluate: false
24
+ option :redirect_on_logout, -> { login_url }, evaluate: false
25
+
26
+ option :logo
27
+
28
+ option :providers, {}
29
+
30
+ def provider(name, *args)
31
+ providers[name] = args
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,51 @@
1
+ module Trestle
2
+ module Omniauth
3
+ module ControllerMethods
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ helper_method :current_user, :logged_in?
8
+
9
+ before_action :require_authenticated_user
10
+ end
11
+
12
+ protected
13
+
14
+ def current_user
15
+ @current_user ||= session[:trestle_user]
16
+ end
17
+
18
+ def login!(auth_hash)
19
+ session[:trestle_user] = request.env["omniauth.auth"].slice("provider", "uid", "info").as_json
20
+ @current_user = auth_hash
21
+ end
22
+
23
+ def logout!
24
+ session.delete(:trestle_user)
25
+ @current_user = nil
26
+ end
27
+
28
+ def logged_in?
29
+ !!current_user
30
+ end
31
+
32
+ def store_location
33
+ session[:trestle_return_to] = request.fullpath
34
+ end
35
+
36
+ def previous_location
37
+ session.delete(:trestle_return_to)
38
+ end
39
+
40
+ def require_authenticated_user
41
+ logged_in? || login_required!
42
+ end
43
+
44
+ def login_required!
45
+ store_location
46
+ redirect_to trestle.login_url
47
+ false
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,27 @@
1
+ require "omniauth"
2
+
3
+ module Trestle
4
+ module Omniauth
5
+ class Engine < ::Rails::Engine
6
+ isolate_namespace Trestle::Omniauth
7
+
8
+ config.assets.precompile << "trestle/omniauth.css" << "trestle/omniauth/userbox.css"
9
+
10
+ config.before_initialize do
11
+ Trestle::Engine.paths["app/helpers"].concat(paths["app/helpers"].existent)
12
+ end
13
+
14
+ config.to_prepare do
15
+ Trestle::ApplicationController.send(:include, Trestle::Omniauth::ControllerMethods)
16
+ end
17
+
18
+ initializer "trestle.omniauth.configure" do
19
+ Trestle::Engine.middleware.use ::OmniAuth::Builder do
20
+ Trestle.config.omniauth.providers.each do |name, args|
21
+ self.provider name, *args
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,5 @@
1
+ module Trestle
2
+ module Omniauth
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: trestle-omniauth
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Harry Brundage
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-04-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 6.0.0.beta3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 6.0.0.beta3
27
+ - !ruby/object:Gem::Dependency
28
+ name: trestle
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.8'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.8'
41
+ - !ruby/object:Gem::Dependency
42
+ name: omniauth
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 1.9.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 1.9.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: sqlite3
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Use omniauth auth providers to authenticate with Trestle
70
+ email:
71
+ - harry.brundage@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - MIT-LICENSE
77
+ - README.md
78
+ - Rakefile
79
+ - app/assets/config/trestle_omniauth_manifest.js
80
+ - app/assets/stylesheets/trestle/_custom-omniauth.scss
81
+ - app/assets/stylesheets/trestle/omniauth.scss
82
+ - app/assets/stylesheets/trestle/omniauth/_defaults.scss
83
+ - app/assets/stylesheets/trestle/omniauth/userbox.scss
84
+ - app/controllers/trestle/omniauth/application_controller.rb
85
+ - app/controllers/trestle/omniauth/sessions_controller.rb
86
+ - app/helpers/trestle/omniauth/application_helper.rb
87
+ - app/helpers/trestle/omniauth/title_helper.rb
88
+ - app/helpers/trestle/omniauth/user_helper.rb
89
+ - app/jobs/trestle/omniauth/application_job.rb
90
+ - app/mailers/trestle/omniauth/application_mailer.rb
91
+ - app/models/trestle/omniauth/application_record.rb
92
+ - app/views/layouts/trestle/omniauth.html.erb
93
+ - app/views/trestle/omniauth/_userbox.html.erb
94
+ - app/views/trestle/omniauth/sessions/new.html.erb
95
+ - config/initializers/trestle.rb
96
+ - config/routes.rb
97
+ - lib/tasks/trestle/omniauth_tasks.rake
98
+ - lib/trestle/omniauth.rb
99
+ - lib/trestle/omniauth/configuration.rb
100
+ - lib/trestle/omniauth/controller_methods.rb
101
+ - lib/trestle/omniauth/engine.rb
102
+ - lib/trestle/omniauth/version.rb
103
+ homepage: https://github.com/airhorns/trestle-omniauth
104
+ licenses:
105
+ - MIT
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubygems_version: 3.0.1
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: Use omniauth auth providers to authenticate with Trestle
126
+ test_files: []