hanami-authentication 0.2.1 → 0.3.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 +4 -4
 - data/lib/hanami/authentication.rb +2 -2
 - data/lib/hanami/authentication/token.rb +64 -0
 - data/lib/hanami/authentication/version.rb +1 -1
 - metadata +3 -2
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            SHA1:
         
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: d2a31214eb2008661d6f5e2c152cfc7e8615597d
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: c8a3bffaf2da59cb1c09db861f7b868cc2e44d39
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: e3ae35f3e5640d99b3ffc54c081a693f8a8087159395560213848a6270738da7af2d19da110eb536f8c90e4637c760078fc2eee2e8c3ccb9ee3655c2f2bb9c7d
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: 12a9a048158e62ca9ccb29322846b7b6f0bf24d91d43bf6317ae55803760d6ab182153722527067924b62f2b417e81cf106d9c6d201cdb2dc80900136e5e307a
         
     | 
| 
         @@ -67,9 +67,9 @@ module Hanami 
     | 
|
| 
       67 
67 
     | 
    
         
             
                      include Utils::ClassAttribute
         
     | 
| 
       68 
68 
     | 
    
         | 
| 
       69 
69 
     | 
    
         
             
                      class_attribute :after_session_expired_callbacks
         
     | 
| 
       70 
     | 
    
         
            -
                      self.after_session_expired_callbacks = Utils::Callbacks::Chain.new
         
     | 
| 
       71 
     | 
    
         
            -
             
     | 
| 
       72 
70 
     | 
    
         
             
                      class_attribute :after_authentication_failed_callbacks
         
     | 
| 
      
 71 
     | 
    
         
            +
             
     | 
| 
      
 72 
     | 
    
         
            +
                      self.after_session_expired_callbacks = Utils::Callbacks::Chain.new
         
     | 
| 
       73 
73 
     | 
    
         
             
                      self.after_authentication_failed_callbacks = Utils::Callbacks::Chain.new
         
     | 
| 
       74 
74 
     | 
    
         
             
                    end
         
     | 
| 
       75 
75 
     | 
    
         
             
                  end
         
     | 
| 
         @@ -0,0 +1,64 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require 'hanami/authentication/version'
         
     | 
| 
      
 2 
     | 
    
         
            +
            require 'bcrypt'
         
     | 
| 
      
 3 
     | 
    
         
            +
            require 'securerandom'
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
            module Hanami
         
     | 
| 
      
 6 
     | 
    
         
            +
              module Authentication
         
     | 
| 
      
 7 
     | 
    
         
            +
                module Token
         
     | 
| 
      
 8 
     | 
    
         
            +
                  private
         
     | 
| 
      
 9 
     | 
    
         
            +
             
     | 
| 
      
 10 
     | 
    
         
            +
                  def create_token
         
     | 
| 
      
 11 
     | 
    
         
            +
                    SecureRandom.uuid
         
     | 
| 
      
 12 
     | 
    
         
            +
                  end
         
     | 
| 
      
 13 
     | 
    
         
            +
             
     | 
| 
      
 14 
     | 
    
         
            +
                  def current_user
         
     | 
| 
      
 15 
     | 
    
         
            +
                    @current_user
         
     | 
| 
      
 16 
     | 
    
         
            +
                  end
         
     | 
| 
      
 17 
     | 
    
         
            +
             
     | 
| 
      
 18 
     | 
    
         
            +
                  def authenticate
         
     | 
| 
      
 19 
     | 
    
         
            +
                    bearer_token = token_from_header
         
     | 
| 
      
 20 
     | 
    
         
            +
                    halt 401 unless bearer_token
         
     | 
| 
      
 21 
     | 
    
         
            +
                    token = self.class.find_token_block.call(bearer_token)
         
     | 
| 
      
 22 
     | 
    
         
            +
                    halt 401 unless token
         
     | 
| 
      
 23 
     | 
    
         
            +
                    @current_user = self.class.find_user_block.call(token)
         
     | 
| 
      
 24 
     | 
    
         
            +
                    halt 401 unless @current_user
         
     | 
| 
      
 25 
     | 
    
         
            +
                  end
         
     | 
| 
      
 26 
     | 
    
         
            +
             
     | 
| 
      
 27 
     | 
    
         
            +
                  def authenticated?
         
     | 
| 
      
 28 
     | 
    
         
            +
                    !!@current_user
         
     | 
| 
      
 29 
     | 
    
         
            +
                  end
         
     | 
| 
      
 30 
     | 
    
         
            +
             
     | 
| 
      
 31 
     | 
    
         
            +
                  def token_from_header
         
     | 
| 
      
 32 
     | 
    
         
            +
                    header = request.get_header('Authorization')
         
     | 
| 
      
 33 
     | 
    
         
            +
                    return unless header
         
     | 
