Created
April 2, 2011 05:34
-
-
Save lucasmazza/899262 to your computer and use it in GitHub Desktop.
a Last.FM Strategy for OmniAuth
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'omniauth/core' | |
require 'digest/md5' | |
require 'rest-client' | |
require 'multi_json' | |
module OmniAuth | |
module Strategies | |
class LastFM | |
include OmniAuth::Strategy | |
attr_accessor :api_key, :secret_key, :options | |
def initialize(app, api_key, secret_key, options = {}) | |
super(app, :lastfm) | |
@api_key = api_key | |
@secret_key = secret_key | |
@options = options | |
end | |
def request_phase | |
params = {:api_key => api_key } | |
query_string = params.map{ |key,value| "#{key}=#{Rack::Utils.escape(value)}" }.join('&') | |
redirect "http://www.last.fm/api/auth/?#{query_string}" | |
end | |
def callback_phase | |
token = request.params['token'] | |
params = { :api_key => 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 | |
def auth_hash | |
OmniAuth::Utils.deep_merge(super, {:lastfm => @json['session']}) | |
end | |
protected | |
def signature(token) | |
sign = "api_key#{api_key}methodauth.getSessiontoken#{token}#{secret_key}" | |
Digest::MD5.hexdigest(sign) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hi Brandon,
I'm still tweaking some stuff on it and it's lacking some tests. For instance, naming it
LastFM
name doesn't work if you're using Devise, it should beLastfm
(or your setttings should point to:last_f_m
, which is damn ugly)