omniauth-yhd-oauth2 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2a209c7aa6dad7b5bdf48e196a59e45358ffad7e
4
+ data.tar.gz: 191910d35c9a3b475fa9724af21981e1fcc9e130
5
+ SHA512:
6
+ metadata.gz: 955dabf9d387d71725b2b84761f5a4e64f6f0f34d5fe5f2c880ffc4f438a64fd093d728667863956ee02710e29b3d6a6983975c9f6f607a2c59bab314cfc0da9
7
+ data.tar.gz: 0511747763d3eaf727f5a80174ad673a7355db451af7e18079c01da713d7b5a7bfe7564128313a03d5b1087f92e86c5d8f93fbcca8e24b1ccc8194c71f793a98
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in omniauth-yhd-oauth2.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Howl王
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,69 @@
1
+ # OmniAuth Yhd OAuth2
2
+
3
+ Yhd OAuth2 Strategy for OmniAuth 1.0.
4
+
5
+ Read Yhd OAuth2 docs for more details: http://open.yhd.com/opendoc.do
6
+
7
+ ## Installing
8
+
9
+ Add to your `Gemfile`:
10
+
11
+ ```ruby
12
+ gem 'omniauth-yhd-oauth2'
13
+ ```
14
+
15
+ Then `bundle install`.
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install omniauth-yhd-oauth2
20
+
21
+ ## Usage
22
+
23
+ `OmniAuth::Strategies::Yhd` is simply a Rack middleware. Read the OmniAuth 1.0 docs for detailed instructions: https://github.com/intridea/omniauth.
24
+
25
+ Here's a quick example, adding the middleware to a Rails app in `config/initializers/omniauth.rb`:
26
+
27
+ ```ruby
28
+ Rails.application.config.middleware.use OmniAuth::Builder do
29
+ provider :yhd, ENV['YHD_KEY'], ENV['YHD_SECRET']
30
+ end
31
+ ```
32
+
33
+ ## Authentication Hash
34
+
35
+ Here's an example *Authentication Hash* available in `request.env['omniauth.auth']`:
36
+
37
+ ```ruby
38
+ {
39
+ "provider" => "yhd",
40
+ "uid" => xxxxx,
41
+ "info" => {
42
+ "nickname" => "xxxxx旗舰店"
43
+ },
44
+ "credentials" => {
45
+ "token" => "f841875xxxxxxxxx81509d",
46
+ "expires_at" => xxxxxxxxx,
47
+ "expires" => true
48
+ },
49
+ "extra" => {
50
+ "raw_info" => {
51
+ "storeId" => xxxxxxxxx,
52
+ "storeName" => "xxxxxxxx旗舰店",
53
+ "storeAddress" => "http://shop.yhd.com/html/xxxxx",
54
+ "storeOpenTime" => "2xxx-xx-03 17:xx:13",
55
+ "storeCategoryCode" => xxx,
56
+ "storeNickName" => ""
57
+ }
58
+ }
59
+ }
60
+ ```
61
+ *PS.* Built and tested on MRI Ruby 1.9.3
62
+
63
+ ## Contributing
64
+
65
+ 1. Fork it
66
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
67
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
68
+ 4. Push to the branch (`git push origin my-new-feature`)
69
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ module OAuth2
4
+ class YhdClient < Client
5
+ def get_token(params, access_token_opts = {}, access_token_class = AccessToken)
6
+ opts = {:raise_errors => options[:raise_errors], :parse => params.delete(:parse)}
7
+ if options[:token_method] == :post
8
+ headers = params.delete(:headers)
9
+ opts[:body] = params
10
+ opts[:headers] = {'Content-Type' => 'application/x-www-form-urlencoded'}
11
+ opts[:headers].merge!(headers) if headers
12
+ else
13
+ opts[:params] = params
14
+ end
15
+ response = request(options[:token_method], token_url, opts)
16
+ error = Error.new(response)
17
+ fail(error) if options[:raise_errors] && !(response.parsed.is_a?(Hash) && response.parsed['accessToken'])
18
+ yhd_hash = response.parsed
19
+ puts yhd_hash
20
+ new_hash = {
21
+ access_token: yhd_hash['accessToken'],
22
+ expires_in: yhd_hash['expiresIn'].to_i/1000
23
+ }
24
+ puts new_hash
25
+ puts '_'*88
26
+ access_token_class.from_hash(self, new_hash.merge(access_token_opts))
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,5 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'omniauth-yhd-oauth2/version'
3
+ require 'oauth2/yhd_client'
4
+ require 'typhoeus/adapters/faraday'
5
+ require 'omniauth/strategies/yhd'
@@ -0,0 +1,6 @@
1
+ # -*- encoding: utf-8 -*-
2
+ module Omniauth
3
+ module YhdOauth2
4
+ VERSION = "0.0.1"
5
+ end
6
+ end
@@ -0,0 +1,76 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'omniauth-oauth2'
3
+
4
+ module OmniAuth
5
+ module Strategies
6
+ class Yhd < OmniAuth::Strategies::OAuth2
7
+ option :name, 'yhd'
8
+ option :client_options, {
9
+ site: "https://member.yhd.com", #
10
+ authorize_url: "/login/authorize.do",
11
+ token_url: "/login/token.do"
12
+ }
13
+
14
+ option :token_params, {
15
+ parse: :json
16
+ }
17
+
18
+ uid do
19
+ raw_info['storeId']
20
+ end
21
+
22
+ info do
23
+ {
24
+ nickname: raw_info['storeName']
25
+ }
26
+ end
27
+
28
+ extra do
29
+ {
30
+ raw_info: raw_info
31
+ }
32
+ end
33
+
34
+ def client
35
+ ::OAuth2::YhdClient.new(options.client_id, options.client_secret, deep_symbolize(options.client_options))
36
+ end
37
+
38
+ def raw_info
39
+ access_token.options[:mode] = :query
40
+ @raw_info ||= ::OAuth2::Response.new(conn.post('/app/api/rest/router', signed_params), parse: :json).parsed['response']['storeMerchantStoreInfo'] rescue {}
41
+ end
42
+
43
+ private
44
+
45
+ def conn
46
+ @conn ||= Faraday.new(url: "http://openapi.yhd.com") do |b|
47
+ b.response :logger
48
+ b.adapter :typhoeus
49
+ end
50
+ end
51
+
52
+ def params
53
+ {
54
+ method: 'yhd.store.get',
55
+ sessionKey: access_token.token,
56
+ appKey: client.id,
57
+ timestamp: timestamp,
58
+ format: 'json',
59
+ ver: '1.0'
60
+ }
61
+ end
62
+
63
+ def signed_params
64
+ rand = client.secret + params.sort.flatten.join + client.secret
65
+ token = Digest::MD5.hexdigest(rand)
66
+ params.merge(sign: token)# 數字簽名
67
+ end
68
+
69
+ def timestamp # 时间戳
70
+ Time.now.strftime("%Y-%m-%d %H:%M:%S")
71
+ end
72
+ end
73
+ end
74
+ end
75
+
76
+ OmniAuth.config.add_camelization "yhd", "Yhd"
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'omniauth-yhd-oauth2/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "omniauth-yhd-oauth2"
8
+ spec.version = Omniauth::YhdOauth2::VERSION
9
+ spec.authors = ["Howl王"]
10
+ spec.email = ["mimosa@shareday.com"]
11
+ spec.description = %q{OmniAuth Oauth2 strategy for yhd.com.}
12
+ spec.summary = %q{OmniAuth Oauth2 strategy for yhd.com.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
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_runtime_dependency "typhoeus"
22
+ spec.add_development_dependency "bundler", "~> 1.3"
23
+ spec.add_development_dependency "rake"
24
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: omniauth-yhd-oauth2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Howl王
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: typhoeus
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: OmniAuth Oauth2 strategy for yhd.com.
56
+ email:
57
+ - mimosa@shareday.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/oauth2/yhd_client.rb
68
+ - lib/omniauth-yhd-oauth2.rb
69
+ - lib/omniauth-yhd-oauth2/version.rb
70
+ - lib/omniauth/strategies/yhd.rb
71
+ - omniauth-yhd-oauth2.gemspec
72
+ homepage: ''
73
+ licenses:
74
+ - MIT
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.3.0
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: OmniAuth Oauth2 strategy for yhd.com.
96
+ test_files: []