View Source WeChat.Plug.OAuth2Checker (wechat v0.16.0)

网页授权

官方文档

工作流程:

  1. 检查 session, 判断是否已经有授权,有则继续执行后续的 plug, 没有则跳到步骤 2;
  2. 检查 query 是否有 code, 有则执行 oauth2_callback_fun 函数, 没有则跳到步骤 3;
  3. 执行 authorize_url_fun 函数,跳转到 authorize_url.

可选参数:

注意 : 服务器角色为 hub_client 时,请确保已经配置 hub_springboard_url:

WeChat.HubClient.set_hub_springboard_url(Client, "https://wx.example.com")

Usage

将下面的代码加到 router 里面:

pipeline :oauth2_checker do
  plug WeChat.Plug.OAuth2Checker, clients: [Client, ...]
end

# for 公众号
scope "/:app" do
  pipe_through :oauth2_checker
  get "/path", YourController, :your_action
end

# for 企业微信
scope "/:app/:agent" do
  pipe_through :oauth2_checker
  # 在PC端授权访问网页
  get "/:qr/your_path", YourController, :your_action
  # 在企业微信内授权访问网页
  get "/your_path", YourController, :your_action
end

Summary

Types

@type authorize_url_fun() :: (authorize_url_type(),
                        Plug.Conn.t(),
                        WeChat.client(),
                        WeChat.Work.Agent.t() ->
                          authorize_url :: String.t())

获取授权链接函数

@type authorize_url_type() :: :normal | :work | :work_qr

授权回调处理函数

Link to this type

oauth2_callback_type()

View Source
@type oauth2_callback_type() :: :normal | :work

Functions

@spec auth_fail(Plug.Conn.t()) :: Plug.Conn.t()
Link to this function

auth_success(conn, client, info)

View Source
@spec auth_success(Plug.Conn.t(), WeChat.client(), info :: map()) :: Plug.Conn.t()
Link to this function

auth_success(conn, client, agent, info)

View Source
Link to this function

authorize_url(type, conn, client, agent)

View Source
Link to this function

authorize_url_by_server_role(type, conn, client, agent)

View Source
@spec authorize_url_by_server_role(
  authorize_url_type(),
  Plug.Conn.t(),
  WeChat.client(),
  WeChat.Work.Agent.t()
) :: Plug.Conn.t()
@spec callback_uri(Plug.Conn.t()) :: uri :: String.t()
Link to this function

check_code(conn, options, type, client, agent)

View Source
@spec check_code(
  Plug.Conn.t(),
  options :: map(),
  oauth2_callback_type(),
  WeChat.client(),
  WeChat.Work.Agent.t()
) :: Plug.Conn.t()
Link to this function

hub_springboard_authorize_url(type, conn, client, agent)

View Source
@spec hub_springboard_authorize_url(
  authorize_url_type(),
  Plug.Conn.t(),
  WeChat.client(),
  WeChat.Work.Agent.t()
) :: Plug.Conn.t()
Link to this function

hub_springboard_callback_uri(conn, hub_springboard_url)

View Source
@spec hub_springboard_callback_uri(Plug.Conn.t(), hub_springboard_url :: String.t()) ::
  uri :: String.t()
Link to this function

oauth2_callback(type, conn, code, client, agent)

View Source
@spec oauth2_callback(
  oauth2_callback_type(),
  Plug.Conn.t(),
  code :: String.t(),
  WeChat.client(),
  WeChat.Work.Agent.t()
) :: Plug.Conn.t()