View Source WeChat.Plug.OAuth2Checker (wechat v0.16.0)
网页授权
工作流程:
- 检查
session
, 判断是否已经有授权,有则继续执行后续的 plug, 没有则跳到步骤 2; - 检查
query
是否有code
, 有则执行oauth2_callback_fun
函数, 没有则跳到步骤 3; - 执行
authorize_url_fun
函数,跳转到authorize_url
.
可选参数:
:oauth2_callback_fun
: 定义, 默认值: &oauth2_callback/5:authorize_url_fun
: 定义, 默认值: &authorize_url_by_server_role/4
注意 : 服务器角色为 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
@type oauth2_callback_fun() :: (oauth2_callback_type(), Plug.Conn.t(), WeChat.WebPage.code(), WeChat.client(), WeChat.Work.Agent.t() -> Plug.Conn.t())
授权回调处理函数
@type oauth2_callback_type() :: :normal | :work
Functions
@spec auth_fail(Plug.Conn.t()) :: Plug.Conn.t()
@spec auth_success(Plug.Conn.t(), WeChat.client(), info :: map()) :: Plug.Conn.t()
@spec authorize_url( authorize_url_type(), Plug.Conn.t(), WeChat.client(), WeChat.Work.Agent.t() ) :: Plug.Conn.t()
@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()
@spec check_code( Plug.Conn.t(), options :: map(), oauth2_callback_type(), WeChat.client(), WeChat.Work.Agent.t() ) :: Plug.Conn.t()
@spec hub_springboard_authorize_url( authorize_url_type(), Plug.Conn.t(), WeChat.client(), WeChat.Work.Agent.t() ) :: Plug.Conn.t()
@spec hub_springboard_callback_uri(Plug.Conn.t(), hub_springboard_url :: String.t()) :: uri :: String.t()
@spec oauth2_callback( oauth2_callback_type(), Plug.Conn.t(), code :: String.t(), WeChat.client(), WeChat.Work.Agent.t() ) :: Plug.Conn.t()