2015-06-08 19:36:49 +00:00
|
|
|
import logging
|
2015-03-02 20:37:54 +00:00
|
|
|
import urllib
|
|
|
|
|
2015-01-08 20:55:24 +00:00
|
|
|
from django.http import JsonResponse
|
2015-03-02 20:37:54 +00:00
|
|
|
|
2015-02-18 18:07:22 +00:00
|
|
|
from oidc_provider.lib.errors import *
|
|
|
|
from oidc_provider.lib.utils.params import *
|
|
|
|
from oidc_provider.lib.utils.token import *
|
|
|
|
from oidc_provider.models import *
|
|
|
|
from oidc_provider import settings
|
2015-01-08 20:55:24 +00:00
|
|
|
|
2015-06-08 19:36:49 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2015-01-08 20:55:24 +00:00
|
|
|
|
|
|
|
class TokenEndpoint(object):
|
|
|
|
|
|
|
|
def __init__(self, request):
|
|
|
|
self.request = request
|
2015-01-09 17:59:23 +00:00
|
|
|
self.params = Params()
|
2015-01-08 20:55:24 +00:00
|
|
|
self._extract_params()
|
|
|
|
|
2015-06-08 19:36:49 +00:00
|
|
|
logger.debug('Request %s', self.request)
|
|
|
|
logger.debug('TokenEndPoint request.POST --> : %s', self.request.POST)
|
|
|
|
logger.debug('TokenEndpoint request.GET --> : %s', self.request.GET)
|
|
|
|
logger.debug('TokenEndPoint extract_params --> : %s', self.params.__dict__)
|
|
|
|
|
2015-01-08 20:55:24 +00:00
|
|
|
def _extract_params(self):
|
|
|
|
query_dict = self.request.POST
|
|
|
|
|
|
|
|
self.params.client_id = query_dict.get('client_id', '')
|
|
|
|
self.params.client_secret = query_dict.get('client_secret', '')
|
2015-01-28 20:00:04 +00:00
|
|
|
self.params.redirect_uri = urllib.unquote(
|
|
|
|
query_dict.get('redirect_uri', ''))
|
2015-01-08 20:55:24 +00:00
|
|
|
self.params.grant_type = query_dict.get('grant_type', '')
|
|
|
|
self.params.code = query_dict.get('code', '')
|
|
|
|
self.params.state = query_dict.get('state', '')
|
|
|
|
|
|
|
|
def validate_params(self):
|
|
|
|
if not (self.params.grant_type == 'authorization_code'):
|
2015-06-08 19:36:49 +00:00
|
|
|
logger.error('Unsupported grant type: --> : %s', self.params.grant_type)
|
2015-01-08 20:55:24 +00:00
|
|
|
raise TokenError('unsupported_grant_type')
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.client = Client.objects.get(client_id=self.params.client_id)
|
|
|
|
|
|
|
|
if not (self.client.client_secret == self.params.client_secret):
|
2015-06-08 19:36:49 +00:00
|
|
|
logger.error('Invalid client, client secret -->: %s', self.params.client_secret)
|
2015-01-08 20:55:24 +00:00
|
|
|
raise TokenError('invalid_client')
|
|
|
|
|
|
|
|
if not (self.params.redirect_uri in self.client.redirect_uris):
|
2015-06-08 19:36:49 +00:00
|
|
|
logger.error('Invalid client, redirect_uri --> : %s', self.params.redirect_uri)
|
2015-01-08 20:55:24 +00:00
|
|
|
raise TokenError('invalid_client')
|
|
|
|
|
|
|
|
self.code = Code.objects.get(code=self.params.code)
|
|
|
|
|
2015-04-21 17:28:59 +00:00
|
|
|
if not (self.code.client == self.client) \
|
|
|
|
or self.code.has_expired():
|
2015-06-08 19:36:49 +00:00
|
|
|
logger.error('Invalid grant, code client --> %s', self.code.client)
|
2015-01-08 20:55:24 +00:00
|
|
|
raise TokenError('invalid_grant')
|
|
|
|
|
|
|
|
except Client.DoesNotExist:
|
|
|
|
raise TokenError('invalid_client')
|
|
|
|
|
|
|
|
except Code.DoesNotExist:
|
|
|
|
raise TokenError('invalid_grant')
|
|
|
|
|
|
|
|
def create_response_dic(self):
|
2015-03-02 20:37:54 +00:00
|
|
|
id_token_dic = create_id_token(
|
2015-04-29 21:55:48 +00:00
|
|
|
user=self.code.user,
|
|
|
|
aud=self.client.client_id)
|
2015-01-08 20:55:24 +00:00
|
|
|
|
|
|
|
token = create_token(
|
2015-01-28 18:19:36 +00:00
|
|
|
user=self.code.user,
|
|
|
|
client=self.code.client,
|
|
|
|
id_token_dic=id_token_dic,
|
|
|
|
scope=self.code.scope)
|
2015-01-08 20:55:24 +00:00
|
|
|
|
|
|
|
# Store the token.
|
|
|
|
token.save()
|
|
|
|
|
|
|
|
# We don't need to store the code anymore.
|
|
|
|
self.code.delete()
|
|
|
|
|
|
|
|
id_token = encode_id_token(id_token_dic, self.client.client_secret)
|
|
|
|
|
|
|
|
dic = {
|
|
|
|
'access_token': token.access_token,
|
|
|
|
'token_type': 'bearer',
|
2015-02-26 19:14:36 +00:00
|
|
|
'expires_in': settings.get('OIDC_TOKEN_EXPIRE'),
|
2015-01-08 20:55:24 +00:00
|
|
|
'id_token': id_token,
|
|
|
|
}
|
2015-06-08 19:36:49 +00:00
|
|
|
logger.debug('Response dictionary --> : %s', dic)
|
2015-01-08 20:55:24 +00:00
|
|
|
return dic
|
|
|
|
|
|
|
|
@classmethod
|
2015-01-29 15:54:13 +00:00
|
|
|
def response(cls, dic, status=200):
|
2015-01-28 18:19:36 +00:00
|
|
|
"""
|
2015-01-08 20:55:24 +00:00
|
|
|
Create and return a response object.
|
2015-01-28 18:19:36 +00:00
|
|
|
"""
|
2015-01-08 20:55:24 +00:00
|
|
|
response = JsonResponse(dic, status=status)
|
|
|
|
response['Cache-Control'] = 'no-store'
|
|
|
|
response['Pragma'] = 'no-cache'
|
|
|
|
|
2015-06-08 19:36:49 +00:00
|
|
|
logger.debug('JSON Response --> : %s', response.__dict__)
|
|
|
|
|
2015-01-28 18:19:36 +00:00
|
|
|
return response
|