Assent.Strategy.OAuth (Assent v0.4.0)

Copy Markdown View Source

OAuth 1.0a strategy.

authorize_url/1 returns a map with a :session_params and :url key. The :session_params key carries the :oauth_token and :oauth_token_secret values for the request.

Configuration

  • :consumer_key - the OAuth consumer key, required

  • :base_url - the base URL of the OAuth server, required

  • :signature_method - the signature method, optional, defaults to :hmac_sha1. The value may be one of the following:

    • :hmac_sha1 - generates signature with HMAC-SHA1

    • :rsa_sha1 - generates signature with RSA-SHA1

    • :plaintext - doesn't generate signature

  • :consumer_secret - the OAuth consumer secret, required if :signature_method is either :hmac_sha1 or :plaintext

  • :private_key_path - the path for the private key, required if :signature_method is :rsa_sha1 and :private_key hasn't been set

  • :private_key - the private key content that can be defined instead of :private_key_path, required if :signature_method is :rsa_sha1 and :private_key_path hasn't been set

Usage

defmodule OAuth do
  import Plug.Conn

  alias Assent.Strategy.OAuth

  defp config do
    [
      consumer_key: Application.fetch_env!(:my_app, :oauth, :consumer_key),
      consumer_secret: Application.fetch_env!(:my_app, :oauth, :consumer_secret),
      base_url: "https://auth.example.com",
      authorization_params: [scope: "user:read user:write"],
      user_url: "https://example.com/api/user",
      redirect_uri: "http://localhost:4000/auth/callback"
    ]
  end

  def request(conn) do
    config()
    |> OAuth.authorize_url()
    |> case do
      {:ok, %{url: url, session_params: session_params}} ->
        conn
        |> put_session(:session_params, session_params)
        |> put_resp_header("location", url)
        |> send_resp(302, "")

      {:error, _error} ->
        send_resp(conn, 500, "Failed authorization")
    end
  end

  def callback(conn) do
    %{params: params} = fetch_query_params(conn)
    session_params = get_session(conn, :session_params)
    conn = delete_session(conn, :session_params)

    config()
    |> Keyword.put(:session_params, session_params)
    |> OAuth.callback(params)
    |> case do
      {:ok, %{user: user, token: token}} ->
        conn
        |> put_session(:user, user)
        |> put_session(:token, token)
        |> put_resp_header("location", "/")
        |> send_resp(302, "")

      {:error, _error} ->
        send_resp(conn, 500, "Failed authorization")
    end
  end
end

Summary

Types

Return value of callback/3.

The session parameters returned from authorize_url/1, to be stored and passed back into callback/3.

Functions

Generates an authorization URL for the request phase.

Callback phase for generating an access token and fetching user data.

Performs a signed HTTP request to the API using the OAuth token.

Types

on_authorize_url()

@type on_authorize_url() ::
  {:ok, %{session_params: session_params(), url: binary()}} | {:error, term()}

Return value of authorize_url/1.

on_callback()

@type on_callback() :: {:ok, %{token: map(), user: map()}} | {:error, term()}

Return value of callback/3.

session_params()

@type session_params() :: %{oauth_token: binary(), oauth_token_secret: binary()}

The session parameters returned from authorize_url/1, to be stored and passed back into callback/3.

Functions

authorize_url(config)

@spec authorize_url(Keyword.t()) :: on_authorize_url()

Generates an authorization URL for the request phase.

Options

  • :redirect_uri - the URI that the server redirects the user to after authentication, required

  • :request_token_url - the path or URL to fetch the token from, optional, defaults to /request_token

  • :authorize_url - the path or URL for the OAuth server to redirect users to, defaults to /authorize

  • :authorization_params - the authorization parameters, defaults to []

callback(config, params, strategy \\ __MODULE__)

@spec callback(Keyword.t(), map(), atom()) :: on_callback()

Callback phase for generating an access token and fetching user data.

Options

  • :access_token_url - the path or URL to fetch the access token from, optional, defaults to /access_token

  • :user_url - the path or URL to fetch user data, required

  • :session_params - the session parameters that were returned from authorize_url/1, required

request(config, token, method, url, params \\ [], headers \\ [])

@spec request(Keyword.t(), map(), atom(), binary(), map() | Keyword.t(), [
  {binary(), binary()}
]) ::
  {:ok, map()} | {:error, term()}

Performs a signed HTTP request to the API using the OAuth token.