Right now, I have an API that sits on a server that also issues access tokens. The API is created from Django Rest Framework and is protected by OAuth2TokenAuthentication from django-oauth-toolkit, while this is working fine, but the authentication is done against token stored locally.
class OAuth2Authentication(BaseAuthentication):
"""
OAuth 2 authentication backend using `django-oauth-toolkit`
"""
www_authenticate_realm = 'api'
def authenticate(self, request):
"""
Returns two-tuple of (user, token) if authentication succeeds, or None otherwise.
"""
oauthlib_core = get_oauthlib_core()
valid, r = oauthlib_core.verify_request(request, scopes=[])
if valid:
return r.user, r.access_token
else:
return None
def authenticate_header(self, request):
"""
Bearer is the only finalized type currently
"""
return 'Bearer realm="{}"'.format(self.www_authenticate_realm)
I would like to split the server into 2 servers, Authentication Server and Resource Server, so that service that hosts the API does not need to have token storage mechanism. As you can see from the code above, r.access_token is a model instance of AccessToken.
I'm unsure what's the best way to change the API authentication to check against the AS server remotely (perhaps there is a written package already?)
I had a search on the internet about token validations such as this one, while it provides some ideas but don't seem to be specific enough for my problem.