{"id":13513858,"url":"https://github.com/pow-auth/assent","last_synced_at":"2025-05-14T07:08:22.203Z","repository":{"id":35169261,"uuid":"210714529","full_name":"pow-auth/assent","owner":"pow-auth","description":"Multi-provider framework in Elixir","archived":false,"fork":false,"pushed_at":"2025-03-06T11:07:27.000Z","size":774,"stargazers_count":478,"open_issues_count":3,"forks_count":50,"subscribers_count":7,"default_branch":"main","last_synced_at":"2025-05-11T22:47:59.809Z","etag":null,"topics":["apple-sign-in","auth0","azure-active-directory","basecamp","discord","elixir","facebook","github","gitlab","google","instagram","multi-provider","oauth","oauth2","openid-connect","sign-in-with-apple","slack","twitter","vk","vkontakte"],"latest_commit_sha":null,"homepage":"https://powauth.com","language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pow-auth.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2019-09-24T23:23:16.000Z","updated_at":"2025-05-11T10:54:03.000Z","dependencies_parsed_at":"2024-02-25T02:26:51.715Z","dependency_job_id":"be3a7c2b-25a8-4774-8751-92de07552a7a","html_url":"https://github.com/pow-auth/assent","commit_stats":{"total_commits":332,"total_committers":25,"mean_commits":13.28,"dds":0.1506024096385542,"last_synced_commit":"0647b8c157b995bad1e2abaf2030db8ad9ee2e16"},"previous_names":[],"tags_count":44,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pow-auth%2Fassent","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pow-auth%2Fassent/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pow-auth%2Fassent/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pow-auth%2Fassent/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pow-auth","download_url":"https://codeload.github.com/pow-auth/assent/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254092659,"owners_count":22013290,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["apple-sign-in","auth0","azure-active-directory","basecamp","discord","elixir","facebook","github","gitlab","google","instagram","multi-provider","oauth","oauth2","openid-connect","sign-in-with-apple","slack","twitter","vk","vkontakte"],"created_at":"2024-08-01T05:00:38.901Z","updated_at":"2025-05-14T07:08:22.181Z","avatar_url":"https://github.com/pow-auth.png","language":"Elixir","readme":"# Assent\n\n[![Github CI](https://github.com/pow-auth/assent/workflows/CI/badge.svg)](https://github.com/pow-auth/assent/actions?query=workflow%3ACI)\n[![hexdocs.pm](https://img.shields.io/badge/api-docs-green.svg?style=flat)](https://hexdocs.pm/assent)\n[![hex.pm](https://img.shields.io/hexpm/v/assent.svg?style=flat)](https://hex.pm/packages/assent)\n\n\u003c!-- MDOC !--\u003e\n\nMulti-provider authentication framework.\n\n## Features\n\n* Includes the following base strategies:\n  * OAuth 1.0 - `Assent.Strategy.OAuth`\n  * OAuth 2.0 - `Assent.Strategy.OAuth2`\n  * OpenID Connect - `Assent.Strategy.OIDC`\n* Includes the following provider strategies:\n  * Apple Sign In - `Assent.Strategy.Apple`\n  * Auth0 - `Assent.Strategy.Auth0`\n  * Azure AD - `Assent.Strategy.AzureAD`\n  * Basecamp - `Assent.Strategy.Basecamp`\n  * Bitbucket - `Assent.Strategy.Bitbucket`\n  * DigitalOcean - `Assent.Strategy.DigitalOcean`\n  * Discord - `Assent.Strategy.Discord`\n  * Facebook - `Assent.Strategy.Facebook`\n  * Github - `Assent.Strategy.Github`\n  * Gitlab - `Assent.Strategy.Gitlab`\n  * Google - `Assent.Strategy.Google`\n  * Instagram - `Assent.Strategy.Instagram`\n  * LINE Login - `Assent.Strategy.LINE`\n  * Linkedin - `Assent.Strategy.Linkedin`\n  * Spotify - `Assent.Strategy.Spotify`\n  * Strava - `Assent.Strategy.Strava`\n  * Slack - `Assent.Strategy.Slack`\n  * Stripe Connect - `Assent.Strategy.Stripe`\n  * Telegram - `Assent.Strategy.Telegram`\n  * Twitch - `Assent.Strategy.Twitch`\n  * Twitter - `Assent.Strategy.Twitter`\n  * VK - `Assent.Strategy.VK`\n  * Zitadel - `Assent.Strategy.Zitadel`\n\n## Usage\n\nA strategy consists of two phases; request and callback. In the request phase, the user will be redirected to the auth provider for authentication and then returned to initiate the callback phase.\n\n### Single provider\n\nThis is an example using the `Assent.Strategy.Github` strategy with `Plug`:\n\n```elixir\ndefmodule GithubAuth do\n  import Plug.Conn\n\n  alias Assent.Strategy.Github\n\n  defp config do\n    [\n      client_id: Application.fetch_env!(:my_app, :github, :client_id),\n      client_secret: Application.fetch_env!(:my_app, :github, :client_secret),\n      redirect_uri: \"http://localhost:4000/auth/github/callback\"\n    ]\n  end\n\n  # http://localhost:4000/auth/github\n  def request(conn) do\n    config()\n    |\u003e Github.authorize_url()\n    |\u003e case do\n      {:ok, %{url: url, session_params: session_params}} -\u003e\n        # Session params (used for OAuth 2.0 and OIDC strategies) will be\n        # retrieved when user returns for the callback phase\n        conn = put_session(conn, :session_params, session_params)\n\n        # Redirect end-user to Github to authorize access to their account\n        conn\n        |\u003e put_resp_header(\"location\", url)\n        |\u003e send_resp(302, \"\")\n\n      {:error, _error} -\u003e\n        # Something went wrong generating the request authorization url\n        send_resp(conn, 500, \"Failed authorization\")\n    end\n  end\n\n  # http://localhost:4000/auth/github/callback\n  def callback(conn) do\n    # End-user will return to the callback URL with params attached to the\n    # request. These must be passed on to the strategy. In this example we only\n    # expect GET query params, but the provider could also return the user with\n    # a POST request where the params is in the POST body.\n    %{params: params} = fetch_query_params(conn)\n\n    # The session params (used for OAuth 2.0 and OIDC strategies) stored in the\n    # request phase will be used in the callback phase\n    session_params = get_session(conn, :session_params)\n\n    conn = delete_session(conn, :session_params)\n\n    config()\n    # Session params should be added to the config so the strategy can use them\n    |\u003e Keyword.put(:session_params, session_params)\n    |\u003e Github.callback(params)\n    |\u003e case do\n      {:ok, %{user: user, token: token}} -\u003e\n        # Authorization succesful\n        conn\n        |\u003e put_session(:user, user)\n        |\u003e put_session(:token, token)\n        |\u003e put_resp_header(\"location\", \"/\")\n        |\u003e send_resp(302, \"\")\n\n      {:error, _error} -\u003e\n        # Authorizaiton failed\n        send_resp(conn, 500, \"Failed authorization\")\n    end\n  end\nend\n```\n\n### Multi-provider\n\nAll assent strategies work the same way, so if you have more than one strategy you may want to set up a single module to handle any of the auth strategies. This example is a generalized flow that's similar to what's used in `PowAssent`.\n\n```elixir\nconfig :my_app, :strategies,\n  github: [\n    client_id: \"REPLACE_WITH_CLIENT_ID\",\n    client_secret: \"REPLACE_WITH_CLIENT_SECRET\",\n    strategy: Assent.Strategy.Github\n  ],\n  # Other strategies\n```\n\n```elixir\ndefmodule MultiProviderAuth do\n  @spec authorize_url(atom()) :: {:ok, map()} | {:error, term()}\n  def authorize_url(provider) do\n    config = config!(provider)\n\n    config[:strategy].authorize_url()\n  end\n\n  @spec callback(atom(), map(), map()) :: {:ok, map()} | {:error, term()}\n  def callback(provider, params, session_params) do\n    config = config!(provider)\n\n    config\n    |\u003e Keyword.put(:session_params, session_params)\n    |\u003e config[:strategy].callback(params)\n  end\n\n  defp config!(provider) do\n    config =\n      Application.get_env(:my_app, :strategies)[provider] ||\n        raise \"No provider configuration for #{provider}\"\n    \n    Keyword.put(config, :redirect_uri, \"http://localhost:4000/oauth/#{provider}/callback\")\n  end\nend\n```\n\n### Custom provider\n\nYou can create custom strategies. Here's an example of an OAuth 2.0 implementation using `Assent.Strategy.OAuth2.Base`:\n\n```elixir\ndefmodule TestProvider do\n  use Assent.Strategy.OAuth2.Base\n\n  @impl true\n  def default_config(_config) do\n    [\n      # `:base_url` will be used for any paths below\n      base_url: \"http://localhost:4000/api/v1\",\n       # Definining an absolute URI overrides the `:base_url`\n      authorize_url: \"http://localhost:4000/oauth/authorize\",\n      token_url: \"/oauth/access_token\",\n      user_url: \"/user\",\n      authorization_params: [scope: \"email profile\"],\n      auth_method: :client_secret_post\n    ]\n  end\n\n  @impl true\n  def normalize(_config, user) do\n    {:ok,\n      # Conformed to https://openid.net/specs/openid-connect-core-1_0.html#rfc.section.5.1\n      %{\n        \"sub\"      =\u003e user[\"sub\"],\n        \"name\"     =\u003e user[\"name\"],\n        \"nickname\" =\u003e user[\"username\"],\n        \"email\"    =\u003e user[\"email\"]\n      # },\n      # # Provider specific data not part of the standard claims spec\n      # %{\n      #   \"http://localhost:4000/bio\" =\u003e user[\"bio\"]\n      }\n    }\n  end\nend\n```\n\nThe normalized user map should conform to the [OpenID Connect Core 1.0 Standard Claims spec](https://openid.net/specs/openid-connect-core-1_0.html#rfc.section.5.1), and should return either `{:ok, userinfo_claims}` or `{:ok, userinfo_claims, additional}`. Any keys defined in the userinfo claims that aren't part of the specs will not be included in the user map. Instead, they should be set in the additional data that will then be merged on top of the userinfo claims excluding any keys that have already been set.\n\nYou can use any of the `Assent.Strategy.OAuth2.Base`, `Assent.Strategy.OAuth.Base`, and `Assent.Strategy.OIDC.Base` macros to set up the strategy.\n\nIf you need more control over the strategy than what the macros give you, you can implement your provider using the `Assent.Strategy` behaviour:\n\n```elixir\ndefmodule TestProvider do\n  @behaviour Assent.Strategy\n\n  alias Assent.Strategy, as: Helpers\n\n  @impl Assent.Strategy\n  def authorize_url(config) do\n    # Generate redirect URL\n\n    {:ok, %{url: url}}\n  end\n\n  @impl Assent.Strategy\n  def callback(config, params) do\n    # Fetch user data\n\n    user = Helpers.normalize_userinfo(userinfo)\n\n    {:ok, %{user: user}}\n  end\nend\n```\n\n## HTTP Client\n\nAssent supports [`Req`](https://github.com/wojtekmach/req), [`Finch`](https://github.com/sneako/finch), and [`:httpc`](https://www.erlang.org/doc/man/httpc.html) out of the box. The `Req` HTTP client adapter will be used by default if enabled, otherwise Erlang's `:httpc` adapter will be used.\n\nYou can explicitly set the HTTP client adapter in the configuration:\n\n```elixir\nconfig = [\n  client_id: \"REPLACE_WITH_CLIENT_ID\",\n  client_secret: \"REPLACE_WITH_CLIENT_SECRET\",\n  http_adapter: Assent.HTTPAdapter.Httpc\n]\n```\n\nOr globally in the config:\n\n```elixir\nconfig :assent, http_adapter: Assent.HTTPAdapter.Httpc\n```\n\n### Req\n\n`Req` doesn't require any additional configuration and will work out of the box:\n\n```elixir\ndefp deps do\n  [\n    # ...\n    {:req, \"~\u003e 0.4\"}\n  ]\nend\n```\n\n### :httpc\n\nIf `Req` is not available, Erlangs built-in `:httpc` is used for requests. SSL verification is automatically enabled when `:certifi` and `:ssl_verify_fun` packages are available. `:httpc` only supports HTTP/1.1.\n\n```elixir\ndefp deps do\n  [\n    # ...\n    # Required for SSL validation if using the `:httpc` adapter\n    {:certifi, \"~\u003e 2.4\"},\n    {:ssl_verify_fun, \"~\u003e 1.1\"}\n  ]\nend\n```\n\nYou must include `:inets` to `:extra_applications` to include `:httpc` in your release.\n\n### Finch\n\n`Finch` will require a supervisor in your application.\n\nUpdate `mix.exs`:\n\n```elixir\ndefp deps do\n  [\n    # ...\n    {:finch, \"~\u003e 0.16\"}\n  ]\nend\n```\n\nEnsure you start the Finch supervisor in your application, and set `:http_adapter` in your provider configuration using your connection pool:\n\n```elixir\nconfig = [\n  client_id: \"REPLACE_WITH_CLIENT_ID\",\n  client_secret: \"REPLACE_WITH_CLIENT_SECRET\",\n  http_adapter: {Assent.HTTPAdapter.Finch, supervisor: MyFinch}\n]\n```\n\n## JWT Adapter\n\nBy default the built-in `Assent.JWTAdapter.AssentJWT` is used for JWT parsing, but you can change it to any third-party library with a custom `Assent.JWTAdapter`. A [JOSE](https://github.com/potatosalad/erlang-jose) adapter `Assent.JWTAdapter.JOSE` is included.\n\nTo use JOSE, update `mix.exs`:\n\n```elixir\ndefp deps do\n  [\n    # ...\n    {:jose, \"~\u003e 1.8\"}\n  ]\nend\n```\n\nAnd pass the `:jwt_adapter` with your provider configuration:\n\n```elixir\nconfig = [\n  client_id: \"REPLACE_WITH_CLIENT_ID\",\n  client_secret: \"REPLACE_WITH_CLIENT_SECRET\",\n  jwt_adapter: Assent.JWTAdapter.JOSE\n]\n```\n\nOr globally in the config:\n\n```elixir\nconfig :assent, jwt_adapter: Assent.JWTAdapter.JOSE\n```\n\n\u003c!-- MDOC !--\u003e\n\n## Installation\n\nAdd Assent to your list of dependencies in `mix.exs`:\n\n```elixir\ndefp deps do\n  [\n    # ...\n    {:assent, \"~\u003e 0.3.0\"}\n  ]\nend\n```\n\nRun `mix deps.get` to install it.\n\n## LICENSE\n\n(The MIT License)\n\nCopyright (c) 2019-present Dan Schultzer \u0026 the Contributors\n\nPermission 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:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE 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.\n","funding_links":[],"categories":["Elixir"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpow-auth%2Fassent","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpow-auth%2Fassent","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpow-auth%2Fassent/lists"}