Skip to content

Instantly share code, notes, and snippets.

@lucasmazza
Created April 2, 2011 05:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lucasmazza/899262 to your computer and use it in GitHub Desktop.
Save lucasmazza/899262 to your computer and use it in GitHub Desktop.
a Last.FM Strategy for OmniAuth
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
@brandonweiss
Copy link

This is awesome, but why haven't you forked OmniAuth and added this as a provider?

@lucasmazza
Copy link
Author

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 be Lastfm (or your setttings should point to :last_f_m, which is damn ugly)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment