Skip to content

Instantly share code, notes, and snippets.

@dira
Created December 1, 2010 01:56
Show Gist options
  • Select an option

  • Save dira/722793 to your computer and use it in GitHub Desktop.

Select an option

Save dira/722793 to your computer and use it in GitHub Desktop.

Revisions

  1. dira revised this gist Jul 12, 2011. 2 changed files with 3 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion omniauth.rb
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,8 @@
    # config/initializers/omniauth.rb
    module OmniAuth
    module Strategies
    # tell OmniAuth to load our strategy
    autoload :Pixelation, 'lib/pixelation_authorization'
    autoload :Pixelation, 'lib/pixelation_strategy'
    end
    end

    1 change: 1 addition & 0 deletions pixelation.rb → pixelation_strategy.rb
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    # lib/pixelation_strategy.rb
    require 'omniauth/core'
    module OmniAuth
    module Strategies
  2. dira revised this gist Dec 2, 2010. 1 changed file with 1 addition and 4 deletions.
    5 changes: 1 addition & 4 deletions pixelation.rb
    Original file line number Diff line number Diff line change
    @@ -19,10 +19,7 @@ def request_phase
    end

    def callback_phase
    uid = request.params["uid"]
    username = request.params["username"]
    avatar = request.params["avatar"]
    token = request.params["token"]
    uid, username, avatar, token = request.params["uid"], request.params["username"], request.params["avatar"], request.params["token"]
    sha1 = Digest::SHA1.hexdigest("a mix of #{@secret}, #{uid}, #{username}, #{avatar}")

    # check if the request comes from Pixelation or not
  3. dira revised this gist Dec 2, 2010. 2 changed files with 13 additions and 14 deletions.
    11 changes: 6 additions & 5 deletions omniauth.rb
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,13 @@
    module OmniAuth
    module Strategies
    # this strategy is not loaded by default, so specify the path
    autoload :Pixelation, 'lib/pixelation_authorization'
    # tell OmniAuth to load our strategy
    autoload :Pixelation, 'lib/pixelation_authorization'
    end
    end

    Rails.application.config.middleware.use OmniAuth::Builder do
    provider :twitter, ENV["OAUTH_TWITTER_APP"], ENV["OAUTH_TWITTER_SECRET"]
    provider :facebook, ENV["OAUTH_FB_APP"], ENV["OAUTH_FB_SECRET"], :scope => ''
    provider :pixelation, ENV["PIXELATION_SECRET"], ENV["PIXELATION_AUTH_REDIRECT"]
    provider :twitter, "app_name", "secret"
    provider :facebook, "app_name", "secret", :scope => ''
    # pass the 2 parameters to the constructor
    provider :pixelation, "secret", "redirect URL"
    end
    16 changes: 7 additions & 9 deletions pixelation.rb
    Original file line number Diff line number Diff line change
    @@ -4,14 +4,14 @@ module Strategies
    class Pixelation
    include OmniAuth::Strategy

    # the last parameter must be an options hash
    # receive parameters from the strategy declaration and save them
    def initialize(app, secret, auth_redirect, options = {})
    @secret = secret
    @auth_redirect = auth_redirect
    super(app, :pixelation, options)
    end

    # redirect to the URL received in initialization
    # redirect to the Pixelation website
    def request_phase
    r = Rack::Response.new
    r.redirect @auth_redirect
    @@ -24,11 +24,10 @@ def callback_phase
    avatar = request.params["avatar"]
    token = request.params["token"]
    sha1 = Digest::SHA1.hexdigest("a mix of #{@secret}, #{uid}, #{username}, #{avatar}")
    # check if it comes from Pixelation or not

    # check if the request comes from Pixelation or not
    if sha1 == token
    @uid = uid
    @username = username
    @avatar = avatar
    @uid, @username, @avatar = uid, username, avatar
    # OmniAuth takes care of the rest
    super
    else
    @@ -37,9 +36,8 @@ def callback_phase
    end
    end

    # invoked by OmniAuth
    # normalize user's data according to https://github.com/intridea/omniauth/wiki/Auth-Hash-Schema
    def auth_hash
    # must respect https://github.com/intridea/omniauth/wiki/Auth-Hash-Schema
    OmniAuth::Utils.deep_merge(super(), {
    'uid' => @uid,
    'user_info' => {
    @@ -51,4 +49,4 @@ def auth_hash
    end
    end
    end
    end
    end
  4. dira revised this gist Dec 1, 2010. 2 changed files with 12 additions and 0 deletions.
    12 changes: 12 additions & 0 deletions omniauth.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    module OmniAuth
    module Strategies
    # this strategy is not loaded by default, so specify the path
    autoload :Pixelation, 'lib/pixelation_authorization'
    end
    end

    Rails.application.config.middleware.use OmniAuth::Builder do
    provider :twitter, ENV["OAUTH_TWITTER_APP"], ENV["OAUTH_TWITTER_SECRET"]
    provider :facebook, ENV["OAUTH_FB_APP"], ENV["OAUTH_FB_SECRET"], :scope => ''
    provider :pixelation, ENV["PIXELATION_SECRET"], ENV["PIXELATION_AUTH_REDIRECT"]
    end
    File renamed without changes.
  5. dira created this gist Dec 1, 2010.
    54 changes: 54 additions & 0 deletions lib/pixelation.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    require 'omniauth/core'
    module OmniAuth
    module Strategies
    class Pixelation
    include OmniAuth::Strategy

    # the last parameter must be an options hash
    def initialize(app, secret, auth_redirect, options = {})
    @secret = secret
    @auth_redirect = auth_redirect
    super(app, :pixelation, options)
    end

    # redirect to the URL received in initialization
    def request_phase
    r = Rack::Response.new
    r.redirect @auth_redirect
    r.finish
    end

    def callback_phase
    uid = request.params["uid"]
    username = request.params["username"]
    avatar = request.params["avatar"]
    token = request.params["token"]
    sha1 = Digest::SHA1.hexdigest("a mix of #{@secret}, #{uid}, #{username}, #{avatar}")
    # check if it comes from Pixelation or not
    if sha1 == token
    @uid = uid
    @username = username
    @avatar = avatar
    # OmniAuth takes care of the rest
    super
    else
    # OmniAuth takes care of the rest
    fail!(:invalid_credentials)
    end
    end

    # invoked by OmniAuth
    def auth_hash
    # must respect https://github.com/intridea/omniauth/wiki/Auth-Hash-Schema
    OmniAuth::Utils.deep_merge(super(), {
    'uid' => @uid,
    'user_info' => {
    'name' => @username,
    'nickname' => @username,
    'image' => @avatar
    }
    })
    end
    end
    end
    end