當前位置: 首頁>>代碼示例>>Python>>正文


Python adal.AuthenticationContext方法代碼示例

本文整理匯總了Python中adal.AuthenticationContext方法的典型用法代碼示例。如果您正苦於以下問題:Python adal.AuthenticationContext方法的具體用法?Python adal.AuthenticationContext怎麽用?Python adal.AuthenticationContext使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在adal的用法示例。


在下文中一共展示了adal.AuthenticationContext方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: authorized

# 需要導入模塊: import adal [as 別名]
# 或者: from adal import AuthenticationContext [as 別名]
def authorized():
    """Handler for the application's Redirect Uri."""
    code = bottle.request.query.code
    auth_state = bottle.request.query.state
    if auth_state != SESSION.auth_state:
        raise Exception('state returned to redirect URL does not match!')
    auth_context = adal.AuthenticationContext(config.AUTHORITY_URL, api_version=None)
    token_response = auth_context.acquire_token_with_authorization_code(
        code, config.REDIRECT_URI, config.RESOURCE, config.CLIENT_ID, config.CLIENT_SECRET)
    SESSION.headers.update({'Authorization': f"Bearer {token_response['accessToken']}",
                            'User-Agent': 'adal-sample',
                            'Accept': 'application/json',
                            'Content-Type': 'application/json',
                            'SdkVersion': 'sample-python-adal',
                            'return-client-request-id': 'true'})
    return bottle.redirect('/graphcall') 
開發者ID:microsoftgraph,項目名稱:python-sample-auth,代碼行數:18,代碼來源:sample_adal_bottle.py

示例2: authorized

# 需要導入模塊: import adal [as 別名]
# 或者: from adal import AuthenticationContext [as 別名]
def authorized():
    """Handler for the application's Redirect Uri."""
    code = flask.request.args['code']
    auth_state = flask.request.args['state']
    if auth_state != SESSION.auth_state:
        raise Exception('state returned to redirect URL does not match!')
    auth_context = adal.AuthenticationContext(config.AUTHORITY_URL, api_version=None)
    token_response = auth_context.acquire_token_with_authorization_code(
        code, config.REDIRECT_URI, config.RESOURCE, config.CLIENT_ID, config.CLIENT_SECRET)
    SESSION.headers.update({'Authorization': f"Bearer {token_response['accessToken']}",
                            'User-Agent': 'adal-sample',
                            'Accept': 'application/json',
                            'Content-Type': 'application/json',
                            'SdkVersion': 'sample-python-adal',
                            'return-client-request-id': 'true'})
    return flask.redirect('/graphcall') 
開發者ID:microsoftgraph,項目名稱:python-sample-auth,代碼行數:18,代碼來源:sample_adal.py

示例3: authenticate_device_code

# 需要導入模塊: import adal [as 別名]
# 或者: from adal import AuthenticationContext [as 別名]
def authenticate_device_code():
    """
    Authenticate the end-user using device auth.
    """
    authority_host_uri = 'https://login.microsoftonline.com'
    tenant = '<TENANT_ID_OR_DOMAIN>'
    authority_uri = authority_host_uri + '/' + tenant
    resource_uri = 'https://management.core.windows.net/'
    client_id = '04b07795-8ddb-461a-bbee-02f9e1bf7b46'

    context = adal.AuthenticationContext(authority_uri, api_version=None)
    code = context.acquire_user_code(resource_uri, client_id)
    print(code['message'])
    mgmt_token = context.acquire_token_with_device_code(resource_uri, code, client_id)
    credentials = AADTokenCredentials(mgmt_token, client_id)

    return credentials 
開發者ID:Azure-Samples,項目名稱:data-lake-analytics-python-auth-options,代碼行數:19,代碼來源:sample.py

示例4: authenticate_username_password

