google-directory 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ae80e614af5bed3fee0dfdc688da21f4758732c0
4
- data.tar.gz: c9e47a038f68047d0405635fda1271fccb0ba246
3
+ metadata.gz: 80b661f11277fc8460c4e7c2397ecb5f88693bf9
4
+ data.tar.gz: 225a3ddcd0aefb129923c95b24297164bd449729
5
5
  SHA512:
6
- metadata.gz: b7eff4e53da4d5ad2288ea779397838bd946b1cbb32978293c560cc207884ed99fe9743b5fae79226e2e495e4c37877256af0e5a89f12787f41ccab9d0799589
7
- data.tar.gz: f6919051596fb55a8289fbea37097b18f8894dfa87c397c7d8d8e5ed6605d72d92b18b962a87daf8d6bdbf4530b4cb2790ff3a8b8145efe03cf6b1b98f6be2c4
6
+ metadata.gz: f8a06cce05e7ea09eb87f3220d1adf3e0c3f0df9bd4e50f03fdd2f2c35fc876603894cead23f1d39e184b0618a5ae168eece2954d960db8610453366cfd5ab63
7
+ data.tar.gz: 0550b57fd0e411351c199d7b5292b49e222a1c5654b93cda61a1987635753d54362f804a50600fc8f2150f6e23e168283704b3029e789ef8b7839cd0ec8b8bb0
data/README.md CHANGED
@@ -3,7 +3,17 @@
3
3
  ## Description
4
4
 
5
5
  Simple Google Directory API wrapper for Ruby on Rails.
