├── .gitignore ├── Gemfile ├── README.md ├── Rakefile ├── lib ├── omniauth-open_wechat.rb ├── omniauth-open_wechat │ └── version.rb └── omniauth │ └── strategies │ └── open_wechat.rb └── omniauth-open_wechat.gemspec /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | *.rbc 3 | .bundle 4 | .config 5 | .yardoc 6 | Gemfile.lock 7 | InstalledFiles 8 | _yardoc 9 | coverage 10 | doc/ 11 | lib/bundler/man 12 | /pkg 13 | rdoc 14 | tmp 15 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | # Specify your gem's dependencies in omniauth-open_wechat.gemspec 4 | gemspec 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # OmniAuth OpenWechat OAuth2 2 | 3 | Read Wechat OAuth2 docs for more details: [微信开放平台开发指南](https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&lang=zh_CN) 4 | 5 | ## Installation 6 | 7 | Add this line to your application's Gemfile: 8 | 9 | ```ruby 10 | gem 'omniauth-open_wechat' 11 | ``` 12 | 13 | And then execute: 14 | 15 | $ bundle 16 | 17 | Or install it yourself as: 18 | 19 | $ gem install omniauth-open_wechat 20 | 21 | ## Usage 22 | 23 | Here's a quick example, adding the middleware to a Rails app in 24 | `config/initializers/omniauth.rb:` 25 | 26 | ``` 27 | Rails.application.config.middleware.use OmniAuth::Builder do 28 | provider :open_wechat, ENV['OPEN_WECHAT_KEY'], ENV['OPEN_WECHAT_SECRET'], :scope => 'snsapi_login' 29 | end 30 | ``` 31 | 32 | ## Current Version 33 | 34 | `0.0.4` 35 | 36 | ## Authentication Hash 37 | 38 | Here's an example Authentication Hash available in `request.env['omniauth.auth']` : 39 | 40 | ``` 41 | { 42 | "provider"=>"open_wechat", 43 | "uid"=>"xxx...", 44 | "info"=>{ 45 | "nickname"=>"free", 46 | "sex"=>1, 47 | "province"=>"", 48 | "city"=>"", 49 | "country"=>"CN", 50 | "headimgurl"=>"http://wx.qlogo.cn/mmopen/574VdhMFwaGdhhGuGWZicMYibnBSnzYSU8S6c3mJrqneYpm1YmGkBjHX5T9xj4TdeRWfHPmXORTqIt7F0G2y4TJA/0", 51 | "name"=>"free" 52 | }, 53 | "credentials"=>{ 54 | "token"=>"xxx...", 55 | "refresh_token"=>"xxx...", 56 | "expires_at"=>2015-06-04 16:17:26 +0800, 57 | "expires"=>true 58 | }, 59 | "extra"=>{ 60 | "raw_info"=>{ 61 | "openid"=>"xxx...", 62 | "nickname"=>"free", 63 | "sex"=>1, 64 | "language"=>"zh_CN", 65 | "city"=>"", 66 | "province"=>"", 67 | "country"=>"CN", 68 | "headimgurl"=>"http://wx.qlogo.cn/mmopen/574VdhMFwaGdhhGuGWZicMYibnBSnzYSU8S6c3mJrqneYpm1YmGkBjHX5T9xj4TdeRWfHPmXORTqIt7F0G2y4TJA/0", 69 | "privilege"=>[], 70 | "unionid"=>"xxx..." 71 | } 72 | } 73 | } 74 | ``` 75 | 76 | ## Contributing 77 | 78 | 1. Fork it ( https://github.com/[my-github-username]/omniauth-open_wechat/fork ) 79 | 2. Create your feature branch (`git checkout -b my-new-feature`) 80 | 3. Commit your changes (`git commit -am 'Add some feature'`) 81 | 4. Push to the branch (`git push origin my-new-feature`) 82 | 5. Create a new Pull Request 83 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require "bundler/gem_tasks" 2 | 3 | -------------------------------------------------------------------------------- /lib/omniauth-open_wechat.rb: -------------------------------------------------------------------------------- 1 | # require "omniauth-open_wechat/version" 2 | require 'omniauth/strategies/open_wechat' 3 | -------------------------------------------------------------------------------- /lib/omniauth-open_wechat/version.rb: -------------------------------------------------------------------------------- 1 | module OmniAuth 2 | module OpenWechat 3 | VERSION = "0.0.4" 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /lib/omniauth/strategies/open_wechat.rb: -------------------------------------------------------------------------------- 1 | require 'omniauth-oauth2' 2 | 3 | module OmniAuth 4 | module Strategies 5 | class OpenWechat < OmniAuth::Strategies::OAuth2 6 | 7 | option :name, "open_wechat" 8 | 9 | option :client_options, { 10 | site: "https://api.weixin.qq.com", 11 | authorize_url: "https://open.weixin.qq.com/connect/qrconnect#wechat_redirect", 12 | token_url: "/sns/oauth2/access_token", 13 | token_method: :get 14 | } 15 | 16 | option :token_params, { 17 | :parse => :json 18 | } 19 | 20 | uid do 21 | raw_info['openid'] 22 | end 23 | 24 | info do 25 | { 26 | nickname: raw_info['nickname'], 27 | sex: raw_info['sex'], 28 | province: raw_info['province'], 29 | city: raw_info['city'], 30 | country: raw_info['country'], 31 | headimgurl: raw_info['headimgurl'] 32 | } 33 | end 34 | 35 | extra do 36 | {raw_info: raw_info} 37 | end 38 | 39 | def request_phase 40 | params = client.auth_code.authorize_params.merge(redirect_uri: callback_url).merge(authorize_params) 41 | params["appid"] = params.delete("client_id") 42 | redirect client.authorize_url(params) 43 | end 44 | 45 | def raw_info 46 | @uid ||= access_token["openid"] 47 | access_token.options[:mode] = :query 48 | access_token.options[:param_name] = 'access_token' 49 | @raw_info ||= JSON access_token.get("/sns/userinfo", :params => {"openid" => @uid}).parsed 50 | end 51 | 52 | protected 53 | 54 | def build_access_token 55 | params = { 56 | 'appid' => client.id, 57 | 'secret' => client.secret, 58 | 'code' => request.params['code'], 59 | 'grant_type' => 'authorization_code', 60 | :parse => :json 61 | } 62 | client.get_token(params) 63 | end 64 | 65 | end 66 | end 67 | end -------------------------------------------------------------------------------- /omniauth-open_wechat.gemspec: -------------------------------------------------------------------------------- 1 | # -*- encoding: utf-8 -*- 2 | require File.expand_path('../lib/omniauth-open_wechat/version', __FILE__) 3 | 4 | Gem::Specification.new do |gem| 5 | gem.authors = ["free1"] 6 | gem.email = ["747549945@qq.com"] 7 | gem.description = %q{OmniAuth strategy for OpenWechat.} 8 | gem.summary = %q{OmniAuth strategy for OpenWechat.} 9 | gem.homepage = "https://github.com/free1/omniauth-open_wechat" 10 | gem.license = "MIT" 11 | 12 | # gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } 13 | gem.files = `git ls-files`.split("\n") 14 | gem.name = "omniauth-open_wechat" 15 | gem.require_paths = ["lib"] 16 | gem.version = OmniAuth::OpenWechat::VERSION 17 | 18 | gem.add_dependency 'omniauth', '~> 1.0' 19 | gem.add_dependency 'omniauth-oauth2', '>= 1.1.1', '< 2.0' 20 | # gem.add_development_dependency 'rspec', '~> 2.7' 21 | # gem.add_development_dependency 'rack-test' 22 | # gem.add_development_dependency 'simplecov' 23 | # gem.add_development_dependency 'webmock' 24 | end 25 | --------------------------------------------------------------------------------