ó
pî.Uc           @   sŒ   d  Z  d d l Z d d l Z d d l Z d d l Td d l m Z d d l m Z d d l	 j
 j Z d e f d „  ƒ  YZ d d	 „ Z d S(
   sö   
   RPX Authentication for web2py
   Developed by Nathan Freeze (Copyright Â© 2009)
   Email <nathan@freezable.com>
   Modified by Massimo Di Pierro

   This file contains code to allow using RPXNow.com (now Jainrain.com)
   services with web2py
iÿÿÿÿN(   t   *(   t   fetch(   t   Storaget
   RPXAccountc        	   B   sA   e  Z d  Z d d d e d d d d d „ Z d „  Z d „  Z RS(	   sq  
    from gluon.contrib.login_methods.rpx_account import RPXAccount
    auth.settings.actions_disabled=['register','change_password',
        'request_reset_password']
    auth.settings.login_form = RPXAccount(request,
              api_key="...",
              domain="...",
              url = "http://localhost:8000/%s/default/user/login" % request.application)
    t    s#   https://rpxnow.com/api/v2/auth_infot   ent   rpxc
         C   s±   | |  _  | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ d  |  _ | |  _	 |	 |  _
 t ƒ  |  _ i d d 6d d 6}
 |
 d „ |  j _ |
 d „ |  j _ d „  |  j _ d  S(   NR   t	   givenNamet
   familyNamec         S   sy   t  d |  j d d ƒ d |  j d d ƒ d |  j d d ƒ d |  j d | ƒ j d	 d ƒ d
 |  j d | ƒ j d d ƒ ƒ S(   Nt   registration_idt
   identifierR   t   usernamet   preferredUsernamet   emailt
   first_namet   nameR   t	   last_nameR   (   t   dictt   get(   t   profilet   dn(    (    sI   /var/www/c4bv.valis/web2py/gluon/contrib/login_methods/janrain_account.pyt   <lambda><   s
    c         S   sy   t  d |  j d d ƒ d |  j d d ƒ d |  j d d ƒ d |  j d | ƒ j d	 d ƒ d
 |  j d | ƒ j d d ƒ ƒ S(   NR	   R
   R   R   R   R   R   R   R   R   R   (   R   R   (   R   R   (    (    sI   /var/www/c4bv.valis/web2py/gluon/contrib/login_methods/janrain_account.pyR   B   s
    c         S   sU   t  d |  j d d ƒ d |  j d d ƒ d |  j d d ƒ d |  j d d ƒ d d ƒ S(	   NR	   R
   R   R   R   R   R   R   (   R   R   (   R   (    (    sI   /var/www/c4bv.valis/web2py/gluon/contrib/login_methods/janrain_account.pyR   H   s
    (   t   requestt   api_keyt   embedt   auth_urlt   domaint	   token_urlt   languaget   NoneR   t   promptt   on_login_failureR   t   mappingst   Facebookt   Googlet   default(   t   selfR   R   R   t   urlR   R   R   R   R   R   (    (    sI   /var/www/c4bv.valis/web2py/gluon/contrib/login_methods/janrain_account.pyt   __init__#   s    										c         C   sì   |  j  } | j j rè t ƒ  } t j t d |  j d | j j ƒ ƒ } t |  j	 d | ƒ } t
 j | ƒ } | d d k rÌ | d |  _ t j d d |  j d	 ƒ } |  j j | |  j j ƒ |  j ƒ } | S|  j rè t |  j ƒ qè n  d  S(
   Nt   apiKeyt   tokent   ?t   statt   okR   s   [^\w\-]R   t   providerName(   R   t   varsR(   R   t   urllibt	   urlencodeR   R   R   R   t   jsont   loadsR   t   ret   subR    R   R#   R   t   redirectR   (   R$   R   t   usert   datat   auth_info_jsont	   auth_infot   provider(    (    sI   /var/www/c4bv.valis/web2py/gluon/contrib/login_methods/janrain_account.pyt   get_userO   s     		!		c         C   s8   |  j  } | j } d |  j |  j |  j f } t | ƒ S(   NsK  
        <script type="text/javascript">
        (function() {
            if (typeof window.janrain !== 'object') window.janrain = {};
            if (typeof window.janrain.settings !== 'object') window.janrain.settings = {};
            janrain.settings.tokenUrl = '%s';
            function isReady() { janrain.ready = true; };
            if (document.addEventListener) {
                document.addEventListener("DOMContentLoaded", isReady, false);
            } else {
                window.attachEvent('onload', isReady);
            }
            var e = document.createElement('script');
            e.type = 'text/javascript';
            e.id = 'janrainAuthWidget';
            if (document.location.protocol === 'https:') {
                e.src = 'https://rpxnow.com/js/lib/%s/engage.js';
            } else {
                e.src = 'http://widget-cdn.rpxnow.com/js/lib/%s/engage.js';
            }
            var s = document.getElementsByTagName('script')[0];
            s.parentNode.insertBefore(e, s);
        })();
        </script>
        <div id="janrainEngageEmbed"></div>(   R   t   argsR   R   t   XML(   R$   R   R;   t   rpxform(    (    sI   /var/www/c4bv.valis/web2py/gluon/contrib/login_methods/janrain_account.pyt
   login_formb   s    		N(   t   __name__t
   __module__t   __doc__t   TrueR   R&   R:   R>   (    (    (    sI   /var/www/c4bv.valis/web2py/gluon/contrib/login_methods/janrain_account.pyR      s   
#	s   private/janrain.keyc   	   	   K   sÍ   t  j j t j j | ƒ } t  j j | ƒ rÉ t j } t | d ƒ j ƒ  j	 ƒ  j
 d ƒ \ } } t j j j } t d d d d d t ƒ} d d	 d
 g |  j _ t | d | d | d | | |  j _ n  d  S(   Nt   rt   :R#   R5   R;   t   logint   schemet   registert   change_passwordt   request_reset_passwordR   R   R%   (   t   ost   patht   joint   currentR   t   foldert   existst   opent   readt   stript   splitt   envt	   http_hostt   URLRB   t   settingst   actions_disabledR   R>   (	   t   autht   filenamet   kwargsRK   R   R   t   keyt   hostR%   (    (    sI   /var/www/c4bv.valis/web2py/gluon/contrib/login_methods/janrain_account.pyt   use_janrain€   s    	*(   RA   RJ   R2   R.   t   gluont   gluon.toolsR   t   gluon.storageR   t   gluon.contrib.simplejsont   contribt
   simplejsonR0   t   objectR   R^   (    (    (    sI   /var/www/c4bv.valis/web2py/gluon/contrib/login_methods/janrain_account.pyt   <module>   s   
i