# Path: gitmostwanted/app.py

# 
# Path: gitmostwanted/models/user.py
# class User(db.Model):
#     __tablename__ = 'users'
# 
#     id = db.Column(db.Integer, primary_key=True)
#     email = db.Column(db.String(120), unique=True)
#     username = db.Column(db.String(80))
#     github_id = db.Column(db.BigInteger, unique=True)
# 
# Path: gitmostwanted/services/oauth.py
# def instance(app):


from flask import Blueprint, g, redirect, request, session, url_for
from gitmostwanted.app import app, db
from gitmostwanted.models.user import User
from gitmostwanted.services import oauth as service_oauth
    return oauth.github.authorize_redirect(
        redirect_uri=url_for('user_oauth.authorized', next=url_next(), _external=True),
        scope=request.args.get('scope')
    )


@user_oauth.route('/oauth/authorized')
def authorized():
    next_url = url_next() or url_for('/')

    resp = oauth.github.authorize_access_token()
    if resp is None or 'access_token' not in resp:
        return redirect(next_url)

    session.permanent = True
    session['oauth_access_token'] = (resp['access_token'], resp['scope'].split(','))

    result = oauth.github.get('user')
    if result:
        json = result.json()
        user = user_get_or_create(json['id'], json['email'], json['login'])
        session['user_id'] = user.id

    return redirect(next_url)


def user_get_or_create(uid: int, user_email: str, user_name: str):
    entity = User.query.filter_by(github_id=uid).first()
    if not entity:
        entity = User(github_id=uid, username=user_name, email=user_email or None)

# Path: gitmostwanted/app.py

# 
# Path: gitmostwanted/models/user.py
# class User(db.Model):
#     __tablename__ = 'users'
# 
#     id = db.Column(db.Integer, primary_key=True)
#     email = db.Column(db.String(120), unique=True)
#     username = db.Column(db.String(80))
#     github_id = db.Column(db.BigInteger, unique=True)
# 
# Path: gitmostwanted/services/oauth.py
# def instance(app):


# Path: gitmostwanted/blueprints/user_oauth.py
from flask import Blueprint, g, redirect, request, session, url_for
from gitmostwanted.app import app, db
from gitmostwanted.models.user import User
from gitmostwanted.services import oauth as service_oauth

    return oauth.github.authorize_redirect(
        redirect_uri=url_for('user_oauth.authorized', next=url_next(), _external=True),
        scope=request.args.get('scope')
    )


@user_oauth.route('/oauth/authorized')
def authorized():
    next_url = url_next() or url_for('/')

    resp = oauth.github.authorize_access_token()
    if resp is None or 'access_token' not in resp:
        return redirect(next_url)

    session.permanent = True
    session['oauth_access_token'] = (resp['access_token'], resp['scope'].split(','))

    result = oauth.github.get('user')
    if result:
        json = result.json()
        user = user_get_or_create(json['id'], json['email'], json['login'])
        session['user_id'] = user.id

    return redirect(next_url)


def user_get_or_create(uid: int, user_email: str, user_name: str):
    entity = User.query.filter_by(github_id=uid).first()
    if not entity:
        entity = User(github_id=uid, username=user_name, email=user_email or None)

        db.session.add(entity)