Skip to content

Commit

Permalink
Dabbling with OpenID::Store and MongoDB. Now put those associations a…
Browse files Browse the repository at this point in the history
…nd nonces in those mongos.
  • Loading branch information
Dylan Egan committed Jun 5, 2010
0 parents commit 98d3367
Show file tree
Hide file tree
Showing 11 changed files with 443 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.DS_Store

*.tmproj
tmtags

*~
\#*
.\#*

*.swp

pkg

.bundle
9 changes: 9 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
source :rubygems

gem 'ruby-openid', '~> 2.1.7'
gem 'bson_ext', '~> 1.0.1'
gem 'mongoid', '~> 1.9.0'

group :rake do
gem 'rake'
end
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
## openid_mongoid_store

Storing your OpenIDs in your Mongos.

### usage

database = Mongo::Connection.new.db('openid_store_mongoid')
store = OpenID::Store::Mongoid.new(database)
server = OpenID::Server::Server.new(store, ...)

### license (MIT)

Copyright (c) 2010 Dylan Egan

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CON
11 changes: 11 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
begin
require ::File.expand_path('.bundle/environment', __FILE__)
rescue LoadError
require "rubygems"
require "bundler"
Bundler.setup
end

Bundler.require(:default, :rake)

Dir["tasks/*.rake"].sort.each { |file| load file }
66 changes: 66 additions & 0 deletions lib/openid/store/mongoid.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
require 'openid/store/interface'

module OpenID
module Store
class Mongoid < OpenID::Store::Interface
def initialize(connection)
::Mongoid.database = connection
end

def store_association(server_url, assoc)
remove_association(server_url, assoc.handle)
OpenIDStoreMongoid::Association.create!(:server_url => server_url,
:handle => assoc.handle,
:secret => BSON::Binary.new(assoc.secret),
:issued => assoc.issued,
:lifetime => assoc.lifetime,
:assoc_type => assoc.assoc_type)
end

def get_association(server_url, handle=nil)
assocs = if handle.blank?
OpenIDStoreMongoid::Association.all(:conditions => { :server_url => server_url })
else
OpenIDStoreMongoid::Association.all(:conditions => { :server_url => server_url, :handle => handle })
end

assocs.to_a.reverse.each do |assoc|
a = assoc.from_record
if a.expires_in == 0
assoc.destroy
else
return a
end
end if assocs.any?

return nil
end

def remove_association(server_url, handle)
OpenIDStoreMongoid::Association.destroy_all(:conditions => { :server_url => server_url, :handle => handle }) > 0 ? true : false
end

def use_nonce(server_url, timestamp, salt)
return false if OpenIDStoreMongoid::Nonce.first(:conditions => { :server_url => server_url, :timestamp => timestamp, :salt => salt }) or (timestamp - Time.now.to_i).abs > OpenID::Nonce.skew
OpenIDStoreMongoid::Nonce.create!(:server_url => server_url, :timestamp => timestamp, :salt => salt)
return true
end

def cleanup_nonces
now = Time.now.to_i
OpenIDStoreMongoid::Nonce.destroy_all(:conditions => { :timestamp.gt => (now + OpenID::Nonce.skew), :timestamp.lt => (now - OpenID::Nonce.skew) })
end

def cleanup_associations
count = 0
OpenIDStoreMongoid::Association.all(:conditions => { :issued.gt => 0 }).each do |association|
if association.lifetime + association.issued > Time.now.to_i
association.destroy
count += 1
end
end
count
end
end
end
end
4 changes: 4 additions & 0 deletions lib/openid_store_mongoid.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'mongoid'
require 'openid_store_mongoid/association'
require 'openid_store_mongoid/nonce'
require 'openid/store/mongoid'
15 changes: 15 additions & 0 deletions lib/openid_store_mongoid/association.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module OpenIDStoreMongoid
class Association
include ::Mongoid::Document
field :handle
field :secret, :type => Binary
field :issued, :type => Integer
field :lifetime, :type => Integer
field :assoc_type
field :server_url, :type => Binary

def from_record
OpenID::Association.new(handle, secret.to_s, issued, lifetime, assoc_type)
end
end
end
8 changes: 8 additions & 0 deletions lib/openid_store_mongoid/nonce.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module OpenIDStoreMongoid
class Nonce
include ::Mongoid::Document
field :salt
field :server_url
field :timestamp, :type => Integer
end
end
3 changes: 3 additions & 0 deletions lib/openid_store_mongoid/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module OpenIDStoreMongoid
VERSION = '0.0.1'
end
43 changes: 43 additions & 0 deletions tasks/gem.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require 'rubygems/package_task'
require File.dirname(__FILE__) + '/../lib/openid_store_mongoid/version'

GEM_SPEC = Gem::Specification.new do |s|
s.name = "openid_store_mongoid"
s.version = OpenIDStoreMongoid::VERSION
s.platform = Gem::Platform::RUBY

s.summary = 'Storing your OpenIDs in your Mongos.'
s.description = "Why does a squirrel swim on its back?\nTo keep its nuts dry"

s.required_ruby_version = ">= 1.8.6"
s.required_rubygems_version = ">= 1.3.5"

# dependencies
s.add_dependency 'rake', '>= 0.8.3', '< 0.9'
bundle = Bundler::Definition.from_gemfile("Gemfile")
bundle.dependencies.select { |d| !d.groups.include?(:development) and !d.groups.include?(:rake) }.
each { |d| s.add_dependency(d.name, d.requirement.to_s) }

bundle.dependencies.select { |d| d.groups.include?(:development) }.
each { |d| s.add_development_dependency(d.name, d.requirement.to_s) }

s.files = FileList["lib/**/*.rb", "test/**/*.rb", "tasks/**/*.rake", "Rakefile", "README.md"]

s.bindir = 'bin'
s.executables = []

s.require_path = 'lib'

s.extra_rdoc_files = %w(README.md)

s.homepage = 'http://github.com/abcde/openid_store_mongoid'
s.licenses = ['MIT']

s.author = 'Dylan Egan'
s.email = 'dylanegan@gmail.com'
end

gem_package = Gem::PackageTask.new(GEM_SPEC) do |pkg|
pkg.need_tar = false
pkg.need_zip = false
end
Loading

0 comments on commit 98d3367

Please sign in to comment.