<commit_before>#!/usr/bin/env python
'''
API Authentication
'''

from flask import g, jsonify, request
from flask.ext.httpauth import HTTPBasicAuth
from ooiservices.app.models import User
from ooiservices.app.main import api
from ooiservices.app.main.errors import unauthorized, forbidden
from ooiservices.app.decorators import scope_required

auth = HTTPBasicAuth()


@auth.verify_password
def verify_password(email_or_token, password):
    return verify_auth(email_or_token, password)

def verify_auth(email_or_token, password):
    if email_or_token == '':
        return True
    if password == '':
        g.current_user = User.verify_auth_token(email_or_token)
        g.token_used = True
        return g.current_user is not None
    user = User.query.filter(User.user_name==email_or_token, User.active==True).first()
    if not user:
        return False
    g.current_user = user
    g.token_used = False
    return user.verify_password(password)

@auth.error_handler
def auth_error():
    return unauthorized('Invalid credentials')

@api.route('/token')
@auth.login_required
def get_token():
    if g.current_user.is_anonymous() or g.token_used:
        return unauthorized('Invalid credentials')
    return jsonify({'token': g.current_user.generate_auth_token(
        expiration=3600), 'expiration': 3600})

@api.route('/logged_in')
def logged_in():
    '''
    Checks the TOKEN not the user identity to see if it's current and valid.
    '''
    auth = request.authorization
    if not auth:
        return jsonify(valid=False)
    token, password = auth.username, auth.password
    if token and not password:
        user = User.verify_auth_token(token)
        return jsonify(valid=(user is not None))
    return jsonify(valid=False)<commit_msg>Sets the token to a full day.  Used in conjunction with UI timout update<commit_after>#!/usr/bin/env python
'''
API Authentication
'''

from flask import g, jsonify, request
from flask.ext.httpauth import HTTPBasicAuth
from ooiservices.app.models import User
from ooiservices.app.main import api
from ooiservices.app.main.errors import unauthorized, forbidden
from ooiservices.app.decorators import scope_required

auth = HTTPBasicAuth()


@auth.verify_password
def verify_password(email_or_token, password):
    return verify_auth(email_or_token, password)

def verify_auth(email_or_token, password):
    if email_or_token == '':
        return True
    if password == '':
        g.current_user = User.verify_auth_token(email_or_token)
        g.token_used = True
        return g.current_user is not None
    user = User.query.filter(User.user_name==email_or_token, User.active==True).first()
    if not user:
        return False
    g.current_user = user
    g.token_used = False
    return user.verify_password(password)

@auth.error_handler
def auth_error():
    return unauthorized('Invalid credentials')

@api.route('/token')
@auth.login_required
def get_token():
    if g.current_user.is_anonymous() or g.token_used:
        return unauthorized('Invalid credentials')
    return jsonify({'token': g.current_user.generate_auth_token(
        expiration=86400), 'expiration': 86400}) #24 hours

@api.route('/logged_in')
def logged_in():
    '''
    Checks the TOKEN not the user identity to see if it's current and valid.
    '''
    auth = request.authorization
    if not auth:
        return jsonify(valid=False)
    token, password = auth.username, auth.password
    if token and not password:
        user = User.verify_auth_token(token)
        return jsonify(valid=(user is not None))
    return jsonify(valid=False)
{'fraction_non_alphanumeric': 0.09746588693957114, 'fraction_numerical': 0.00556947925368978, 'mean_word_length': 3.8672086720867207, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3090472', 'n_tokens_mistral': 1106, 'n_tokens_neox': 1079, 'n_words': 288}