Class: Qihu::Auth

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id, client_secret, token = {}, redirect_uri = 'oob', site = 'https://openapi.360.cn') ⇒ Auth

Returns a new instance of Auth.



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/qihu/auth.rb', line 10

def initialize(client_id, client_secret, token={}, redirect_uri='oob', site='https://openapi.360.cn')
  @redirect_uri = redirect_uri
  @oauth2 = OAuth2::Client.new(client_id, client_secret,
    :site => site,
    :authorize_url => '/oauth2/authorize',
    :token_url => '/oauth2/access_token',
    :ssl => {:verify => false},
  )

  if token or !token.empty?
    @token = OAuth2::AccessToken.from_hash(@oauth2, token)
  end
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



7
8
9
# File 'lib/qihu/auth.rb', line 7

def access_token
  @access_token
end

#expires_atObject (readonly)

Returns the value of attribute expires_at.



7
8
9
# File 'lib/qihu/auth.rb', line 7

def expires_at
  @expires_at
end

#expires_inObject (readonly)

Returns the value of attribute expires_in.



7
8
9
# File 'lib/qihu/auth.rb', line 7

def expires_in
  @expires_in
end

#oauth2Object (readonly)

Returns the value of attribute oauth2.



7
8
9
# File 'lib/qihu/auth.rb', line 7

def oauth2
  @oauth2
end

#redirect_uriObject

Returns the value of attribute redirect_uri.



8
9
10
# File 'lib/qihu/auth.rb', line 8

def redirect_uri
  @redirect_uri
end

#refresh_tokenObject (readonly)

Returns the value of attribute refresh_token.



7
8
9
# File 'lib/qihu/auth.rb', line 7

def refresh_token
  @refresh_token
end

#tokenObject

Returns the value of attribute token.



8
9
10
# File 'lib/qihu/auth.rb', line 8

def token
  @token
end

Instance Method Details

#authorize_url(options = {}) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/qihu/auth.rb', line 24

def authorize_url(options={})
  @redirect_uri = options[:redirect_uri] if options[:redirect_uri]
  scope = options[:scope] ? options[:scope] : 'basic'
  display = options[:display] ? options[:display] : 'default'

  @oauth2.auth_code.authorize_url(:redirect_uri => @redirect_uri, :scope => scope, :display => display)
end

#get_code_from_account(username, password, options = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/qihu/auth.rb', line 44

def (username, password, options={})
  @redirect_uri = options[:redirect_uri] if options[:redirect_uri]
  conn = Faraday.new(@oauth2.site)
  res = conn.post(@oauth2.authorize_url, {
    :client_id => @oauth2.id,
    :redirect_uri => @redirect_uri, 
    :response_type => 'code',
    :username => username,
    :password => password,
    })

  query = CGI.parse(URI(res.headers[:location]).query)
  query["code"].pop
end

#get_token(code, redirect_uri = '') ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/qihu/auth.rb', line 32

def get_token(code, redirect_uri='')
  @redirect_uri = redirect_uri unless redirect_uri.empty?
  @token = @oauth2.auth_code.get_token(code, :redirect_uri => @redirect_uri)

  @refresh_token = @token.refresh_token
  @expires_at = @token.expires_at
  @expires_in = @token.expires_in
  @access_token = @token.token

  return self
end

#get_token_from_account(username, password, options = {}) ⇒ Object



59
60
61
# File 'lib/qihu/auth.rb', line 59

def (username, password, options={})
  self.get_token(self.(username, password, options))
end

#get_token_from_hash(token = {}) ⇒ Object



63
64
65
66
# File 'lib/qihu/auth.rb', line 63

def get_token_from_hash(token={})
  @token = OAuth2::AccessToken.from_hash(@oauth2, token)
  return self
end