예제 #1
0
    def post(cls):
        data = request.get_json()
        code = data["code"]
        url = "https://github.com/login/oauth/access_token"
        my_data = {
            "code": code,
            "client_id": "c1d53a4b044962c3379a",
            "client_secret": "a2b7acacd1b94dde26fc1dae18b8c6e7f9e1f22f"
        }
        x = requests.post(url,
                          data=my_data,
                          headers={"Accept": "application/json"})

        # print(x.json())
        data = x.json()

        g.access_token = data["access_token"]

        github_user = github.get("user")
        github_email = github.get("user/emails")
        print(github_user.data)
        print(github_email.data)
        # gives complete user data
        # if user doesnot exist
        # add to database and get the user id
        # else get the user Id
        # use this to generate a jwt token
        # send it as a response with which they will authenticate

        return {"token": "xyz"}
예제 #2
0
    def get(cls):
        resp = github.authorized_response()

        if resp is None or resp.get("access_token") is None:
            return {
                "error": request.args["error"],
                "error_description": request.args["error_description"]
            }, 500

        g.access_token = resp["access_token"]
        github_user = github.get("user")
        github_username = github_user.data["login"]

        user = UserModel.find_by_username(github_username)
        if not user:
            user = UserModel(username=github_username, password=None)
            user.save_to_db()
            confirmation = ConfirmationModel(user_id=user.id)
            confirmation.confirmed = True
            confirmation.save_to_db()

        access_token = create_access_token(identity=user.id, fresh=True)
        refresh_token = create_refresh_token(user.id)

        return {
            'access_token': access_token,
            'refresh_token': refresh_token
        }, 200
예제 #3
0
    def get(cls):
        res = github.authorized_response()

        if res is None or res.get("access_token") is None:
            error_response = {
                "error": request.args["error"],
                "error_description": request.args["error_description"],
            }
            return error_response

        g.access_token = res["access_token"]
        github_user = github.get("user")
        github_username = github_user.data["login"]

        user = UserModel.find_by_username(github_username)

        if not user:
            user = UserModel(username=github_username, password=None)
            user.save_to_db()

        access_token = create_access_token(identity=user.id, fresh=True)
        refresh_token = create_refresh_token(user.id)

        return (
            {
                "access_token": access_token,
                "refresh_token": refresh_token
            },
            200,
        )
예제 #4
0
    def get(cls):
        resp = github.authorized_response()
        if resp is None or resp.get('access_token') is None:
            error_response = {
                "error": request.args['error'],
                "error_description": request.args['error_description']
            }
            return error_response

        g.access_token = resp['access_token']
        github_user = github.get(
            'user')  # this uses the access_token from the tokengetter function
        github_username = github_user.data['login']

        user = UserModel.query.filter_by(username=github_username).first()

        if not user:
            user = UserModel(username=github_username, password=None)
            user.save_to_db()

        access_token = create_access_token(identity=user.id, fresh=True)
        refresh_token = create_refresh_token(user.id)

        return {
            "access_token": access_token,
            "refresh_token": refresh_token
        }, 200
예제 #5
0
    def get(cls):
        """Take github data and send it to GitHub post request to retrieve user access token"""
        resp = github.authorized_response()

        if resp is None or resp.get('access_token') is None:
            error_response = {
                "error": request.args['error'],
                "error_description": request.args["error_description"]
            }
            return error_response

        g.access_token = resp['access_token']  # put access token inside flask_global
        github_user = github.get('user')
        github_username = github_user.data['login']

        user = UserModel.find_by_username(github_username)

        if not user:
            user = UserModel(username=github_username, password=None) # if login with github, there is no password given
            user.save_to_db()

        access_token = create_access_token(identity=user.id, fresh=True)
        refresh_token = create_refresh_token(user.id)

        return {"access_token": access_token, "refresh_token": refresh_token}, 200
    def get(cls):
        resp = github.authorized_response()

        if resp is None or resp.get('access_token') is None:
            error_response = {
                'error': request.args['error'],
                'error_description': request.args['error_description'],
            }

            return error_response

        g.access_token = resp['access_token']
        github_user = github.get('user')
        github_username = github_user.data['login']
        github_email = github_user.data['email']

        user = UserModel.find_by_username(github_username)

        if not user:
            user = UserModel(username=github_username,
                             password=None,
                             email=github_email)
            user.save_to_db()

        access_token = create_access_token(identity=user.id, fresh=True)
        refresh_token = create_refresh_token(user.id)

        return {
            'access_token': access_token,
            'refresh_token': refresh_token,
        }, 200
