Giter Site home page Giter Site logo

ex-okta-auth's Introduction

ExOktaAuth

Elixir CI

An Elixir library that enables your application to work with Okta via OAuth 2.0/OIDC

Installation

If available in Hex, the package can be installed by adding ex_okta_auth to your list of dependencies in mix.exs:

  def deps do
    [{:ex_okta_auth, "~> 0.1.0"}]
  end

Add :ex_okta_auth to your applications:

  def application do
    [extra_applications: [:ex_okta_auth]]
  end

Add your configuration for okta to your applications config.ex

config :ex_okta_auth, ExOktaAuth.Okta,
  client_id: System.get_env("OKTA_CLIENT_ID"),
  client_secret: System.get_env("OKTA_CLIENT_SECRET"),
  site: "https://your-doman.okta.com",
  redirect_uri: "https://your-apps-callback-uri"

Create scope in your routes to handle the requests and callbacks

  scope "/signin", MyAppWeb do
    pipe_through :browser
    get "/:provider", AuthController, :request
    get "/:provider/callback", AuthController, :callback
    post "/:provider/callback", AuthController, :callback
  end

Create a controller that will handle requests and callbacks to and from okta

defmodule MyAppWeb.AuthController do
  use MyAppWeb, :controller

  def request(conn, _params) do
    ExOktaAuth.Okta.authorize_url!(conn)
  end

  def callback(conn, %{"provider" => _provider, "code" => code, "state" => _state}) do
    client = ExOktaAuth.Okta.get_token_without_auth!(code: code)
    user = get_user_information(client)
    conn
    |> put_session(:current_user, user)
    |> put_session(:access_token, client.token.access_token)
    |> put_flash(:info, "Welcome #{user["given_name"]}")
    |> redirect(to: "/")
  end

  defp get_user_information(client) do
    {:ok, resp} = ExOktaAuth.Okta.get_user_info(client)
    resp.body
  end
end

License

The library is available as open source under the terms of the MIT License.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.