lotus-auth0 0.0.2

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 393b6026dcc47cf6506f8f188c20950e00f06f80
4
+ data.tar.gz: fa22122c30c19d5c990d920d90ab2387b7d2523f
5
+ SHA512:
6
+ metadata.gz: 3b03145ac650298cf217ff1e1b9faa86ac1255f9d94b920b77f8227e67f48fecff82bf54a198bf44c9a2b7b390507485000580f82747f90ee395e3f40ca8287d
7
+ data.tar.gz: 5b3989e0da8cdffc14c146324d0836e4e6a6e33fc0f5d95ed6c6811a6f536c361e2ca53ed6deeeaecab43f6a310bc34677d8827dcfaf821da45a72769dd64b91
@@ -0,0 +1,5 @@
1
+ module Lotus
2
+ module Auth0
3
+ VERSION = '0.0.2'
4
+ end
5
+ end
@@ -0,0 +1,114 @@
1
+ require 'forwardable'
2
+ require 'lotus/utils/kernel'
3
+
4
+ module Lotus
5
+ module Model
6
+ module Adapters
7
+ module Auth0
8
+ class Collection
9
+ attr_reader :client
10
+
11
+ InvalidCollectionName = Class.new(StandardError)
12
+
13
+ def initialize(_mapped_collection, _name, _identity)
14
+ raise InvalidCollectionName unless allowed_collection_names.include?(_name.to_s)
15
+
16
+ @client_token = ENV['AUTH0_TOKEN']
17
+ @client_domain = ENV['AUTH0_DOMAIN']
18
+
19
+ @client = Auth0Client.new(
20
+ api_version: 2,
21
+ token: @client_token,
22
+ domain: @client_domain
23
+ )
24
+
25
+ @mapped_collection = _mapped_collection
26
+ @name = _name
27
+ @identity = _identity
28
+ end
29
+
30
+ def search(query, options = {})
31
+ results = case @name
32
+ when :users
33
+ # def users( per_page: nil, page: nil, include_totals: nil, sort: nil, connection: nil, fields: nil, q: nil )
34
+ @client.users(q: query.to_s, page: options[:offset], per_page: options[:limit])
35
+ when :connections
36
+ results = @client.connections
37
+ query.conditions.each do |condition|
38
+ case condition.type
39
+ when :where
40
+ results.reject! { |result| result[condition.key.to_s] != condition.value }
41
+ results
42
+ when :or
43
+ raise "#{@name} does not support this query condition"
44
+ end
45
+ end
46
+ results
47
+ else
48
+ raise NotImplementedError, 'This collection can not be searched'
49
+ end
50
+ results
51
+ end
52
+
53
+ def insert(entity)
54
+ inserted_entity = case @name
55
+ when :users
56
+ entity.delete(:identities)
57
+ @client.create_user(entity[:name], entity)
58
+ when :connections
59
+ @client.create_connection(entity)
60
+ end
61
+
62
+ inserted_entity
63
+ end
64
+
65
+ def update(entity)
66
+ updated_entities = case @name
67
+ when :users
68
+ # We can't modify these
69
+ entity.delete(:identities)
70
+ entity.delete(:name)
71
+
72
+ id = entity.delete(identity)
73
+ @client.patch_user(id, entity)
74
+ end
75
+
76
+ updated_entities
77
+ end
78
+
79
+ def delete(entity)
80
+ deleted_entity = case @name
81
+ when :users
82
+ id = entity.delete(identity)
83
+ @client.delete_user(id)
84
+ when :connections
85
+ id = entity.delete(identity)
86
+ @client.delete_connection(id)
87
+ end
88
+ end
89
+
90
+ def truncate
91
+ case @name
92
+ when :users
93
+ @client.delete_users
94
+ when :connections
95
+ @client.connections.each do |connection|
96
+ @client.delete_connection(connection['id'])
97
+ end
98
+ end
99
+ end
100
+
101
+ private
102
+
103
+ def allowed_collection_names
104
+ %w(users connections)
105
+ end
106
+
107
+ def identity
108
+ @mapped_collection.identity
109
+ end
110
+ end
111
+ end
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,57 @@
1
+ require 'forwardable'
2
+ require 'lotus/utils/kernel'
3
+
4
+ module Lotus
5
+ module Model
6
+ module Adapters
7
+ module Auth0
8
+ class Command
9
+ def initialize(_collection, _mapped_collection)
10
+ @collection = _collection
11
+ @mapped_collection = _mapped_collection
12
+ end
13
+
14
+ def create(entity)
15
+ serialized_entity = _serialize(entity)
16
+ created_entity = @collection.insert(serialized_entity)
17
+ # oh no
18
+ # @todo deep_symbolize_keys!
19
+ created_entity = eval(created_entity.to_s.gsub(/\"(\w+)\"(?==>)/, ':\1'))
20
+ _deserialize(created_entity)
21
+ end
22
+
23
+ def update(entity)
24
+ serialized_entity = _serialize(entity)
25
+ updated_entity = @collection.update(serialized_entity)
26
+ updated_entity = eval(updated_entity.to_s.gsub(/\"(\w+)\"(?==>)/, ':\1'))
27
+ _deserialize(updated_entity)
28
+ end
29
+
30
+ def delete(entity)
31
+ serialized_entity = _serialize(entity)
32
+ @collection.delete(serialized_entity)
33
+ end
34
+
35
+ def truncate
36
+ @collection.truncate
37
+ end
38
+
39
+ private
40
+
41
+ def _serialize(entity)
42
+ @mapped_collection.serialize(entity)
43
+ end
44
+
45
+ def _deserialize(entity)
46
+ @mapped_collection.deserialize([entity]).first
47
+ end
48
+
49
+ def _identity
50
+ @mapped_collection.identity
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
57
+
@@ -0,0 +1,90 @@
1
+ require 'forwardable'
2
+ require 'lotus/utils/kernel'
3
+
4
+ module Lotus
5
+ module Model
6
+ module Adapters
7
+ module Auth0
8
+ class LuceneQuery
9
+ class Where
10
+ attr_accessor :condition
11
+
12
+ def initialize(_condition)
13
+ @condition = _condition
14
+ end
15
+
16
+ def key
17
+ condition.keys.first
18
+ end
19
+
20
+ def value
21
+ condition.values.first
22
+ end
23
+
24
+ def type
25
+ :where
26
+ end
27
+
28
+ def to_s
29
+ "#{key}:#{value}"
30
+ end
31
+ end
32
+
33
+ class Or
34
+ attr_accessor :condition
35
+
36
+ def initialize(_condition)
37
+ @condition = _condition
38
+ end
39
+
40
+ def to_s
41
+ "#{condition.keys.first}:#{condition.values.first}"
42
+ end
43
+ end
44
+
45
+ def initialize(_conditions)
46
+ @conditions = _conditions
47
+ end
48
+
49
+ def to_s
50
+ previous_condition = nil
51
+ result = ''
52
+
53
+ conditions.each do |condition|
54
+ case previous_condition
55
+ when Where
56
+ if condition.is_a?(Or)
57
+ result = "(#{result}) OR #{condition}"
58
+ else
59
+ result = "#{result} AND #{condition}"
60
+ end
61
+ when Or
62
+ lhs, rhs = result.split('OR')
63
+ result = [lhs, "(#{[condition, rhs].join(' AND ')})"].join(' OR ')
64
+ else
65
+ result = condition.to_s
66
+ end
67
+
68
+ previous_condition = condition
69
+ end
70
+
71
+ result
72
+ end
73
+
74
+ def conditions
75
+ (@conditions || []).collect do |condition_type, condition|
76
+ raise ArgumentError, 'condition is invalid' if condition.keys.count > 1 || condition.values.count > 1
77
+
78
+ case condition_type
79
+ when :where, :and
80
+ Where.new(condition)
81
+ when :or
82
+ Or.new(condition)
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,82 @@
1
+ require 'uri'
2
+ require 'lotus/utils/kernel'
3
+
4
+ module Lotus
5
+ module Model
6
+ module Adapters
7
+ module Auth0
8
+ class Query
9
+ attr_reader :conditions
10
+
11
+ def initialize(collection, mapped_collection, context = nil, &block)
12
+ @collection, @mapped_collection, @context = collection, mapped_collection, context
13
+
14
+ @conditions = []
15
+ @limit = nil
16
+ @sort_order = nil
17
+ @sort_key = nil
18
+ @offset = nil
19
+
20
+ instance_eval(&block) if block_given?
21
+ end
22
+
23
+ def all
24
+ _deserialize(run)
25
+ end
26
+
27
+ def limit(limit)
28
+ @limit = limit
29
+ all
30
+ end
31
+
32
+ def sort(*args)
33
+ if args[-1].is_a?(Array)
34
+ @sort_order = args.pop
35
+ end
36
+ raise ArgumentError, 'you can only sort on one key' unless args.count == 1
37
+ @sort_key = args.first
38
+ end
39
+
40
+ def offset(num)
41
+ @offset = num
42
+ end
43
+
44
+ def where(condition = nil, &block)
45
+ _push_to_conditions(:where, condition || blk)
46
+ self
47
+ end
48
+ alias :and :where
49
+
50
+ def or(condition = nil, &block)
51
+ _push_to_conditions(:or, condition || blk)
52
+ self
53
+ end
54
+
55
+ private
56
+
57
+ def run
58
+ @collection.search(
59
+ LuceneQuery.new(conditions), {
60
+ offset: @offset,
61
+ limit: @limit,
62
+ sort_order: @sort_order,
63
+ sort_key: @sort_key
64
+ })
65
+ end
66
+
67
+ def _push_to_conditions(type, condition)
68
+ raise ArgumentError.new('You need to specify a condition.') if condition.nil?
69
+
70
+ conditions.push([type, condition])
71
+ end
72
+
73
+ def _deserialize(entity)
74
+ entity = [entity] unless entity.is_a?(Array)
75
+ entity = eval(entity.to_s.gsub(/\"(\w+)\"(?==>)/, ':\1'))
76
+ @mapped_collection.deserialize(entity)
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,65 @@
1
+ require 'lotus/model/adapters/abstract'
2
+ require 'lotus/model/adapters/implementation'
3
+
4
+ require 'lotus/model/adapters/auth0/query'
5
+ require 'lotus/model/adapters/auth0/collection'
6
+ require 'lotus/model/adapters/auth0/command'
7
+ require 'lotus/model/adapters/auth0/lucene_query'
8
+
9
+ require 'auth0'
10
+
11
+ module Lotus
12
+ module Model
13
+ module Adapters
14
+ class Auth0Adapter < Abstract
15
+ include Implementation
16
+
17
+ def initialize(mapper, uri = nil)
18
+ super
19
+
20
+ @collections = {}
21
+ end
22
+
23
+ def create(collection, entity)
24
+ command(collection).create(entity)
25
+ end
26
+
27
+ def update(collection, entity)
28
+ command(collection).update(entity)
29
+ end
30
+
31
+ def delete(collection, entity)
32
+ command(collection).delete(entity)
33
+ end
34
+
35
+ def clear(collection)
36
+ command(collection).truncate
37
+ end
38
+
39
+ def command(collection)
40
+ Auth0::Command.new(_collection(collection),
41
+ _mapped_collection(collection))
42
+ end
43
+
44
+ def query(collection, context = nil, &blk)
45
+ Auth0::Query.new(_collection(collection),
46
+ _mapped_collection(collection), &blk)
47
+ end
48
+
49
+ private
50
+
51
+ def _collection(name)
52
+ @collections[name] ||= Auth0::Collection.new(
53
+ _mapped_collection(name),
54
+ name,
55
+ _identity(name),
56
+ )
57
+ end
58
+
59
+ def _identity(_name)
60
+ _mapped_collection(_name).identity
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,10 @@
1
+ require 'lotus/model'
2
+ require 'lotus/entity'
3
+
4
+ require 'lotus/auth0/version'
5
+ require 'lotus/model/adapters/auth0_adapter'
6
+
7
+ module Lotus
8
+ module Auth0
9
+ end
10
+ end
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lotus-auth0
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Taylor Finnell
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-11-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: auth0
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry-byebug
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Auth0 integration for the Lotus framework
84
+ email:
85
+ - tmfinnell@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - lib/lotus-auth0.rb
91
+ - lib/lotus/auth0/version.rb
92
+ - lib/lotus/model/adapters/auth0/collection.rb
93
+ - lib/lotus/model/adapters/auth0/command.rb
94
+ - lib/lotus/model/adapters/auth0/lucene_query.rb
95
+ - lib/lotus/model/adapters/auth0/query.rb
96
+ - lib/lotus/model/adapters/auth0_adapter.rb
97
+ homepage: https://github.com
98
+ licenses:
99
+ - MIT
100
+ metadata: {}
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubyforge_project:
117
+ rubygems_version: 2.4.5
118
+ signing_key:
119
+ specification_version: 4
120
+ summary: Auth0 integration for the Lotus framework
121
+ test_files: []
122
+ has_rdoc: