google_directory 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: be15ff583f69da807620a4b2aef6466b6a896e5d9fc50bdd36f5855fb63b4d50
4
+ data.tar.gz: b7ecff040096c31c35a8b29ff13859e763f9cb4fcb3d025247c255f32fb6e801
5
+ SHA512:
6
+ metadata.gz: dc47b4ce9e1d17d53ff5ec0a109feec4b0cc82951917cdfe3b1392aa16688d594152710a321d7c7225654348962995ddef87c2b368d0250412a4992ac675d17a
7
+ data.tar.gz: 0f482a2d2cfc9ae100b0e5b0617c8892a5445821ac64da29fc9334c32aedd0bb616748b63751e500ca3a1e97dddffdc6863659b0bdd3ea1ff4d081af82a86cb6
@@ -0,0 +1,6 @@
1
+ require "google_directory/version"
2
+ require "google_directory/connection"
3
+
4
+ module GoogleDirectory
5
+ # Your code goes here...
6
+ end
@@ -0,0 +1,100 @@
1
+ require 'google/apis/admin_directory_v1'
2
+ require 'googleauth'
3
+ require 'googleauth/stores/file_token_store'
4
+
5
+ require 'fileutils'
6
+
7
+ require "google_directory/user_commands"
8
+
9
+ module GoogleDirectory
10
+
11
+ # The GoogleDirectory, makes it easy to work with Google Directory.
12
+ # @since 0.1.0
13
+ #
14
+ # @note Its important to have your oauth setup and its client_secret.json file downloaded in the root directory
15
+ # @note You can also use environment variables to override google defaults as wanted.
16
+ class Connection
17
+
18
+ include GoogleDirectory::UserCommands
19
+
20
+ # default settings from google for all users
21
+ OOB_URI = ENV['OOB_URI'] || 'urn:ietf:wg:oauth:2.0:oob'
22
+ CREDENTIALS_PATH = ENV['CREDENTIALS_PATH'] || File.join( Dir.home, '.credentials', "admin-directory_v1-ruby-accounts.yaml")
23
+
24
+ # Get info the Google Cloud Admin
25
+ # https://console.cloud.google.com/apis/ or
26
+ # build using: https://developers.google.com/api-client-library/ruby/guide/aaa_client_secrets
27
+ CLIENT_SECRETS_PATH = ENV['CLIENT_SECRETS_PATH'] || 'client_secret.json'
28
+
29
+ # Scope options - https://www.googleapis.com/auth/admin.directory.user
30
+ SCOPE = Google::Apis::AdminDirectoryV1::AUTH_ADMIN_DIRECTORY_USER
31
+
32
+ # Initialize the API
33
+ # https://www.rubydoc.info/github/google/google-api-ruby-client/Google/Apis/AdminDirectoryV1/DirectoryService
34
+ # https://github.com/google/google-api-ruby-client/issues/360
35
+
36
+ # @note make connection to google directory services
37
+ # @param service [Class] the default is: Google::Apis::AdminDirectoryV1::DirectoryService
38
+ def initialize( service: Google::Apis::AdminDirectoryV1::DirectoryService )
39
+ app_name ||= ENV['APPLICATION_NAME'] || 'google_cloud_app_name'
40
+ @service = service.new
41
+ @service.client_options.application_name = app_name
42
+ @service.authorization = authorize
43
+ end
44
+
45
+ # @note Run a command against Google Directory
46
+ #
47
+ # @param action [Symbol] choose action to perform these include: :user_get, :user_exists? (t/f), :user_create, :user_delete, :user_update & convience commands :user_suspend, :user_reactivate, :user_change_password
48
+ # @param attributes [Hash] attributes needed to perform action
49
+ # @return [Hash] will hopefully return a hash with {success: {action: :action, attributes: {primary_email: "user@domain"}, response: "whatever google answered - usually a hash"} }
50
+ def run( action:, attributes: {} )
51
+ response = { success: nil, error: nil }
52
+ begin
53
+ response[:success] = send( action, attributes: attributes )
54
+ rescue Google::Apis::ClientError => error
55
+ response[:error] = {action: action, attributes: attributes,
56
+ error: error}
57
+ end
58
+ response
59
+ end
60
+ alias_method :execute, :run
61
+
62
+ # # answer = GoogleDirectory.(action: :user_get, attributes: {primary_email: "btihen@las.ch"})
63
+ # def self.call(service: Google::Apis::AdminDirectoryV1::DirectoryService,
64
+ # app_name: nil,
65
+ # action:, attributes: {} )
66
+ # new(service: service, app_name: app_name).
67
+ # run(action: action, attributes: attributes)
68
+ # end
69
+
70
+ private
71
+ attr_reader :service
72
+ ##
73
+ # FROM:
74
+ # https://www.rubydoc.info/github/google/google-api-ruby-client/Google/Apis/AdminDirectoryV1/DirectoryService
75
+ # Ensure valid credentials, either by restoring from the saved credentials
76
+ # files or intitiating an OAuth2 authorization. If authorization is required,
77
+ # the user's default browser will be launched to approve the request.
78
+ # @return [Google::Auth::UserRefreshCredentials] OAuth2 credentials
79
+ def authorize
80
+ FileUtils.mkdir_p(File.dirname(CREDENTIALS_PATH))
81
+
82
+ client_id = Google::Auth::ClientId.from_file(CLIENT_SECRETS_PATH)
83
+ token_store = Google::Auth::Stores::FileTokenStore.new(file: CREDENTIALS_PATH)
84
+ authorizer = Google::Auth::UserAuthorizer.new( client_id, SCOPE, token_store )
85
+ user_id = 'default'
86
+ credentials = authorizer.get_credentials(user_id)
87
+ if credentials.nil?
88
+ url = authorizer.get_authorization_url(
89
+ base_url: OOB_URI)
90
+ puts "Open the following URL in the browser and enter the " +
91
+ "resulting code after authorization"
92
+ puts url
93
+ code = gets
94
+ credentials = authorizer.get_and_store_credentials_from_code(
95
+ user_id: user_id, code: code, base_url: OOB_URI)
96
+ end
97
+ credentials
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,117 @@
1
+ # already required in Google API
2
+ # require 'SecureRandom'
3
+
4
+ module GoogleDirectory
5
+
6
+ # @note DirectoryService Ruby API Commands - https://www.rubydoc.info/github/google/google-api-ruby-client/Google/Apis/AdminDirectoryV1/DirectoryService
7
+ # @note GoogleUser Attributes - https://www.rubydoc.info/github/google/google-api-ruby-client/Google/Apis/AdminDirectoryV1/User
8
+ module UserCommands
9
+
10
+ # @note Get GoogleDirectory User Info
11
+ #
12
+ # @param attributes [Hash] this attribute MUST include: { primary_email: "username@domain.com" }
13
+ # @return [Hash] formatted as {success: {action: :user_get, attributes: {primary_email: "user@domain"}, response: GoogleUserObject } }
14
+ def user_get( attributes: )
15
+ response = service.get_user( attributes[:primary_email] )
16
+ {action: :user_get, user: attributes[:primary_email], response: response}
17
+ end
18
+
19
+ # @note Test if user exists in Google Directory
20
+ #
21
+ # @param attributes [Hash] this attribute MUST include: { primary_email: "username@domain.com" }
22
+ # @return [Hash] formatted as {success: {action: :user_exists?, attributes: {primary_email: "user@domain"}, response: Boolean } }
23
+ def user_exists?( attributes: )
24
+ begin
25
+ response = service.get_user( attributes[:primary_email] )
26
+ return {action: :user_exists?, user: attributes[:primary_email], response: true}
27
+ rescue Google::Apis::ClientError => error
28
+ if error.message.include? 'notFound'
29
+ return {action: :user_exists?, user: attributes[:primary_email], response: false}
30
+ else
31
+ raise error
32
+ end
33
+ end
34
+ end
35
+
36
+ # @note creates a new Google Directory User
37
+ #
38
+ # @param attributes [Hash] this attribute MUST include: { primary_email: "username@domain.com", name: {given_name: "First Names", family_name: "LAST NAMES" } }
39
+ # @return [Hash] formatted as {success: {action: :user_create, attributes: {primary_email: "user@domain"}, response: GoogleUserObject } }
40
+ def user_create( attributes: )
41
+ # http://blog.liveedu.tv/ruby-generate-random-string/
42
+ password = SecureRandom.base64
43
+ defaults = { suspended: true, password: password, change_password_at_next_login: true }
44
+ user_attr = defaults.merge( attributes )
45
+ # create a google user object
46
+ user_object = Google::Apis::AdminDirectoryV1::User.new user_attr
47
+ # create user in directory services
48
+ response = service.insert_user( user_object )
49
+ {action: :user_create, user: attributes[:primary_email], response: response}
50
+ end
51
+
52
+ # @note updates an exising Google Directory User
53
+ #
54
+ # @param attributes [Hash] this attribute MUST include: { primary_email: "username@domain.com", attributes_to_change: "" } }
55
+ # @return [Hash] formatted as {success: {action: :user_update, attributes: {primary_email: "user@domain"}, response: GoogleUserObject } }
56
+ def user_update( attributes: )
57
+ # create a user object for google to update
58
+ response = update_user( attributes )
59
+ {action: :user_update, user: attributes[:primary_email], response: response}
60
+ end
61
+
62
+ # @note updates an exising Google Directory User password - convience method instead of using :user_update
63
+ #
64
+ # @param attributes [Hash] this attribute MUST include: { primary_email: "username@domain.com", password: "secret" } - if no password is included a random password will be assigned
65
+ # @return [Hash] formatted as {success: {action: :user_change_password, attributes: {primary_email: "user@domain"}, response: GoogleUserObject } }
66
+ def user_change_password( attributes: )
67
+ password = SecureRandom.base64
68
+ defaults = { password: password, change_password_at_next_login: true }
69
+ user_attr = defaults.merge( attributes )
70
+
71
+ response = update_user( user_attr )
72
+ {action: :user_change_password, user: attributes[:primary_email], response: response}
73
+ end
74
+
75
+ # @note activates an exising Google Directory User password - convience method instead of using :user_update
76
+ #
77
+ # @param attributes [Hash] this attribute MUST include: { primary_email: "username@domain.com" }
78
+ # @return [Hash] formatted as {success: {action: :user_reactivate, attributes: {primary_email: "user@domain"}, response: GoogleUserObject } }
79
+ def user_reactivate( attributes: )
80
+ defaults = { :suspended => false }
81
+ user_attr = defaults.merge( attributes )
82
+
83
+ response = update_user( user_attr )
84
+ {action: :user_reactivate, user: attributes[:primary_email], response: response}
85
+ end
86
+
87
+ # @note suspends an exising Google Directory User password - convience method instead of using :user_update
88
+ #
89
+ # @param attributes [Hash] this attribute MUST include: { primary_email: "username@domain.com" }
90
+ # @return [Hash] formatted as {success: {action: :user_suspend, attributes: {primary_email: "user@domain"}, response: GoogleUserObject } }
91
+ def user_suspend( attributes: )
92
+ defaults = { :suspended => true }
93
+ user_attr = defaults.merge( attributes )
94
+
95
+ response = update_user( user_attr )
96
+ {action: :user_suspend, user: attributes[:primary_email], response: response}
97
+ end
98
+
99
+ # @note deletes an exising Google Directory User
100
+ #
101
+ # @param attributes [Hash] this attribute MUST include: { primary_email: "username@domain.com" }
102
+ # @return [Hash] formatted as {success: {action: :user_delete, attributes: {primary_email: "user@domain"}, response: "" } }
103
+ def user_delete( attributes: )
104
+ response = service.delete_user( attributes[:primary_email] )
105
+ {action: :user_delete, user: attributes[:primary_email], response: response}
106
+ end
107
+
108
+ private
109
+ def update_user( user_attr )
110
+ # create a user object that google will create
111
+ user_object = Google::Apis::AdminDirectoryV1::User.new user_attr
112
+ # send user object to google directory
113
+ service.update_user( user_attr[:primary_email], user_object )
114
+ end
115
+
116
+ end
117
+ end
@@ -0,0 +1,24 @@
1
+ # already required in Google API
2
+ # require 'SecureRandom'
3
+
4
+ module GoogleDirectory
5
+
6
+ # DirectoryService Ruby API Commands
7
+ # https://www.rubydoc.info/github/google/google-api-ruby-client/Google/Apis/AdminDirectoryV1/DirectoryService
8
+ module UsersCommands
9
+
10
+ # Usage hints
11
+ # https://github.com/google/google-api-ruby-client/issues/360
12
+
13
+ # get multiple users
14
+ # if you don't want the defaults { max_results: 10, order_by: 'email' }
15
+ # you must override (a nil disables the option)
16
+ def users_list( attributes: {} )
17
+ defaults = { max_results: 10, order_by: 'email' }
18
+ filters = defaults.merge( attributes )
19
+ response = service.list_users( filters )
20
+ {action: :users_list, filters: filters, response: response}
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,5 @@
1
+ module GoogleDirectory
2
+ module Version
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: google_directory
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Bill Tihen
8
+ - Lee Weisbecker
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2018-05-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: google-api-client
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '0.21'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '0.21'
28
+ - !ruby/object:Gem::Dependency
29
+ name: bundler
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '1.16'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '1.16'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rake
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '10.0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '10.0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rspec
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '3.0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '3.0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: pry
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: '0.11'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: '0.11'
84
+ description: Authenticate, list, make, check, suspend and activate google users.
85
+ email:
86
+ - btihen@gmail.com
87
+ - lweisbecker@las.ch
88
+ executables: []
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - lib/google_directory.rb
93
+ - lib/google_directory/connection.rb
94
+ - lib/google_directory/user_commands.rb
95
+ - lib/google_directory/users_commands.rb
96
+ - lib/google_directory/version.rb
97
+ homepage: https://github.com/LAS-IT/google_directory
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.7.6
118
+ signing_key:
119
+ specification_version: 4
120
+ summary: Simple ruby wrapper to manage google accounts.
121
+ test_files: []