Permalink
Browse files

Work towards live examples.

  • Loading branch information...
1 parent 547e857 commit dd70e00fdc92b117ec9391ffe0d063804c062f80 Michael Bleigh committed Oct 29, 2011
Showing with 383 additions and 15 deletions.
  1. +3 −1 .gitignore
  2. +1 −0 Gemfile
  3. +2 −0 Gemfile.lock
  4. +42 −2 application.rb
  5. +108 −0 lib/openid-redis-store.rb
  6. +32 −0 providers.yml
  7. BIN public/images/icon.png
  8. +116 −12 public/stylesheets/screen.css
  9. +61 −0 sass/screen.sass
  10. +8 −0 views/callback.erb
  11. +10 −0 views/home.erb
View
@@ -1 +1,3 @@
-/.sass-cache
+.sass-cache
+.powenv
+/tmp
View
@@ -2,6 +2,7 @@ source 'http://rubygems.org'
gem 'sinatra'
gem 'compass'
+gem 'redis'
gem 'omniauth', '~> 1.0.0.rc2'
gem 'omniauth-github', '~> 1.0.0.rc2', :git => 'git://github.com/intridea/omniauth-github.git'
View
@@ -61,6 +61,7 @@ GEM
ruby-openid (>= 2.1.8)
rack-protection (1.1.4)
rack
+ redis (2.2.2)
ruby-openid (2.1.8)
sass (3.1.10)
sinatra (1.3.1)
@@ -79,4 +80,5 @@ DEPENDENCIES
omniauth-facebook!
omniauth-github (~> 1.0.0.rc2)!
omniauth-openid (~> 1.0.0.rc2)
+ redis
sinatra
View
@@ -1,14 +1,54 @@
+$:.unshift File.dirname(__FILE__) + '/lib'
ENV['RACK_ENV'] ||= 'development'
require 'bundler'
Bundler.require :default, ENV['RACK_ENV']
+require 'redis'
+require 'openid-redis-store'
+
+if ENV['REDISTOGO_URL']
+ uri = URI.parse(ENV["REDISTOGO_URL"])
+ $redis = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
+else
+ $redis = Redis.new
+end
+
use Rack::Session::Cookie
use OmniAuth::Builder do
- provider :twitter, ENV['TWITTER_KEY'], ENV['TWITTER_SECRET']
provider :facebook, ENV['FACEBOOK_KEY'], ENV['FACEBOOK_SECRET']
provider :github, ENV['GITHUB_KEY'], ENV['GITHUB_SECRET']
- provider :open_id
+ provider :open_id, :store => OpenID::Store::Redis.new($redis, 'openid:')
+ provider :twitter, ENV['TWITTER_KEY'], ENV['TWITTER_SECRET']
+end
+
+OMNIAUTH_STRATEGIES = YAML.load_file(File.dirname(__FILE__) + '/providers.yml').inject([]) do |arr, (provider, hash)|
+ arr << Hashie::Mash.new(hash.merge(slug: provider))
+ arr
+end
+
+helpers do
+ def htmlize_hash(hash, nested = false)
+ output = "<table class='hash'>"
+ hash.each_pair do |key, value|
+ output << "<tr><th>#{key}</th><td>"
+ case value
+ when Hash
+ if nested
+ output << "<span class='object'>Hash</span>"
+ else
+ output << htmlize_hash(value, true)
+ end
+ when String
+ output << value
+ else
+ output << "<span class='object'>#{value.class.to_s}</span>"
+ end
+ output << "</td></tr>"
+ end
+ output << "</table>"
+ output
+ end
end
get '/' do
@@ -0,0 +1,108 @@
+# Pulled from https://raw.github.com/dekart/openid-redis-store/master/lib/open_id/store/redis.rb
+require 'openid/util'
+require 'openid/store/interface'
+require 'openid/store/nonce'
+require 'time'
+
+module OpenID
+ module Store
+ class Redis < Interface
+ attr_accessor :key_prefix
+
+ def initialize(cache_client, key_prefix = 'openid-store:')
+ @cache_client = cache_client
+ @key_prefix = key_prefix
+ end
+
+ # Put a Association object into storage.
+ # When implementing a store, don't assume that there are any limitations
+ # on the character set of the server_url. In particular, expect to see
+ # unescaped non-url-safe characters in the server_url field.
+ def store_association(server_url, association)
+ key = assoc_key(server_url, association.handle)
+ value = serialize(association)
+
+ @cache_client.setex(key, association.lifetime, value)
+ end
+
+ # Returns a Association object from storage that matches
+ # the server_url. Returns nil if no such association is found or if
+ # the one matching association is expired. (Is allowed to GC expired
+ # associations when found.)
+ def get_association(server_url, handle=nil)
+ value = @cache_client.get(assoc_key(server_url, handle))
+
+ value ? deserialize(value) : nil
+ end
+
+ # If there is a matching association, remove it from the store and
+ # return true, otherwise return false.
+ def remove_association(server_url, handle)
+ deleted = delete(assoc_key(server_url, handle))
+ server_assoc = get_association(server_url)
+
+ if server_assoc && server_assoc.handle == handle
+ deleted = delete(assoc_key(server_url)) | deleted
+ end
+
+ deleted
+ end
+
+ # Return true if the nonce has not been used before, and store it
+ # for a while to make sure someone doesn't try to use the same value
+ # again. Return false if the nonce has already been used or if the
+ # timestamp is not current.
+ # You can use OpenID::Store::Nonce::SKEW for your timestamp window.
+ # server_url: URL of the server from which the nonce originated
+ # timestamp: time the nonce was created in seconds since unix epoch
+ # salt: A random string that makes two nonces issued by a server in
+ # the same second unique
+ def use_nonce(server_url, timestamp, salt)
+ return false if (timestamp - Time.now.to_i).abs > Nonce.skew
+ ts = timestamp.to_s # base 10 seconds since epoch
+ nonce_key = key_prefix + 'N' + server_url + '|' + ts + '|' + salt
+
+ if result = @cache_client.setnx(nonce_key, '')
+ @cache_client.expire(nonce_key, Nonce.skew + 5)
+ end
+
+ result
+ end
+
+ def assoc_key(server_url, assoc_handle=nil)
+ key = key_prefix + 'A' + server_url
+
+ if assoc_handle
+ key += '|' + assoc_handle
+ end
+
+ key
+ end
+
+ def cleanup_nonces
+ end
+
+ def cleanup
+ end
+
+ def cleanup_associations
+ end
+
+ protected
+
+ def delete(key)
+ @cache_client.del(key)
+ end
+
+ def serialize(assoc)
+ Marshal.dump(assoc)
+ end
+
+ def deserialize(assoc_str)
+ Marshal.load(assoc_str)
+ rescue ArgumentError
+ nil
+ end
+ end
+ end
+end
View
@@ -0,0 +1,32 @@
+concur:
+ name: Concur
+ author: dkastner
+ url: https://github.com/dkastner/omniauth-concur
+facebook:
+ name: Facebook
+ author: mkdynamic
+ url: https://github.com/mkdynamic/omniauth-facebook
+github:
+ name: GitHub
+ author: intridea
+ url: https://github.com/intridea/omniauth-github
+identity:
+ name: Identity
+ author: intridea
+ url: https://github.com/intridea/omniauth-identity
+ldap:
+ name: LDAP
+ author: intridea
+ url: https://github.com/intridea/omniauth-ldap
+linkedin:
+ name: LinkedIn
+ author: skorks
+ url: https://github.com/skorks/omniauth-linkedin
+open_id:
+ name: OpenID
+ author: intridea
+ url: https://github.com/intridea/omniauth-openid
+twitter:
+ name: Twitter
+ author: intridea
+ url: https://github.com/intridea/omniauth-twitter
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.

0 comments on commit dd70e00

Please sign in to comment.