card-mod-api_key 0.11.4
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 +7 -0
- data/README.md +0 -0
- data/config/initializers/api_key.rb +1 -0
- data/lib/card/auth/api_key.rb +35 -0
- data/set/right/account.rb +9 -0
- data/set/right/api_key.rb +85 -0
- data/set/right/api_key/core.haml +11 -0
- metadata +85 -0
    
        checksums.yaml
    ADDED
    
    | @@ -0,0 +1,7 @@ | |
| 1 | 
            +
            ---
         | 
| 2 | 
            +
            SHA256:
         | 
| 3 | 
            +
              metadata.gz: 1fcb9c135c5326571c3b819a9ec738cb8eee20df645d43b5d168c3ba65d18b32
         | 
| 4 | 
            +
              data.tar.gz: f1ecbaa490b2f226b82ffe8c515b863320cc11e9232c6d6bce6c43c71e1ea05a
         | 
| 5 | 
            +
            SHA512:
         | 
| 6 | 
            +
              metadata.gz: 0c3b0c73e799123380e7fd8af851d87a60e0e248e1bacf4acbead02404914faffdace23eaedd518f13720e98fea7df055f61d8e81a90bec1990ad411c3cf8dd7
         | 
| 7 | 
            +
              data.tar.gz: 60d90897d2282a579262490346489f41421b13527a6c2b183114a766e1e6f039219f1322c6c5662965565dc3df5a2dbe9cf455c8f88df06f5bb827a78b83442b
         | 
    
        data/README.md
    ADDED
    
    | 
            File without changes
         | 
| @@ -0,0 +1 @@ | |
| 1 | 
            +
            Card::Auth.extend Card::Auth::ApiKey
         | 
| @@ -0,0 +1,35 @@ | |
| 1 | 
            +
            require "jwt"
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            class Card
         | 
| 4 | 
            +
              module Auth
         | 
| 5 | 
            +
                # methods for setting current account
         | 
| 6 | 
            +
                module ApiKey
         | 
| 7 | 
            +
                  def signin_with opts={}
         | 
| 8 | 
            +
                    if opts[:token]
         | 
| 9 | 
            +
                      signin_with_token opts[:token]
         | 
| 10 | 
            +
                    elsif opts[:api_key]
         | 
| 11 | 
            +
                      signin_with_api_key opts[:api_key]
         | 
| 12 | 
            +
                    else
         | 
| 13 | 
            +
                      signin_with_session
         | 
| 14 | 
            +
                    end
         | 
| 15 | 
            +
                  end
         | 
| 16 | 
            +
             | 
| 17 | 
            +
                  # set the current user based on api_key
         | 
| 18 | 
            +
                  def signin_with_api_key api_key
         | 
| 19 | 
            +
                    account = find_account_by_api_key api_key
         | 
| 20 | 
            +
                    unless account&.authenticate_api_key api_key
         | 
| 21 | 
            +
                      raise Card::Error::PermissionDenied, "API key authentication failed"
         | 
| 22 | 
            +
                    end
         | 
| 23 | 
            +
             | 
| 24 | 
            +
                    signin account.left_id
         | 
| 25 | 
            +
                  end
         | 
| 26 | 
            +
             | 
| 27 | 
            +
                  # find +\*account card by +\*api card
         | 
| 28 | 
            +
                  # @param api_key [String]
         | 
| 29 | 
            +
                  # @return [+*account card, nil]
         | 
| 30 | 
            +
                  def find_account_by_api_key api_key
         | 
| 31 | 
            +
                    find_account_by :api_key, api_key.strip
         | 
| 32 | 
            +
                  end
         | 
| 33 | 
            +
                end
         | 
| 34 | 
            +
              end
         | 
| 35 | 
            +
            end
         | 
| @@ -0,0 +1,85 @@ | |
| 1 | 
            +
            include_set Abstract::AccountField
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            event :generate_api_key, :prepare_to_validate, trigger: :required do
         | 
| 4 | 
            +
              generate
         | 
| 5 | 
            +
            end
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            event :validate_api_key, :validate do
         | 
| 8 | 
            +
              errors.add :content, t(:api_key_invalid) unless content.match?(/^\w{20,}$/)
         | 
| 9 | 
            +
              errors.add :content, t(:api_key_taken) if api_key_taken?
         | 
| 10 | 
            +
            end
         | 
| 11 | 
            +
             | 
| 12 | 
            +
            def api_key_taken?
         | 
| 13 | 
            +
              return false unless (acct = Card::Auth.find_account_by_api_key content)
         | 
| 14 | 
            +
              acct.id == left_id
         | 
| 15 | 
            +
            end
         | 
