Skip to content

Commit

Permalink
v0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
sdogruyol committed Nov 26, 2016
0 parents commit 7333469
Show file tree
Hide file tree
Showing 9 changed files with 178 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/doc/
/libs/
/lib/
/.crystal/
/.shards/


# Libraries don't need dependency lock
# Dependencies will be locked in application that uses them
/shard.lock

1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
language: crystal
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016 Serdar Dogruyol

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# kemal-basic-auth

Add basic auth to your [Kemal[http://github.com/kemalcr/kemal] application.

## Installation


Add this to your application's `shard.yml`:

```yaml
dependencies:
kemal-basic-auth:
github: kemalcr/kemal-basic-auth
```
## Usage
```crystal
require "kemal-basic-auth"
```


TODO: Write usage instructions here

## Development

TODO: Write development instructions here

## Contributing

1. Fork it ( https://github.com/kemalcr/kemal-basic-auth/fork )
2. Create your feature branch (git checkout -b my-new-feature)
3. Commit your changes (git commit -am 'Add some feature')
4. Push to the branch (git push origin my-new-feature)
5. Create a new Pull Request

## Contributors

- [[your-github-name]](https://github.com/[your-github-name]) Serdar Dogruyol - creator, maintainer
15 changes: 15 additions & 0 deletions shard.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: kemal-basic-auth
version: 0.1.0

development_dependencies:
kemal:
github: sdogruyol/kemal
branch: master

authors:
- Serdar Dogruyol <dogruyolserdar@gmail.com>


crystal: 0.20.0

license: MIT
33 changes: 33 additions & 0 deletions spec/kemal-basic-auth_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require "./spec_helper"

describe "HTTPBasicAuth" do
it "goes to next handler with correct credentials" do
auth_handler = HTTPBasicAuth.new("serdar", "123")
request = HTTP::Request.new(
"GET",
"/",
headers: HTTP::Headers{"Authorization" => "Basic c2VyZGFyOjEyMw=="},
)

io_with_context = create_request_and_return_io(auth_handler, request)
client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false)
client_response.status_code.should eq 404
end

it "returns 401 with incorrect credentials" do
auth_handler = HTTPBasicAuth.new("serdar", "123")
request = HTTP::Request.new(
"GET",
"/",
headers: HTTP::Headers{"Authorization" => "NotBasic"},
)
io_with_context = create_request_and_return_io(auth_handler, request)
client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false)
client_response.status_code.should eq 401
end

it "adds HTTPBasicAuthHandler" do
basic_auth "serdar", "123"
Kemal.config.handlers.size.should eq 6
end
end
12 changes: 12 additions & 0 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require "spec"
require "../src/kemal-basic-auth"

def create_request_and_return_io(handler, request)
io = IO::Memory.new
response = HTTP::Server::Response.new(io)
context = HTTP::Server::Context.new(request, response)
handler.call(context)
response.close
io.rewind
io
end
41 changes: 41 additions & 0 deletions src/kemal-basic-auth.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require "base64"
require "kemal"

# This middleware adds HTTP Basic Auth support to your application.
# Returns 401 "Unauthorized" with wrong credentials.
#
# basic_auth "username", "password"
#
class HTTPBasicAuth < HTTP::Handler
BASIC = "Basic"
AUTH = "Authorization"
AUTH_MESSAGE = "Could not verify your access level for that URL.\nYou have to login with proper credentials"
HEADER_LOGIN_REQUIRED = "Basic realm=\"Login Required\""

def initialize(@username : String?, @password : String?)
end

def call(context)
if context.request.headers[AUTH]?
if value = context.request.headers[AUTH]
if value.size > 0 && value.starts_with?(BASIC)
return call_next(context) if authorized?(value)
end
end
end
headers = HTTP::Headers.new
context.response.status_code = 401
context.response.headers["WWW-Authenticate"] = HEADER_LOGIN_REQUIRED
context.response.print AUTH_MESSAGE
end

def authorized?(value)
username, password = Base64.decode_string(value[BASIC.size + 1..-1]).split(":")
@username == username && @password == password
end
end

# Helper to easily add HTTP Basic Auth support.
def basic_auth(username, password)
add_handler HTTPBasicAuth.new(username, password)
end
3 changes: 3 additions & 0 deletions src/kemal-basic-auth/version.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module Kemal::Basic::Auth
VERSION = "0.1.0"
end

0 comments on commit 7333469

Please sign in to comment.