Sessions, lastfm auth workarounds
This commit is contained in:
parent
47b21961a0
commit
4326a4960b
|
@ -0,0 +1,72 @@
|
|||
require 'open-uri'
|
||||
|
||||
class LastFmController < ApplicationController
|
||||
|
||||
@@api_key = '04fda005dbf61a50af5abc3e90f111f2'
|
||||
@@secret = '19e70e98b291e9f15d0516925945eb1b'
|
||||
|
||||
def connect
|
||||
unless params[:sid].nil? or params[:token].nil?
|
||||
session = Session.find_by_key(params[:sid])
|
||||
unless session.nil?
|
||||
session.user.lastfm_token = params[:token]
|
||||
session.user.save
|
||||
render :text => '<script>window.close();</script>'
|
||||
else
|
||||
render :text => 'You Don\'t Fool Me'
|
||||
end
|
||||
else
|
||||
render :text => 'So Much Trouble In The World'
|
||||
end
|
||||
end
|
||||
|
||||
def getinfo
|
||||
#return unless authorized?
|
||||
@res = {}
|
||||
|
||||
user = User.find_by_vkid(1217744)#params[:mid])
|
||||
@res[:connected] = false
|
||||
@res[:lastfm_login_url] = 'http://www.last.fm/api/auth?api_key='+ @@api_key +'&cb=http://localhost/lastfm/connect/?sid='+ user.session.key
|
||||
|
||||
unless user.lastfm_token.nil?
|
||||
lastfm_response = auth_query({:method => 'auth.getSession', :token => user.lastfm_token})
|
||||
render :json => lastfm_response
|
||||
return
|
||||
if lastfm_response
|
||||
user.lastfm_token = lastfm_response[1]
|
||||
user.save
|
||||
@res[:connected] = true
|
||||
@res[:username] = lastfm_response[0];
|
||||
end
|
||||
end
|
||||
|
||||
render :json => @res
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def auth_query params
|
||||
url = 'http://ws.audioscrobbler.com/2.0/?'
|
||||
params[:api_key] = @@api_key
|
||||
params.each do |k, v|
|
||||
url << k.to_s << '=' << v << '&'
|
||||
end
|
||||
url << 'api_sig=' << get_signature(params)
|
||||
begin
|
||||
open(url).read.match(/<name>(.*?)<\/name>.*?<key>(.*?)<\/key>/m)
|
||||
rescue
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
def get_signature params
|
||||
params = params.to_a.sort! { |a, b| a[0] <=> b[0] }
|
||||
params = Hash[params]
|
||||
str = '';
|
||||
params.each do |k, v|
|
||||
str << k.to_s << v
|
||||
end
|
||||
Digest::MD5.hexdigest(str + @@secret)
|
||||
end
|
||||
|
||||
end
|
|
@ -13,6 +13,9 @@ class UserController < ApplicationController
|
|||
end
|
||||
@res[:id] = user.id
|
||||
@res[:username] = user.name
|
||||
session = Session.find_or_create_by_user_id(user.id)
|
||||
session.key = Digest::SHA256.hexdigest(rand(99999999).to_s + user.id.to_s + rand(99999999).to_s)
|
||||
session.save
|
||||
|
||||
render :json => @res
|
||||
end
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
module LastFmHelper
|
||||
end
|
|
@ -0,0 +1,3 @@
|
|||
class Session < ActiveRecord::Base
|
||||
belongs_to :user
|
||||
end
|
|
@ -1,2 +1,3 @@
|
|||
class User < ActiveRecord::Base
|
||||
has_one :session
|
||||
end
|
||||
|
|
|
@ -59,6 +59,10 @@ Beathaven::Application.routes.draw do
|
|||
match 'user/auth' => 'user#auth'
|
||||
match 'user/update' => 'user#update'
|
||||
|
||||
match 'lastfm/login' => 'last_fm#login'
|
||||
match 'lastfm/connect' => 'last_fm#connect'
|
||||
match 'lastfm/getinfo' => 'last_fm#getinfo'
|
||||
|
||||
match 'artist/autocomplete' => 'artist#autocomplete'
|
||||
match 'artist/(:name)/' => 'artist#data', :constraints => { :name => /[^\/]*/ }
|
||||
end
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
class AddLastfmTokenToUser < ActiveRecord::Migration
|
||||
def self.up
|
||||
add_column :users, :lastfm_token, :string
|
||||
end
|
||||
|
||||
def self.down
|
||||
remove_column :users, :lastfm_token
|
||||
end
|
||||
end
|
|
@ -0,0 +1,14 @@
|
|||
class CreateSessions < ActiveRecord::Migration
|
||||
def self.up
|
||||
create_table :sessions do |t|
|
||||
t.integer :user_id
|
||||
t.string :key
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
|
||||
def self.down
|
||||
drop_table :sessions
|
||||
end
|
||||
end
|
10
db/schema.rb
10
db/schema.rb
|
@ -10,7 +10,7 @@
|
|||
#
|
||||
# It's strongly recommended to check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(:version => 20110622000253) do
|
||||
ActiveRecord::Schema.define(:version => 20110622053238) do
|
||||
|
||||
create_table "albums", :force => true do |t|
|
||||
t.string "name"
|
||||
|
@ -72,6 +72,13 @@ ActiveRecord::Schema.define(:version => 20110622000253) do
|
|||
t.datetime "updated_at"
|
||||
end
|
||||
|
||||
create_table "sessions", :force => true do |t|
|
||||
t.integer "user_id"
|
||||
t.string "key"
|
||||
t.datetime "created_at"
|
||||
t.datetime "updated_at"
|
||||
end
|
||||
|
||||
create_table "tracks", :force => true do |t|
|
||||
t.string "name"
|
||||
t.integer "album_id"
|
||||
|
@ -91,6 +98,7 @@ ActiveRecord::Schema.define(:version => 20110622000253) do
|
|||
t.integer "vkid"
|
||||
t.datetime "created_at"
|
||||
t.datetime "updated_at"
|
||||
t.string "lastfm_token"
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
var Settings = {
|
||||
|
||||
lastfm_login_url: null,
|
||||
|
||||
getAccountInfo: function(callback) {
|
||||
Session.query('/user/update', {}, callback);
|
||||
},
|
||||
|
@ -29,14 +31,27 @@ $('.settings-container .tabs .tab').live('click', function(){
|
|||
$('.form-container').html($('.forms .'+ $(this).attr('data-fieldset')).html());
|
||||
Settings.loadFormData($(this).attr('data-fieldset'));
|
||||
}
|
||||
if ($(this).attr('data-fieldset') == 'lastfm') {
|
||||
Session.query('/lastfm/getinfo', {}, function(data){
|
||||
console.log(data);
|
||||
Settings.lastfm_login_url = data.lastfm_login_url;
|
||||
})
|
||||
}
|
||||
});
|
||||
|
||||
$('.lastfm-connect').live('click', function(){
|
||||
window.open(Settings.lastfm_login_url);
|
||||
});
|
||||
|
||||
$('.settings-container .form input').live('blur', function(){
|
||||
params = {
|
||||
'username': $('.settings-container .form input[name$="username"]').first().val(),
|
||||
'email': $('.settings-container .form input[name$="email"]').first().val(),
|
||||
};
|
||||
Settings.saveAccountInfo(params, function(){
|
||||
$('#header-container .hello .greating').text('Hi there, '+ (params.username.length > 0 ? params.username : '%username%') +'!');
|
||||
});
|
||||
var active_tab = $('.settings-container .tabs .tab.active').attr('data-fieldset');
|
||||
if (active_tab == 'account') {
|
||||
params = {
|
||||
'username': $('.settings-container .form input[name$="username"]').first().val(),
|
||||
'email': $('.settings-container .form input[name$="email"]').first().val(),
|
||||
};
|
||||
Settings.saveAccountInfo(params, function(){
|
||||
$('#header-container .hello .greating').text('Hi there, '+ (params.username.length > 0 ? params.username : '%username%') +'!');
|
||||
});
|
||||
}
|
||||
})
|
|
@ -10,7 +10,7 @@ function authInfo(response) {
|
|||
if (!ar.username) {
|
||||
VK.Api.call('getVariable', {key: 1281}, function(r) {
|
||||
Session.query('/user/update', {'name': r.response}, function(ar2) {
|
||||
$('#header-container .hello .greating').text('Hi there, '+ ar2.username +'!');
|
||||
$('#header-container .hello .greating').text('Hi there, '+ (ar2.username ? ar2.username : '%username%') +'!');
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
<div class="left-space"></div>
|
||||
<div class="tab" data-fieldset="account">Account</div>
|
||||
<div class="middle-space"></div>
|
||||
<div class="tab" data-fieldset="lastfm">Last.fm</div>
|
||||
</div>
|
||||
<div class="form-container"></div>
|
||||
<div class="forms">
|
||||
|
@ -18,5 +19,14 @@
|
|||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="lastfm">
|
||||
<div class="form">
|
||||
<div class="field">
|
||||
<div class="label">Username:</div>
|
||||
<div class="value"><input name="username" type="text" disabled="disabled" value="Not connected" /></div>
|
||||
<div class="action-button"><input type="button" class="lastfm-connect" value="Connect"/></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
|
@ -68,6 +68,9 @@
|
|||
background-color: #EAEAEA;
|
||||
margin-right: -10px;
|
||||
}
|
||||
.settings-container .form-container .form .field .value {
|
||||
float: left;
|
||||
}
|
||||
.settings-container .form-container .form .field input[type="text"]{
|
||||
font-size: 24px;
|
||||
padding: 4px;
|
||||
|
@ -77,6 +80,17 @@
|
|||
border-radius: 3px;
|
||||
width: 300px;
|
||||
}
|
||||
.settings-container .form-container .form .field input[type="button"]{
|
||||
width: 120px;
|
||||
font-size: 24px;
|
||||
padding: 4px;
|
||||
background-color: #DDD;
|
||||
border: #CCC 1px solid;
|
||||
-webkit-border-radius: 3px;
|
||||
-moz-border-radius: 3px;
|
||||
border-radius: 3px;
|
||||
margin-left: 5px;
|
||||
}
|
||||
.settings-container .forms {
|
||||
display: none;
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
|
||||
|
||||
one:
|
||||
user_id: 1
|
||||
key: MyString
|
||||
|
||||
two:
|
||||
user_id: 1
|
||||
key: MyString
|
|
@ -0,0 +1,8 @@
|
|||
require 'test_helper'
|
||||
|
||||
class LastFmControllerTest < ActionController::TestCase
|
||||
# Replace this with your real tests.
|
||||
test "the truth" do
|
||||
assert true
|
||||
end
|
||||
end
|
|
@ -0,0 +1,4 @@
|
|||
require 'test_helper'
|
||||
|
||||
class LastFmHelperTest < ActionView::TestCase
|
||||
end
|
|
@ -0,0 +1,8 @@
|
|||
require 'test_helper'
|
||||
|
||||
class SessionTest < ActiveSupport::TestCase
|
||||
# Replace this with your real tests.
|
||||
test "the truth" do
|
||||
assert true
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue