Class: G5AuthenticationClient::Client

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Initializes the client.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :debug (true, false)

    true enabled debug logging (defaults to false)

  • :logger (Logger)

    a custom logger instance (defaults to STDOUT)

  • :username (String)

    The username for authenticating

  • :password (String)

    The password for authenticating

  • :endpoint (String)

    The authentication endpoint

  • :client_id (String)

    The client id for this application

  • :client_secret (String)

    The client secret for this application

  • :redirect_uri (String)

    The client callback url for this application.

  • :authorization_code (String)

    The authentication code from the authorization server.

  • :allow_password_credentials (String)

    The client will use username and password if true.



65
66
67
# File 'lib/g5_authentication_client/client.rb', line 65

def initialize(options={})
  options.each { |k,v| self.send("#{k}=", v) if self.respond_to?("#{k}=") }
end

Instance Attribute Details

#access_tokenString

Returns access token value provided by authorization server.

Returns:

  • (String)

    access token value provided by authorization server



48
49
50
# File 'lib/g5_authentication_client/client.rb', line 48

def debug?
  self.debug.to_s == 'true'
end

#allow_password_credentialsString

username and password attributes

Returns:

  • (String)

    ‘true’ if the client is using the



# File 'lib/g5_authentication_client/client.rb', line 14

#authorization_codeString

Returns code provided by authorization server.

Returns:

  • (String)

    code provided by authorization server



# File 'lib/g5_authentication_client/client.rb', line 42

#client_idString

Returns client id for this application.

Returns:

  • (String)

    client id for this application



# File 'lib/g5_authentication_client/client.rb', line 33

#client_secretString

Returns client secret for this application.

Returns:

  • (String)

    client secret for this application



# File 'lib/g5_authentication_client/client.rb', line 36

#debugString

Returns ‘true’ if debug logging is enabled.

Returns:

  • (String)

    ‘true’ if debug logging is enabled



# File 'lib/g5_authentication_client/client.rb', line 27

#endpointString

Returns the g5-authentication service endpoint URL.

Returns:

  • (String)

    the g5-authentication service endpoint URL



# File 'lib/g5_authentication_client/client.rb', line 18

#loggerLogger

Returns custom logger instance.

Returns:

  • (Logger)

    custom logger instance



# File 'lib/g5_authentication_client/client.rb', line 30

#passwordString

Returns the password for authentication.

Returns:

  • (String)

    the password for authentication



# File 'lib/g5_authentication_client/client.rb', line 24

#redirect_uriString

Returns callback url for application.

Returns:

  • (String)

    callback url for application



# File 'lib/g5_authentication_client/client.rb', line 39

#usernameString

Returns the username for authentication.

Returns:

  • (String)

    the username for authentication



# File 'lib/g5_authentication_client/client.rb', line 21

Instance Method Details

#allow_password_credentials?Boolean

Tells whether a client instance will use the username/password credentials

Returns:

  • (Boolean)

    whether the client will use username/password



71
72
73
# File 'lib/g5_authentication_client/client.rb', line 71

def allow_password_credentials?
  allow_password_credentials=='true'
end

#create_user(options = {}) ⇒ G5AuthenticationClient::User

Create a user from the options

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :email (String)

    The new user’s email address

  • :password (String)

    The new user’s password

  • :password_confirmation (String)

    The new user’s password confirmation string

Returns:



98
99
100
101
102
103
104
# File 'lib/g5_authentication_client/client.rb', line 98

def create_user(options={})
  user=User.new(options)
  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

Returns:

  • (Boolean)


48
49
50
# File 'lib/g5_authentication_client/client.rb', line 48

def debug?
  self.debug.to_s == 'true'
end

#delete_user(id) ⇒ G5AuthenticationClient::User

Delete a user

Parameters:

  • id (Integer)

    the user ID in the remote service

Returns:



142
143
144
145
# File 'lib/g5_authentication_client/client.rb', line 142

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

Parameters:

  • email (String)

    address

Returns:



122
123
124
125
126
127
128
129
# File 'lib/g5_authentication_client/client.rb', line 122

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_tokenString

Retrieves the access token as a string

Returns:

  • (String)

    the access token value



77
78
79
# File 'lib/g5_authentication_client/client.rb', line 77

def get_access_token
  oauth_access_token.token
end

#get_user(id) ⇒ G5AuthenticationClient::User

Get a user

Parameters:

  • id (Integer)

    the user ID in the remote service

Returns:



134
135
136
137
# File 'lib/g5_authentication_client/client.rb', line 134

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.

Parameters:

  • attribute (Symbol)

    the name of the attribute

Returns:

  • (String)

    the value of the attribute

See Also:

  • G5AuthenticationClient.configure


88
89
90
# File 'lib/g5_authentication_client/client.rb', line 88

def get_value(attribute)
  instance_variable_get("@#{attribute}") || G5AuthenticationClient.send(attribute)
end

#list_rolesArray<G5AuthenticationClient::Role>

Return all user roles from the remote service

Returns:



182
183
184
185
# File 'lib/g5_authentication_client/client.rb', line 182

def list_roles
  response = oauth_access_token.get('/v1/roles')
  response.parsed.collect { |parsed_role| Role.new(parsed_role) }
end

#list_usersArray<G5AuthenticationClient::User>

Return all users from the remote service

Returns:



175
176
177
178
# File 'lib/g5_authentication_client/client.rb', line 175

def list_users
  response=oauth_access_token.get("/v1/users")
  response.parsed.collect { |parsed_user| User.new(parsed_user) }
end

#meG5AuthenticationClient::User

Get the current user based on configured credentials



149
150
151
152
# File 'lib/g5_authentication_client/client.rb', line 149

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.

Parameters:

  • redirect_url (String) (defaults to: nil)

    the URL that the auth server should redirect back to after sign out

Returns:

  • (String)

    the auth server endpoint for signing out



166
167
168
169
170
171
# File 'lib/g5_authentication_client/client.rb', line 166

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_infoG5AuthenticationClient::TokenInfo

Get the access token info for the currently active token



156
157
158
159
# File 'lib/g5_authentication_client/client.rb', line 156

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

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :email (String)

    The new user’s email address

  • :password (String)

    The new user’s password

  • :password_confirmation (String)

    The new user’s password confirmation string

Returns:



112
113
114
115
116
117
# File 'lib/g5_authentication_client/client.rb', line 112

def update_user(options={})
  user=User.new(options)
  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_tokenObject



187
188
189
190
191
192
# File 'lib/g5_authentication_client/client.rb', line 187

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