def get_config(self): """ Load the updated server configuration """ # Generate a new Django secret for the engine and portal engine_secret = "'%s'" % rstring(64) portal_secret = "'%s'" % rstring(64) # Return the updated server configuration return { 'server': { 'host': self.input.response.get('api_host'), 'port': self.input.response.get('api_port'), 'secret': engine_secret }, 'db': { 'host': self.input.response.get('db_host'), 'port': self.input.response.get('db_port'), 'user': self.input.response.get('db_user'), 'password': self.input.response.get('db_user_password') }, 'portal': { 'host': self.input.response.get('portal_host'), 'port': self.input.response.get('portal_port'), 'secret': portal_secret }, 'socket': { 'host': self.input.response.get('socket_host'), 'port': self.input.response.get('socket_port'), 'bind_ip': self.input.response.get('socket_bind_ipaddr') } }
def create(self, id=None): """ Generate a new API authentication token. """ token_str = rstring(255) expires = datetime.datetime.now() + datetime.timedelta(hours=settings.API_TOKEN_LIFE) # Create a new API token self.log.info('Generating API token for client [%s]' % id) db_token = DBUserAPITokens(id = None, user=DBUser.objects.get(username=id), token=token_str, expires=expires) db_token.save() # Return the token return token_str
def launch(self): """ Worker method to handle resetting a user's password. """ # Construct a list of authorized users auth_users = self.api.acl.authorized_objects('user', path='user', method=HTTP_GET) # If the user does not exist or access is denied if not self.user in auth_users.ids: return invalid('Cannot reset password for user [%s], not found or access denied' % self.user) self.api.log.info('Resetting password for user [%s]' % self.user) # Generate a new random password new_pw = rstring() # Get the user object and set the new password try: user_obj = DBUser.objects.get(username=self.user) user_obj.set_password(new_pw) user_obj.save() self.api.log.info('Successfully reset password for user [%s]' % self.user) # Critical error when resetting user password except Exception as e: return invalid('Failed to reset password for user [%s]: %s' % (self.user, str(e))) # Send the email try: # Email properties email_sub = 'CloudScape Password Reset: %s' % self.user email_txt = 'Your password has been reset. You may login with your new password "%s".' % new_pw email_from = '*****@*****.**' email_to = [user_obj.email] # Send the email if self.api.email.send(email_sub, email_txt, email_from, email_to): self.api.log.info('Sent email confirmation for password reset to user [%s]' % self.user) # Return the response return valid('Successfully reset user password') # Critical error when sending password reset notification except Exception as e: return invalid(self.api.log.error('Failed to send password reset confirmation: %s' % str(e)))
def create(self): """ Generate a 64 character API key. """ return rstring(64)
def launch(self): """ Worker method used to handle creation of a new user account. """ # Validate the user creation request req_status = self._validate() if not req_status['valid']: return req_status # Try to create the new user account try: # Generate a random password password = rstring() # If manually setting the password if ('password' in self.api.data): # Must confirm the password if not ('password_confirm' in self.api.data): return invalid('Missing [password_confirm] request parameter') # Passwords don't match if not (self.api.data['password'] == self.api.data['password_confirm']): return invalid('Request parameters [password] and [password_confirm] do not match') # Set the password password = self.api.data['password'] # Check if specifying a manual user UUID if self.api.data.get('uuid', False): if DBUser.objects.filter(uuid=self.api.data['uuid']).count(): return invalid(self.api.log.error('Cannot create user with duplicate UUID <%s>' % self.api.data['uuid'])) # Create the user account new_user = DBUser.objects.create_user( uuid = self.api.data.get('uuid', None), group = self.api.data['group'], username = self.api.data['username'], email = self.api.data['email'], password = self.api.data['password'], ) # Save the user account details new_user.save() self.api.log.info('Created user account [%s]' % (self.api.data['username'])) # Send the account creation email try: # Email properties email_sub = 'CloudScape New Account: %s' % new_user.username email_txt = 'Your account has been created. You may login with your password "%s".' % password email_from = '*****@*****.**' email_to = [new_user.email] # Send the email if self.api.email.send(email_sub, email_txt, email_from, email_to): self.api.log.info('Sent email confirmation for new account [%s] to [%s]' % (new_user.username, new_user.email)) # Return valid return valid('Successfully sent account creation confirmation') # Critical error when sending email confirmation, continue but log exception except Exception as e: self.api.log.exception('Failed to send account creation confirmation: %s' % str(e)) # Something failed during creation except Exception as e: return invalid(self.api.log.exception('Failed to create user account [%s]: %s' % (self.api.data['username'], str(e)))) # Get the new user's API key api_key = DBUserAPIKeys.objects.filter(user=new_user.uuid).values()[0]['api_key'] # Return the response return valid('Successfully created user account', { 'uuid': new_user.uuid, 'username': new_user.username, 'first_name': new_user.first_name, 'last_name': new_user.last_name, 'email': new_user.email, 'api_key': api_key })