~ubuntu-branches/debian/sid/social-auth-core/sid

« back to all changes in this revision

Viewing changes to social_core/backends/lastfm.py

  • Committer: Package Import Robot
  • Author(s): Andre Bianchi
  • Date: 2018-02-22 19:49:12 UTC
  • Revision ID: package-import@ubuntu.com-20180222194912-4lqv8mlhnqc4ncd3
Tags: upstream-1.7.0
ImportĀ upstreamĀ versionĀ 1.7.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import hashlib
 
2
 
 
3
from ..utils import handle_http_errors
 
4
from .base import BaseAuth
 
5
 
 
6
 
 
7
class LastFmAuth(BaseAuth):
 
8
    """
 
9
    Last.Fm authentication backend. Requires two settings:
 
10
        SOCIAL_AUTH_LASTFM_KEY
 
11
        SOCIAL_AUTH_LASTFM_SECRET
 
12
 
 
13
    Don't forget to set the Last.fm callback to something sensible like
 
14
        http://your.site/lastfm/complete
 
15
    """
 
16
    name = 'lastfm'
 
17
    AUTH_URL = 'http://www.last.fm/api/auth/?api_key={api_key}'
 
18
    EXTRA_DATA = [
 
19
        ('key', 'session_key')
 
20
    ]
 
21
 
 
22
    def auth_url(self):
 
23
        return self.AUTH_URL.format(api_key=self.setting('KEY'))
 
24
 
 
25
    @handle_http_errors
 
26
    def auth_complete(self, *args, **kwargs):
 
27
        """Completes login process, must return user instance"""
 
28
        key, secret = self.get_key_and_secret()
 
29
        token = self.data['token']
 
30
 
 
31
        signature = hashlib.md5(''.join(
 
32
            ('api_key', key, 'methodauth.getSession', 'token', token, secret)
 
33
        ).encode()).hexdigest()
 
34
 
 
35
        response = self.get_json('http://ws.audioscrobbler.com/2.0/', data={
 
36
            'method': 'auth.getSession',
 
37
            'api_key': key,
 
38
            'token': token,
 
39
            'api_sig': signature,
 
40
            'format': 'json'
 
41
        }, method='POST')
 
42
 
 
43
        kwargs.update({'response': response['session'], 'backend': self})
 
44
        return self.strategy.authenticate(*args, **kwargs)
 
45
 
 
46
    def get_user_id(self, details, response):
 
47
        """Return a unique ID for the current user, by default from server
 
48
        response."""
 
49
        return response.get('name')
 
50
 
 
51
    def get_user_details(self, response):
 
52
        fullname, first_name, last_name = self.get_user_names(response['name'])
 
53
        return {
 
54
            'username': response['name'],
 
55
            'email': '',
 
56
            'fullname': fullname,
 
57
            'first_name': first_name,
 
58
            'last_name': last_name
 
59
        }