예제 #7
0
    def get(cls):
        # this make post request to GitHub and give back to us access token in response
        resp = github.authorized_response() # authorized_response() give us access token

        if resp is None or resp.get('access_token') is None:
            error_response = {
                "error": request.args['error'],
                "error_description": request.args['error_description']
            }
            return error_response

        g.access_token = resp['access_token'] # put access token into g
        github_user = github.get('user')  # this uses the access_token from the tokengetter function in oa.py
        github_username = github_user.data['login'] # s777610 in this case
        
        user = UserModel.query.filter_by(username=github_username).first()

        if not user:
            user = UserModel(username=github_username, password=None)
            user.save_to_db()

        access_token = create_access_token(identity=user.id, fresh=True)
        refresh_token = create_refresh_token(user.id)

        return {"access_token": access_token, "refresh_token": refresh_token}, 200
예제 #8
0
    def get(cls):
        """Take github data and send it to GitHub post request to retrieve user access token"""
        resp = github.authorized_response()

        if resp is None or resp.get('access_token') is None:
            error_response = {
                "error": request.args['error'],
                "error_description": request.args["error_description"]
            }

            session['message_warning'] = request.args["error_description"]
            return redirect(url_for('survey'))

        g.access_token = resp['access_token']  # put access token inside flask_global
        github_user = github.get('user')
        github_email = github_user.data['login']
        user = UserModel.find_by_username(github_email)

        if not user:
            user = UserModel(username=github_email, email=github_email,
                             password=None)  # if login with github, there is no password given
            user.save_to_db()

        login_user(user)
        session['message_success'] = gettext("user_logged_in").format(github_email)
        return redirect('/')
    def get(cls):
        resp = github.authorized_response()

        # error handling
        if resp is None or resp.get('access_token') is None:
            error_response = {
                'error': request.
                args['error'],  # ?error=blablabla&error_description=blablabla
                'error_description': request.args['error_description']
            }
            return error_response

        g.access_token = resp['access_token']
        github_user = github.get('user')  # using tokengetter in oa.py
        github_username = github_user.data['login']

        user = UserModel.find_by_username(github_username)

        if not user:
            user = UserModel(username=github_username, password=None)
            user.save_to_db()

        access_token = create_access_token(identity=user.id, fresh=True)
        refresh_token = create_refresh_token(user.id)

        return {
            'access_token': access_token,
            'refresh_token': refresh_token
        }, 200
    def get(cls):
        resp = github.authorized_response()

        if resp is None or resp.get('access_token') is None:
            error_response = {
                'error': request.args['error'],
                'error_description': request.args['error_description']
            }
            return error_response

        g.access_token = resp['access_token']
        github_user = github.get(
            'user')  # this uses the access_token from the tokengetter function
        github_username = github_user.data['login']

        user = UserModel.find_by_username(github_username)

        if not user:
            user = UserModel(
                username=github_username, password=None
            )  # since the user logged in with github account, no password is provided
            user.save_to_db()

        access_token = create_access_token(identity=user.id, fresh=True)
        refresh_token = create_refresh_token(user.id)

        return {
            'access_token': access_token,
            'refresh_token': refresh_token
        }, 200
예제 #11
0
def authorized():
    response = github.authorized_response()
    if response is None or response["access_token"] is None:
        flash("We have a problem with github authentication. Try again later as we fix the issue or login normaly.")
        return redirect(url_for("login"))
        
    access_token = response['access_token']
    github_user = github.get('user', token= access_token)
    #  github info about the user
    username= github_user.data["login"]
    email = github_user.data["email"]


        # working with username
    if email is  None and username is not None:
        github_username= Users_.query.filter_by(user_name= username).first()
        if github_username:
            
            session["logged_in"] = True
            session["username"] = github_username.user_name
            session["id"] = github_username.id
            return redirect(url_for("home"))
        else:
            save_username=Users_(user_name=username)
            save_username.create()

            github_username = Users_.query.filter_by(user_name= username).first()
            
            session["logged_in"] = True
            session["username"] = github_username.user_name
            session["id"] = github_username.id
            return redirect(url_for("home"))

    
    # working with email
    if email is not None:
        # checking if email is in our db
        user_email=Users_.query.filter_by(email=email).first()
        if user_email:
            # checking if user has username
            if user_email.user_name is not None:
                        session["logged_in"] = True
                        session["username"] = user_email.user_name
                        session["id"] = user_email.id
                        return redirect(url_for("home"))
            else:
                    session["logged_in"] = True
                    session["username"] = user_email.email
                    session["id"] = user_email.id
                    return redirect(url_for("home"))
        else:
            save_email= Users_(email=email)
            save_email.create()

            user_email=Users_.query.filter_by(email=email).first()
            session["logged_in"] = True
            session["username"] = user_email.email
            session["id"] = user_email.id
            return redirect(url_for("home"))
