class OmniAuth::Strategies::WeChatService

Public Instance Methods

authorize_params() click to toggle source
# File lib/omniauth/strategies/wechat_service.rb, line 41
def authorize_params
  params = options.authorize_params.merge({
    appid: options.client_id,
    redirect_uri: callback_url,
    response_type: 'code',
    state: request.params['state'],
    scope: request.params['scope'] || 'snsapi_userinfo'
  })
  if OmniAuth.config.test_mode
    @env ||= {}
    @env["rack.session"] ||= {}
  end
  unless options.provider_ignores_state
    params[:state] = SecureRandom.hex(24)
    session["omniauth.state"] = params[:state]
  end
  params
end
callback_phase() click to toggle source
# File lib/omniauth/strategies/wechat_service.rb, line 64
def callback_phase
  # 其它 app 需要获取微信openid 时,将 callbackurl 传入 state 参数
  if request.params['state'].match(/\Ahttps?:\/\/(.*\.)?geekpark\.net\/.*\z/)
    uri = URI(request.params['state'])
    query = Rack::Utils.parse_nested_query(uri.query).merge(code: request.params['code'])
    uri.query = URI.encode_www_form query
    env['omniauth.redirect'] = uri.to_s
    call_app!
  else
    super
  end
end
raw_info() click to toggle source
# File lib/omniauth/strategies/wechat_service.rb, line 23
def raw_info
  @raw_info ||= begin
                  response = access_token.get(
                    '/sns/userinfo',
                    { params: { access_token: access_token.token,
                                openid: access_token['openid'],
                                lang: 'zh-CN' },
                                parse: :json }
                  ).parsed
                  log :debug, response
                  response
                end
end
request_phase() click to toggle source
# File lib/omniauth/strategies/wechat_service.rb, line 37
def request_phase
  redirect client.authorize_url(authorize_params)+'#wechat_redirect'
end
token_params() click to toggle source
# File lib/omniauth/strategies/wechat_service.rb, line 60
def token_params
  { appid: options.client_id, secret: options.client_secret }
end

Protected Instance Methods

build_access_token() click to toggle source
# File lib/omniauth/strategies/wechat_service.rb, line 79
def build_access_token
  request_params = {
    appid: options.client_id,
    secret: options.client_secret,
    code: request.params['code'],
    grant_type: 'authorization_code',
    parse: :json
  }
  client.get_token(request_params, {mode: :query})
end