Esempio n. 1
0
def callback():
    # Get authorization code Google sent back to you
    code = request.args.get("code")

    # Find out what URL to hit to get tokens that allow you to ask for
    # things on behalf of a user
    google_provider_cfg = get_google_provider_cfg()
    token_endpoint = google_provider_cfg["token_endpoint"]

    # Prepare and send a request to get tokens! Yay tokens!
    token_url, headers, body = client.prepare_token_request(
        token_endpoint,
        authorization_response=request.url,
        redirect_url=request.base_url,
        code=code)
    token_response = requests.post(
        token_url,
        headers=headers,
        data=body,
        auth=(GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET),
    )

    # Parse the tokens!
    client.parse_request_body_response(json.dumps(token_response.json()))

    # Now that you have tokens (yay) let's find and hit the URL
    # from Google that gives you the user's profile information,
    # including their Google profile image and email
    userinfo_endpoint = google_provider_cfg["userinfo_endpoint"]
    uri, headers, body = client.add_token(userinfo_endpoint)
    userinfo_response = requests.get(uri, headers=headers, data=body)

    # You want to make sure their email is verified.
    # The user authenticated with Google, authorized your
    # app, and now you've verified their email through Google!
    if userinfo_response.json().get("email_verified"):
        unique_id = userinfo_response.json()["sub"]
        users_email = userinfo_response.json()["email"]
        picture = userinfo_response.json()["picture"]
        users_name = userinfo_response.json()["given_name"]
    else:
        return "User email not available or not verified by Google.", 400

    # Create a user in your db with the information provided
    # by Google
    user = User(id_=unique_id,
                name=users_name,
                email=users_email,
                profile_pic=picture,
                address="")

    # Doesn't exist? Add it to the database.
    if not User.get(unique_id):
        User.create(unique_id, users_name, users_email, picture, "")

    # Begin user session by logging the user in
    login_user(user)

    # Send user back to homepage
    return redirect("/dashboard/users/" + unique_id)
Esempio n. 2
0
 def test_squad_members_get_by_status(self):
     squad_members = [
         User.create(username='******',
                     password='******',
                     description='Hi my name is James',
                     location='Sydney',
                     birthdate='DD/MM/YYYY',
                     image='/file/img.png'),
         User.create(username='******',
                     password='******',
                     description='Hi my name is Tim',
                     location='Syd',
                     birthdate='DD/MM/YYYY',
                     image='/file/imag.png')
     ]
     result = SquadMembers.get_by_status('Pending...', 0)
     self.assertListEqual(squad_members, result)
Esempio n. 3
0
 def test_user_create(self):
     user = User()
     result = User.create(username='******',
                          password='******',
                          description='Hi my name is James',
                          location='Sydney',
                          birthdate='DD/MM/YYYY',
                          image='/file/img.png')
     self.assertEqual(user, result)