cosmos_authentication 0.0.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/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "cosmos_authentication/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "cosmos_authentication"
7
+ s.version = CosmosAuthentication::VERSION
8
+ s.authors = ["Sebastian Edwards"]
9
+ s.email = ["sebastian@uprise.co.nz"]
10
+ s.homepage = "https://github.com/SebastianEdwards/cosmos_authentication"
11
+ s.summary = %q{A client for the cosmos authentication service.}
12
+ s.description = s.summary
13
+
14
+ s.rubyforge_project = "cosmos_authentication"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_dependency "cosmos"
22
+ end
@@ -0,0 +1,25 @@
1
+ require "cosmos_authentication/stub"
2
+ require "cosmos_authentication/resources"
3
+
4
+ module Cosmos
5
+ module Authentication
6
+ class ResourceOwner
7
+ def initialize(client, collection)
8
+ @client = client
9
+ @collection = collection
10
+ end
11
+
12
+ def data
13
+ @data ||= @collection.items.first.data.inject({}) do |hash, data|
14
+ hash.merge!({data.name => data.value})
15
+ end
16
+ end
17
+
18
+ def resources
19
+ @resources ||= @collection.links.inject({}) do |hash, link|
20
+ hash.merge!({link.rel => Stub.new(@client, Resources, link.href)})
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,29 @@
1
+ module Cosmos
2
+ module Authentication
3
+ class Resources
4
+ def initialize(client, collection)
5
+ @client = client
6
+ @collection = collection
7
+ end
8
+
9
+ def has_resource?(uri)
10
+ matches = @collection.items.select do |item|
11
+ item.href == uri
12
+ end.length > 0
13
+ end
14
+
15
+ def all
16
+ @collection.items.map do |item|
17
+ Stub.new(@client, Resource, item.href)
18
+ end
19
+ end
20
+ end
21
+
22
+ class Resource
23
+ def initialize(client, collection)
24
+ @client = client
25
+ @collection = collection
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,71 @@
1
+ require "cosmos/service"
2
+ require "cosmos_authentication/resource_owner"
3
+
4
+ module Cosmos
5
+ module Authentication
6
+ class Service < Cosmos::Service
7
+ attr_accessor :client_id, :client_secret
8
+
9
+ def client_with_credentials
10
+ raise "No credentials configured." unless client_id && client_secret
11
+ @client_with_credentials ||= client.dup.tap do |client|
12
+ client.params['client_id'] = client_id
13
+ client.params['client_secret'] = client_secret
14
+ end
15
+ end
16
+
17
+ def client_with_token(access_token)
18
+ client.dup.tap do |client|
19
+ client.headers['Authentication'] = "Bearer #{access_token}"
20
+ end
21
+ end
22
+
23
+ def get_token_with_code(code)
24
+ client = client_with_credentials
25
+ response = client.post token_link.href, {
26
+ grant_type: 'code',
27
+ code: code
28
+ }
29
+ response.body
30
+ end
31
+
32
+ def get_token_with_refresh_token(refresh_token)
33
+ client = client_with_credentials
34
+ response = client.post token_link.href, {
35
+ :grant_type => 'refresh_token',
36
+ :refresh_token => refresh_token
37
+ }
38
+ response.body
39
+ end
40
+
41
+ def get_token_with_username_and_password(username, password, scope = '')
42
+ client = client_with_credentials
43
+ response = client.post token_link.href, {
44
+ :grant_type => 'password',
45
+ :username => username,
46
+ :password => password,
47
+ :scope => 'manage_companies'
48
+ }
49
+ response.body
50
+ end
51
+
52
+ def resource_owner(access_token, klass = ResourceOwner)
53
+ client = client_with_token(access_token)
54
+ href = endpoint.link('resource_owner').href
55
+ response = client.get(href).body
56
+ if response.items.length > 0
57
+ response = client.get(response.items.first.href).body
58
+ klass.new(client, response)
59
+ end
60
+ end
61
+
62
+ def token_link
63
+ endpoint.link('oauth2_token')
64
+ end
65
+
66
+ def use_for_warden_authentication
67
+ Cosmos::Authentication.warden_service self
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,22 @@
1
+ module Cosmos
2
+ module Authentication
3
+ class Stub
4
+ attr_reader :href
5
+
6
+ def initialize(client, klass, href)
7
+ @client = client
8
+ @klass = klass
9
+ @href = href
10
+ end
11
+
12
+ def method_missing(method_sym, *args)
13
+ @object ||= @klass.new(@client, @client.get(href).body)
14
+ if @object.respond_to?(method_sym)
15
+ @object.send method_sym, *args
16
+ else
17
+ super
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ module CosmosAuthentication
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,28 @@
1
+ module Cosmos
2
+ module Authentication
3
+ module Warden
4
+ module StrategyMixin
5
+ def service
6
+ Cosmos::Authentication.warden_service
7
+ end
8
+
9
+ def resource_owner(access_token)
10
+ service.resource_owner access_token
11
+ end
12
+
13
+ def find_user_by_access_token(access_token)
14
+ if ro = resource_owner(access_token)
15
+ success!(ro)
16
+ else
17
+ session[:access_token] = nil
18
+ end
19
+ end
20
+ end
21
+
22
+ require "cosmos_authentication/warden_strategies/access_token"
23
+ require "cosmos_authentication/warden_strategies/code"
24
+ require "cosmos_authentication/warden_strategies/password"
25
+ require "cosmos_authentication/warden_strategies/refresh_token"
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,17 @@
1
+ module Cosmos
2
+ module Authentication
3
+ module Warden
4
+ ::Warden::Strategies.add(:access_token) do
5
+ include StrategyMixin
6
+
7
+ def valid?
8
+ session[:access_token]
9
+ end
10
+
11
+ def authenticate!
12
+ find_user_by_access_token(session[:access_token])
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,20 @@
1
+ module Cosmos
2
+ module Authentication
3
+ module Warden
4
+ ::Warden::Strategies.add(:code) do
5
+ include StrategyMixin
6
+
7
+ def valid?
8
+ params[:code]
9
+ end
10
+
11
+ def authenticate!
12
+ if token = service.get_token_with_code(params[:code])
13
+ session[:refresh_token] = token['refresh_token']
14
+ session[:access_token] = token['access_token']
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,21 @@
1
+ module Cosmos
2
+ module Authentication
3
+ module Warden
4
+ ::Warden::Strategies.add(:password) do
5
+ include StrategyMixin
6
+
7
+ def valid?
8
+ params[:username] && params[:password]
9
+ end
10
+
11
+ def authenticate!
12
+ args = [params[:username], params[:password]]
13
+ if token = service.get_token_with_username_and_password(*args)
14
+ session[:refresh_token] = token['refresh_token']
15
+ session[:access_token] = token['access_token']
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ module Cosmos
2
+ module Authentication
3
+ module Warden
4
+ ::Warden::Strategies.add(:refresh_token) do
5
+ include StrategyMixin
6
+
7
+ def valid?
8
+ session[:refresh_token]
9
+ end
10
+
11
+ def authenticate!
12
+ if token = service.get_token_with_refresh_token(session[:refresh_token])
13
+ session[:refresh_token] = token['refresh_token']
14
+ session[:access_token] = token['access_token']
15
+ find_user_by_access_token(session[:access_token])
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,12 @@
1
+ require "cosmos_authentication/version"
2
+ require "cosmos_authentication/service"
3
+ require "cosmos_authentication/warden" if Warden
4
+
5
+ module Cosmos
6
+ module Authentication
7
+ def self.warden_service(service = nil)
8
+ @warden_service = service unless service.nil?
9
+ @warden_service || raise('No authentication service configured for warden.'.inspect)
10
+ end
11
+ end
12
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cosmos_authentication
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Sebastian Edwards
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: cosmos
16
+ requirement: &70098052432800 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70098052432800
25
+ description: A client for the cosmos authentication service.
26
+ email:
27
+ - sebastian@uprise.co.nz
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - Rakefile
35
+ - cosmos_authentication.gemspec
36
+ - lib/cosmos_authentication.rb
37
+ - lib/cosmos_authentication/resource_owner.rb
38
+ - lib/cosmos_authentication/resources.rb
39
+ - lib/cosmos_authentication/service.rb
40
+ - lib/cosmos_authentication/stub.rb
41
+ - lib/cosmos_authentication/version.rb
42
+ - lib/cosmos_authentication/warden.rb
43
+ - lib/cosmos_authentication/warden_strategies/access_token.rb
44
+ - lib/cosmos_authentication/warden_strategies/code.rb
45
+ - lib/cosmos_authentication/warden_strategies/password.rb
46
+ - lib/cosmos_authentication/warden_strategies/refresh_token.rb
47
+ homepage: https://github.com/SebastianEdwards/cosmos_authentication
48
+ licenses: []
49
+ post_install_message:
50
+ rdoc_options: []
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ requirements: []
66
+ rubyforge_project: cosmos_authentication
67
+ rubygems_version: 1.8.15
68
+ signing_key:
69
+ specification_version: 3
70
+ summary: A client for the cosmos authentication service.
71
+ test_files: []