def signup(): """ Sign-up view function. :return: if success, render sign-in HTML page. """ form = UserForm(request.form) if request.method == 'POST' and form.validate(): app.logger.debug(form.data) try: user_exist = None for item in User.email_index.query(form.email.data): user_exist = item.email if not user_exist: user = User(uuid.uuid4().hex) user.email = form.email.data user.password = generate_password_hash(form.password.data) user.username = form.username.data user.save() app.logger.debug(user) flash('You have been signed up successfully!') return redirect(url_for('userView.signin', form=form)) else: flash('Your email is already registered. Please try again!') app.logger.debug('Email is already registered : %s ', form.email.data) return redirect(url_for('userView.signup', form=form)) except Exception as e: app.logger.error(e) return errorHandler.server_error(e) return render_template('signup.html')
def callback(): """Exchange the 'code' for Cognito tokens""" #http://docs.aws.amazon.com/cognito/latest/developerguide/token-endpoint.html csrf_state = request.args.get('state') code = request.args.get('code') request_parameters = { 'grant_type': 'authorization_code', 'client_id': conf['COGNITO_CLIENT_ID'], 'code': code, "redirect_uri": conf['BASE_URL'] + "/callback" } response = requests.post("https://%s/oauth2/token" % conf['COGNITO_DOMAIN'], data=request_parameters, auth=HTTPBasicAuth(conf['COGNITO_CLIENT_ID'], conf['COGNITO_CLIENT_SECRET'])) # the response: # http://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-using-tokens-with-identity-providers.html if response.status_code == requests.codes.ok: verify(response.json()["access_token"]) id_token = verify(response.json()["id_token"], response.json()["access_token"]) ## TODO #8: Write yoir code to set up User objedct using id_token from Cognito user = User() ## -- begin -- user = User() user.id = id_token["cognito:username"] user.email = id_token["email"] user.username = id_token["name"] ## Now, password is managed by Cognito. user.password = '******' user.save() session['id'] = id_token["cognito:username"] session['email'] = id_token["email"] session['name'] = id_token["name"] session['expires'] = id_token["exp"] session['refresh_token'] = response.json()["refresh_token"] ## -- end -- login_user(user, remember=True) return redirect(url_for("siteView.home")) else: return render_template_string("<h1>ERROR!</h1>")