def authenticate(self, username=None, password=None): auth = HTTPBasicAuth(username, password) jive_api_url = settings.JIVE_API_URL url = '{}/people/username/{}'.format(jive_api_url, username) response = requests.get(url, auth=auth) if response.status_code != 200: return None try: user = User.objects.get(username=username) except User.DoesNotExist: person = response.text person = re.sub('^throw.*;\\s*', '', person) try: person = json.loads(person) email, location, phone_number = None, None, None last_name = person['name']['familyName'] first_name = person['name']['givenName'] avatar = person['thumbnailUrl'] emails = person['emails'] addresses = person['addresses'] phone_numbers = person['phoneNumbers'] for eml in emails: if eml['type'] == 'work': email = eml['value'] break if email is None: return None for address in addresses: city = address['value']['locality'] state = address['value']['region'] location = '{}, {}'.format(city, state) break for phone in phone_numbers: if phone['type'] == 'work': phone_number = phone['value'] break user = User() user.last_name = last_name user.first_name = first_name user.email = email user.phone_number = phone_number user.location = location user.avatar = avatar user.username = username user.set_password(password) user.save() return user except (KeyError, ValueError): return None
def sign(): # valid username and password email = request.form['email'] if re.match('\S+@\S+\.\S+', email) is None: flash('Invalid email address.') return redirect(url_for('login')) password = request.form['password'] if re.match('[0-9a-f]{32}', password) is None: flash('Invalid password.') return redirect(url_for('login')) user = User.query.filter_by(email=email).first() if user is not None: flash('Email already exist.') return redirect(url_for('login')) try: msg = Message("Account", sender=app.config.get('MAIL_USERNAME'), recipients=[email]) msg.body = app.config.get('HOST_URL') + "/validate?vcode=%s&email=%s" % \ (md5(app.config.get('SECRET_KEY') + email), email) mail.send(msg) except: import traceback traceback.print_exc() flash('Mail error happend. Contact us!') return redirect(url_for('login')) user = User() user.email = email user.login_pass = password user.service_pass = str(random.randint(1000000, 99999999)) user.transfer_download = 0 user.transfer_upload = 0 user.transfer_enable = 0 user.service_enable = 0 user.service_switch = 0 user.last_service_time = 0 user.last_get_gift_time = 0 user.type = 0 user.service_port = 0 db.session.add(user) flash('Validate email send.') return redirect(url_for('login'))