iauth 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +1 -0
  3. data/README.md +33 -0
  4. data/iauth.gemspec +17 -0
  5. data/lib/iauth.rb +126 -0
  6. metadata +61 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ddb33e554d1940ba4a3847bcc81d29320cbb840a
4
+ data.tar.gz: c6ef5a41df8de9d43f04ac4dd48b671aa448c1a8
5
+ SHA512:
6
+ metadata.gz: f888fdf96c9d0b7b270b150d588b7d27c5de627be51ad230e49ee44c715c3b6ce348ea2ac2c693e90fef58bcdb6bd39c73d5ff1d4c675e03ddb06049990a1156
7
+ data.tar.gz: 27d0f3c5e376ecad3cdce08a78a44f6377b81318185d6cab2aae0c7df30d5d44be074911a336391d859d306dafd4d3e54d9323dff48b76d4bac00d3ae7e2553f
@@ -0,0 +1 @@
1
+ *.gem
@@ -0,0 +1,33 @@
1
+ 北航 ihome 社区第三方验证系统 IAuth Ruby SDK
2
+ ============================================
3
+
4
+ # Install(Gem)
5
+
6
+ ```bash
7
+ gem install iauth
8
+ ```
9
+
10
+ # Install(Bundler)
11
+
12
+ ```bash
13
+ echo "gem 'iauth'" >> Gemfile
14
+ ```
15
+
16
+ # Usage
17
+
18
+ ```ruby
19
+ require 'iauth'
20
+ require 'securerandom'
21
+ iauth = IAuth.new 'your app id here', 'your app secret here'
22
+ state = SecureRandom 8
23
+ login_url = iauth.login_url state
24
+
25
+ # redirect to login url, when logged in, it will redirect to callback url with param verifier and state
26
+
27
+ # if it redirected to login callback url, use `iauth.auth`, else `iauth.login`
28
+ iauth.auth verifier, state
29
+ ```
30
+
31
+ # License
32
+
33
+ MIT License
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ Gem::Specification.new do |gem|
3
+ gem.authors = ["Eric Zhang"]
4
+ gem.email = ["i@qinix.com"]
5
+ gem.description = %q{Authenticate with BUAA's ihome.}
6
+ gem.summary = %q{Authenticate with BUAA's ihome.}
7
+ gem.homepage = "https://github.com/qinix/iauth"
8
+
9
+ gem.add_runtime_dependency 'httparty'
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = "iauth"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = '1.0.1'
17
+ end
@@ -0,0 +1,126 @@
1
+ require 'digest'
2
+ require 'time'
3
+ require 'httparty'
4
+ require 'securerandom'
5
+
6
+ class IAuth
7
+ def initialize(appId, appSecret, accessToken='', accessSecret='', timeOffset=0)
8
+ @APP_ID = appId
9
+ @APP_SECRET = appSecret
10
+ @ACCESS_URL = 'http://i.buaa.edu.cn/plugin/iauth/access.php'
11
+ @GETUID_URL = 'http://i.buaa.edu.cn/plugin/iauth/getuid.php'
12
+ @ACCESS_TOKEN = accessToken
13
+ @ACCESS_SECRET = accessSecret
14
+ @TIME_OFFSET = timeOffset
15
+ end
16
+
17
+ def login_url(state='')
18
+ "http://i.buaa.edu.cn/plugin/iauth/login.php?appid=#{@APP_ID}&state=#{state}"
19
+ end
20
+
21
+ def auth(verifier, state='', ip='')
22
+ options = {
23
+ 'verifier' => verifier
24
+ }
25
+ options['state'] = state unless state == ''
26
+ options['ip'] = ip unless ip == ''
27
+
28
+ # {
29
+ # 'uid' => '...',
30
+ # 'access_token' => '...',
31
+ # 'access_secret' => '...'
32
+ # }
33
+ params = parse_param signed_get @ACCESS_URL, options
34
+ @uid = params['uid']
35
+ @ACCESS_TOKEN = params['access_token']
36
+ @ACCESS_SECRET = params['access_secret']
37
+ params
38
+ end
39
+
40
+ def login(verifier, state='', ip='')
41
+ options = {
42
+ 'verifier' => verifier
43
+ }
44
+ options['state'] = state unless state == ''
45
+ options['ip'] = ip unless ip == ''
46
+
47
+ # {
48
+ # 'uid' => '...',
49
+ # 'access_token' => '...'
50
+ # }
51
+ params = parse_param signed_get @GETUID_URL, options
52
+ @uid = params['uid']
53
+ @ACCESS_TOKEN = params['access_token']
54
+ params
55
+ end
56
+
57
+ def get(url, params)
58
+ request('get', url, params)
59
+ end
60
+
61
+ def post(url, params)
62
+ request('post', url, params)
63
+ end
64
+
65
+ def request(method, url, params)
66
+ nonce = SecureRandom.hex[0..16]
67
+ options = {
68
+ 'nonce' => nonce,
69
+ 'token' => @ACCESS_TOKEN,
70
+ 'hashmethod' => 'MD5',
71
+ 'hash' => Digest::MD5.hexdigest(params.sort.map{|p|p.join '='}.join('&'))
72
+ }
73
+ signed_request(method, url, header_options = options, params = params)
74
+ end
75
+
76
+ private
77
+
78
+ def parse_param(param)
79
+ ret = {}
80
+ param.split('&').map do |p|
81
+ kv = p.split('=')
82
+ ret[kv[0]] = kv[1]
83
+ end
84
+ ret
85
+ end
86
+
87
+ def signed_get(url, header_options, params={})
88
+ signed_request('get', url, header_options = header_options, params = params)
89
+ end
90
+
91
+ def signed_post(url, header_options, params)
92
+ signed_request('post', url, header_options = header_options, params = params)
93
+ end
94
+
95
+ def signed_request(method, url, header_options, params)
96
+ now = Time.now.to_i + @TIME_OFFSET
97
+ header = {
98
+ 'appid'=> @APP_ID,
99
+ 'time'=> now,
100
+ 'sigmethod'=> 'MD5',
101
+ 'version'=> '2.0'
102
+ }
103
+ header.merge! header_options
104
+ headerStr = header.sort.map{|p|p.join '='}.join '&'
105
+ baseStr = "#{method.upcase}&#{url}&#{headerStr}"
106
+ sig = signature(baseStr, @APP_SECRET)
107
+ header['sig'] = sig
108
+
109
+ options = {
110
+ headers: {
111
+ 'Authorization' => header.map{|k,v|"#{k}=\"#{v}\""}.join(',')
112
+ },
113
+ query: params
114
+ }
115
+ if method.upcase == 'GET'
116
+ HTTParty.get(url, options).body
117
+ elsif method.upcase == 'POST'
118
+ HTTParty.post(url, options).body
119
+ end
120
+ end
121
+
122
+ def signature(baseStr, secret)
123
+ Digest::MD5.hexdigest "#{baseStr}&#{secret}"
124
+ end
125
+
126
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: iauth
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Eric Zhang
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
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
+ description: Authenticate with BUAA's ihome.
28
+ email:
29
+ - i@qinix.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - README.md
36
+ - iauth.gemspec
37
+ - lib/iauth.rb
38
+ homepage: https://github.com/qinix/iauth
39
+ licenses: []
40
+ metadata: {}
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubyforge_project:
57
+ rubygems_version: 2.4.3
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: Authenticate with BUAA's ihome.
61
+ test_files: []