Example #1
0
def showSignup():

    if request.method == 'POST':

        data = jsonHandler.loadsJSON(request.data)
        email = validateEmail(data["email"])

        if validateStateToke(data["token"]):
            if not userData.getUserID(email) and email:
                name = data["name"]
                password = saltAndHashed(data["password"])

                if db.createUser(name=name, email=email, password=password):
                    # Register successfully performed
                    return response.builder('Register successfully performed',
                                            200)

                return response.builder(
                    'Error creating the user, please try again later.', 401)

            return response.builder(
                'The user is already registered or entered data are wrong.',
                401)
        return response.builder('Invalid state parameter.', 401)
    else:
        return render_template('signUp.html', token=antiForgeryGenToke())
Example #2
0
def gdisconnect():
    # only disconnect a connected user.
    credentials = session.get("access_token")
    if credentials is None:
        return response.builder('Current user not connected.', 401)

    # Execute HTTP GET request to revoke current token.
    access_token = session.get("access_token")
    url = config.data['google_logout'] % access_token
    result = handleApiRequests(url=url, index=0)
    return "you have been logged out"
Example #3
0
def gdisconnect():
    # only disconnect a connected user.
    credentials = session.get("access_token")
    if credentials is None:
        return response.builder( 'Current user not connected.', 401 )
    
    # Execute HTTP GET request to revoke current token.
    access_token = session.get("access_token")
    url = config.data['google_logout'] % access_token
    result = handleApiRequests( url = url, index = 0 )
    return "you have been logged out"
Example #4
0
def authenticate():
    data = jsonHandler.loadsJSON(request.data)
    user = checkAuthenticationCredentials( data['email'], data["password"] )
    if not user:
        return response.builder( 'Invalid access credentials.', 401 )
    
    session.set( 'provider', 'application' )
    session.set( 'user_id', user.id ) 
    session.set( 'username', user.name ) 
    session.set( 'picture', user.picture ) 
    session.set( 'email', user.email )

    flash("Now logged in as %s" % session.get('username'), "alert-success")
    return response.successLoginMsg( session.get('username'), session.get('picture') )
Example #5
0
def showSignup():

    if request.method == 'POST':

        data = jsonHandler.loadsJSON(request.data)
        email = validateEmail( data["email"] )

        if validateStateToke( data["token"] ):
            if not userData.getUserID( email ) and email:
                name = data["name"]
                password = saltAndHashed( data["password"] )

                if db.createUser(name = name,
                    email = email,password = password):
                    # Register successfully performed
                    return response.builder( 'Register successfully performed', 200 )
                
                return response.builder( 'Error creating the user, please try again later.', 401 )
            
            return response.builder( 'The user is already registered or entered data are wrong.', 401 )
        return response.builder( 'Invalid state parameter.', 401 )
    else:
        return render_template( 'signUp.html', token = antiForgeryGenToke() )
Example #6
0
def authenticate():
    data = jsonHandler.loadsJSON(request.data)
    user = checkAuthenticationCredentials(data['email'], data["password"])
    if not user:
        return response.builder('Invalid access credentials.', 401)

    session.set('provider', 'application')
    session.set('user_id', user.id)
    session.set('username', user.name)
    session.set('picture', user.picture)
    session.set('email', user.email)

    flash("Now logged in as %s" % session.get('username'), "alert-success")
    return response.successLoginMsg(session.get('username'),
                                    session.get('picture'))
