Skip to content
Browse files

Adding files

  • Loading branch information...
1 parent 2ed3662 commit eeaf95be4dc41ac567f627d0cc8697232977ba37 @masterkain committed Mar 8, 2012
Showing with 71 additions and 0 deletions.
  1. +70 −0 lib/omniauth/strategies/lastfm.rb
  2. +1 −0 omniauth-lastfm.gemspec
View
70 lib/omniauth/strategies/lastfm.rb
@@ -0,0 +1,70 @@
+require "multi_json"
+require "oauth"
+require "omniauth"
+require "rest-client"
+
+module OmniAuth
+ module Strategies
+ class Lastfm
+ include OmniAuth::Strategy
+
+ args [:api_key, :secret_key]
+ option :api_key, nil
+ option :secret_key, nil
+ option :name, "lastfm"
+ option :client_options, {
+ :authorize_path => "/api/auth",
+ :site => "http://www.last.fm"
+ }
+
+ attr_reader :json
+
+ def request_phase
+ params = {
+ :api_key => options.api_key,
+ :cb => options.client_options["callback"]
+ }
+ query_string = params.map{ |key,value| "#{key}=#{Rack::Utils.escape(value)}" }.join("&")
+ redirect "#{options.client_options.site}#{options.client_options.authorize_path}/?#{query_string}"
+ end
+
+ def callback_phase
+ token = request.params["token"]
+ params = { :api_key => options.api_key }
+ params[:token] = token
+ params[:api_sig] = signature(token)
+ params[:method] = "auth.getSession"
+ params[:format] = "json"
+ response = RestClient.get("http://ws.audioscrobbler.com/2.0/", { :params => params })
+ @json = MultiJson.decode(response.to_s)
+ super
+ end
+
+ uid { raw_info["name"] }
+
+ info do
+ { :name => raw_info["name"] }
+ end
+
+ extra do
+ { :raw_info => raw_info }
+ end
+
+ credentials do
+ { :token => raw_info["key"] }
+ end
+
+ def raw_info
+ @raw_info ||= @json["session"]
+ rescue ::RestClient::Exception
+ raise ::Timeout::Error
+ end
+
+ protected
+ def signature(token)
+ sign = "api_key#{options.api_key}methodauth.getSessiontoken#{token}#{options.secret_key}"
+ Digest::MD5.hexdigest(sign)
+ end
+ end
+ end
+end
View
1 omniauth-lastfm.gemspec
@@ -23,4 +23,5 @@ Gem::Specification.new do |s|
s.add_development_dependency "simplecov"
s.add_development_dependency "webmock"
s.add_runtime_dependency "omniauth-oauth", "~> 1.0"
+ s.add_runtime_dependency "rest-client", "~> 1.6.6"
end

0 comments on commit eeaf95b

Please sign in to comment.
Something went wrong with that request. Please try again.