def login(request): """Login for Users""" data = request.body if data: try: data = json.loads(data) client_id = data.get("client_id", False) client_secret = data.get("client_secret", False) username = data.get("username", False) password = data.get("password", False) grant_type = data.get("grant_type", False) scope = data.get("scope", False) url = '/oauth/token/' + "?client_id=" + client_id + "&client_secret=" + \ client_secret + "&username="******"&password="******"&grant_type=" + grant_type + "&scope=" + scope from oauth2_provider.settings import oauth2_settings server = oauth2_settings.OAUTH2_SERVER_CLASS(oauth2_settings.OAUTH2_VALIDATOR_CLASS()) headers, body, status_res = server.create_token_response(url, "POST", "",{}, None) res = HttpResponse(content=body, status=status_res, content_type="application/json") for k, v in headers.items(): res[k] = v if res: return res except Exception as e: pass return HttpResponse(status=status.HTTP_400_BAD_REQUEST)
def _get_oauthlib_core(expires_in): """ Based on oauth2_provider.oauth2_backends.get_oauthlib_core, but allows passing in a value for token_expires_in. """ validator = dot_settings.OAUTH2_VALIDATOR_CLASS() server = dot_settings.OAUTH2_SERVER_CLASS(validator, token_expires_in=expires_in) return dot_settings.OAUTH2_BACKEND_CLASS(server)
def create_access_token(self, request, user, scope, client): """ Create and return a new access token. """ _days = 24 * 60 * 60 token_generator = BearerToken( expires_in=settings.OAUTH_EXPIRE_PUBLIC_CLIENT_DAYS * _days, request_validator=oauth2_settings.OAUTH2_VALIDATOR_CLASS(), ) self._populate_create_access_token_request(request, user, scope, client) return token_generator.create_token(request, refresh_token=True)
def create_dot_access_token(request, user, client, expires_in=None, scopes=None): """ Create and return a new (persisted) access token, including a refresh token. The token is returned in the form of a Dict: { u'access_token': u'some string', u'refresh_token': u'another string', u'token_type': u'Bearer', u'expires_in': 36000, u'scope': u'profile email', }, """ expires_in = _get_expires_in_value(expires_in) token_generator = BearerToken( expires_in=expires_in, request_validator=dot_settings.OAUTH2_VALIDATOR_CLASS(), ) _populate_create_access_token_request(request, user, client, scopes) return token_generator.create_token(request, refresh_token=True)