| 
      
 34 
     | 
    
         
            +
                    matched = header.match(/Bearer (.+)$/)
         
     | 
| 
      
 35 
     | 
    
         
            +
                    matched && matched[1]
         
     | 
| 
      
 36 
     | 
    
         
            +
                  end
         
     | 
| 
      
 37 
     | 
    
         
            +
             
     | 
| 
      
 38 
     | 
    
         
            +
                  def self.included(base)
         
     | 
| 
      
 39 
     | 
    
         
            +
                    base.class_eval do
         
     | 
| 
      
 40 
     | 
    
         
            +
                      _expose :current_user
         
     | 
| 
      
 41 
     | 
    
         
            +
                      extend  ClassMethods
         
     | 
| 
      
 42 
     | 
    
         
            +
                    end
         
     | 
| 
      
 43 
     | 
    
         
            +
                  end
         
     | 
| 
      
 44 
     | 
    
         
            +
             
     | 
| 
      
 45 
     | 
    
         
            +
                  module ClassMethods
         
     | 
| 
      
 46 
     | 
    
         
            +
                    def self.extended(base)
         
     | 
| 
      
 47 
     | 
    
         
            +
                      base.class_eval do
         
     | 
| 
      
 48 
     | 
    
         
            +
                        include Utils::ClassAttribute
         
     | 
| 
      
 49 
     | 
    
         
            +
                        class_attribute :find_user_block
         
     | 
| 
      
 50 
     | 
    
         
            +
                        class_attribute :find_token_block
         
     | 
| 
      
 51 
     | 
    
         
            +
                      end
         
     | 
| 
      
 52 
     | 
    
         
            +
                    end
         
     | 
| 
      
 53 
     | 
    
         
            +
             
     | 
| 
      
 54 
     | 
    
         
            +
                    def user_for_authenticate(&blk)
         
     | 
| 
      
 55 
     | 
    
         
            +
                      self.find_user_block = blk
         
     | 
| 
      
 56 
     | 
    
         
            +
                    end
         
     | 
| 
      
 57 
     | 
    
         
            +
             
     | 
| 
      
 58 
     | 
    
         
            +
                    def token_for_authenticate(&blk)
         
     | 
| 
      
 59 
     | 
    
         
            +
                      self.find_token_block = blk
         
     | 
| 
      
 60 
     | 
    
         
            +
                    end
         
     | 
| 
      
 61 
     | 
    
         
            +
                  end
         
     | 
| 
      
 62 
     | 
    
         
            +
                end
         
     | 
| 
      
 63 
     | 
    
         
            +
              end
         
     | 
| 
      
 64 
     | 
    
         
            +
            end
         
     | 
    
        metadata
    CHANGED
    
    | 
         @@ -1,14 +1,14 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            --- !ruby/object:Gem::Specification
         
     | 
| 
       2 
2 
     | 
    
         
             
            name: hanami-authentication
         
     | 
| 
       3 
3 
     | 
    
         
             
            version: !ruby/object:Gem::Version
         
     | 
| 
       4 
     | 
    
         
            -
              version: 0. 
     | 
| 
      
 4 
     | 
    
         
            +
              version: 0.3.0
         
     | 
| 
       5 
5 
     | 
    
         
             
            platform: ruby
         
     | 
| 
       6 
6 
     | 
    
         
             
            authors:
         
     | 
| 
       7 
7 
     | 
    
         
             
            - LegalForce Inc.
         
     | 
| 
       8 
8 
     | 
    
         
             
            autorequire: 
         
     | 
| 
       9 
9 
     | 
    
         
             
            bindir: exe
         
     | 
| 
       10 
10 
     | 
    
         
             
            cert_chain: []
         
     | 
| 
       11 
     | 
    
         
            -
            date: 2017- 
     | 
| 
      
 11 
     | 
    
         
            +
            date: 2017-11-16 00:00:00.000000000 Z
         
     | 
| 
       12 
12 
     | 
    
         
             
            dependencies:
         
     | 
| 
       13 
13 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       14 
14 
     | 
    
         
             
              name: bcrypt
         
     | 
| 
         @@ -97,6 +97,7 @@ files: 
     | 
|
| 
       97 
97 
     | 
    
         
             
            - Rakefile
         
     | 
| 
       98 
98 
     | 
    
         
             
            - hanami-authentication.gemspec
         
     | 
| 
       99 
99 
     | 
    
         
             
            - lib/hanami/authentication.rb
         
     | 
| 
      
 100 
     | 
    
         
            +
            - lib/hanami/authentication/token.rb
         
     | 
| 
       100 
101 
     | 
    
         
             
            - lib/hanami/authentication/version.rb
         
     | 
| 
       101 
102 
     | 
    
         
             
            homepage: https://github.com/legalforce/hanami-authentication
         
     | 
| 
       102 
103 
     | 
    
         
             
            licenses:
         
     |