| 16 | 
            +
             | 
| 17 | 
            +
            def history?
         | 
| 18 | 
            +
              false
         | 
| 19 | 
            +
            end
         | 
| 20 | 
            +
             | 
| 21 | 
            +
            def ok_to_read
         | 
| 22 | 
            +
              own_account? || super
         | 
| 23 | 
            +
            end
         | 
| 24 | 
            +
             | 
| 25 | 
            +
            def ok_to_create
         | 
| 26 | 
            +
              own_account? || super
         | 
| 27 | 
            +
            end
         | 
| 28 | 
            +
             | 
| 29 | 
            +
            def authenticate_api_key api_key
         | 
| 30 | 
            +
              return true unless (error = api_key_validation_error api_key)
         | 
| 31 | 
            +
             | 
| 32 | 
            +
              errors.add error, t(error)
         | 
| 33 | 
            +
              false
         | 
| 34 | 
            +
            end
         | 
| 35 | 
            +
             | 
| 36 | 
            +
            def generate
         | 
| 37 | 
            +
              self.content = SecureRandom.base64.tr "+/=", "Qrt"
         | 
| 38 | 
            +
            end
         | 
| 39 | 
            +
             | 
| 40 | 
            +
            def generate!
         | 
| 41 | 
            +
              generate.tap { save! }
         | 
| 42 | 
            +
            end
         | 
| 43 | 
            +
             | 
| 44 | 
            +
            def simple_token
         | 
| 45 | 
            +
              Card::Auth::Token.encode accounted.id
         | 
| 46 | 
            +
            end
         | 
| 47 | 
            +
             | 
| 48 | 
            +
            private
         | 
| 49 | 
            +
             | 
| 50 | 
            +
            def api_key_validation_error api_key
         | 
| 51 | 
            +
              case
         | 
| 52 | 
            +
              when !real?
         | 
| 53 | 
            +
                :api_key_not_found
         | 
| 54 | 
            +
              when content != api_key
         | 
| 55 | 
            +
                :api_key_incorrect
         | 
| 56 | 
            +
              end
         | 
| 57 | 
            +
            end
         | 
| 58 | 
            +
             | 
| 59 | 
            +
            format :json do
         | 
| 60 | 
            +
              view :token do
         | 
| 61 | 
            +
                { token: card.simple_token }
         | 
| 62 | 
            +
              end
         | 
| 63 | 
            +
            end
         | 
| 64 | 
            +
             | 
| 65 | 
            +
            format :html do
         | 
| 66 | 
            +
              view :core, unknown: true, template: :haml
         | 
| 67 | 
            +
             | 
| 68 | 
            +
              %i[titled titled_content].each do |viewname|
         | 
| 69 | 
            +
                view(viewname, unknown: true) { super() }
         | 
| 70 | 
            +
              end
         | 
| 71 | 
            +
             | 
| 72 | 
            +
              view :token_link do
         | 
| 73 | 
            +
                link_to t(:api_key_get_jwt_token), path: { format: :json, view: :token }
         | 
| 74 | 
            +
              end
         | 
| 75 | 
            +
             | 
| 76 | 
            +
              view :generate_button, perms: :update, unknown: true do
         | 
| 77 | 
            +
                text = card.content.present? ? t(:api_key_regenerate) : t(:api_key_generate)
         | 
| 78 | 
            +
                card_form :update do
         | 
| 79 | 
            +
                  [
         | 
| 80 | 
            +
                    hidden_tags(card: { trigger: :generate_api_key }),
         | 
| 81 | 
            +
                    submit_button(text: text, disable_with: t(:api_key_generating))
         | 
| 82 | 
            +
                  ]
         | 
| 83 | 
            +
                end
         | 
| 84 | 
            +
              end
         | 
| 85 | 
            +
            end
         | 
| @@ -0,0 +1,11 @@ | |
| 1 | 
            +
            .api-key-core
         | 
| 2 | 
            +
              .current-api-key
         | 
| 3 | 
            +
                - if card.content.present?
         | 
| 4 | 
            +
                  %label{ for: :current_api_key }
         | 
| 5 | 
            +
                    = t :api_key_label
         | 
| 6 | 
            +
             | 
| 7 | 
            +
                  = text_field_tag :current_api_key, card.content, readonly: true
         | 
| 8 | 
            +
                - else
         | 
| 9 | 
            +
                  %em No key.
         | 
| 10 | 
            +
              .api-key-generate-button
         | 
| 11 | 
            +
                = render_generate_button
         | 
    
        metadata
    ADDED
    
    | @@ -0,0 +1,85 @@ | |
