# Path: logviewer/auth/base.py
# class AuthBackend(object):
#     def login(self, error=None):
#         raise NotImplementedError()
# 
#     def authenticate(self):
#         raise NotImplementedError()
# 
# Path: logviewer/exc.py
# class AuthenticationError(RuntimeError):
#     pass


import flask
import requests
from flask import request, url_for
from flask.ext.oauthlib.client import OAuth, OAuthException
from .base import AuthBackend
from ..exc import AuthenticationError


oauth = OAuth()


class GoogleAppsAuth(AuthBackend):
    def __init__(self, name, domain, consumer_key, consumer_secret):
        self.domain = domain
        self.remote = oauth.remote_app(
            name,
            consumer_key=consumer_key,
            consumer_secret=consumer_secret,
            base_url=u'https://www.googleapis.com/',
            request_token_url=None,
            request_token_params={'scope': [
                u'https://www.googleapis.com/auth/userinfo.email',
            ]},
            access_token_url=u'https://accounts.google.com/o/oauth2/token',
            access_token_method='POST',
            authorize_url=u'https://accounts.google.com/o/oauth2/auth',
        )
        self.authenticate = self.remote.authorized_handler(self._authorize)

    def login(self, error=None):
        flask.session['_next_url'] = request.args.get('next')
        callback_url = url_for('authenticate', _external=True)
        return self.remote.authorize(callback=callback_url)

    def _authorize(self, resp):
        if resp is None:

# Path: logviewer/auth/base.py
# class AuthBackend(object):
#     def login(self, error=None):
#         raise NotImplementedError()
# 
#     def authenticate(self):
#         raise NotImplementedError()
# 
# Path: logviewer/exc.py
# class AuthenticationError(RuntimeError):
#     pass


# Path: logviewer/auth/googleapps.py
import flask
import requests
from flask import request, url_for
from flask.ext.oauthlib.client import OAuth, OAuthException
from .base import AuthBackend
from ..exc import AuthenticationError



oauth = OAuth()


class GoogleAppsAuth(AuthBackend):
    def __init__(self, name, domain, consumer_key, consumer_secret):
        self.domain = domain
        self.remote = oauth.remote_app(
            name,
            consumer_key=consumer_key,
            consumer_secret=consumer_secret,
            base_url=u'https://www.googleapis.com/',
            request_token_url=None,
            request_token_params={'scope': [
                u'https://www.googleapis.com/auth/userinfo.email',
            ]},
            access_token_url=u'https://accounts.google.com/o/oauth2/token',
            access_token_method='POST',
            authorize_url=u'https://accounts.google.com/o/oauth2/auth',
        )
        self.authenticate = self.remote.authorized_handler(self._authorize)

    def login(self, error=None):
        flask.session['_next_url'] = request.args.get('next')
        callback_url = url_for('authenticate', _external=True)
        return self.remote.authorize(callback=callback_url)

    def _authorize(self, resp):
        if resp is None:

            raise AuthenticationError(