Skip to content

Commit 910528e

Browse files
committed
update
1 parent a7e1f6c commit 910528e

4 files changed

Lines changed: 32 additions & 29 deletions

File tree

‎src/azure-cli-core/azure/cli/core/auth/identity.py‎

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,7 @@ def build_from_credential(cls, tenant_id, client_id, credential):
295295
return ServicePrincipalAuth(entry)
296296

297297
@classmethod
298-
def build_credential(cls, secret_or_certificate=None, client_assertion=None, use_cert_sn_issuer=None,
299-
federated_identity=None):
298+
def build_credential(cls, secret_or_certificate=None, client_assertion=None, use_cert_sn_issuer=None):
300299
"""Build credential from user input. The credential looks like below, but only one key can exist.
301300
{
302301
'client_secret': 'my_secret',
@@ -315,9 +314,6 @@ def build_credential(cls, secret_or_certificate=None, client_assertion=None, use
315314
entry[_CLIENT_SECRET] = secret_or_certificate
316315
elif client_assertion:
317316
entry[_CLIENT_ASSERTION] = client_assertion
318-
elif federated_identity:
319-
# Reuse client_assertion and set it to a const
320-
entry[_CLIENT_ASSERTION] = FEDERATED_IDENTITY
321317
return entry
322318

323319
def get_entry_to_persist(self):
@@ -352,7 +348,9 @@ def get_msal_client_credential(self):
352348
# "client_assertion": "...a JWT with claims aud, exp, iss, jti, nbf, and sub..."
353349
# }
354350
if self.client_assertion:
355-
client_credential = {'client_assertion': self.client_assertion}
351+
client_credential = {
352+
'client_assertion': get_id_token_on_github if self.client_assertion == FEDERATED_IDENTITY
353+
else self.client_assertion}
356354

357355
return client_credential
358356

@@ -461,3 +459,22 @@ def get_environment_credential():
461459
getenv(AZURE_TENANT_ID))
462460
credentials = ServicePrincipalCredential(sp_auth, authority=authority)
463461
return credentials
462+
463+
464+
def get_id_token_on_github():
465+
import os
466+
from urllib.parse import quote
467+
import requests
468+
token = os.environ['ACTIONS_ID_TOKEN_REQUEST_TOKEN']
469+
url = os.environ['ACTIONS_ID_TOKEN_REQUEST_URL']
470+
encodedAudience = quote('api://AzureADTokenExchange')
471+
url = f'{url}&audience={encodedAudience}'
472+
headers = {
473+
'Authorization': f'bearer {token}',
474+
'Accept': 'application/json; api-version=2.0',
475+
'Content-Type': 'application/json'
476+
}
477+
result = requests.get(url, headers=headers)
478+
id_token = result.json()['value']
479+
logger.warning('Got ID token: %s', id_token)
480+
return id_token

‎src/azure-cli-core/azure/cli/core/auth/msal_credentials.py‎

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -107,22 +107,3 @@ def get_token(self, *scopes, **kwargs):
107107
result = self._msal_app.acquire_token_for_client(list(scopes), **kwargs)
108108
check_result(result)
109109
return build_sdk_access_token(result)
110-
111-
112-
def get_id_token_on_github():
113-
import os
114-
from urllib.parse import quote
115-
import requests
116-
token = os.environ['ACTIONS_ID_TOKEN_REQUEST_TOKEN']
117-
url = os.environ['ACTIONS_ID_TOKEN_REQUEST_URL']
118-
encodedAudience = quote('api://AzureADTokenExchange')
119-
url = f'{url}&audience={encodedAudience}'
120-
headers = {
121-
'Authorization': f'bearer {token}',
122-
'Accept': 'application/json; api-version=2.0',
123-
'Content-Type': 'application/json'
124-
}
125-
result = requests.get(url, headers=headers)
126-
id_token = result.json()['value']
127-
logger.warning('Got ID token: %s', id_token)
128-
return id_token

‎src/azure-cli/azure/cli/command_modules/profile/__init__.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from azure.cli.core import AzCommandsLoader
77
from azure.cli.core.commands import CliCommandType
8-
from azure.cli.core.commands.parameters import get_enum_type
8+
from azure.cli.core.commands.parameters import get_enum_type, get_three_state_flag
99

1010
from azure.cli.command_modules.profile._format import transform_account_list
1111
import azure.cli.command_modules.profile._help # pylint: disable=unused-import
@@ -58,7 +58,8 @@ def load_arguments(self, command):
5858
c.argument('use_cert_sn_issuer', action='store_true', help='used with a service principal configured with Subject Name and Issuer Authentication in order to support automatic certificate rolls')
5959
c.argument('scopes', options_list=['--scope'], nargs='+', help='Used in the /authorize request. It can cover only one static resource.')
6060
c.argument('client_assertion', options_list=['--federated-token'], help='Federated token that can be used for OIDC token exchange.')
61-
c.argument('federated_identity', options_list=['--federated-identity'], help='Use federated identity credential.')
61+
c.argument('federated_identity', options_list=['--federated-identity'], arg_type=get_three_state_flag(),
62+
help='Use federated identity credential.')
6263

6364
with self.argument_context('logout') as c:
6465
c.argument('username', help='account user, if missing, logout the current active account')

‎src/azure-cli/azure/cli/command_modules/profile/custom.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ def login(cmd, username=None, password=None, service_principal=None, tenant=None
129129
raise CLIError("usage error: '--use-sn-issuer' is only applicable with a service principal")
130130
if service_principal and not username:
131131
raise CLIError('usage error: --service-principal --username NAME --password SECRET --tenant TENANT')
132+
if client_assertion and federated_identity:
133+
raise CLIError('usage error: Only one of --federated-token and --federated-identity can be specified')
134+
132135
if username and not service_principal and not identity:
133136
logger.warning(USERNAME_PASSWORD_DEPRECATION_WARNING)
134137

@@ -155,8 +158,9 @@ def login(cmd, username=None, password=None, service_principal=None, tenant=None
155158
if service_principal:
156159
from azure.cli.core.auth.identity import ServicePrincipalAuth
157160
password = ServicePrincipalAuth.build_credential(
158-
secret_or_certificate=password, client_assertion=client_assertion,
159-
use_cert_sn_issuer=use_cert_sn_issuer, federated_identity=federated_identity)
161+
secret_or_certificate=password,
162+
client_assertion='FEDERATED_IDENTITY' if federated_identity else client_assertion,
163+
use_cert_sn_issuer=use_cert_sn_issuer)
160164

161165
login_experience_v2 = cmd.cli_ctx.config.getboolean('core', 'login_experience_v2', fallback=True)
162166
# Send login_experience_v2 config to telemetry

0 commit comments

Comments
 (0)