# -*- encoding: utf-8 -*-
from flask import Blueprint, redirect, session, url_for, request, jsonify
from flask_oauthlib.client import OAuthException

from lazyblacksmith.oauth import eve_oauth
from lazyblacksmith.utils.crestutils import get_crest


sso = Blueprint('sso', __name__)

@sso.route('/crest/login')
def crest_login():
    return eve_oauth.authorize(
        callback = url_for(
            'sso.crest_callback',
            next = request.args.get('next') or request.referrer or None,
            _external = True
        ),
    )

@sso.route('/crest/callback')
def crest_callback():
    auth_response = eve_oauth.authorized_response()

    if auth_response is None:
        return 'Access denied: reason=%s error=%s' % (
            request.args['error_reason'],
            request.args['error_description']
        )

    if isinstance(auth_response, OAuthException):
        return 'Error while validating your authentification'



    crest_auth = get_crest()
    crest_auth = crest_auth.temptoken_authorize(auth_response['access_token'], auth_response['expires_in'], auth_response['refresh_token'])

    # save user
    # redirect

    return jsonify(crest_auth.whoami())


@sso.route('/logout')
def crest_logout():
    session.clear()
    return redirect(url_for("home.index"))

@eve_oauth.tokengetter
def get_eve_oauth_access_token(token='access'):
    print "get_eve_oauth_access_token !!!!!!!!"
    return None
@@ -241,16 +241,139 @@
 _crest%0A%0A
+from flask.ext.login import current_user, login_user, logout_user%0Afrom lazyblacksmith.utils.time import utcnow%0Aimport time%0A
 %0Asso = B
@@ -543,81 +543,8 @@
 k',%0A
-            next = request.args.get('next') or request.referrer or None,%0A
     
@@ -988,16 +988,174 @@
 ation'%0A%0A
+    # put in session for tokengetter%0A    session%5B'access_token'%5D = auth_response%5B'access_token'%5D%0A    session%5B'refresh_token'%5D = auth_response%5B'refresh_token'%5D
 %0A%0A    cr
@@ -1334,16 +1334,171 @@
 ve user%0A
+    if current_user.is_authenticated():%0A        # update data%0A        pass%0A%0A    else:%0A        pass%0A%0A    # login_user(eve_user)%0A%0A    # delete session data%0A%0A
     # re
@@ -1504,17 +1504,16 @@
 edirect%0A
-%0A
     retu
@@ -1596,20 +1596,18 @@
     
-session.clea
+logout_use
 r()%0A
@@ -1681,77 +1681,680 @@
 def 
-get_eve_oauth_access_token(token='access'):%0A    print %22get_eve_oauth_
+eve_oauth_tokengetter(token=None):%0A    if 'access_token' in session:%0A        return (session%5B'access_token'%5D,)%0A    elif current_user.is_authenticated():%0A        expires_at = current_user.access_token_expires_at%0A        if expires_at is None or (expires_at - utcnow()).total_seconds() %3C= 60:%0A            authed_crest = current_user.get_authed_crest()%0A            authed_crest.refresh()%0A%0A            current_user.access_token = authed_crest.token%0A            current_user.access_token_expires_in = authed_crest.expires - time.time()%0A            current_user.access_token_expires_on = datetime.fromtimestamp(authed_crest.expires, tz = pytz.utc)%0A%0A        return (current_user.
 acce
@@ -2365,18 +2365,10 @@
 oken
- !!!!!!!!%22
+,)
 %0A   

Add a few thing for sso login and sessions