Class: Livedoor::API::Auth

Inherits:
Object
  • Object
show all
Defined in:
lib/livedoor/api/auth.rb,
lib/livedoor/api/version.rb

Defined Under Namespace

Modules: VERSION

Constant Summary collapse

BASE_URI =
'http://auth.livedoor.com/'
PATH =
{
  :auth => '/login/',
  :rpc_auth => '/rpc/auth'
}.freeze
API_VERSION =
'1.0'
TIMEOUT =
60 * 10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Auth

Returns a new instance of Auth.



31
32
33
34
# File 'lib/livedoor/api/auth.rb', line 31

def initialize(options = {})
  @app_key = options[:app_key]
  @secret  = options[:secret]
end

Instance Attribute Details

#app_keyObject

Returns the value of attribute app_key.



29
30
31
# File 'lib/livedoor/api/auth.rb', line 29

def app_key
  @app_key
end

#secretObject

Returns the value of attribute secret.



29
30
31
# File 'lib/livedoor/api/auth.rb', line 29

def secret
  @secret
end

Instance Method Details

#get_livedoor_id(param) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/livedoor/api/auth.rb', line 58

def get_livedoor_id(param)
  uri = uri_to_auth_rpc(param)
  result = nil
  Net::HTTP.start(uri.host) do |http|
    result = JSON.parse( 
      http.post(uri.path, uri.query, 
        {"Content-type" => "application/x-www-form-urlencoded"}).body)
  end
  if result['error'].to_i == 0
    result['user']['livedoor_id']
  else
    raise AuthError.new(result['message'])
  end
end

#uri_to_login(request = {}) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/livedoor/api/auth.rb', line 36

def (request = {})
  uri = URI.parse BASE_URI
  uri.path = PATH[:auth]
  request[:perms] = 'userhash' unless request.key?(:perms)
  uri.query = query_with_api_sig(request)
  uri
end

#validate_response(query) ⇒ Object



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

def validate_response(query)
  param = {}
  pr = CGI.parse(query)
  pr.each do |k,v| 
    param[k.to_sym] = v.first 
  end
  if param[:sig] == sig(param) 
    raise 'LOCAL TIMEOUT' if param[:t].to_i + TIMEOUT < Time.now.to_i
  else
    raise 'INVALID SIG'
  end
  {:userdata => param[:userdata], :userhash => param[:userhash], :token => param[:token]}
end