xoauth 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fa1e3c393c54802feb8922d6cd6501eba8bde3ce
4
+ data.tar.gz: ef307ed7a4874805281cad24fdda6fde192feb9c
5
+ SHA512:
6
+ metadata.gz: 70ef44a8407263c1d55cd3b311da7f169950a7939c7adfbebd1ba7eb23dc2b3270fbfa8f149276d14aaa347593109ff1c5694bdebf9bee61ad063fa046cbd89a
7
+ data.tar.gz: 2c797cec63abfeff48965cb6ca30e280f3cd6250718841fbeb1548a3a92cc07e91ae7854b1d447ee2e703b435bd41a0b6292aecbf23d129710d3a032997749f5
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in oauth.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 binz
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # Oauth
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'oauth'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install oauth
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/oauth/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,25 @@
1
+ module Mongoid
2
+ module Xoauth
3
+ extend ActiveSupport::Concern
4
+ included do
5
+ index "oauths.uid" => 1, "oauths._type" => 1
6
+
7
+ def find_by_oauth_uid(uid, klass)
8
+ where('oauths.uid' => uid.to_s, 'oauths._type' => klass.to_s).first
9
+ end
10
+
11
+ def oauth(klass)
12
+ self.oauths.find_by(_type: klass.to_s) # "Oauth::#{name.to_s.capitalize}"
13
+ end
14
+ end
15
+
16
+ module ClassMethods
17
+ def xoauth(params) # weibo_web/mobile: {appid, secret, callback}, qq: {key, secret}
18
+ raise "invalid coauth params #{params.to_s}" unless params.is_a?(Hash)
19
+ embeds_many :oauths, class_name: 'Oauth::Provider', as: :oauthable
20
+ # config oauths
21
+ params.each_pair{|key, param| Oauth::Configure[key.to_s] = param}
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,17 @@
1
+ module Oauth
2
+ class Configure
3
+ @@value = {}
4
+
5
+ def self.[]=(key, value)
6
+ @@value[key] = value
7
+ end
8
+
9
+ def self.[](key)
10
+ @@value[key]
11
+ end
12
+
13
+ def self.value
14
+ @@value
15
+ end
16
+ end
17
+ end
data/lib/oauth/info.rb ADDED
@@ -0,0 +1,8 @@
1
+ module Oauth
2
+ class Info
3
+ include Mongoid::Document
4
+ belongs_to :oauth, class_name: 'Oauth::Provider', inverse_of: :info
5
+
6
+ field :data, type: Hash, default: {}
7
+ end
8
+ end
@@ -0,0 +1,64 @@
1
+ module Oauth
2
+ class QQ < Provider
3
+
4
+ def fetch_info
5
+ api_access('get_user_info')
6
+ end
7
+
8
+ def api_access(api, http_params = {}, http_method = 'get')
9
+ url = 'https://graph.qq.com/user/' + api
10
+ http_params.merge!({"access_token" => access_token, "openid"=> uid, "oauth_consumer_key" => Configure['qq']['appid']})
11
+ Oauth::QQ.request(url, http_params, http_method, 'json')
12
+ end
13
+
14
+ class << self
15
+ def authenticate?(access_token, uid)
16
+ openid = self.openid(access_token)
17
+ openid && (openid == uid)
18
+ end
19
+
20
+ def openid(access_token)
21
+ str = get('https://graph.qq.com/oauth2.0/me', {access_token: access_token})
22
+ if str
23
+ str.match(%r{"openid":"([A-z0-9]{32})"}).try(:[], 1)
24
+ end
25
+ end
26
+
27
+ def authorize_url
28
+ get_params = {
29
+ response_type: 'code',
30
+ client_id: Configure['qq']['appid'],
31
+ redirect_uri: Configure['qq']['callback'],
32
+ state: 1,
33
+ scope: 'get_user_info,get_info,do_like'
34
+ }
35
+ "https://graph.qq.com/oauth2.0/authorize?#{URI.encode_www_form(get_params)}"
36
+ end
37
+
38
+ def detail_of_code(code)
39
+ get_params = {
40
+ grant_type: 'authorization_code',
41
+ client_id: Configure['qq']['appid'],
42
+ client_secret: Configure['qq']['secret'],
43
+ code: code,
44
+ redirect_uri: Configure['qq']['callback']
45
+ }
46
+ response = getJSON('https://graph.qq.com/oauth2.0/token', get_params)
47
+ if response # access_token=xxx&expires_in=7776000&refresh_token=xxx
48
+ detail = Hash[response.split('&').map{|q| q.split('=')}]
49
+ detail["created_at"] = Time.now
50
+ detail['uid'] = openid(detail["access_token"])
51
+ if detail['uid']
52
+ detail
53
+ else
54
+ nil
55
+ end
56
+ else
57
+ nil
58
+ end
59
+ end
60
+ end
61
+
62
+ end
63
+ class QQMobile < QQ; end
64
+ end
@@ -0,0 +1,72 @@
1
+ module Oauth
2
+ class Weibo < Provider
3
+
4
+ def follow(uid = nil)
5
+ uid = Configure['weibo']['official'] if uid.nil?
6
+ api_access('friendships/create', {'uid'=> uid}, 'post')
7
+ end
8
+
9
+ def publish(content)
10
+ api_access('statuses/update', {'status'=>content}, 'post')
11
+ end
12
+
13
+ def fetch_info
14
+ api_access('users/show',{'uid' => uid})
15
+ end
16
+
17
+ def api_access(api, http_params, http_method = 'get')
18
+ return nil if expired? # expired
19
+ url = 'https://api.weibo.com/2/' + api + '.json'
20
+ http_params.merge!({"access_token" => access_token})
21
+ Oauth::Weibo.request(url, http_params, http_method, 'json')
22
+ end
23
+
24
+ def refresh
25
+ info = Oauth::Weibo.postJSON('https://api.weibo.com/oauth2/get_token_info', {access_token: access_token})
26
+ if info
27
+ self.created_at = info["create_at"]
28
+ user.save!
29
+ end
30
+ end
31
+
32
+ class << self
33
+ def self.authenticate?(access_token, uid)
34
+ result = postJSON('https://api.weibo.com/oauth2/get_token_info', {access_token: access_token})
35
+ if result.try(:[], 'uid').to_i == uid.to_i
36
+ uid.to_i > 0
37
+ else
38
+ false
39
+ end
40
+ end
41
+
42
+ def authorize_url
43
+ get_params = {
44
+ 'client_id' => Configure['weibo']['appid'],
45
+ 'redirect_uri' => Configure['weibo']['callback'],
46
+ 'response_type' => 'code',
47
+ 'display' => 'default' # for different divice, default|mobile|wap|client|apponweibo
48
+ }
49
+ "https://api.weibo.com/oauth2/authorize?#{URI.encode_www_form(get_params)}";
50
+ end
51
+
52
+ def detail_of_code(code)
53
+ url = 'https://api.weibo.com/oauth2/access_token'
54
+ post_params = {
55
+ 'client_id' => Configure['weibo']['appid'],
56
+ 'client_secret' => Configure['weibo']['secret'],
57
+ 'grant_type' => 'authorization_code',
58
+ 'code' => code,
59
+ 'redirect_uri' => Configure['weibo']['callback']
60
+ }
61
+ response = postJSON(url,post_params)
62
+ if response
63
+ response["created_at"] = Time.now
64
+ response.delete('remind_in')
65
+ end
66
+ response
67
+ end
68
+ end
69
+ end
70
+
71
+ class WeiboMobile < Weibo; end
72
+ end
@@ -0,0 +1,4 @@
1
+ module Oauth
2
+ class Weixin < Provider
3
+ end
4
+ end
@@ -0,0 +1,65 @@
1
+ require 'net/http'
2
+ require 'net/https'
3
+ require 'uri'
4
+ require 'json'
5
+
6
+ module Oauth
7
+ class Provider
8
+ include Mongoid::Document
9
+ field :access_token
10
+ field :uid, type: String
11
+ field :created_at, type: Time
12
+ field :expires_in, type: Integer
13
+ field :refresh_token
14
+
15
+ embedded_in :oauthable, polymorphic: true, inverse_of: :oauth
16
+ has_one :info, class_name: 'Oauth::Info', inverse_of: :oauth
17
+
18
+ def fetch_info!
19
+ return nil if expired?
20
+ info = fetch_info
21
+ if info
22
+ self.info = Info.new(data: info)
23
+ self.save
24
+ end
25
+ self.info
26
+ end
27
+
28
+ def expired?
29
+ created_at + expires_in < Time.now + 10
30
+ end
31
+
32
+ class << self
33
+ def type
34
+ self.to_s.split("::").last.downcase
35
+ end
36
+ # get/post/getJSON/postJSON
37
+ ['get', 'post'].product([nil, 'json']).each do |method, format|
38
+ define_method "#{method}#{format && format.upcase}", ->(url, params){ request(url, params, method, format) }
39
+ end
40
+
41
+ def request(url, request_params, method = 'get', format = nil)
42
+ uri = URI(url)
43
+ klass = (uri.scheme == 'https' ? Net::HTTPS : Net::HTTP)
44
+ begin
45
+ if method == 'get'
46
+ uri.query = (uri.query.nil? ? '' : (uri.query + "&")) + URI.encode_www_form(request_params)
47
+ response = klass.get_response(uri)
48
+ else
49
+ response = klass.post_form(uri, request_params)
50
+ end
51
+ rescue Exception => e
52
+ puts e.message
53
+ return nil
54
+ end
55
+ if response.kind_of? Net::HTTPSuccess
56
+ format == 'json' ? JSON.parse(response.body) : response.body
57
+ else
58
+ nil
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+
65
+ Dir[File.dirname(__FILE__) + '/provider/*.rb'].each {|file| require file }
@@ -0,0 +1,3 @@
1
+ module Oauth
2
+ VERSION = "0.1.0"
3
+ end
data/lib/oauth.rb ADDED
@@ -0,0 +1,7 @@
1
+ require "oauth/version"
2
+ require "oauth/provider"
3
+ require "oauth/configure"
4
+
5
+ module Oauth
6
+ # Your code goes here...
7
+ end
data/oauth.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'oauth/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "xoauth"
8
+ spec.version = Oauth::VERSION
9
+ spec.authors = ["binz"]
10
+ spec.email = ["xinkiang@gmail.com"]
11
+ spec.summary = %q{Chinese Social Network Oauth2.0 Implemetation.}
12
+ spec.description = %q{Chinese Social Network Oauth2.0 Implemetation.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+
24
+ spec.add_runtime_dependency "mongoid"
25
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: xoauth
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - binz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: mongoid
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Chinese Social Network Oauth2.0 Implemetation.
56
+ email:
57
+ - xinkiang@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - lib/mongoid/xoauth.rb
68
+ - lib/oauth.rb
69
+ - lib/oauth/configure.rb
70
+ - lib/oauth/info.rb
71
+ - lib/oauth/provider.rb
72
+ - lib/oauth/provider/qq.rb
73
+ - lib/oauth/provider/weibo.rb
74
+ - lib/oauth/provider/weixin.rb
75
+ - lib/oauth/version.rb
76
+ - oauth.gemspec
77
+ homepage: ''
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.2.2
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Chinese Social Network Oauth2.0 Implemetation.
101
+ test_files: []