Class: IAuth

Inherits:
Object
  • Object
show all
Defined in:
lib/iauth.rb

Instance Method Summary collapse

Constructor Details

#initialize(appId, appSecret, accessToken = '', accessSecret = '', timeOffset = 0) ⇒ IAuth

Returns a new instance of IAuth.



7
8
9
10
11
12
13
14
15
# File 'lib/iauth.rb', line 7

def initialize(appId, appSecret, accessToken='', accessSecret='', timeOffset=0)
  @APP_ID = appId
  @APP_SECRET = appSecret
  @ACCESS_URL = 'http://i.buaa.edu.cn/plugin/iauth/access.php'
  @GETUID_URL = 'http://i.buaa.edu.cn/plugin/iauth/getuid.php'
  @ACCESS_TOKEN = accessToken
  @ACCESS_SECRET = accessSecret
  @TIME_OFFSET = timeOffset
end

Instance Method Details

#auth(verifier, state = '', ip = '') ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/iauth.rb', line 21

def auth(verifier, state='', ip='')
  options = {
    'verifier' => verifier
  }
  options['state'] = state unless state == ''
  options['ip'] = ip unless ip == ''

  # {
  #   'uid' => '...',
  #   'access_token' => '...',
  #   'access_secret' => '...'
  # }
  params = parse_param signed_get @ACCESS_URL, options
  @uid = params['uid']
  @ACCESS_TOKEN = params['access_token']
  @ACCESS_SECRET = params['access_secret']
  params
end

#get(url, params) ⇒ Object



57
58
59
# File 'lib/iauth.rb', line 57

def get(url, params)
  request('get', url, params)
end

#login(verifier, state = '', ip = '') ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/iauth.rb', line 40

def (verifier, state='', ip='')
  options = {
    'verifier' => verifier
  }
  options['state'] = state unless state == ''
  options['ip'] = ip unless ip == ''

  # {
  #   'uid' => '...',
  #   'access_token' => '...'
  # }
  params = parse_param signed_get @GETUID_URL, options
  @uid = params['uid']
  @ACCESS_TOKEN = params['access_token']
  params
end

#login_url(state = '') ⇒ Object



17
18
19
# File 'lib/iauth.rb', line 17

def (state='')
  "http://i.buaa.edu.cn/plugin/iauth/login.php?appid=#{@APP_ID}&state=#{state}"
end

#post(url, params) ⇒ Object



61
62
63
# File 'lib/iauth.rb', line 61

def post(url, params)
  request('post', url, params)
end

#request(method, url, params) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/iauth.rb', line 65

def request(method, url, params)
  nonce = SecureRandom.hex[0..16]
  options = {
    'nonce' => nonce,
    'token' => @ACCESS_TOKEN,
    'hashmethod' => 'MD5',
    'hash' => Digest::MD5.hexdigest(params.sort.map{|p|p.join '='}.join('&'))
  }
  signed_request(method, url, header_options = options, params = params)
end