6
- This relies on the [Google API Ruby Client](https://github.com/Omac/google-directory.git).
6
+ This relies on the [Google API Ruby Client](https://github.com/google/google-api-ruby-client).
7
+
8
+ **This library is in alpha. Future incompatible changes may be necessary.**
9
+
10
+ ## Requirements
11
+
12
+ * An active project in the Google Developers Console.
13
+ * A service-to-service Client ID. [How to](https://developers.google.com/console/help/new/#serviceaccounts).
14
+ * A valid P12 key.
15
+ * The Admin SDK enabled.
16
+ * Authorization from the Google Apps Admin Console to the Client ID to access the scope `https://www.googleapis.com/auth/admin.directory.user`.
7
17
 
8
18
  ## Install
9
19
 
@@ -15,22 +25,22 @@ gem 'google-directory'
15
25
 
16
26
  ## Configuration
17
27
 
18
- First configure your API in `initializers/google_directory.rb`
28
+ First configure your API in `config/initializers/google_directory.rb`
19
29
 
20
30
  ``` ruby
21
31
  GoogleDirectory.configure do
22
32
 
23
- # Optional Token Store
33
+ # OPTIONAL. Use a YAML file to store the requested access tokens. When the token is refreshed, this file will be updated.
24
34
  use_yaml Rails.root.join('config', 'google_directory.yaml')
25
35
 
26
- # Required
27
- admin_email 'admin@domain.com'
28
- key_file Rails.root.join('config', 'keys', 'private_key.p12')
29
- key_passphrase 'notasecret'
30
- issuer 'xxxxxxx@developer.gserviceaccount.com'
36
+ # Required attributes
37
+ admin_email 'admin@domain.com'
38
+ key_file Rails.root.join('config', 'keys', 'private_key.p12')
39
+ key_passphrase 'notasecret'
40
+ issuer 'xxxxxxx@developer.gserviceaccount.com'
31
41
 
32
42
  # Optional attributes
33
- application_name 'My Application'
43
+ application_name 'My Application'
34
44
  application_version '1.0.0'
35
45
 
36
46
  end
@@ -47,14 +57,12 @@ development:
47
57
  expires_in: 3600
48
58
  ```
49
59
 
50
- ### Multiple Google API access tokens using scopes
60
+ ### Multiple API clients using scopes
51
61
 
52
- Specify a scope in the configuration `initializers/google_directory.rb`.
62
+ Specify a scope in the configuration `config/initializers/google_directory.rb`.
53
63
 
54
64
  ``` ruby
55
65
  GoogleDirectory.configure do
56
-
57
- use_yaml Rails.root.join('config', 'google_directory.yaml')
58
66
 
59
67
  scope :domain_one do
60
68
  admin_email 'admin@domain_one.com'
@@ -68,21 +76,21 @@ GoogleDirectory.configure do
68
76
 
69
77
  end
70
78
  ```
71
- Keep in mind that the Token Store will be the same for all the scopes configured.
72
79
 
73
80
  ## Usage
81
+
74
82
  ``` ruby
75
83
  google = GoogleDirectory::Client.new
76
84
 
77
85
  google.find_users
78
86
 
79
- google.create_user(email, "given_name", "family_name", "password")
87
+ google.create_user("email", "given_name", "family_name", "password")
80
88
 
81
- google.update_user(email, update_data)
89
+ google.update_user("email", update_data)
82
90
 
83
- google.delete_user(email)
91
+ google.delete_user("email")
84
92
 
85
- google.update_user_password(email, new_password)
93
+ google.update_user_password("email", "new_password")
86
94
 
87
95
  ```
88
96
 
@@ -100,5 +108,7 @@ domain_two.find_users
100
108
 
101
109
  * `use_active_model` for database token store.
102
110
  * Build the configuration generator.
103
- * Implement more parameters from the Google API.
111
+ * Implement more parameters and calls to the Admin SDK.
112
+ * Better error handling.
113
+ * Documentation.
104
114
  * Testing.
@@ -7,19 +7,18 @@ require "google-directory/version"
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "google-directory"
9
9
  s.version = GoogleDirectory::VERSION
10
+
10
11
  s.authors = ["Omar Osorio"]
11
12
  s.email = ["omar@kioru.com"]
12
- s.homepage = "https://github.com/Omac/google-directory"
13
+ s.homepage = "https://github.com/kioru/google-directory"
14
+
13
15
  s.summary = "Simple Google Directory API client wrapper for Rails"
14
- s.description = "Relying on the Google Ruby API gem you are able to set up a Rails project for consuming the Directory API (or the Admin SDK). Even multiple domain access is allowed using scopes."
16
+ s.description = "Relying on the Google Ruby API gem you are able to set up a Rails project for consuming the Directory API (or the Admin SDK). Even multiple domain access is possible using scopes."
15
17
  s.license = "MIT"
16
18
 
17
- s.files = `git ls-files`.split("\n") # Dir["{app,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.rdoc"]
19
+ s.files = `git ls-files -z`.split("\x0")
18
20
  s.test_files = Dir["test/**/*"]
19
21
  s.require_paths = ['lib']
20
22
 
21
23
  s.add_dependency 'google-api-client', '~> 0.8'
22
-
23
- s.add_development_dependency 'rails', '~> 4.2'
24
- s.add_development_dependency "sqlite3"
25
24
  end
@@ -4,109 +4,98 @@ require 'google/api_client'
4
4
 
5
5
  module GoogleDirectory
6
6
 
7
- class Client
8
-
9
- attr_reader :directory_api, :client, :config
10
-
11
- def initialize(scope = :main)
12
- @config = GoogleDirectory.configuration
13
- @config = @config.using(scope) if @config.scope_name != scope
14
-
15
-
16
- @key = Google::APIClient::KeyUtils.load_from_pkcs12(@config.key_file, @config.key_passphrase)
17
- @client = Google::APIClient.new(:application_name => @config.application_name, :application_version => @config.application_version )
18
-
19
- @client.authorization = Signet::OAuth2::Client.new(
20
- :token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
21
- :audience => 'https://accounts.google.com/o/oauth2/token',
22
- :scope => 'https://www.googleapis.com/auth/admin.directory.user',
23
- :issuer => @config.issuer,
24
- :person => @config.admin_email,
25
- :signing_key => @key
26
- )
27
-
28
- if token = @config.load_token
29
-
30
- token['issued_at'] = Time.parse( token['issued_at'] )
31
- @client.authorization.update_token!(token)
32
-
33
- else
34
- token = @client.authorization.fetch_access_token!
35
- token['issued_at'] = @client.authorization.issued_at.to_s
36
- @config.save_token(token)
37
-
38
- end
39
-
40
- @directory_api = @client.discovered_api('admin', 'directory_v1')
41
- end
42
-
43
-
44
- #
45
- # https://developers.google.com/admin-sdk/directory/v1/reference/users/list#auth
46
- #
47
- # Optional Parameters:
48
- # customFieldMask
49
- # customer
50
- # domain
51
- # maxResults
52
- # orderBy
53
- # pageToken
54
- # projection
55
- # query
56
- # showDeleted
57
- # sortOrder
58
- # viewType
59
- #
60
- def find_users(params)
61
- execute(:api_method => directory_api.users.list, :parameters => params)
62
- end
63
-
64
- def find_user_by_email(email)
65
- execute(:api_method => directory_api.users.get, :parameters => { 'userKey' => email })
66
- end
67
-
68
- #
69
- # https://developers.google.com/admin-sdk/directory/v1/reference/users/insert
70
- #
71
- def create_user(email, given_name, family_name, password, opts = {})
72
- opts = {
73
- 'name' => { 'givenName' => given_name, 'familyName' => family_name},
74
- 'password' => password,
75
- 'primaryEmail' => email,
76
- 'changePasswordAtNextLogin' => true
77
- }.merge(opts)
78
-
79
- execute(:api_method => directory_api.users.insert, :body_object => opts)
80
- end
81
-
82
- def delete_user(email)
83
- execute(:api_method => directory_api.users.delete, :parameters => {'userKey' => email})
84
- end
85
-
86
- def update_user(email, update_data = {})
87
- execute(:api_method => directory_api.users.update, :parameters => {'userKey' => email}, :body_object => update_data)
88
- end
89
-
90
- def update_user_password(email, password)
91
- update_user(email, {'password' => password, 'changePasswordAtNextLogin' => false})
92
- end
93
-
94
- private
95
-
96
- def execute(opts)
97
- if client.authorization.expired?
98
- token = client.authorization.refresh!
99
- token['issued_at'] = client.authorization.issued_at.to_s
100
- config.save_token(token)
101
- end
102
-
103
- result = client.execute(opts)
104
- return JSON.parse(result.body) if result.status == 200
105
- return nil if [204, 404].include?(result.status)
106
-
107
- Rails.logger.error("== Google ERROR ==\n\t- execute(#{opts})\n\t- response:\n#{result.response.to_yaml}")
108
-
109
- end
110
- end
7
+ class Client
8
+
9
+ attr_reader :directory_api, :client, :config, :result
10
+
11
+ def initialize(scope = :main)
12
+ @config = GoogleDirectory.configuration
13
+ @config = @config.using(scope) if @config.scope_name != scope
14
+
15
+
16
+ @key = Google::APIClient::KeyUtils.load_from_pkcs12(@config.key_file, @config.key_passphrase)
17
+ @client = Google::APIClient.new(:application_name => @config.application_name, :application_version => @config.application_version )
18
+
19
+ @client.authorization = Signet::OAuth2::Client.new(
20
+ :token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
21
+ :audience => 'https://accounts.google.com/o/oauth2/token',
22
+ :scope => 'https://www.googleapis.com/auth/admin.directory.user',
23
+ :issuer => @config.issuer,
24
+ :person => @config.admin_email,
25
+ :signing_key => @key
26
+ )
27
+
28
+ if token = @config.load_token
29
+
30
+ token['issued_at'] = Time.parse( token['issued_at'] )
31
+ @client.authorization.update_token!(token)
32
+
33
+ else
34
+ token = @client.authorization.fetch_access_token!
35
+ token['issued_at'] = @client.authorization.issued_at.to_s
36
+ @config.save_token(token)
37
+
38
+ end
39
+
40
+ @directory_api = @client.discovered_api('admin', 'directory_v1')
41
+ end
42
+
43
+
44
+ def find_users(params)
45
+ execute(:api_method => directory_api.users.list, :parameters => params)
46
+ end
47
+
48
+ def find_user_by_email(email)
49
+ execute(:api_method => directory_api.users.get, :parameters => { 'userKey' => email })
50
+ end
51
+
52
+ #
53
+ # https://developers.google.com/admin-sdk/directory/v1/reference/users/insert
54
+ #
55
+ def create_user(email, given_name, family_name, password, opts = {})
56
+ opts = {
57
+ 'name' => { 'givenName' => given_name, 'familyName' => family_name},
58
+ 'password' => password,
59
+ 'primaryEmail' => email,
60
+ 'changePasswordAtNextLogin' => true
61
+ }.merge(opts)
62
+
63
+ execute(:api_method => directory_api.users.insert, :body_object => opts)
64
+ end
65
+
66
+ def delete_user(email)
67
+ execute(:api_method => directory_api.users.delete, :parameters => {'userKey' => email})
68
+ end
69
+
70
+ def update_user(email, update_data = {})
71
+ execute(:api_method => directory_api.users.update, :parameters => {'userKey' => email}, :body_object => update_data)
72
+ end
73
+
74
+ def update_user_password(email, password)
75
+ update_user(email, {'password' => password, 'changePasswordAtNextLogin' => false})
76
+ end
77
+
78
+ private
79
+
80
+ def execute(opts)
81
+ if client.authorization.expired?
82
+ token = client.authorization.refresh!
83
+ token['issued_at'] = client.authorization.issued_at.to_s
84
+ config.save_token(token)
85
+ end
86
+
87
+ @result = client.execute(opts)
88
+
89
+ case result.status
90
+ when 204 then return true
91
+ when 404 then return false
92
+ when 200 then return JSON.parse(result.body)
93
+ end
94
+
95
+ Rails.logger.error("== Google ERROR ==\n\t- execute(#{opts})\n\t- response:\n#{result.response.to_yaml}")
96
+
97
+ false
98
+ end
99
+ end
111
100
 
112
101
  end
@@ -1,3 +1,3 @@
1
1
  module GoogleDirectory
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google-directory
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Omar Osorio
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-22 00:00:00.000000000 Z
11
+ date: 2015-08-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: google-api-client
@@ -24,37 +24,9 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0.8'
27
- - !ruby/object:Gem::Dependency
28
- name: rails
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - "~>"
32
- - !ruby/object:Gem::Version
33
- version: '4.2'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: '4.2'
41
- - !ruby/object:Gem::Dependency
42
- name: sqlite3
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - ">="
46
- - !ruby/object:Gem::Version
47
- version: '0'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- version: '0'
55
27
  description: Relying on the Google Ruby API gem you are able to set up a Rails project
56
28
  for consuming the Directory API (or the Admin SDK). Even multiple domain access
57
- is allowed using scopes.
29
+ is possible using scopes.
58
30
  email:
59
31
  - omar@kioru.com
60
32
  executables: []
@@ -114,7 +86,7 @@ files:
114
86
  - test/dummy/public/favicon.ico
115
87
  - test/google-directory_test.rb
116
88
  - test/test_helper.rb
117
- homepage: https://github.com/Omac/google-directory
89
+ homepage: https://github.com/kioru/google-directory
118
90
  licenses:
119
91
  - MIT
120
92
  metadata: {}