예제 #12
0
    def get(cls):
        resp = github.authorized_response()

        if resp is None or resp.get("access_token") is None:
            error_response = {
                "error" : request.args["error"],
                "error_desc": request.args["error_description"]
            }
        g.access_token = resp['access_token']
        github_user = github.get('user')
        github_username = github_user.data['login']

        user = UserModel.find_by_username(github_username)
        if not user:
            user = UserModel(username= github_username, password=1234, email='*****@*****.**')
            user.save_to_db() # saved user that didnt register

        access_token = create_access_token(identity=user.id, fresh= True)
        refresh_token = create_refresh_token(identity=user.id)
        # we give him a token so he can make requests

        return {"access_token": access_token,"refresh_token":refresh_token}, 200
예제 #13
0
    def get(cls):
        resp = github.authorized_response()
        if resp is None or resp.get("access_token") is None:
            error_response = {
                "error": request.args["error"],
                "error_description": request.args["error_description"],
            }
            return error_response
        g.access_token = resp["access_token"]
        github_user = github.get("user")
        github_username = github_user.data["login"]

        user = UserModel.find_by_username(github_username)
        if not user:
            user = user_schema.load({
                "username": github_username,
                "password": "******",
                "email": "*****@*****.**",
            })
            user.create_user()
        access_token = create_access_token(identity=user.id, fresh=True)
        refresh_token = create_refresh_token(user.id)
        return {"access_token": access_token, "refresh_token": refresh_token}
예제 #14
0
    def get(cls):
        """
        Accesses user data and logs user in with email
        Output: Access and Refresh token
        """
        try:
            response = github.authorized_response()
            print(response)
        except:
            return {"msg": request.args["error"]}

        g.access_token = response['access_token']
        github_user = github.get('user', token=g.access_token)

        try:
            github_username = github_user.data['login']
            github_email = github_user.data['email']

            if github_email is None:
                return {
                    'msg':
                    "Your email has not been linked to your Github account. Cannot login"
                }, 400

        except:
            return {
                'msg': "User of this account does not exist in database"
            }, 400

        user = UserModel.find_by_email(email=github_email)
        access_token = create_access_token(identity=str(user.id), fresh=True)
        refresh_token = create_refresh_token(str(user.id))

        return {
            'access_token': access_token,
            'refresh_token': refresh_token
        }, 200
예제 #15
0
    def get(cls):
        resp = github.authorized_response(
        )  # this gives us access to resp['access_token'] by making the POST
        # response for us

        if resp is None or resp.get("access_token") is None:
            error_response = {
                "error": request.args["error"],
                "error_description": request.args["error_description"]
            }
            return error_response

        g.access_token = resp[
            'access_token']  # this gives us access_token, but no user info.
        github_user = github.get(
            'user'
        )  # This will get the user's information from the github client
        print(f"github_user.data = {github_user.data}")
        github_username = github_user.data[
            'login']  # the username of how they logged in to github

        user = UserModel.find_by_username(github_username)

        if not user:
            user = UserModel(username=github_username,
                             password=None,
                             email=None,
                             soc_security_num=None)
            user.save_to_db()

        access_token = create_access_token(identity=user.id, fresh=True)
        refresh_token = create_refresh_token(user.id)

        return {
            "access_token": access_token,
            "refresh_token": refresh_token
        }, 200
예제 #16
0
 def get(cls):
     resp = github.authorized_response()
     if resp is None or resp.get("access_token") is None:
         error_response = {
             "error": request.args["error"],
             "error_description": request.args["error_description"],
         }
         return error_response
     g.access_token = resp["access_token"]
     github_user = github.get("user")
     github_username = github_user.data["login"]
     github_email = github_user.data["email"]
     user = UserModel.find_by_username(github_username)
     if not user:
         user = UserModel(
             username=github_username, password=None, email=github_email
         )
         user.save_to_db()
         confirmation = ConfirmationModel(user.id, confirmed=True)
         confirmation.force_to_expire()
         confirmation.save_to_db()
     access_token = create_access_token(identity=user.id, fresh=True)
     refresh_token = create_refresh_token(user.id)
     return {"access_token": access_token, "refresh_token": refresh_token}, 200