hub_identity_ruby 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7150d6fb299d13222c6c6e8405c81120a3169373101f28ec0a4b226a0a72762c
4
+ data.tar.gz: b798852d84e06d895d441271adb092268482ea973460718d5fbdceace308c139
5
+ SHA512:
6
+ metadata.gz: 0b364c5594fd0dbbfe97dcc988b8d1d3f3660960f762b80fcd4525c5108595832226c149610536f5f61642970ab09ef698b7c86e1d411de6a64c2d33207db890
7
+ data.tar.gz: 99b1791c83af3e9720806ebf3fd963d5d68dabe391f76bfd556f71577838501de79dfe0860a3fd90cb829ae9f98e15087dc915ec8356063ae643752cd5441939
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2021 erin boeger
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,64 @@
1
+ # HubIdentityRuby
2
+ A Rails Engine designed to make implementing HubIdentity authentication easy and fast.
3
+ In order to use this package you need to have an account with [HubIdentity](https://stage-identity.hubsynch.com/)
4
+
5
+ Currently this is only for [Hivelocity](https://www.hivelocity.co.jp/) uses. If you have a
6
+ commercial interest please contact the Package Manager Erin Boeger through linkedIn or Github or
7
+ through [Hivelocity](https://www.hivelocity.co.jp/contact/).
8
+
9
+ ## Installation
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'hub_identity_ruby'
14
+ ```
15
+
16
+ And then execute:
17
+ ```bash
18
+ $ bundle
19
+ ```
20
+
21
+ Or install it yourself as:
22
+ ```bash
23
+ $ gem install hub_identity_ruby
24
+ ```
25
+
26
+ inside your ApplicationController:
27
+ ```ruby
28
+ include HubIdentityRuby::ControllerHelpers
29
+ ```
30
+
31
+ inside your routes.rb mount the HubIdentity routes.
32
+ ```ruby
33
+ mount HubIdentityRuby::Engine => "/hub_identity_ruby"
34
+ ```
35
+ This will add the following routes to your application:
36
+ - sessions_new GET /sessions/new(.:format) hub_identity_ruby/sessions#new
37
+ - sessions_create GET /sessions/create(.:format) hub_identity_ruby/sessions#create
38
+ - sessions_destroy DELETE /sessions/destroy(.:format) hub_identity_ruby/sessions#destroy
39
+
40
+
41
+ ## Environmental Variables
42
+ set your public and private keys and HubIdentity url
43
+ ```bash
44
+ HUBIDENTITY_PRIVATE_KEY="a private key from HubIdentity website"
45
+ HUBIDENTITY_PUBLIC_KEY="a public key from HubIdentity website"
46
+ HUBIDENTITY_URL="for production deployment defaults to staging server"
47
+ ```
48
+
49
+ Currently the HUBIDENTITY_URL defaults to staging HubIdentity server.
50
+
51
+ ## Restricted routes
52
+
53
+ For authentication required (restricted) routes add the `before_action` helpers.
54
+ for example:
55
+
56
+ ```ruby
57
+ before_action :authenticate_user!, only: [:page_1, :page_2]
58
+ before_action :set_current_user
59
+ ```
60
+ use the `before_action :authenticate_user!` to restrict routes and require a user to authenticate.
61
+ use the `before_action :set_current_user` helper to have an `@current_user` in your views to help with navigation.
62
+
63
+ ## License
64
+ 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,18 @@
1
+ require "bundler/setup"
2
+
3
+ APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
4
+ load "rails/tasks/engine.rake"
5
+
6
+ load "rails/tasks/statistics.rake"
7
+
8
+ require "bundler/gem_tasks"
9
+
10
+ require "rake/testtask"
11
+
12
+ Rake::TestTask.new(:test) do |t|
13
+ t.libs << 'test'
14
+ t.pattern = 'test/**/*_test.rb'
15
+ t.verbose = false
16
+ end
17
+
18
+ task default: :test
@@ -0,0 +1 @@
1
+ //= link_directory ../stylesheets/hub_identity_ruby .css
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
+ * files in this directory. Styles in this file should be added after the last require_* statement.
11
+ * It is generally better to create a new file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,4 @@
1
+ module HubIdentityRuby
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,38 @@
1
+ require_dependency "hub_identity_ruby/application_controller"
2
+
3
+ module HubIdentityRuby
4
+ class SessionsController < ApplicationController
5
+
6
+ def new
7
+ redirect_to "#{HubIdentityRuby::Server.hostname}/browser/v1/providers?api_key=#{public_key}"
8
+ end
9
+
10
+ def create
11
+ current_user = get_current_user
12
+ if current_user.present?
13
+ session[:current_user] = current_user
14
+ flash[:notice] = "logged in sucessfully through HubIdentity"
15
+ redirect_to "/"
16
+ else
17
+ flash[:alert] = "authentication failure"
18
+ redirect_to "/"
19
+ end
20
+ end
21
+
22
+ def destroy
23
+ session.destroy
24
+ flash[:notice] = "logged out sucessfully"
25
+ redirect_to "/"
26
+ end
27
+
28
+ private
29
+
30
+ def get_current_user
31
+ HubIdentityRuby::Server.get_current_user(params["user_token"])
32
+ end
33
+
34
+ def public_key
35
+ ENV['HUBIDENTITY_PUBLIC_KEY']
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,4 @@
1
+ module HubIdentityRuby
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module HubIdentityRuby
2
+ class ApplicationJob < ActiveJob::Base
3
+ end
4
+ end
@@ -0,0 +1,6 @@
1
+ module HubIdentityRuby
2
+ class ApplicationMailer < ActionMailer::Base
3
+ default from: 'from@example.com'
4
+ layout 'mailer'
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ module HubIdentityRuby
2
+ class ApplicationRecord < ActiveRecord::Base
3
+ self.abstract_class = true
4
+ end
5
+ end
@@ -0,0 +1,41 @@
1
+ module HubIdentityRuby
2
+ class CurrentUser
3
+
4
+ def initialize(json_params)
5
+ user_params = parse(json_params)
6
+ @email = user_params["email"]
7
+ @owner_type = user_params["owner_type"]
8
+ @owner_uid = user_params["owner_uid"]
9
+ @user_type = user_params["user_type"]
10
+ @uid = user_params["uid"]
11
+ end
12
+
13
+ def hash
14
+ hash_values if valid?
15
+ end
16
+
17
+ private
18
+
19
+ def hash_values
20
+ {
21
+ "email" => @email,
22
+ "owner_type" => @owner_type,
23
+ "owner_uid" => @owner_uid,
24
+ "user_type" => @user_type,
25
+ "uid" => @uid
26
+ }
27
+ end
28
+
29
+ def parse(json_params)
30
+ begin
31
+ JSON.parse(json_params)
32
+ rescue
33
+ {}
34
+ end
35
+ end
36
+
37
+ def valid?
38
+ @email.present? && @uid.present? && @user_type.present?
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,44 @@
1
+ module HubIdentityRuby
2
+ class Server
3
+ HUBIDENTITY_BASE_URL = "https://stage-identity.hubsynch.com"
4
+
5
+ def self.certs
6
+ # response = Faraday.get "#{hostname}/api/v1/oauth/certs"
7
+ response = Excon.get("#{hostname}/api/v1/oauth/certs")
8
+ JSON.parse(response.body, symbolize_names: true)
9
+ end
10
+
11
+ def self.get_cert(key_id)
12
+ certs.find {|key| key[:kid] == key_id}
13
+ end
14
+
15
+ def self.get_current_user(user_token)
16
+ if user_token.present?
17
+ url = "#{hostname}/api/v1/current_user/#{user_token}"
18
+ response = Excon.get(url, :headers => {'x-api-key' => private_api_key})
19
+ # response = Faraday.get(url) do |req|
20
+ # req.headers['x-api-key'] = private_api_key
21
+ # end
22
+
23
+ if response.status == 200
24
+ CurrentUser.new(response.body).hash
25
+ else
26
+ nil
27
+ end
28
+ end
29
+ end
30
+
31
+ def self.hostname
32
+ ENV['HUBIDENTITY_URL'] || HUBIDENTITY_BASE_URL
33
+ end
34
+
35
+ class << self
36
+ private
37
+
38
+ def private_api_key
39
+ ENV['HUBIDENTITY_PRIVATE_KEY']
40
+ end
41
+ end
42
+ end
43
+ end
44
+
@@ -0,0 +1,83 @@
1
+ require 'jwt'
2
+
3
+ module HubIdentityRuby
4
+ class Token
5
+
6
+ def initialize(jwt_token)
7
+ token_array = jwt_token.split(".")
8
+ @headers = token_array[0]
9
+ @claims = token_array[1]
10
+ @signature = Base64.urlsafe_decode64(token_array[2])
11
+ end
12
+
13
+ def current_user
14
+ if valid? && type == "access"
15
+ user_params
16
+ else
17
+ nil
18
+ end
19
+ end
20
+
21
+ def decoded_claims
22
+ decode_and_parse(@claims)
23
+ end
24
+
25
+ def decoded_headers
26
+ decode_and_parse(@headers)
27
+ end
28
+
29
+ def expired?
30
+ Time.now > expiration_time
31
+ end
32
+
33
+ def expiration_time
34
+ Time.at(decoded_claims[:exp])
35
+ end
36
+
37
+ def issuer
38
+ decoded_claims[:iss]
39
+ end
40
+
41
+ def type
42
+ decoded_claims[:typ]
43
+ end
44
+
45
+ def valid?
46
+ valid_signature? && !expired? && issuer == "HubIdentity"
47
+ end
48
+
49
+ def valid_signature?
50
+ begin
51
+ rsa_public_key.verify(OpenSSL::Digest.new('sha256'), @signature, @headers + "." + @claims)
52
+ rescue
53
+ false
54
+ end
55
+ end
56
+
57
+ private
58
+
59
+ def decode_and_parse(string)
60
+ decoded = Base64.urlsafe_decode64(string)
61
+ JSON.parse(decoded, symbolize_names: true)
62
+ end
63
+
64
+ def rsa_public_key
65
+ key = Server.get_cert(decoded_headers[:kid])
66
+ JWT::JWK.import(key).public_key if key
67
+ end
68
+
69
+ def user_params
70
+ {
71
+ email: decoded_claims[:email],
72
+ owner_type: decoded_claims[:owner_type],
73
+ owner_uid: decoded_claims[:owner_uid],
74
+ uid: decoded_claims[:uid],
75
+ user_type: user_type
76
+ }
77
+ end
78
+
79
+ def user_type
80
+ decoded_claims[:sub].split(":").first
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,15 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Hub identity ruby</title>
5
+ <%= csrf_meta_tags %>
6
+ <%= csp_meta_tag %>
7
+
8
+ <%= stylesheet_link_tag "hub_identity_ruby/application", media: "all" %>
9
+ </head>
10
+ <body>
11
+
12
+ <%= yield %>
13
+
14
+ </body>
15
+ </html>
data/config/routes.rb ADDED
@@ -0,0 +1,5 @@
1
+ HubIdentityRuby::Engine.routes.draw do
2
+ get '/sessions/new', to: 'sessions#new'
3
+ get '/sessions/create', to: 'sessions#create'
4
+ delete '/sessions/destroy', to: 'sessions#destroy'
5
+ end
@@ -0,0 +1,7 @@
1
+ require "hub_identity_ruby/version"
2
+ require "hub_identity_ruby/engine"
3
+ require 'hub_identity_ruby/controller_helpers'
4
+ require 'excon'
5
+
6
+ module HubIdentityRuby
7
+ end
@@ -0,0 +1,16 @@
1
+ module HubIdentityRuby
2
+ module ControllerHelpers
3
+
4
+ def authenticate_user!
5
+ redirect_to hub_identity_ruby.sessions_new_path unless current_user?
6
+ end
7
+
8
+ def current_user?
9
+ session[:current_user]
10
+ end
11
+
12
+ def set_current_user
13
+ @current_user = session[:current_user]
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,5 @@
1
+ module HubIdentityRuby
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace HubIdentityRuby
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module HubIdentityRuby
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :hub_identity_ruby do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hub_identity_ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - erin boeger
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-03-10 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.1.3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 6.1.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: excon
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.79.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.79.0
41
+ description: HubIdentity is an authentication service which features various authentication
42
+ methods for an applications users.
43
+ email:
44
+ - erin@hivelocity.co.jp
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - MIT-LICENSE
50
+ - README.md
51
+ - Rakefile
52
+ - app/assets/config/hub_identity_ruby_manifest.js
53
+ - app/assets/stylesheets/hub_identity_ruby/application.css
54
+ - app/controllers/hub_identity_ruby/application_controller.rb
55
+ - app/controllers/hub_identity_ruby/sessions_controller.rb
56
+ - app/helpers/hub_identity_ruby/application_helper.rb
57
+ - app/jobs/hub_identity_ruby/application_job.rb
58
+ - app/mailers/hub_identity_ruby/application_mailer.rb
59
+ - app/models/hub_identity_ruby/application_record.rb
60
+ - app/models/hub_identity_ruby/current_user.rb
61
+ - app/models/hub_identity_ruby/server.rb
62
+ - app/models/hub_identity_ruby/token.rb
63
+ - app/views/layouts/hub_identity_ruby/application.html.erb
64
+ - config/routes.rb
65
+ - lib/hub_identity_ruby.rb
66
+ - lib/hub_identity_ruby/controller_helpers.rb
67
+ - lib/hub_identity_ruby/engine.rb
68
+ - lib/hub_identity_ruby/version.rb
69
+ - lib/tasks/hub_identity_ruby_tasks.rake
70
+ homepage: https://stage-identity.hubsynch.com/
71
+ licenses:
72
+ - MIT
73
+ metadata:
74
+ homepage_uri: https://stage-identity.hubsynch.com/
75
+ source_code_uri: https://github.com/ErinHivelociy/hub_identity_ruby
76
+ changelog_uri: https://github.com/ErinHivelociy/hub_identity_ruby
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubygems_version: 3.1.4
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: Easy Rails integration of HubIdentity authentication.
96
+ test_files: []