# 需要導入模塊: import adal [as 別名]
# 或者: from adal import AuthenticationContext [as 別名]
def authenticate_username_password():
    """
    Authenticate using user w/ username + password.
    This doesn't work for users or tenants that have multi-factor authentication required.
    """
    authority_host_uri = 'https://login.microsoftonline.com'
    tenant = '<TENANT>'
    authority_uri = authority_host_uri + '/' + tenant
    resource_uri = 'https://management.core.windows.net/'
    username = '<USERNAME>'
    password = '<PASSWORD>'
    client_id = '<CLIENT_ID>'

    context = adal.AuthenticationContext(authority_uri, api_version=None)
    mgmt_token = context.acquire_token_with_username_password(resource_uri, username, password, client_id)
    credentials = AADTokenCredentials(mgmt_token, client_id)

    return credentials 
開發者ID:Azure-Samples,項目名稱:data-lake-analytics-python-auth-options,代碼行數:20,代碼來源:sample.py

示例5: authenticate_client_key

# 需要導入模塊: import adal [as 別名]
# 或者: from adal import AuthenticationContext [as 別名]
def authenticate_client_key():
    """
    Authenticate using service principal w/ key.
    """
    authority_host_uri = 'https://login.microsoftonline.com'
    tenant = '<TENANT>'
    authority_uri = authority_host_uri + '/' + tenant
    resource_uri = 'https://management.core.windows.net/'
    client_id = '<CLIENT_ID>'
    client_secret = '<CLIENT_SECRET>'

    context = adal.AuthenticationContext(authority_uri, api_version=None)
    mgmt_token = context.acquire_token_with_client_credentials(resource_uri, client_id, client_secret)
    credentials = AADTokenCredentials(mgmt_token, client_id)

    return credentials 
開發者ID:Azure-Samples,項目名稱:data-lake-analytics-python-auth-options,代碼行數:18,代碼來源:sample.py

示例6: authenticate_client_cert

# 需要導入模塊: import adal [as 別名]
# 或者: from adal import AuthenticationContext [as 別名]
def authenticate_client_cert():
    """
    Authenticate using service principal w/ cert.
    """
    authority_host_uri = 'https://login.microsoftonline.com'
    tenant = '<TENANT>'
    authority_uri = authority_host_uri + '/' + tenant
    resource_uri = 'https://management.core.windows.net/'
    client_id = '<CLIENT_ID>'
    client_cert = '<CLIENT_CERT>'
    client_cert_thumbprint = '<CLIENT_CERT_THUMBPRINT>'

    context = adal.AuthenticationContext(authority_uri, api_version=None)

    mgmt_token = context.acquire_token_with_client_certificate(resource_uri, client_id, client_cert, client_cert_thumbprint)
    credentials = AADTokenCredentials(mgmt_token, client_id)

    return credentials 
開發者ID:Azure-Samples,項目名稱:data-lake-analytics-python-auth-options,代碼行數:20,代碼來源:sample.py

示例7: _create_adal_context

# 需要導入模塊: import adal [as 別名]
# 或者: from adal import AuthenticationContext [as 別名]
def _create_adal_context(self):
        authority_url = self.cloud_environment.endpoints.active_directory
        is_adfs = bool(re.match('.+(/adfs|/adfs/)$', authority_url, re.I))
        if is_adfs:
            authority_url = authority_url.rstrip('/')  # workaround: ADAL is known to reject auth urls with trailing /
        else:
            authority_url = authority_url + '/' + self._tenant

        self._context = adal.AuthenticationContext(
            authority_url,
            timeout=self._timeout,
            verify_ssl=self._verify,
            proxies=self._proxies,
            validate_authority=not is_adfs,
            cache=self._cache,
            api_version=None
        ) 
開發者ID:Azure,項目名稱:msrestazure-for-python,代碼行數:19,代碼來源:azure_active_directory.py

示例8: _refresh_azure_token

# 需要導入模塊: import adal [as 別名]
# 或者: from adal import AuthenticationContext [as 別名]
def _refresh_azure_token(self, config):
        if 'adal' not in globals():
            raise ImportError('refresh token error, adal library not imported')

        tenant = config['tenant-id']
        authority = 'https://login.microsoftonline.com/{}'.format(tenant)
        context = adal.AuthenticationContext(
            authority, validate_authority=True,
        )
        refresh_token = config['refresh-token']
        client_id = config['client-id']
        token_response = context.acquire_token_with_refresh_token(
            refresh_token, client_id, '00000002-0000-0000-c000-000000000000')

        provider = self._user['auth-provider']['config']
        provider.value['access-token'] = token_response['accessToken']
        provider.value['expires-on'] = token_response['expiresOn']
        if self._config_persister:
            self._config_persister(self._config.value) 
