All Projects → msa7 → multi_auth

msa7 / multi_auth

Licence: MIT license
Standardized multi-provider OAuth authentication

Programming Languages

crystal
512 projects
Makefile
30231 projects

Projects that are alternatives of or similar to multi auth

qrencode.cr
Crystal bindings for libqrencode (qrencode), a library for QR code generation
Stars: ✭ 28 (-72.55%)
Mutual labels:  crystal-lang
crystime
Advanced time, calendar, schedule, and remind library for Crystal
Stars: ✭ 23 (-77.45%)
Mutual labels:  crystal-lang
omniauth-kerberos
OmniAuth strategy for kerberos authentication.
Stars: ✭ 13 (-87.25%)
Mutual labels:  omniauth
rails 6 devise example
rails 6 with devise + bootstrap + github oauth
Stars: ✭ 65 (-36.27%)
Mutual labels:  omniauth
Devise token auth
Token based authentication for Rails JSON APIs. Designed to work with jToker and ng-token-auth.
Stars: ✭ 3,263 (+3099.02%)
Mutual labels:  omniauth
tallboy
Declarative API for drawing unicode/ascii character tables in crystal lang
Stars: ✭ 49 (-51.96%)
Mutual labels:  crystal-lang
cr-xmpp
XMPP/Jabber Library for Crystal
Stars: ✭ 16 (-84.31%)
Mutual labels:  crystal-lang
laravel-multi-auth-tutorial
Tutorial demonstrating the implementation of multiple authentication in Laravel
Stars: ✭ 26 (-74.51%)
Mutual labels:  multiauth
triki
Mysql, PostgreSQL and SQL dump obfuscator aka anonimizer
Stars: ✭ 28 (-72.55%)
Mutual labels:  crystal-lang
amber-introduction
Introduction to the Amber web framework and its features
Stars: ✭ 53 (-48.04%)
Mutual labels:  crystal-lang
omniauth-okta
OAuth2 strategy for Okta
Stars: ✭ 29 (-71.57%)
Mutual labels:  omniauth
oauthable
Setup, test, and implement the OAuth connections to over 70 different APIs.
Stars: ✭ 12 (-88.24%)
Mutual labels:  omniauth
walter.cr
Keep your crystal clean!
Stars: ✭ 14 (-86.27%)
Mutual labels:  crystal-lang
Devise-Omniauth-Multiple-Providers
Devise Multiple Omniauth Providers
Stars: ✭ 34 (-66.67%)
Mutual labels:  omniauth
omniauth-mastodon
OmniAuth strategy for Mastodon
Stars: ✭ 27 (-73.53%)
Mutual labels:  omniauth
crystal-nodejs
Node.js engine for crystal-lang. JS code and npm module executes on crystal-nodejs
Stars: ✭ 42 (-58.82%)
Mutual labels:  crystal-lang
brotli.cr
Crystal bindings to the Google brotli compression library
Stars: ✭ 20 (-80.39%)
Mutual labels:  crystal-lang
proxes
A simple management interface for Elasticsearch.
Stars: ✭ 16 (-84.31%)
Mutual labels:  omniauth
docker-crystal
Docker wrapper for the Crystal CLI
Stars: ✭ 13 (-87.25%)
Mutual labels:  crystal-lang
gosu.cr
Shard for the Gosu game library
Stars: ✭ 28 (-72.55%)
Mutual labels:  crystal-lang

MultiAuth

Build Status

MultiAuth is a library that standardizes multi-provider authentication for web applications. Currently supported providers:

Installation

Add this to your application's shard.yml:

dependencies:
  multi_auth:
    github: msa7/multi_auth

Usage

MultiAuth public interface

  require "multi_auth"

  MultiAuth.config("github", ENV['ID'], ENV['SECRET']) # configuration

  multi_auth = MultiAuth.make(provider, redirect_uri) # initialize engine
  multi_auth.authorize_uri  # URL to provider authentication dialog

  # on http callback, like /multi_auth/github/callback
  user = multi_auth.user(params) # get signed in user

MultiAuth build with no dependency, it can be used with any web framework. Information about signed in user described in User class here src/multi_auth/user.cr. Supported providers src/multi_auth/providers. I hope it easy to add new providers.

Kemal integration example

<a href="/multi_auth/github">Sign in with Github</a>
MultiAuth.config("facebook", "facebookClientID", "facebookSecretKey")
MultiAuth.config("google", "googleClientID", "googleSecretKey")

def self.multi_auth(env)
  provider = env.params.url["provider"]
  redirect_uri = "#{Kemal.config.scheme}://#{env.request.host_with_port.as(String)}/multi_auth/#{provider}/callback"
  MultiAuth.make(provider, redirect_uri)
end

get "/multi_auth/:provider" do |env|
  env.redirect(multi_auth(env).authorize_uri)
end

get "/multi_auth/:provider/callback" do |env|
  user = multi_auth(env).user(env.params.query)
  p user.email
  user
end

Lucky integration example

# config/watch.yml
host: myapp.lvh.me
port: 5000

# config/multi_auth_handler.cr
require "multi_auth"

class MultiAuthHandler
  MultiAuth.config("facebook", "facebookClientID", "facebookSecretKey")
  MultiAuth.config("google", "googleClientID", "googleSecretKey")

  def self.authorize_uri(provider : String)
    MultiAuth.make(provider, "#{Lucky::RouteHelper.settings.base_uri}/oauth/#{provider}/callback").authorize_uri(scope: "email")
  end

  def self.user(provider : String, params : Enumerable({String, String}))
    MultiAuth.make(provider, "#{Lucky::RouteHelper.settings.base_uri}/oauth/#{provider}/callback").user(params)
  end
end

# src/actions/oauth/handler.cr
class OAuth::Handler < BrowserAction
  get "/oauth/:provider" do
    redirect to: MultiAuthHandler.authorize_uri(provider)
  end
end

# src/actions/oauth/handler/callback.cr
class OAuth::Handler::Callback < BrowserAction
  get "/oauth/:provider/callback" do
    user = MultiAuthHandler.user(provider, request.query_params)
    text user.email.to_s
  end
end

Amber integration example

# config/initializers/multi_auth.cr
require "multi_auth"

MultiAuth.config("facebook", "facebookClientID", "facebookSecretKey")
MultiAuth.config("google", "googleClientID", "googleSecretKey")

# config/routes.cr
routes :web do
  ...
  get "/multi_auth/:provider", MultiAuthController, :new
  get "/multi_auth/:provider/callback", MultiAuthController, :callback
end

# src/controllers/multi_auth_controller.cr
class MultiAuthController < ApplicationController
  def new
    redirect_to multi_auth.authorize_uri(scope: "email")
  end

  def callback
    multi_auth_user = multi_auth.user(request.query_params)

    if user = User.find_by email: multi_auth_user.email
      login user
    else
      user = User.create!(
        first_name: multi_auth_user.first_name,
        last_name: multi_auth_user.last_name,
        email: multi_auth_user.email
      )
      login user
    end

    redirect_to "/"
  end

  def login(user)
    context.session["user_id"] = user.id
  end

  def provider
    params[:provider]
  end

  def redirect_uri
    "#{Amber.settings.secrets["base_url"]}/multi_auth/#{provider}/callback"
  end

  def multi_auth
    MultiAuth.make(provider, redirect_uri)
  end
end

Development

Install docker

Setup everythings

make setup

Run specs

make t
make t c=spec/providers/twitter_spec.cr

Run code linter

make l

Contributors

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].