Example #7
0
def gconnect():

    # Obtain authorization code
    code = request.data

    try:
        # Upgrade the authorization code into a credentials object
        oauth_flow = flow_from_clientsecrets(config.data["google_data"],
                                             scope='')
        oauth_flow.redirect_uri = 'postmessage'
        credentials = oauth_flow.step2_exchange(code)
    except FlowExchangeError:
        return response.builder('Failed to upgrade the authorization code.',
                                401)

    # Check that the access token is valid.
    access_token = credentials.access_token
    url = (config.data["google_access_token_check"] + access_token)
    result = handleApiRequestsJSON(url)

    # If there was an error in the access token info, abort.
    if result.get('error') is not None:
        return response.builder('Error', 500)

    # Verify that the access token is used for the intended user.
    gplus_id = credentials.id_token['sub']
    if result['user_id'] != gplus_id:
        return response.builder("Token's user ID doesn't match given user ID.",
                                401)

    # Verify that the access token is valid for this app.
    gclient_id = jsonHandler.openLocalJSON(
        config.data["google_data"])['web']['client_id']
    if result['issued_to'] != gclient_id:
        return response.builder("Token's client ID does not match app's.", 401)

    # Check if user is already logedin
    stored_credentials = session.get('credentials')
    stored_gplus_id = session.get('gplus_id')
    if stored_credentials is not None and gplus_id == stored_gplus_id:
        return response.builder("Current user is already connected.", 200)

    # Store the access token in the session for later use.
    session.set('credentials', credentials.to_json())
    session.set('access_token', credentials.access_token)
    session.set('gplus_id', gplus_id)
    session.set('provider', 'google')

    # Get user info
    userinfo_url = config.data["google_user_info"]
    params = {'access_token': credentials.access_token, 'alt': 'json'}
    answer = requests.get(userinfo_url, params=params)

    data = answer.json()

    # Store user data.
    session.set('username', data['name'])
    session.set('picture', data['picture'])
    session.set('email', data['email'])

    # See if user exists, if it doesn't make a new one
    session.set('user_id', userData.ifnotUser(data['email']))

    flash("Now logged in as %s" % session.get('username'), "alert-success")
    return response.successLoginMsg(session.get('username'),
                                    session.get('picture'))
Example #8
0
 def decorated_function(*args, **kwargs):
     if not validateStateToke(request.args.get('state')):
         return response.builder('Invalid state parameter.', 401)
     return func(*args, **kwargs)
 def decorated_function(*args, **kwargs):
     if not validateStateToke( request.args.get('state') ):
         return response.builder( 'Invalid state parameter.', 401 )
     return func(*args, **kwargs)
Example #10
0
def gconnect():
    
    # Obtain authorization code
    code = request.data
    
    try:
        # Upgrade the authorization code into a credentials object 
        oauth_flow = flow_from_clientsecrets( config.data["google_data"], scope='' )
        oauth_flow.redirect_uri = 'postmessage'
        credentials = oauth_flow.step2_exchange( code )
    except FlowExchangeError:
        return response.builder( 'Failed to upgrade the authorization code.', 401 )

    # Check that the access token is valid.
    access_token = credentials.access_token
    url = ( config.data["google_access_token_check"] + access_token )
    result = handleApiRequestsJSON( url )

    # If there was an error in the access token info, abort.
    if result.get('error') is not None:
        return response.builder( 'Error', 500 )

    # Verify that the access token is used for the intended user.
    gplus_id = credentials.id_token['sub']
    if result['user_id'] != gplus_id:
        return response.builder( "Token's user ID doesn't match given user ID.", 401 )
    
    # Verify that the access token is valid for this app.
    gclient_id = jsonHandler.openLocalJSON( config.data["google_data"] )['web']['client_id']
    if result['issued_to'] != gclient_id:
        return response.builder( "Token's client ID does not match app's.", 401 )

    # Check if user is already logedin
    stored_credentials = session.get('credentials')
    stored_gplus_id = session.get('gplus_id')
    if stored_credentials is not None and gplus_id == stored_gplus_id:
        return response.builder( "Current user is already connected.", 200 )

    # Store the access token in the session for later use.
    session.set( 'credentials', credentials.to_json() ) 
    session.set( 'access_token', credentials.access_token ) 
    session.set( 'gplus_id', gplus_id )
    session.set( 'provider', 'google' )

    # Get user info
    userinfo_url = config.data["google_user_info"]
    params = {'access_token': credentials.access_token, 'alt': 'json'}
    answer = requests.get( userinfo_url, params=params )

    data = answer.json()

    # Store user data.
    session.set( 'username', data['name'] ) 
    session.set( 'picture', data['picture'] ) 
    session.set( 'email', data['email'] ) 

    # See if user exists, if it doesn't make a new one
    session.set( 'user_id', userData.ifnotUser( data['email'] ) )

    flash("Now logged in as %s" % session.get('username'), "alert-success")      
    return response.successLoginMsg( session.get('username'), session.get('picture') )