soundcloud-auth 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.markdown CHANGED
@@ -1,2 +1,75 @@
1
- SoundCloudAuth
2
- ===========
1
+ SoundcloudAuth
2
+ ===========
3
+
4
+ SoundcloudAuth aims to provide a complete authentication and API access solution for creating SoundCloud applications in Rails. It provides a generator and all of the necessary components to use Soundcloud as the sole authentication provider for an application using OAuth.
5
+
6
+ ### Major Props
7
+
8
+ SoundcloudAuth is largely influenced and in a lot of ways identical to [Michael Bleigh's](http://github.com/mbleigh) amazing [TwitterAuth](http://github.com/mbleigh/twitter-auth) plugin. I've used TwitterAuth so many times on my Twitter app development that I found it sorely missed when I tried to tackle the SoundCloud platform. All props and donations should be forwarded to Michael.
9
+
10
+ Installation
11
+ ============
12
+
13
+ You can include SoundcloudAuth as a gem in your project like so:
14
+
15
+ config.gem `soundcloud-auth`, :lib => `soundcloud_auth`
16
+
17
+ Or you can install it as a traditional Rails plugin:
18
+
19
+ script/plugin install git://github.com/leemartin/soundcloud-auth.git
20
+
21
+ Note that because SoundcloudAuth utilizes Rails Engines functionality introduced in Rails 2.3, it will not work with earlier versions of Rails.
22
+
23
+ Usage
24
+ =====
25
+
26
+ To utilize SoundcloudAuth in your application you will need to run the generator:
27
+
28
+ script/generate soundcloud_auth
29
+
30
+ This will generate a migration as well as set up the stubs needed to use the Rails Engines controllers and models set up by SoundcloudAuth. It will also create a User class that inherits from SoundcloudUser, abstracting away all of the SoundCloud authentication functionality and leaving you a blank slate to work with for your application.
31
+
32
+ Finally, it will create a configuration file in `config/soundcloud_auth.yml` in which you should input your OAuth consumer key and secret as well as a custom callback for development (the 'oauth_callback' option is where SoundCloud will send the browser after authentication is complete).
33
+
34
+ Usage Basics
35
+ ------------
36
+
37
+ SoundcloudAuth borrows heavily from [Restful Authentication](http://github.com/technoweenie/restful-authentication) for its API because it's simple and well-known. Here are some of the familiar methods that are available:
38
+
39
+ * `login_required`: a before filter that can be added to a controller to require that a user logs in before he/she can view the page.
40
+ * `current_user`: returns the logged in user if one exists, otherwise returns `nil`.
41
+ * `logged_in?`: true if logged in, false otherwise.
42
+ * `redirect_back_or_default(url)`: redirects to the location where `store_location` was last called or the specified default URL.
43
+ * `store_location`: store the current URL for returning to when a `redirect_back_or_default` is called.
44
+ * `authorized?`: override this to add fine-grained access control for when `login_required` is already called.
45
+
46
+ Accessing the SoundCloud API
47
+ -------------------------
48
+
49
+ The `User` class will have a `soundcloud` method that provides a generic dispatcher with HTTP verb commands available (`get`, `put`, `post`, and `delete`). These are automatically initialized to the `base_url` you specified in the `twitter_auth.yml` file, so you need only specify a path. Additionally, it will automatically append a .json extension and parse the JSON if you don't provide.
50
+
51
+ user.soundcloud.get('/me')
52
+ # => {'username' => 'leemartin', 'full_name' => 'Lee Martin' ... }
53
+
54
+ user.soundcloud.put('/me', 'user[website]=http://lee.ma/rtin')
55
+ # => {'username' => 'leemartin', 'website' => 'http://lee.ma/rtin'}
56
+
57
+ If SoundCloud returns something other than a 200 response code, SoundcloudAuth will catch it and try to raise a salient error message. The exception class is `SoundcloudAuth::Dispatcher::Error`.
58
+
59
+ Customizing SoundcloudAuth
60
+ -----------------------
61
+
62
+ There are a number of hooks to extend the functionality of SoundcloudAuth. Here is a brief description of each of them.
63
+
64
+ ### Controller Methods
65
+
66
+ SoundcloudAuth provides some default controller methods that may be overridden in your `ApplicationController` to behave differently.
67
+
68
+ * `authentication_failed(message)`: called when SoundCloud authorization has failed during the process. By default, simply redirects to the site root and sets the `flash[:error]`.
69
+ * `authentication_succeeded(message=default)`: called when SoundCloud authorization has completed successfully. By default, simply redirects to the site root and sets the `flash[:notice]`.
70
+ * `access_denied`: what happens when the `login_required` before filter fails. By default it stores the current location to return to and redirects to the login process.
71
+
72
+ Copyright
73
+ ---------
74
+
75
+ **SoundcloudAuth** is Copyright (c) 2010 [Lee Martin](http://lee.ma/rtin), released under the MIT License.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
@@ -32,16 +32,14 @@ class SessionsController < ApplicationController
32
32
 
33
33
  session[:request_token] = nil
34
34
  session[:request_token_secret] = nil
35
-
36
- puts @access_token
37
35
 
38
- #@user = User.identify_or_create_from_access_token(@access_token)
36
+ @user = User.identify_or_create_from_access_token(@access_token)
39
37
 
40
- #session[:user_id] = @user.id
38
+ session[:user_id] = @user.id
41
39
 
42
- #cookies[:remember_token] = @user.remember_me
40
+ cookies[:remember_token] = @user.remember_me
43
41
 
44
- #authentication_succeeded
42
+ authentication_succeeded
45
43
 
46
44
  rescue Net::HTTPServerException => e
47
45
  case e.message
@@ -1,7 +1,7 @@
1
1
  module SoundcloudAuth
2
2
  class GenericUser < ActiveRecord::Base
3
3
 
4
- attr_protected :soundcloud_id, :remember_token, :remember_token_expires_at
4
+ attr_protected :soundcloud_id, :remember_token, :remember_token_expires_at, :access_token, :access_secret
5
5
 
6
6
  SOUNDCLOUD_ATTRIBUTES = [
7
7
  :plan,
@@ -67,7 +67,8 @@ module SoundcloudAuth
67
67
 
68
68
  token = OAuth::AccessToken.new(SoundcloudAuth.consumer, token, secret) unless token.is_a?(OAuth::AccessToken)
69
69
 
70
- user_info = JSON.parse(token.get(SoundcloudAuth.path_prefix + '/me.json'))
70
+ response = token.get(SoundcloudAuth.path_prefix + '/me.json')
71
+ user_info = JSON.parse(response.body)
71
72
 
72
73
  if user = User.find_by_soundcloud_id(user_info['id'].to_s)
73
74
  user.assign_soundcloud_attributes(user_info)
@@ -94,7 +95,7 @@ module SoundcloudAuth
94
95
  end
95
96
 
96
97
  def soundcloud
97
- # OAuth::AccessToken.new(TwitterAuth.consumer, access_token, access_secret)
98
+ SoundcloudAuth::Dispatcher::Oauth.new(self)
98
99
  end
99
100
 
100
101
  def remember_me
@@ -29,7 +29,7 @@ module SoundcloudAuth
29
29
  def self.consumer
30
30
 
31
31
  options = {
32
- :site => TwitterAuth.base_url,
32
+ :site => SoundcloudAuth.base_url,
33
33
  :request_token_path => "/oauth/request_token",
34
34
  :access_token_path => "/oauth/access_token",
35
35
  :authorize_path => "/oauth/authorize"
@@ -43,9 +43,14 @@ module SoundcloudAuth
43
43
 
44
44
  end
45
45
 
46
- =end
47
-
48
46
  end
49
47
 
50
48
  require 'soundcloud_auth/controller_extensions'
49
+ require 'soundcloud_auth/dispatcher/oauth'
51
50
 
51
+ module SoundcloudAuth
52
+ module Dispatcher
53
+ class Error < StandardError; end
54
+ class Unauthorized < Error; end
55
+ end
56
+ end
@@ -0,0 +1,56 @@
1
+ require 'oauth'
2
+
3
+ module SoundcloudAuth
4
+ module Dispatcher
5
+ class Oauth < OAuth::AccessToken
6
+
7
+ attr_accessor :user
8
+
9
+ def append_extension_to(path)
10
+ path, query_string = *(path.split("?"))
11
+ path << '.json' unless path.match(/\.(:?xml|json)\z/i)
12
+ "#{path}#{"?#{query_string}" if query_string}"
13
+ end
14
+
15
+ def handle_response(response)
16
+ case response
17
+ when Net::HTTPOK
18
+ begin
19
+ JSON.parse(response.body)
20
+ rescue JSON::ParserError
21
+ response.body
22
+ end
23
+ when Net::HTTPUnauthorized
24
+ raise SoundcloudAuth::Dispatcher::Unauthorized, 'The credentials provided did not authorize the user.'
25
+ else
26
+ message = begin
27
+ JSON.parse(response.body)['error']
28
+ rescue JSON::ParserError
29
+ if match = response.body.match(/<error>(.*)<\/error>/)
30
+ match[1]
31
+ else
32
+ 'An error occurred processing your SoundCloud request.'
33
+ end
34
+ end
35
+
36
+ raise SoundcloudAuth::Dispatcher::Error, message
37
+ end
38
+ end
39
+
40
+ def initialize(user)
41
+ self.user = user
42
+ super(SoundcloudAuth.consumer, user.access_token, user.access_secret)
43
+ end
44
+
45
+ def request(http_method, path, *arguments)
46
+ path = SoundcloudAuth.path_prefix + path
47
+ path = append_extension_to(path)
48
+
49
+ response = super
50
+
51
+ handle_response(response)
52
+ end
53
+
54
+ end
55
+ end
56
+ end
Binary file
@@ -0,0 +1,56 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{soundcloud-auth}
8
+ s.version = "0.1.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Lee Martin"]
12
+ s.date = %q{2010-06-25}
13
+ s.description = %q{SoundcloudAuth is a Rails plugin gem that provides Single Sign-On capabilities for Rails applications via SoundCloud.}
14
+ s.email = %q{lee@soundcloud.com}
15
+ s.extra_rdoc_files = [
16
+ "README.markdown"
17
+ ]
18
+ s.files = [
19
+ "README.markdown",
20
+ "Rakefile",
21
+ "VERSION",
22
+ "app/controllers/sessions_controller.rb",
23
+ "app/models/soundcloud_auth/generic_user.rb",
24
+ "config/routes.rb",
25
+ "generators/soundcloud_auth/USAGE",
26
+ "generators/soundcloud_auth/soundcloud_auth_generator.rb",
27
+ "generators/soundcloud_auth/templates/migration.rb",
28
+ "generators/soundcloud_auth/templates/soundcloud_auth.yml",
29
+ "generators/soundcloud_auth/templates/user.rb",
30
+ "init.rb",
31
+ "lib/soundcloud_auth.rb",
32
+ "lib/soundcloud_auth/controller_extensions.rb",
33
+ "lib/soundcloud_auth/dispatcher/oauth.rb",
34
+ "pkg/soundcloud-auth-0.1.0.gem",
35
+ "rails/init.rb",
36
+ "soundcloud-auth.gemspec"
37
+ ]
38
+ s.homepage = %q{http://github.com/leemartin/soundcloud-auth}
39
+ s.rdoc_options = ["--charset=UTF-8"]
40
+ s.require_paths = ["lib"]
41
+ s.rubygems_version = %q{1.3.7}
42
+ s.summary = %q{SoundcloudAuth is a Rails plugin gem that provides Single Sign-On capabilities for Rails applications via SoundCloud.}
43
+
44
+ if s.respond_to? :specification_version then
45
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
46
+ s.specification_version = 3
47
+
48
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
49
+ s.add_runtime_dependency(%q<oauth>, [">= 0.3.1"])
50
+ else
51
+ s.add_dependency(%q<oauth>, [">= 0.3.1"])
52
+ end
53
+ else
54
+ s.add_dependency(%q<oauth>, [">= 0.3.1"])
55
+ end
56
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: soundcloud-auth
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 0
10
- version: 0.1.0
9
+ - 1
10
+ version: 0.1.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Lee Martin
@@ -57,7 +57,10 @@ files:
57
57
  - init.rb
58
58
  - lib/soundcloud_auth.rb
59
59
  - lib/soundcloud_auth/controller_extensions.rb
60
+ - lib/soundcloud_auth/dispatcher/oauth.rb
61
+ - pkg/soundcloud-auth-0.1.0.gem
60
62
  - rails/init.rb
63
+ - soundcloud-auth.gemspec
61
64
  has_rdoc: true
62
65
  homepage: http://github.com/leemartin/soundcloud-auth
63
66
  licenses: []