To support a specific provider (like GitHub), create a module that uses OAuth2.Strategy. You must implement the strategy's public API (like authorize_url! and get_token!) and provide callbacks that delegate to the appropriate underlying OAuth2 logic.
defmodule GitHub do
use OAuth2.Strategy
# Public API
def client do
OAuth2.Client.new([
strategy: __MODULE__,
client_id: System.get_env("GITHUB_CLIENT_ID"),
client_secret: System.get_env("GITHUB_CLIENT_SECRET"),
redirect_uri: "http://myapp.com/auth/callback",
site: "https://api.github.com",
authorize_url: "https://github.com/login/oauth/authorize",
token_url: "https://github.com/login/oauth/access_token"
])
|> OAuth2.Client.put_serializer("application/json", Jason)
end
def authorize_url! do
OAuth2.Client.authorize_url!(client(), scope: "user,public_repo")
end
def get_token!(params \ [], headers \ [], opts \ []) do
OAuth2.Client.get_token!(client(), params, headers, opts)
end
# Strategy Callbacks
def authorize_url(client, params) do
OAuth2.Strategy.AuthCode.authorize_url(client, params)
end
def get_token(client, params, headers) do
client
|> put_header("accept", "application/json")
|> OAuth2.Strategy.AuthCode.get_token(params, headers)
end
end