|
1 |
| -from typing import Optional |
| 1 | +from typing import Optional, List, Callable |
2 | 2 |
|
3 | 3 | from requests import Response
|
4 | 4 | from typing_extensions import Self
|
5 | 5 |
|
6 | 6 | from office365.runtime.auth.authentication_context import AuthenticationContext
|
7 | 7 | from office365.runtime.auth.client_credential import ClientCredential
|
| 8 | +from office365.runtime.auth.token_response import TokenResponse |
8 | 9 | from office365.runtime.auth.user_credential import UserCredential
|
9 | 10 | from office365.runtime.http.request_options import RequestOptions
|
10 | 11 | from office365.runtime.odata.request import ODataRequest
|
11 | 12 | from office365.runtime.odata.v3.json_light_format import JsonLightFormat
|
12 | 13 |
|
13 | 14 |
|
14 | 15 | class SharePointRequest(ODataRequest):
|
15 |
| - def __init__(self, base_url): |
| 16 | + def __init__( |
| 17 | + self, base_url, environment="commercial", allow_ntlm=False, browser_mode=False |
| 18 | + ): |
16 | 19 | """
|
17 | 20 | :param str base_url: Absolute Web or Site Url
|
| 21 | + :param str environment: The Office 365 Cloud Environment endpoint used for authentication |
| 22 | + defaults to 'commercial'. |
| 23 | + :param bool allow_ntlm: Flag indicates whether NTLM scheme is enabled. Disabled by default |
| 24 | + :param bool browser_mode: Allow browser authentication |
18 | 25 | """
|
19 | 26 | super().__init__(JsonLightFormat())
|
20 |
| - self._auth_context = AuthenticationContext(url=base_url) |
| 27 | + self._auth_context = AuthenticationContext( |
| 28 | + url=base_url, |
| 29 | + environment=environment, |
| 30 | + allow_ntlm=allow_ntlm, |
| 31 | + browser_mode=browser_mode, |
| 32 | + ) |
21 | 33 | self.beforeExecute += self._authenticate_request
|
22 | 34 |
|
23 | 35 | def execute_request(self, path):
|
24 | 36 | # type: (str) -> Response
|
25 |
| - request_url = "{0}/_api/{1}".format(self._auth_context.url, path) |
| 37 | + request_url = "{0}/{1}".format(self.service_root_url, path) |
26 | 38 | return self.execute_request_direct(RequestOptions(request_url))
|
27 | 39 |
|
28 |
| - def with_credentials(self, credentials, environment="commercial"): |
29 |
| - # type: (UserCredential|ClientCredential, Optional[str]) -> Self |
| 40 | + def with_credentials(self, credentials): |
| 41 | + # type: (UserCredential|ClientCredential) -> Self |
30 | 42 | """
|
31 | 43 | Initializes a client to acquire a token via user or client credentials
|
32 |
| - :type credentials: UserCredential or ClientCredential |
33 |
| - :param str environment: The Office 365 Cloud Environment endpoint used for authentication |
34 |
| - defaults to 'commercial'. |
35 | 44 | """
|
36 |
| - self._auth_context.with_credentials(credentials, environment=environment) |
| 45 | + self._auth_context.with_credentials(credentials) |
| 46 | + return self |
| 47 | + |
| 48 | + def with_client_certificate( |
| 49 | + self, |
| 50 | + tenant, |
| 51 | + client_id, |
| 52 | + thumbprint, |
| 53 | + cert_path=None, |
| 54 | + private_key=None, |
| 55 | + scopes=None, |
| 56 | + passphrase=None, |
| 57 | + ): |
| 58 | + # type: (str, str, str, Optional[str], Optional[str], Optional[List[str]], Optional[str]) -> Self |
| 59 | + """ |
| 60 | + Creates authenticated SharePoint context via certificate credentials |
| 61 | +
|
| 62 | + :param str tenant: Tenant name |
| 63 | + :param str or None cert_path: Path to A PEM encoded certificate private key. |
| 64 | + :param str or None private_key: A PEM encoded certificate private key. |
| 65 | + :param str thumbprint: Hex encoded thumbprint of the certificate. |
| 66 | + :param str client_id: The OAuth client id of the calling application. |
| 67 | + :param list[str] or None scopes: Scopes requested to access a protected API (a resource) |
| 68 | + :param str passphrase: Passphrase if the private_key is encrypted |
| 69 | + """ |
| 70 | + self._auth_context.with_client_certificate( |
| 71 | + tenant, client_id, thumbprint, cert_path, private_key, scopes, passphrase |
| 72 | + ) |
| 73 | + return self |
| 74 | + |
| 75 | + def with_device_flow(self, tenant, client_id, scopes=None): |
| 76 | + # type: (str, str, Optional[List[str]]) -> Self |
| 77 | + """ |
| 78 | + Initializes a client to acquire a token via device flow auth. |
| 79 | +
|
| 80 | + :param str tenant: Tenant name, for example: contoso.onmicrosoft.com |
| 81 | + :param str client_id: The OAuth client id of the calling application. |
| 82 | + :param list[str] or None scopes: Scopes requested to access a protected API (a resource) |
| 83 | + """ |
| 84 | + self._auth_context.with_device_flow(tenant, client_id, scopes) |
| 85 | + return self |
| 86 | + |
| 87 | + def with_interactive(self, tenant, client_id, scopes=None): |
| 88 | + # type: (str, str, Optional[List[str]]) -> Self |
| 89 | + """ |
| 90 | + Initializes a client to acquire a token interactively i.e. via a local browser. |
| 91 | +
|
| 92 | + Prerequisite: In Azure Portal, configure the Redirect URI of your |
| 93 | + "Mobile and Desktop application" as ``http://localhost``. |
| 94 | +
|
| 95 | + :param str tenant: Tenant name, for example: contoso.onmicrosoft.com |
| 96 | + :param str client_id: The OAuth client id of the calling application. |
| 97 | + :param list[str] or None scopes: Scopes requested to access a protected API (a resource) |
| 98 | + """ |
| 99 | + self._auth_context.with_interactive(tenant, client_id, scopes) |
| 100 | + return self |
| 101 | + |
| 102 | + def with_access_token(self, token_func): |
| 103 | + # type: (Callable[[], TokenResponse]) -> Self |
| 104 | + """ |
| 105 | + Initializes a client to acquire a token from a callback |
| 106 | + :param () -> TokenResponse token_func: A token callback |
| 107 | + """ |
| 108 | + self._auth_context.with_access_token(token_func) |
37 | 109 | return self
|
38 | 110 |
|
39 | 111 | def _authenticate_request(self, request):
|
40 | 112 | # type: (RequestOptions) -> None
|
41 | 113 | """Authenticate request"""
|
42 | 114 | self._auth_context.authenticate_request(request)
|
| 115 | + |
| 116 | + @property |
| 117 | + def authentication_context(self): |
| 118 | + return self._auth_context |
| 119 | + |
| 120 | + @property |
| 121 | + def base_url(self): |
| 122 | + """Represents Base Url""" |
| 123 | + return self._auth_context.url |
| 124 | + |
| 125 | + @property |
| 126 | + def service_root_url(self): |
| 127 | + return "{0}/_api".format(self._auth_context.url) |
0 commit comments