開發者ID:adobe,項目名稱:ops-cli,代碼行數:21,代碼來源:kube_config.py

示例9: get_token_func

# 需要導入模塊: import adal [as 別名]
# 或者: from adal import AuthenticationContext [as 別名]
def get_token_func():
        """
        This function makes a call to AAD to fetch an OAuth token
        :return: the OAuth token and the interval to wait before refreshing it
        """
        print("{}: token updater was triggered".format(datetime.datetime.now()))

        # in this example, the OAuth token is obtained using the ADAL library
        # however, the user can use any preferred method
        context = adal.AuthenticationContext(
            str.format("https://login.microsoftonline.com/{}", settings.ACTIVE_DIRECTORY_TENANT_ID),
            api_version=None, validate_authority=True)

        oauth_token = context.acquire_token_with_client_credentials(
            "https://storage.azure.com",
            settings.ACTIVE_DIRECTORY_APPLICATION_ID,
            settings.ACTIVE_DIRECTORY_APPLICATION_SECRET)

        # return the token itself and the interval to wait before this function should be called again
        # generally oauth_token['expiresIn'] - 180 is a good interval to give, as it tells the caller to
        # refresh the token 3 minutes before it expires, so here we are assuming that the token expiration
        # is at least longer than 3 minutes, the user should adjust it according to their AAD policy
        return oauth_token['accessToken'], oauth_token['expiresIn'] - 180 
開發者ID:Azure,項目名稱:azure-storage-python,代碼行數:25,代碼來源:oauth.py

示例10: _refresh_azure_token

# 需要導入模塊: import adal [as 別名]
# 或者: from adal import AuthenticationContext [as 別名]
def _refresh_azure_token(self, config):
        if 'adal' not in globals():
            raise ImportError('refresh token error, adal library not imported')

        tenant = config['tenant-id']
        authority = 'https://login.microsoftonline.com/{}'.format(tenant)
        context = adal.AuthenticationContext(
            authority, validate_authority=True, api_version='1.0'
        )
        refresh_token = config['refresh-token']
        client_id = config['client-id']
        apiserver_id = '00000002-0000-0000-c000-000000000000'
        try:
            apiserver_id = config['apiserver-id']
        except ConfigException:
            # We've already set a default above
            pass
        token_response = context.acquire_token_with_refresh_token(
            refresh_token, client_id, apiserver_id)

        provider = self._user['auth-provider']['config']
        provider.value['access-token'] = token_response['accessToken']
        provider.value['expires-on'] = token_response['expiresOn']
        if self._config_persister:
            self._config_persister() 
開發者ID:kubernetes-client,項目名稱:python-base,代碼行數:27,代碼來源:kube_config.py

示例11: refresh_credential

# 需要導入模塊: import adal [as 別名]
# 或者: from adal import AuthenticationContext [as 別名]
def refresh_credential(self, credentials):
        """
        Refresh credentials
        """
        print_debug('Refreshing credentials')
        authority_uri = AUTHORITY_HOST_URI + '/' + self.get_tenant_id()
        existing_cache = self.context.cache
        context = adal.AuthenticationContext(authority_uri, cache=existing_cache)
        new_token = context.acquire_token(credentials.token['resource'],
                                          credentials.token['user_id'],
                                          credentials.token['_client_id'])

        new_credentials = AADTokenCredentials(new_token, credentials.token.get('_client_id'))
        return new_credentials 
開發者ID:nccgroup,項目名稱:ScoutSuite,代碼行數:16,代碼來源:authentication_strategy.py

示例12: _get_token

