예제 #1
0
def register():
    try:
        data = registerSchema.validate(request.json)
    except SchemaError as e:
        error = {
            'code': 'auth.register.invalid',
            'message': 'Invalid data for register',
            'details': str(e)
        }
        return jsonify(error=error), 403

    if User.by_username(request.json['username']).count() > 0:
        error = {
            'code': 'auth.register.username_taken',
            'message': 'Username already taken'
        }
        return jsonify(error=error), 403

    if User.by_email(request.json['email']).count() > 0:
        error = {
            'code': 'auth.register.email_in_use',
            'message': 'A user with the supplied email already exists'
        }
        return jsonify(error=error), 403

    user = userstore.create_user(**data)
    session.add(user)
    session.commit()

    login_user(user, True)
    auth_token = user.get_auth_token()
    return jsonify(user=user.as_personal_json(),
                   auth_token=auth_token), 201
예제 #2
0
def save_and_commit_workout(workout, user, identifier):
    workout.user_id = user.id

    try:
        source = Source(workout=workout)
        source.name = "strava"
        source.remote_identifier = identifier

        session.add(workout)
        session.add(source)

        session.commit()
    except Exception:
        raise SyncWorkerException("strava")
예제 #3
0
def callback_provider(provider):
    auth_provider = provider_from_string(provider)
    after = request.args.get('next')
    if auth_provider is None:
        error = {
            'code': 'auth.social.unsupported_provider',
            'message': 'The specified provider is not supported'
        }

        return jsonify(error=error), 403

    response = auth_provider.authorized_response()
    if response is None:
        error = {
            'code': 'auth.social.%s.denied' % (provider),
            'message': 'You denied the request to sign in'
        }

        return jsonify(error=error), 401

    # Store the token
    # If the user already has an auth for the provider
    # then update the existing auth
    auth = SocialAuth.query.filter_by(user_id=current_user.id) \
                           .filter_by(provider=provider)

    if auth.count() == 1:
        auth = auth.one()
    else:
        auth = SocialAuth(user_id=current_user.id,
                          provider=provider)

    if provider == PROVIDER_TWITTER or provider == PROVIDER_ENDOMONDO:
        auth.auth_token = response['oauth_token']
        auth.auth_secret = response['oauth_token_secret']
    elif provider == PROVIDER_RUNKEEPER or provider == PROVIDER_STRAVA:
        auth.auth_token = response['access_token']

    session.add(auth)
    session.commit()

    return redirect(after or url_for('index'))