Class: Sorcery::Providers::Wechat
- Includes:
- Sorcery::Protocols::Oauth2
- Defined in:
- lib/sorcery/providers/wechat.rb
Overview
This class adds support for OAuth with open.wx.qq.com.
config.wechat.key = <key>
config.wechat.secret = <secret>
...
Instance Attribute Summary collapse
-
#auth_url ⇒ Object
Returns the value of attribute auth_url.
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
-
#param_name ⇒ Object
readonly
Returns the value of attribute param_name.
-
#parse ⇒ Object
readonly
Returns the value of attribute parse.
-
#scope ⇒ Object
Returns the value of attribute scope.
-
#token_url ⇒ Object
Returns the value of attribute token_url.
-
#user_info_path ⇒ Object
Returns the value of attribute user_info_path.
Attributes inherited from Base
#access_token, #callback_url, #key, #original_callback_url, #secret, #site, #state, #user_info_mapping
Instance Method Summary collapse
- #authorize_url(options = {}) ⇒ Object
- #get_access_token(args, options = {}) ⇒ Object
- #get_user_hash(access_token) ⇒ Object
-
#initialize ⇒ Wechat
constructor
A new instance of Wechat.
- #login_url(_params, _session) ⇒ Object
- #process_callback(params, _session) ⇒ Object
Methods included from Sorcery::Protocols::Oauth2
Methods inherited from Base
#auth_hash, descendants, #has_callback?, name
Constructor Details
#initialize ⇒ Wechat
Returns a new instance of Wechat.
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/sorcery/providers/wechat.rb', line 15 def initialize super @scope = 'snsapi_login' @auth_url = 'https://open.weixin.qq.com/connect/qrconnect' @user_info_path = 'https://api.weixin.qq.com/sns/userinfo' @token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token' @state = SecureRandom.hex(16) @mode = :body @parse = :json @param_name = 'access_token' end |
Instance Attribute Details
#auth_url ⇒ Object
Returns the value of attribute auth_url.
13 14 15 |
# File 'lib/sorcery/providers/wechat.rb', line 13 def auth_url @auth_url end |
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
12 13 14 |
# File 'lib/sorcery/providers/wechat.rb', line 12 def mode @mode end |
#param_name ⇒ Object (readonly)
Returns the value of attribute param_name.
12 13 14 |
# File 'lib/sorcery/providers/wechat.rb', line 12 def param_name @param_name end |
#parse ⇒ Object (readonly)
Returns the value of attribute parse.
12 13 14 |
# File 'lib/sorcery/providers/wechat.rb', line 12 def parse @parse end |
#scope ⇒ Object
Returns the value of attribute scope.
13 14 15 |
# File 'lib/sorcery/providers/wechat.rb', line 13 def scope @scope end |
#token_url ⇒ Object
Returns the value of attribute token_url.
13 14 15 |
# File 'lib/sorcery/providers/wechat.rb', line 13 def token_url @token_url end |
#user_info_path ⇒ Object
Returns the value of attribute user_info_path.
13 14 15 |
# File 'lib/sorcery/providers/wechat.rb', line 13 def user_info_path @user_info_path end |
Instance Method Details
#authorize_url(options = {}) ⇒ Object
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/sorcery/providers/wechat.rb', line 28 def ( = {}) oauth_params = { appid: @key, redirect_uri: @callback_url, response_type: 'code', scope: scope, state: @state } "#{[:authorize_url]}?#{oauth_params.to_query}#wechat_redirect" end |
#get_access_token(args, options = {}) ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/sorcery/providers/wechat.rb', line 54 def get_access_token(args, = {}) client = build_client() client.auth_code.get_token( args[:code], { appid: @key, secret: @secret, parse: parse }, ) end |
#get_user_hash(access_token) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/sorcery/providers/wechat.rb', line 39 def get_user_hash(access_token) response = access_token.get( user_info_path, params: { access_token: access_token.token, openid: access_token.params['openid'] } ) {}.tap do |h| h[:user_info] = JSON.parse(response.body) h[:uid] = h[:user_info]['unionid'] end end |
#login_url(_params, _session) ⇒ Object
63 64 65 |
# File 'lib/sorcery/providers/wechat.rb', line 63 def login_url(_params, _session) authorize_url: auth_url end |
#process_callback(params, _session) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/sorcery/providers/wechat.rb', line 67 def process_callback(params, _session) args = {}.tap do |a| a[:code] = params[:code] if params[:code] end get_access_token( args, token_url: token_url, mode: mode, param_name: param_name ) end |