Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_request_body(self):
client = BackendApplicationClient(self.client_id)
# Basic, no extra arguments
body = client.prepare_request_body(body=self.body)
self.assertFormBodyEqual(body, self.body_up)
rclient = BackendApplicationClient(self.client_id)
body = rclient.prepare_request_body(body=self.body)
self.assertFormBodyEqual(body, self.body_up)
# With extra parameters
body = client.prepare_request_body(body=self.body, **self.kwargs)
self.assertFormBodyEqual(body, self.body_kwargs)
def get_oauth_session(self):
known_state = session.get(AUTH_STATE_KEY)
redirect_url = urljoin(request.url_root, self.redirect_path)
if self.grant_type and self.grant_type == CLIENT_CREDENTIALS_GRANT_TYPE:
client = BackendApplicationClient(client_id=self.client_id)
oauth_session = OAuth2Session(client=client, token=session.get(AUTH_TOKEN_KEY))
elif self.grant_type and self.grant_type == IMPLICIT_GRANT_TYPE:
client = MobileApplicationClient(self.client_id)
client.response_type = self.config.get(RESPONSE_TYPE_CONFIG)
oauth_session = OAuth2Session(client_id=self.client_id,
state=known_state,
scope=self.config.get(SCOPE_CONFIG).split(),
redirect_uri=redirect_url,
client=client,
token=session.get(AUTH_TOKEN_KEY))
else:
client = WebApplicationClient(self.client_id)
oauth_session = OAuth2Session(client_id=self.client_id,
state=known_state,
scope=self.config.get(SCOPE_CONFIG).split(),
def get_oauth_session(self):
client = BackendApplicationClient(self.consumer_key)
token = None
if self.access_token:
token = {'access_token': self.access_token, 'token_type': self.token_type }
return self.configure_oauth_session(OAuth2Session(client=client, token=token))
def create_session(self):
if self.auth_type is None:
raise ValueError('Cannot create session without knowing the auth type')
if isinstance(self.credentials, OAuth2Credentials):
if self.auth_type != OAUTH2:
raise ValueError('Auth type must be %r for credentials type OAuth2Credentials' % OAUTH2)
token_url = 'https://login.microsoftonline.com/%s/oauth2/v2.0/token' % self.credentials.tenant_id
scope = ['https://outlook.office365.com/.default']
client = BackendApplicationClient(client_id=self.credentials.client_id)
session = self.raw_session(client)
# Fetch the token explicitly -- it doesn't occur implicitly
session.fetch_token(token_url=token_url, client_id=self.credentials.client_id,
client_secret=self.credentials.client_secret, scope=scope)
session.auth = get_auth_instance(auth_type=OAUTH2, client=client)
elif self.credentials:
if self.auth_type == NTLM and self.credentials.type == self.credentials.EMAIL:
username = '\\' + self.credentials.username
else:
username = self.credentials.username
session = self.raw_session()
session.auth = get_auth_instance(auth_type=self.auth_type, username=username,
password=self.credentials.password)
else:
if self.auth_type not in (GSSAPI, SSPI):
raise ValueError('Auth type %r requires credentials' % self.auth_type)
def _create_authenticated_http_session(
client_id: str, client_secret: str
) -> requests.Session:
oauth2_client = BackendApplicationClient(client_id=client_id)
session = OAuth2Session(client=oauth2_client)
session.fetch_token(
token_url="https://auth.sbanken.no/identityserver/connect/token",
client_id=client_id,
client_secret=urllib.parse.quote(client_secret),
)
return session
def _call(self, group, method, **kwargs):
data = {'method': method}
# Apply the provided keyword arguments to our data payload
data.update(kwargs)
# Fetch an OAuth token
client = BackendApplicationClient(client_id=self.client_id)
oauth = OAuth2Session(client=client)
token = oauth.fetch_token(token_url='https://services.rdio.com/oauth2/token',
client_id=self.client_id, client_secret=self.client_secret)
# Make the request
headers = {'Authorization': 'Bearer %s' % token.get('access_token')}
resp = requests.post('https://services.rdio.com/api/1/%s' % group,
headers=headers, data=data).json()
# Check for an error
if resp.get('status') != 'ok':
raise RuntimeError(resp.get('message'))
return resp.get('result')
def authenticate_server(self):
self.flow = AuthFlow.CLIENT_CREDENTIALS
default_user = User.default()
self.user_id = default_user.id
self.username = default_user.username
session = self.session or self.get_session(
client=BackendApplicationClient(self.client_id)
)
session.token_updater = User.token_updater(default_user.id)
if default_user.token:
session.token = default_user.token
else:
default_user.token = session.fetch_token(
token_url=API.TOKEN.value,
client_id=self.client_id,
client_secret=self.client_secret,
)
return session
def get_app_session(self, domain):
if self.can_auth_with_client_credentials:
sess = self.app_sessions.get(domain)
if not sess:
client_id = self.get_credentials(domain)[0]
sess = self.session_class(client=BackendApplicationClient(client_id))
self.app_sessions[domain] = sess
if not sess.token:
access_token_url = self.access_token_url.format(domain=domain)
client_id, client_secret = self.get_credentials(domain)
sess.fetch_token(access_token_url, client_id=client_id,
client_secret=client_secret)
return sess
else:
return self.get_auth_session(domain)