| 1 | 
            +
            --- !ruby/object:Gem::Specification
         | 
| 2 | 
            +
            name: card-mod-api_key
         | 
| 3 | 
            +
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            +
              version: 0.11.4
         | 
| 5 | 
            +
            platform: ruby
         | 
| 6 | 
            +
            authors:
         | 
| 7 | 
            +
            - Ethan McCutchen
         | 
| 8 | 
            +
            - Philipp Kühl
         | 
| 9 | 
            +
            - Gerry Gleason
         | 
| 10 | 
            +
            autorequire:
         | 
| 11 | 
            +
            bindir: bin
         | 
| 12 | 
            +
            cert_chain: []
         | 
| 13 | 
            +
            date: 2021-05-05 00:00:00.000000000 Z
         | 
| 14 | 
            +
            dependencies:
         | 
| 15 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 16 | 
            +
              name: card
         | 
| 17 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 18 | 
            +
                requirements:
         | 
| 19 | 
            +
                - - '='
         | 
| 20 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 21 | 
            +
                    version: 1.101.4
         | 
| 22 | 
            +
              type: :runtime
         | 
| 23 | 
            +
              prerelease: false
         | 
| 24 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 25 | 
            +
                requirements:
         | 
| 26 | 
            +
                - - '='
         | 
| 27 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 28 | 
            +
                    version: 1.101.4
         | 
| 29 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 30 | 
            +
              name: card-mod-account
         | 
| 31 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 32 | 
            +
                requirements:
         | 
| 33 | 
            +
                - - '='
         | 
| 34 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 35 | 
            +
                    version: 0.11.4
         | 
| 36 | 
            +
              type: :runtime
         | 
| 37 | 
            +
              prerelease: false
         | 
| 38 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 39 | 
            +
                requirements:
         | 
| 40 | 
            +
                - - '='
         | 
| 41 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 42 | 
            +
                    version: 0.11.4
         | 
| 43 | 
            +
            description: ''
         | 
| 44 | 
            +
            email:
         | 
| 45 | 
            +
            - info@decko.org
         | 
| 46 | 
            +
            executables: []
         | 
| 47 | 
            +
            extensions: []
         | 
| 48 | 
            +
            extra_rdoc_files: []
         | 
| 49 | 
            +
            files:
         | 
| 50 | 
            +
            - README.md
         | 
| 51 | 
            +
            - config/initializers/api_key.rb
         | 
| 52 | 
            +
            - lib/card/auth/api_key.rb
         | 
| 53 | 
            +
            - set/right/account.rb
         | 
| 54 | 
            +
            - set/right/api_key.rb
         | 
| 55 | 
            +
            - set/right/api_key/core.haml
         | 
| 56 | 
            +
            homepage: https://decko.org
         | 
| 57 | 
            +
            licenses:
         | 
| 58 | 
            +
            - GPL-3.0
         | 
| 59 | 
            +
            metadata:
         | 
| 60 | 
            +
              source_code_uri: https://github.com/decko-commons/decko
         | 
| 61 | 
            +
              homepage_uri: https://decko.org
         | 
| 62 | 
            +
              bug_tracker_uri: https://github.com/decko-commons/decko/issues
         | 
| 63 | 
            +
              wiki_uri: https://decko.org
         | 
| 64 | 
            +
              documentation_url: http://docs.decko.org/
         | 
| 65 | 
            +
              card-mod: api_key
         | 
| 66 | 
            +
            post_install_message:
         | 
| 67 | 
            +
            rdoc_options: []
         | 
| 68 | 
            +
            require_paths:
         | 
| 69 | 
            +
            - lib
         | 
| 70 | 
            +
            required_ruby_version: !ruby/object:Gem::Requirement
         | 
| 71 | 
            +
              requirements:
         | 
| 72 | 
            +
              - - ">="
         | 
| 73 | 
            +
                - !ruby/object:Gem::Version
         | 
| 74 | 
            +
                  version: '2.5'
         | 
| 75 | 
            +
            required_rubygems_version: !ruby/object:Gem::Requirement
         | 
| 76 | 
            +
              requirements:
         | 
| 77 | 
            +
              - - ">="
         | 
| 78 | 
            +
                - !ruby/object:Gem::Version
         | 
| 79 | 
            +
                  version: '0'
         | 
| 80 | 
            +
            requirements: []
         | 
| 81 | 
            +
            rubygems_version: 3.1.4
         | 
| 82 | 
            +
            signing_key:
         | 
| 83 | 
            +
            specification_version: 4
         | 
| 84 | 
            +
            summary: API Keys and JWT Tokens for Decko
         | 
| 85 | 
            +
            test_files: []
         |