View Source Assent.Strategy.OAuth (Assent v0.2.10)

OAuth 1.0a strategy.

authorize_url/1 returns a map with a :session_params and :url key. The :session_params key carries a :oauth_token_secret value 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

config = [
  consumer_key: "REPLACE_WITH_CONSUMER_KEY",
  consumer_secret: "REPLACE_WITH_CONSUMER_SECRET",
  base_url: "https://auth.example.com",
  authorization_params: [scope: "user:read user:write"],
  user_url: "https://example.com/api/user"
]

{:ok, {url: url, session_params: session_params}} =
  config
  |> Assent.Config.put(:redirect_uri, "http://localhost:4000/auth/callback")
  |> OAuth.authorize_url()

{:ok, %{user: user, token: token}} =
  config
  |> Assent.Config.put(:session_params, session_params)
  |> OAuth.callback(params)

Summary

Functions

Generate authorization URL for request phase.

Callback phase for generating access token and fetch user data.

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

Functions

@spec authorize_url(Assent.Config.t()) ::
  {:ok, %{url: binary(), session_params: %{oauth_token_secret: binary()}}}
  | {:error, term()}

Generate authorization URL for request phase.

Configuration

  • :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 /oauth/request_token
  • :authorize_url - The path or URL for the OAuth server to redirect users to, defaults to /oauth/authenticate
  • :authorization_params - The authorization parameters, defaults to []
Link to this function

callback(config, params, strategy \\ __MODULE__)

View Source
@spec callback(Assent.Config.t(), map(), atom()) ::
  {:ok, %{user: map(), token: map()}} | {:error, term()}

Callback phase for generating access token and fetch user data.

Configuration

  • :access_token_url - The path or URL to fetch the access token from, optional, defaults to /oauth/access_token
  • :user_url - The path or URL to fetch user data, required
  • :session_params - The session parameters that was returned from authorize_url/1, optional
Link to this function

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

View Source
@spec request(Assent.Config.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.