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.



67
68
69
# File 'lib/g5_authentication_client/client.rb', line 67

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



50
51
52
# File 'lib/g5_authentication_client/client.rb', line 50

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 16

#authorization_codeString

Returns code provided by authorization server.

Returns:

  • (String)

    code provided by authorization server



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

#client_idString

Returns client id for this application.

Returns:

  • (String)

    client id for this application



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

#client_secretString

Returns client secret for this application.

Returns:

  • (String)

    client secret for this application



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

#debugString

Returns ‘true’ if debug logging is enabled.

Returns:

  • (String)

    ‘true’ if debug logging is enabled



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

#endpointString

Returns the g5-authentication service endpoint URL.

Returns:

  • (String)

    the g5-authentication service endpoint URL



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

#loggerLogger

Returns custom logger instance.

Returns:

  • (Logger)

    custom logger instance



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

#passwordString

Returns the password for authentication.

Returns:

  • (String)

    the password for authentication



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

#redirect_uriString

Returns callback url for application.

Returns:

  • (String)

    callback url for application



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

#usernameString

Returns the username for authentication.

Returns:

  • (String)

    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

Returns:

  • (Boolean)

    whether the client will use username/password



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

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:



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

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)


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

Parameters:

  • id (Integer)

    the user ID in the remote service

Returns:



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

Parameters:

  • email (String)

    address

Returns:



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_tokenString

Retrieves the access token as a string

Returns:

  • (String)

    the access token value



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

Parameters:

  • id (Integer)

    the user ID in the remote service

Returns:



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.

Parameters:

  • attribute (Symbol)

    the name of the attribute

Returns:

  • (String)

    the value of the attribute

See Also:

  • G5AuthenticationClient.configure


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_rolesArray<G5AuthenticationClient::Role>

Return all user roles from the remote service

Returns:



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_usersArray<G5AuthenticationClient::User>

Return all users from the remote service

Returns:



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

#meG5AuthenticationClient::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.

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



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

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:



114
115
116
117
118
119
# File 'lib/g5_authentication_client/client.rb', line 114

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



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