Sha256: 81fde8589110dd2eee548b5a8b1d6d0da976f83e343a2f9fcca8ac892563f423

Contents?: true

Size: 1.77 KB

Versions: 11

Compression:

Stored size: 1.77 KB

Contents

# frozen_string_literal: true
# Copyright 2016 Liqwyd Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require 'warden'
require 'bcrypt'

# Top level module for the core Cyclid code.
module Cyclid
  # Module for the Cyclid API
  module API
    # Warden Strategies
    module Strategies
      # HTTTP Basic authentication based strategy
      module Basic
        # Authenticate via. HTTP Basic auth.
        Warden::Strategies.add(:basic) do
          def valid?
            request.env['HTTP_AUTHORIZATION'].is_a? String and \
              request.env['HTTP_AUTHORIZATION'] =~ /^Basic .*$/
          end

          def authenticate!
            begin
              authorization = request.env['HTTP_AUTHORIZATION']
              digest = authorization.match(/^Basic (.*)$/).captures.first

              user_pass = Base64.decode64(digest)
              username, password = user_pass.split(':')
            rescue
              fail! 'invalid digest'
            end

            user = User.find_by(username: username)
            if user.nil?
              fail! 'invalid user'
            elsif BCrypt::Password.new(user.password).is_password? password
              success! user
            else
              fail! 'invalid user'
            end
          end
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
cyclid-0.4.0 app/cyclid/sinatra/warden/strategies/basic.rb
cyclid-0.3.3 app/cyclid/sinatra/warden/strategies/basic.rb
cyclid-0.3.2 app/cyclid/sinatra/warden/strategies/basic.rb
cyclid-0.3.1 app/cyclid/sinatra/warden/strategies/basic.rb
cyclid-0.3.0 app/cyclid/sinatra/warden/strategies/basic.rb
cyclid-0.2.5 app/cyclid/sinatra/warden/strategies/basic.rb
cyclid-0.2.4 app/cyclid/sinatra/warden/strategies/basic.rb
cyclid-0.2.3 app/cyclid/sinatra/warden/strategies/basic.rb
cyclid-0.2.2 app/cyclid/sinatra/warden/strategies/basic.rb
cyclid-0.2.1 app/cyclid/sinatra/warden/strategies/basic.rb
cyclid-0.2.0 app/cyclid/sinatra/warden/strategies/basic.rb