Class: Sorcery::Providers::Wechat

Inherits:
Base
  • Object
show all
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

Attributes inherited from Base

#access_token, #callback_url, #key, #original_callback_url, #secret, #site, #state, #user_info_mapping

Instance Method Summary collapse

Methods included from Sorcery::Protocols::Oauth2

#build_client, #oauth_version

Methods inherited from Base

#auth_hash, descendants, #has_callback?, name

Constructor Details

#initializeWechat

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_urlObject

Returns the value of attribute auth_url.



13
14
15
# File 'lib/sorcery/providers/wechat.rb', line 13

def auth_url
  @auth_url
end

#modeObject (readonly)

Returns the value of attribute mode.



12
13
14
# File 'lib/sorcery/providers/wechat.rb', line 12

def mode
  @mode
end

#param_nameObject (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

#parseObject (readonly)

Returns the value of attribute parse.



12
13
14
# File 'lib/sorcery/providers/wechat.rb', line 12

def parse
  @parse
end

#scopeObject

Returns the value of attribute scope.



13
14
15
# File 'lib/sorcery/providers/wechat.rb', line 13

def scope
  @scope
end

#token_urlObject

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_pathObject

Returns the value of attribute user_info_path.



13
14
15
# File 'lib/sorcery/providers/wechat.rb', line 13

def 
  @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 authorize_url(options = {})
  oauth_params = {
    appid: @key,
    redirect_uri: @callback_url,
    response_type: 'code',
    scope: scope,
    state: @state
  }
  "#{options[: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, options = {})
  client = build_client(options)
  client.auth_code.get_token(
    args[:code],
    { appid: @key, secret: @secret, parse: parse },
    options
  )
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(
    ,
    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 (_params, _session)
  authorize_url 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