# Path: apps/core/models.py
# class ServiceMap(models.Model):
#     """
#     denotes single (user, service) pair
#     - sid:             unique mapping id
#     - user:            user object
#     - service:         service object
#     - register_time:   register time
#     - unregister_time: unregister time
#     """
#     sid = models.CharField(max_length=20, primary_key=True)
#     user = models.ForeignKey(User, on_delete=models.CASCADE,
#                              related_name='services')
#     service = models.ForeignKey(Service, on_delete=models.CASCADE)
#     register_time = models.DateTimeField()
#     unregister_time = models.DateTimeField(null=True, blank=True)
# 
#     def __str__(self):
#         return f'{self.user} - {self.service}'


import hmac
import time
import requests
from secrets import token_hex
from django.utils import timezone
from apps.core.models import ServiceMap
        if service.scope == 'TEST':
            map_obj.unregister_time = timezone.now()
            map_obj.save()
            return {'success': True}
        elif service.scope == 'SPARCS':
            return unknown_error
        else:
            return unknown_error

    client_id = service.name
    sid = map_obj.sid
    timestamp = int(time.time())
    sign = hmac.new(
        service.secret_key.encode(),
        ''.join([sid, str(timestamp)]).encode(),
        'md5',
    ).hexdigest()
    try:
        r = requests.post(service.unregister_url, data={
            'client_id': client_id,
            'sid': sid,
            'timestamp': timestamp,
            'sign': sign,
        })
        result = r.json()
    except Exception:
        return unknown_error

    if result.get('success', False):
        map_obj.unregister_time = timezone.now()

# Path: apps/core/models.py
# class ServiceMap(models.Model):
#     """
#     denotes single (user, service) pair
#     - sid:             unique mapping id
#     - user:            user object
#     - service:         service object
#     - register_time:   register time
#     - unregister_time: unregister time
#     """
#     sid = models.CharField(max_length=20, primary_key=True)
#     user = models.ForeignKey(User, on_delete=models.CASCADE,
#                              related_name='services')
#     service = models.ForeignKey(Service, on_delete=models.CASCADE)
#     register_time = models.DateTimeField()
#     unregister_time = models.DateTimeField(null=True, blank=True)
# 
#     def __str__(self):
#         return f'{self.user} - {self.service}'


# Path: apps/core/backends/service.py
import hmac
import time
import requests
from secrets import token_hex
from django.utils import timezone
from apps.core.models import ServiceMap

        if service.scope == 'TEST':
            map_obj.unregister_time = timezone.now()
            map_obj.save()
            return {'success': True}
        elif service.scope == 'SPARCS':
            return unknown_error
        else:
            return unknown_error

    client_id = service.name
    sid = map_obj.sid
    timestamp = int(time.time())
    sign = hmac.new(
        service.secret_key.encode(),
        ''.join([sid, str(timestamp)]).encode(),
        'md5',
    ).hexdigest()
    try:
        r = requests.post(service.unregister_url, data={
            'client_id': client_id,
            'sid': sid,
            'timestamp': timestamp,
            'sign': sign,
        })
        result = r.json()
    except Exception:
        return unknown_error

    if result.get('success', False):
        map_obj.unregister_time = timezone.now()

        map_obj.save()