• Stars
    star
    30
  • Rank 812,236 (Top 17 %)
  • Language
    Crystal
  • License
    MIT License
  • Created almost 8 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Kemal middleware to authentication via HTTP header token using JWT

kemal-auth-token

Gives the current_user to kemal.

Installation

Add this to your application's shard.yml:

dependencies:
  kemal-auth-token:
    github: akwiatkowski/kemal-auth-token

Usage

Initializer

First, you need to initialize middleware.

auth_token_mw = Kemal::AuthToken.new

Sign in

You must provide a way to sign user in. It is your choice how would you like to do it (fetch from DB, have predefined, ...).

You must return UserHash it is alias of Hash(String, (String | Int32 | Nil | Bool)).

You can use login, id, whatever instead of an email.

auth_token_mw.sign_in do |email, password|
  User.sign_in(email, password)
end

There is path to sign in, which you can change if you want.

auth_token_mw.path = "/sign_in" # default value

If you want sign in just execute POST request:

http = HTTP::Client.new("localhost", Kemal.config.port)
result = http.post_form("/sign_in", {"email" => "[email protected]", "password" => "password" })
json = JSON.parse(result.body)

Which return

{"token":"some weird characters"}

Using token

Next request can utilize token based authentication. You must provide it within HTTP headers.

headers = HTTP::Headers.new
headers["X-Token"] = "some weird characters"
http = HTTP::Client.new("localhost", Kemal.config.port)
result = http.exec("GET", "/path", headers)

Get current user

Kemal needs a way how to get user information from JWT token. You must tell how it should do.

auth_token_mw.load_user do |jwt_payload|
  User.load_user(jwt_payload)
end

Keep in mind that jwt_payload is Hash(String, JSON::Type).

You need to provide way to get user information from object (UserHash) stored in JWT token here. That information should be presented also as UserHash.

Later you can access current user information within Kemal code as below:

get "/current_user" do |env|
  env.current_user.to_json
end

Final note

Please read spec file :)

Development

TODO: Write development instructions here

Contributing

  1. Fork it ( https://github.com/akwiatkowski/kemal-auth-token/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

More Repositories

1

crystal_api

Simple PostgreSQL REST API in Crystal with devise-like auth.
Crystal
68
star
2

tremolite

Blog generator
Crystal
20
star
3

gpx2exif

A script to geotag your photos using GPX files
Ruby
14
star
4

technical_graph

Simple technical graphs for Ruby
Ruby
10
star
5

ocranizer

Simple CLI organizer
Crystal
7
star
6

i_hope_you_die

eWuล›
Ruby
4
star
7

akwiatkowski.github.com

Crystal
4
star
8

HomeIO

Home control system
Ruby
4
star
9

tremolite_example

Crystal
3
star
10

crystal_gpx

GPX parser in Crystal + some various utils
Crystal
3
star
11

gpx2png

Convert route storen in GPX format to map using Openstreetmap or OSM compatible tileset.
Ruby
3
star
12

ruby_city

City simulator
Ruby
2
star
13

ranges_merger

Simple gem for merging various range-like data
Ruby
2
star
14

xmpp_chat_bot

Simple bot for xmpp chat. At the moment it get titles of urls.
Ruby
2
star
15

ranger

Range object operation in Crystal
Crystal
2
star
16

waypoint_manager

Simple rails app to manage all waypoints, pois, created in (or for) gps device
Ruby
2
star
17

gpx_utils

Basic utils: track import, waypoint list import/export.
Ruby
2
star
18

crystal_api_sample

Sample project using crystal_api shard
Crystal
1
star
19

herealive

JavaScript
1
star
20

pizza_accounting

Simple accounting for office liabilities, for example when buying pizzas.
Ruby
1
star
21

crystal_weather

Weather fetcher in crystal
Crystal
1
star
22

bobik_utils

Various helpful scripts
Ruby
1
star
23

crystal_metar_parser

Parse METAR weather string. METAR standard is used by every major airport.
Crystal
1
star
24

weather_fetcher

Gem for fetching weather from various services
Ruby
1
star
25

meas_receiver

Library for receiving measurements from HomeIO/IoServer
Ruby
1
star
26

homeio_lite

Ruby
1
star
27

simple_metar_parser

Parsing METARs, simple way.
Ruby
1
star
28

homeio_backend

C++
1
star
29

SimplePowerStabilizer

Simple power stabilizer to convert 40-60V to 36V, with RS-485 communication using special AVR uC.
C
1
star
30

webcam_downloader

Download webcams.
Ruby
1
star
31

koko_rails

Adds Jarzฤ™bina's power to your rails app.
Ruby
1
star
32

simple_show_helper

Simple helper for 'show' pages to help lazy developers.
Ruby
1
star