Exemple #1
0
    def _post_data(self, params):
        params['api_key'] = self.api_key

        if self.session_key is not None:
            params['sk'] = self.session_key
        else:
            raise AuthenticationFailedError("session key must be present to call this method")

        params['api_sig'] = self._get_api_sig(params)
        xml = self._post_url(Api.API_ROOT_URL, params)
        return self._check_xml(xml)
Exemple #2
0
 def _get_api_sig(self, params):
     if self.secret is not None:
         keys = params.keys()[:]
         keys.sort()
         sig = unicode()
         for name in keys:
             if name == 'api_sig': continue
             sig += ("%s%s" % (name, params[name]))
         sig += self.secret
         hashed_sig = md5hash(sig)
         return hashed_sig
     else:
         raise AuthenticationFailedError("api secret must be present to call this method")
Exemple #3
0
    def _fetch_data(self, params, sign=False, session=False, no_cache=False):
        params = params.copy()
        params['api_key'] = self.api_key

        if session:
            if self.session_key is not None:
                params['sk'] = self.session_key
            else:
                raise AuthenticationFailedError(
                    "session key must be present to call this method")

        if sign:
            params['api_sig'] = self._get_api_sig(params)
        xml = self._fetch_url(Api.API_ROOT_URL,
                              params,
                              no_cache=self._no_cache or no_cache)
        return self._check_xml(xml)
Exemple #4
0
 def get_authenticated_user(self, callback = None):
     """
     Get the currently authenticated user.
     
     @param callback: callback function for asynchronous invocation (optional)
     @type callback: C{function}
     
     @return:     The currently authenticated user if the session is authenticated
     @rtype:      L{User}
     
     @see:        L{User.get_authenticated_user}
     @see:        L{async_callback}
     """
     if self.session_key is not None:
         return User.get_authenticated_user(self)
     else:
         raise AuthenticationFailedError("session key must be present to call this method")
Exemple #5
0
def authentication_required(func, *args, **kwargs):
    """
    A decorator to check if the current user is authenticated or not. Used only
    on the functions that need authentication. If not authenticated then an
    exception is raised.
    
    @param func:    a function that needs to be authentication, for being called
    @type func:     C{function}
    
    @return:        a function that wraps the original function
    @rtype:         C{function}
    
    @raise AuthenticationFailedError: If the user is not authenticated, then an
                                      exception is raised.
    """
    self = args[0]
    from lastfm.user import User, Api
    username = None
    if isinstance(self, User):
        username = self.name
        if self.authenticated:
            return func(*args, **kwargs)
    elif hasattr(self, 'user'):
        username = self.user.name
        if self.user.authenticated:
            return func(*args, **kwargs)
    elif hasattr(self, '_subject') and isinstance(self._subject, User):
        username = self._subject.name
        if self._subject.authenticated:
            return func(*args, **kwargs)
    elif hasattr(self, '_api') and isinstance(self._api, Api):
        try:
            user = self._api.get_authenticated_user()
            username = user.name
            return func(*args, **kwargs)
        except AuthenticationFailedError:
            pass
    raise AuthenticationFailedError(
        "user '%s' does not have permissions to access the service" % username)