<commit_before>import json
import os
import datetime
import requests
from flask import Blueprint, jsonify, current_app as app
from flask_caching import Cache
from flask_jwt_extended import create_access_token, JWTManager, get_jwt_identity, jwt_required
from jose import jwt, JWTError, ExpiredSignatureError
from marshmallow import fields
from werkzeug.exceptions import UnprocessableEntity, Unauthorized, HTTPException, InternalServerError

from scuevals_api.models import Student, db, Role, APIKey
from scuevals_api.utils import use_args

auth_bp = Blueprint('auth', __name__)
cache = Cache(config={'CACHE_TYPE': 'simple'})
jwtm = JWTManager()


@auth_bp.route('/auth', methods=['POST'])
@use_args({'id_token': fields.String(required=True)}, locations=('json',))
def auth(args):
    try:
        headers = jwt.get_unverified_header(args['id_token'])
    except JWTError as e:
        raise UnprocessableEntity('invalid id_token format: {}'.format(e))

    key = cache.get(headers['kid'])
    if not key:
        try:
            refresh_key_cache(cache)
        except HTTPException as e:
            raise InternalServerError('failed to get certificates from Google: {}'.format(e))

    key = cache.get(headers['kid'])

    decode_options = {'verify_at_hash': False}

    if app.debug:
        decode_options['verify_exp'] = False

    try:
        data = jwt.decode(
            args['id_token'],
            json.dumps(key),
            audience=os.environ['GOOGLE_CLIENT_ID'],
            issuer=('https://accounts.google.com', 'accounts.google.com'),
            options=decode_options
        )
    except ExpiredSignatureError:
        raise Unauthorized('token is expired')
    except JWTError as e:
        raise UnprocessableEntity('invalid id_token: {}'.format(e))

    if data['hd'] != 'scu.edu':
        raise UnprocessableEntity('invalid id_token')

    user = Student.query.filter_by(email=data['email']).one_or_none()

    if user is None:
        # new user
        status = 'new'

        user = Student(
            email=data['email'],
            first_name=data['given_name'],
            last_name=data['family_name'],
            picture=data['picture'] if 'picture' in data else None,
            roles=[Role.query.get(Role.Incomplete)],<commit_msg>Assume user instead of student in auth<commit_after>import json
import os
import datetime
import requests
from flask import Blueprint, jsonify, current_app as app
from flask_caching import Cache
from flask_jwt_extended import create_access_token, JWTManager, get_jwt_identity, jwt_required
from jose import jwt, JWTError, ExpiredSignatureError
from marshmallow import fields
from werkzeug.exceptions import UnprocessableEntity, Unauthorized, HTTPException, InternalServerError

from scuevals_api.models import Student, User, db, Role, APIKey
from scuevals_api.utils import use_args

auth_bp = Blueprint('auth', __name__)
cache = Cache(config={'CACHE_TYPE': 'simple'})
jwtm = JWTManager()


@auth_bp.route('/auth', methods=['POST'])
@use_args({'id_token': fields.String(required=True)}, locations=('json',))
def auth(args):
    try:
        headers = jwt.get_unverified_header(args['id_token'])
    except JWTError as e:
        raise UnprocessableEntity('invalid id_token format: {}'.format(e))

    key = cache.get(headers['kid'])
    if not key:
        try:
            refresh_key_cache(cache)
        except HTTPException as e:
            raise InternalServerError('failed to get certificates from Google: {}'.format(e))

    key = cache.get(headers['kid'])

    decode_options = {'verify_at_hash': False}

    if app.debug:
        decode_options['verify_exp'] = False

    try:
        data = jwt.decode(
            args['id_token'],
            json.dumps(key),
            audience=os.environ['GOOGLE_CLIENT_ID'],
            issuer=('https://accounts.google.com', 'accounts.google.com'),
            options=decode_options
        )
    except ExpiredSignatureError:
        raise Unauthorized('token is expired')
    except JWTError as e:
        raise UnprocessableEntity('invalid id_token: {}'.format(e))

    if data['hd'] != 'scu.edu':
        raise UnprocessableEntity('invalid id_token')

    user = User.query.filter_by(email=data['email']).one_or_none()

    if user is None:
        # new user
        status = 'new'

        # assume new user is a student for now
        user = Student(
            email=data['email'],
            first_name=data['given_name'],
            last_name=data['family_name'],
            picture=data['picture'] if 'picture' in data else None,
            roles=[Role.query.get(Role.Incomplete)],
{'fraction_non_alphanumeric': 0.1132972972972973, 'fraction_numerical': 0.0, 'mean_word_length': 3.360037700282752, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '5750076', 'n_tokens_mistral': 1385, 'n_tokens_neox': 1351, 'n_words': 338}