Skip to content

Commit

Permalink
Added web basic auth and updated README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
abhisheksarka committed Sep 8, 2024
1 parent e10d24d commit 75e105e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 12 deletions.
41 changes: 30 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,23 +62,13 @@ Add the APICraft Middleware into your Rails application, via the `config/applica
# config/application.rb
module App
class Application < Rails::Application
# Rest of the configuration
# Rest of the configuration...
config.middleware.use Apicraft::Middlewares::Mocker
config.middleware.use Apicraft::Middlewares::Introspector
end
end
```

```ruby
# config/routes.rb
# frozen_string_literal: true

Rails.application.routes.draw do
# Rest of the routes
mount Apicraft::Web::App, at: "/apicraft"
end
```

Now every API in the specification has a functional version. If a path isn't implemented, APICraft serves a mock response; otherwise, it forwards the request to your application as usual.

## Usage
Expand Down Expand Up @@ -174,10 +164,35 @@ Example: `https://yoursite.com/api/orders`
```
### 👀 API Documentation

Mount the documentation views in your route file.

```ruby
# config/routes.rb

Rails.application.routes.draw do
# Rest of the routes...
mount Apicraft::Web::App, at: "/apicraft"
end
```

You can browse API Documentation at
- `/apicraft/swaggerdoc`
- `/apicraft/redoc`

Enable authentication for the `/apicraft` namespace.

```ruby
# config/application.rb
module App
class Application < Rails::Application
# Rest of the configuration...
Apicraft::Web::App.use do |user, password|
[user, password] == ["admin", "password"]
end
end
end
```

## Configuration

List of available configurations.
Expand Down Expand Up @@ -218,6 +233,10 @@ Apicraft.configure do |config|
mock: "Apicraft-Mock"
}
end

Apicraft::Web::App.use do |user, password|
[user, password] == ["admin", "password"]
end
```

## Contributing
Expand Down
25 changes: 24 additions & 1 deletion lib/apicraft/web/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ module Web
# for all the views to be served
class App
def self.call(env)
return unauthorized_response unless authorized?(env)

uri = env["REQUEST_URI"]
method = env["REQUEST_METHOD"]
Router.namespace = env["SCRIPT_NAME"]
Expand All @@ -14,7 +16,7 @@ def self.call(env)
)[-1]

content, content_type = Router.load_response!(
method, path
method, path || "/"
)

raise Errors::RouteNotFound if content.nil?
Expand All @@ -37,6 +39,27 @@ def self.call(env)
["Error: #{e.message}"]
]
end

def self.authorized?(env)
auth = Rack::Auth::Basic::Request.new(env)
username, password = auth.provided? && auth.basic? && auth.credentials
@use.call(username, password).present?
end

def self.use(&block)
@use = block
end

def self.unauthorized_response
[
401,
{
"Content-Type": "text/plain",
"WWW-Authenticate": "Basic realm=\"Restricted Area\""
},
["Unauthorized"]
]
end
end
end
end

0 comments on commit 75e105e

Please sign in to comment.