# 需要導入模塊: import adal [as 別名]
# 或者: from adal import AuthenticationContext [as 別名]
def _get_token(self):
        context = AuthenticationContext(self.authority_uri)
        code = context.acquire_user_code(self.client_uri, self.client_id)
        _prompt_for_code(code)
        self.config_data = context.acquire_token_with_device_code(
            self.client_uri, code, self.client_id
        )
        self._cache_creds() 
開發者ID:microsoft,項目名稱:msticpy,代碼行數:10,代碼來源:keyvault_client.py

示例13: _refresh_creds

# 需要導入模塊: import adal [as 別名]
# 或者: from adal import AuthenticationContext [as 別名]
def _refresh_creds(self):
        context = AuthenticationContext(self.authority_uri)
        self.config_data = context.acquire_token_with_refresh_token(
            self.config_data["refreshToken"], self.client_id, self.client_uri
        )
        if self.debug:
            print(f"got new token expiring {self.config_data['expiresOn']}")
            self._cache_creds() 
開發者ID:microsoft,項目名稱:msticpy,代碼行數:10,代碼來源:keyvault_client.py

示例14: device_flow_session

# 需要導入模塊: import adal [as 別名]
# 或者: from adal import AuthenticationContext [as 別名]
def device_flow_session(client_id, auto=False):
    """Obtain an access token from Azure AD (via device flow) and create
    a Requests session instance ready to make authenticated calls to
    Microsoft Graph.

    client_id = Application ID for registered "Azure AD only" V1-endpoint app
    auto      = whether to copy device code to clipboard and auto-launch browser

    Returns Requests session object if user signed in successfully. The session
    includes the access token in an Authorization header.

    User identity must be an organizational account (ADAL does not support MSAs).
    """
    ctx = AuthenticationContext(config.AUTHORITY_URL, api_version=None)
    device_code = ctx.acquire_user_code(config.RESOURCE,
                                        client_id)

    # display user instructions
    if auto:
        pyperclip.copy(device_code['user_code']) # copy user code to clipboard
        webbrowser.open(device_code['verification_url']) # open browser
        print(f'The code {device_code["user_code"]} has been copied to your clipboard, '
              f'and your web browser is opening {device_code["verification_url"]}. '
              'Paste the code to sign in.')
    else:
        print(device_code['message'])

    token_response = ctx.acquire_token_with_device_code(config.RESOURCE,
                                                        device_code,
                                                        client_id)
    if not token_response.get('accessToken', None):
        return None

    session = requests.Session()
    session.headers.update({'Authorization': f'Bearer {token_response["accessToken"]}',
                            'SdkVersion': 'sample-python-adal',
                            'x-client-SKU': 'sample-python-adal'})
    return session 
開發者ID:microsoftgraph,項目名稱:python-sample-console-app,代碼行數:40,代碼來源:helpers.py

示例15: get_client_with_username_password

# 需要導入模塊: import adal [as 別名]
# 或者: from adal import AuthenticationContext [as 別名]
def get_client_with_username_password(client_id, username, password, authority_url=None, resource_url=None, api_url=None):
        """
        Constructs a client with the option of using common defaults.

        :param client_id: The Power BI Client ID
        :param username: Username
        :param password: Password
        :param authority_url: The authority_url; defaults to 'https://login.windows.net/common'
        :param resource_url: The resource_url; defaults to 'https://analysis.windows.net/powerbi/api'
        :param api_url: The api_url: defaults to 'https://api.powerbi.com'
        :return:
        """
        if authority_url is None:
            authority_url = PowerBIClient.default_authority_url

        if resource_url is None:
            resource_url = PowerBIClient.default_resource_url

        if api_url is None:
            api_url = PowerBIClient.default_api_url

        context = adal.AuthenticationContext(authority=authority_url,
                                             validate_authority=True,
                                             api_version=None)

        # get your authentication token
        token = context.acquire_token_with_username_password(resource=resource_url,
                                                             client_id=client_id,
                                                             username=username,
                                                             password=password)

        return PowerBIClient(api_url, token) 
開發者ID:cmberryau,項目名稱:pypowerbi,代碼行數:34,代碼來源:client.py


注:本文中的adal.AuthenticationContext方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。