Class: G5AuthenticationClient::Client
- Inherits:
-
Object
- Object
- G5AuthenticationClient::Client
- Defined in:
- lib/g5_authentication_client/client.rb
Overview
G5AuthenticationClient::Client can be used to authenticate with the G5 OAuth 2 authorization server. It can also be used to create G5 users.
Instance Attribute Summary collapse
-
#access_token ⇒ String
Access token value provided by authorization server.
-
#allow_password_credentials ⇒ String
username and password attributes.
-
#authorization_code ⇒ String
Code provided by authorization server.
-
#client_id ⇒ String
Client id for this application.
-
#client_secret ⇒ String
Client secret for this application.
-
#debug ⇒ String
‘true’ if debug logging is enabled.
-
#endpoint ⇒ String
The g5-authentication service endpoint URL.
-
#logger ⇒ Logger
Custom logger instance.
-
#password ⇒ String
The password for authentication.
-
#redirect_uri ⇒ String
Callback url for application.
-
#username ⇒ String
The username for authentication.
Instance Method Summary collapse
-
#allow_password_credentials? ⇒ Boolean
Tells whether a client instance will use the username/password credentials.
-
#create_user(options = {}) ⇒ G5AuthenticationClient::User
Create a user from the options.
- #debug? ⇒ Boolean
-
#delete_user(id) ⇒ G5AuthenticationClient::User
Delete a user.
-
#find_user_by_email(email) ⇒ G5AuthenticationClient::User
Find a user by email.
-
#get_access_token ⇒ String
Retrieves the access token as a string.
-
#get_user(id) ⇒ G5AuthenticationClient::User
Get a user.
-
#get_value(attribute) ⇒ String
Retrieves an attribute’s value.
-
#initialize(options = {}) ⇒ Client
constructor
Initializes the client.
-
#list_roles ⇒ Array<G5AuthenticationClient::Role>
Return all user roles from the remote service.
-
#list_users ⇒ Array<G5AuthenticationClient::User>
Return all users from the remote service.
-
#me ⇒ G5AuthenticationClient::User
Get the current user based on configured credentials.
-
#sign_out_url(redirect_url = nil) ⇒ String
Return the URL for signing out of the auth server.
-
#token_info ⇒ G5AuthenticationClient::TokenInfo
Get the access token info for the currently active token.
-
#update_user(options = {}) ⇒ G5AuthenticationClient::User
Update an existing user.
- #username_pw_access_token ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Client
Initializes the client.
67 68 69 |
# File 'lib/g5_authentication_client/client.rb', line 67 def initialize(={}) .each { |k,v| self.send("#{k}=", v) if self.respond_to?("#{k}=") } end |
Instance Attribute Details
#access_token ⇒ String
Returns access token value provided by authorization server.
50 51 52 |
# File 'lib/g5_authentication_client/client.rb', line 50 def debug? self.debug.to_s == 'true' end |
#allow_password_credentials ⇒ String
username and password attributes
|
# File 'lib/g5_authentication_client/client.rb', line 16
|
#authorization_code ⇒ String
Returns code provided by authorization server.
|
# File 'lib/g5_authentication_client/client.rb', line 44
|
#client_id ⇒ String
Returns client id for this application.
|
# File 'lib/g5_authentication_client/client.rb', line 35
|
#client_secret ⇒ String
Returns client secret for this application.
|
# File 'lib/g5_authentication_client/client.rb', line 38
|
#debug ⇒ String
Returns ‘true’ if debug logging is enabled.
|
# File 'lib/g5_authentication_client/client.rb', line 29
|
#endpoint ⇒ String
Returns the g5-authentication service endpoint URL.
|
# File 'lib/g5_authentication_client/client.rb', line 20
|
#logger ⇒ Logger
Returns custom logger instance.
|
# File 'lib/g5_authentication_client/client.rb', line 32
|
#password ⇒ String
Returns the password for authentication.
|
# File 'lib/g5_authentication_client/client.rb', line 26
|
#redirect_uri ⇒ String
Returns callback url for application.
|
# File 'lib/g5_authentication_client/client.rb', line 41
|
#username ⇒ String
Returns the username for authentication.
|
# File 'lib/g5_authentication_client/client.rb', line 23
|
Instance Method Details
#allow_password_credentials? ⇒ Boolean
Tells whether a client instance will use the username/password credentials
73 74 75 |
# File 'lib/g5_authentication_client/client.rb', line 73 def allow_password_credentials? allow_password_credentials=='true' end |
#create_user(options = {}) ⇒ G5AuthenticationClient::User
Create a user from the options
100 101 102 103 104 105 106 |
# File 'lib/g5_authentication_client/client.rb', line 100 def create_user(={}) user=User.new() user.validate_for_create! body = user_hash(user.to_hash) response=oauth_access_token.post('/v1/users', body: body) User.new(response.parsed) end |
#debug? ⇒ Boolean
50 51 52 |
# File 'lib/g5_authentication_client/client.rb', line 50 def debug? self.debug.to_s == 'true' end |
#delete_user(id) ⇒ G5AuthenticationClient::User
Delete a user
144 145 146 147 |
# File 'lib/g5_authentication_client/client.rb', line 144 def delete_user(id) response=oauth_access_token.delete("/v1/users/#{id}") User.new(response.parsed) end |
#find_user_by_email(email) ⇒ G5AuthenticationClient::User
Find a user by email
124 125 126 127 128 129 130 131 |
# File 'lib/g5_authentication_client/client.rb', line 124 def find_user_by_email(email) response = oauth_access_token.get('/v1/users', params: { email: email }) user = response.parsed.first if user user=User.new(user) end user end |
#get_access_token ⇒ String
Retrieves the access token as a string
79 80 81 |
# File 'lib/g5_authentication_client/client.rb', line 79 def get_access_token oauth_access_token.token end |
#get_user(id) ⇒ G5AuthenticationClient::User
Get a user
136 137 138 139 |
# File 'lib/g5_authentication_client/client.rb', line 136 def get_user(id) response=oauth_access_token.get("/v1/users/#{id}") User.new(response.parsed) end |
#get_value(attribute) ⇒ String
Retrieves an attribute’s value. If the attribute has not been set on this object, it is retrieved from the global configuration.
90 91 92 |
# File 'lib/g5_authentication_client/client.rb', line 90 def get_value(attribute) instance_variable_get("@#{attribute}") || G5AuthenticationClient.send(attribute) end |
#list_roles ⇒ Array<G5AuthenticationClient::Role>
Return all user roles from the remote service
184 185 186 187 |
# File 'lib/g5_authentication_client/client.rb', line 184 def list_roles response = oauth_access_token.get('/v1/roles') response.parsed.collect { |parsed_role| Role.new(parsed_role) } end |
#list_users ⇒ Array<G5AuthenticationClient::User>
Return all users from the remote service
177 178 179 180 |
# File 'lib/g5_authentication_client/client.rb', line 177 def list_users response=oauth_access_token.get("/v1/users") response.parsed.collect { |parsed_user| User.new(parsed_user) } end |
#me ⇒ G5AuthenticationClient::User
Get the current user based on configured credentials
151 152 153 154 |
# File 'lib/g5_authentication_client/client.rb', line 151 def me response = oauth_access_token.get('/v1/me') User.new(response.parsed) end |
#sign_out_url(redirect_url = nil) ⇒ String
Return the URL for signing out of the auth server. Clients should redirect to this URL to globally sign out.
168 169 170 171 172 173 |
# File 'lib/g5_authentication_client/client.rb', line 168 def sign_out_url(redirect_url=nil) auth_server_url = Addressable::URI.parse(endpoint) auth_server_url.path = '/users/sign_out' auth_server_url.query_values = {redirect_url: redirect_url} if redirect_url auth_server_url.to_s end |
#token_info ⇒ G5AuthenticationClient::TokenInfo
Get the access token info for the currently active token
158 159 160 161 |
# File 'lib/g5_authentication_client/client.rb', line 158 def token_info response = oauth_access_token.get('/oauth/token/info') TokenInfo.new(response.parsed) end |
#update_user(options = {}) ⇒ G5AuthenticationClient::User
Update an existing user
114 115 116 117 118 119 |
# File 'lib/g5_authentication_client/client.rb', line 114 def update_user(={}) user=User.new() user.validate! response=oauth_access_token.put("/v1/users/#{user.id}", body: user_hash(user.to_hash)) User.new(response.parsed) end |
#username_pw_access_token ⇒ Object
189 190 191 192 193 194 |
# File 'lib/g5_authentication_client/client.rb', line 189 def username_pw_access_token raise_if_blank('username') raise_if_blank('password') raise 'allow_password_credentials must be enabled for username/pw access' unless allow_password_credentials? oauth_client.password.get_token(username, password) end |