@@ -189,6 +189,52 @@ def find_subscriptions_on_login(self,
189189 # use deepcopy as we don't want to persist these changes to file.
190190 return deepcopy (consolidated )
191191
192+ def find_subscriptions_in_cloud_console_thru_raw_token (self , tokens ):
193+ from datetime import datetime , timedelta
194+ import jwt
195+ arm_token = tokens [0 ] # cloud shell gurantees that the 1st is for ARM
196+ arm_token_decoded = jwt .decode (arm_token , verify = False , algorithms = ['RS256' ])
197+ tenant = arm_token_decoded ['tid' ]
198+ user_id = arm_token_decoded ['unique_name' ].split ('#' )[- 1 ]
199+ subscription_finder = SubscriptionFinder (self .cli_ctx , self .auth_ctx_factory , None )
200+ subscriptions = subscription_finder .find_from_raw_token (tenant , arm_token )
201+ consolidated = self ._normalize_properties (user_id , subscriptions , is_service_principal = False )
202+ self ._set_subscriptions (consolidated )
203+
204+ # construct token entries to cache
205+ decoded_tokens = [arm_token_decoded ]
206+ for t in tokens [1 :]:
207+ decoded_tokens .append (jwt .decode (t , verify = False , algorithms = ['RS256' ]))
208+ final_tokens = []
209+ # Note, setting expiration time at 2700 seconds is bit arbitrary, but should not matter
210+ # as shell should update us with new ones every 10~15 minutes
211+ for t in decoded_tokens :
212+ final_tokens .append ({
213+ '_clientId' : _CLIENT_ID ,
214+ 'expiresIn' : '2700' ,
215+ 'expiresOn' : str (datetime .now () + timedelta (seconds = 2700 )),
216+ 'userId' : t ['unique_name' ].split ('#' )[- 1 ],
217+ '_authority' : self .cli_ctx .cloud .endpoints .active_directory .rstrip ('/' ) + '/' + t ['tid' ],
218+ 'resource' : t ['aud' ],
219+ 'isMRRT' : True ,
220+ 'accessToken' : tokens [decoded_tokens .index (t )],
221+ 'tokenType' : 'Bearer' ,
222+ })
223+
224+ # merging with existing cached ones
225+ for t in final_tokens :
226+ cached_tokens = [entry for _ , entry in self ._creds_cache .adal_token_cache .read_items ()]
227+ to_delete = [c for c in cached_tokens if (c ['_clientId' ].lower () == t ['_clientId' ].lower () and
228+ c ['resource' ].lower () == t ['resource' ].lower () and
229+ c ['_authority' ].lower () == t ['_authority' ].lower () and
230+ c ['userId' ].lower () == t ['userId' ].lower ())]
231+ if to_delete :
232+ self ._creds_cache .adal_token_cache .remove (to_delete )
233+ self ._creds_cache .adal_token_cache .add (final_tokens )
234+ self ._creds_cache .persist_cached_creds ()
235+
236+ return deepcopy (consolidated )
237+
192238 def _normalize_properties (self , user , subscriptions , is_service_principal ):
193239 consolidated = []
194240 for s in subscriptions :
@@ -410,8 +456,8 @@ def _retrieve_token():
410456 identity_id , msi_port = Profile ._try_parse_for_msi_port (account [_SUBSCRIPTION_NAME ])
411457 if msi_port is not None :
412458 return Profile .get_msi_token (resource , msi_port , identity_id )
413- elif in_cloud_console () and account [_USER_ENTITY ].get (_CLOUD_SHELL_ID ):
414- return Profile .get_msi_token (resource , _get_cloud_console_token_endpoint ())
459+ # elif in_cloud_console() and account[_USER_ENTITY].get(_CLOUD_SHELL_ID):
460+ # return Profile.get_msi_token(resource, _get_cloud_console_token_endpoint())
415461 elif user_type == _USER :
416462 return self ._creds_cache .retrieve_token_for_user (username_or_sp_id ,
417463 account [_TENANT_ID ], resource )
@@ -448,8 +494,8 @@ def get_raw_token(self, resource=None, subscription=None):
448494 identity_id , msi_port = Profile ._try_parse_for_msi_port (account [_SUBSCRIPTION_NAME ])
449495 if msi_port is not None :
450496 creds = Profile .get_msi_token (resource , msi_port , identity_id )
451- elif in_cloud_console () and account [_USER_ENTITY ].get (_CLOUD_SHELL_ID ):
452- creds = Profile .get_msi_token (resource , _get_cloud_console_token_endpoint ())
497+ # elif in_cloud_console() and account[_USER_ENTITY].get(_CLOUD_SHELL_ID):
498+ # creds = Profile.get_msi_token(resource, _get_cloud_console_token_endpoint())
453499 elif user_type == _USER :
454500 creds = self ._creds_cache .retrieve_token_for_user (username_or_sp_id ,
455501 account [_TENANT_ID ], resource )
0 commit comments