Exemple #1
0
        public void ParseFromStringNoDotTest(string homeAccountId, string objectId, string tenantId)
        {
            AccountId accountId = AccountId.ParseFromString(homeAccountId);

            Assert.AreEqual(objectId, accountId.ObjectId);
            Assert.AreEqual(tenantId, accountId.TenantId);
        }
        internal static MsalTokenResponse CreateFromiOSBrokerResponse(Dictionary <string, string> responseDictionary)
        {
            if (responseDictionary.TryGetValue(BrokerResponseConst.BrokerErrorCode, out string errorCode))
            {
                string metadataOriginal = responseDictionary.ContainsKey(MsalTokenResponse.iOSBrokerErrorMetadata) ? responseDictionary[MsalTokenResponse.iOSBrokerErrorMetadata] : null;
                Dictionary <string, string> metadataDictionary = null;

                if (metadataOriginal != null)
                {
                    string brokerMetadataJson = Uri.UnescapeDataString(metadataOriginal);
                    metadataDictionary = Json.JsonConvert.DeserializeObject <Dictionary <string, string> >(brokerMetadataJson);
                }

                string homeAcctId = null;
                metadataDictionary?.TryGetValue(MsalTokenResponse.iOSBrokerHomeAccountId, out homeAcctId);
                return(new MsalTokenResponse
                {
                    Error = errorCode,
                    ErrorDescription = responseDictionary.ContainsKey(BrokerResponseConst.BrokerErrorDescription) ? CoreHelpers.UrlDecode(responseDictionary[BrokerResponseConst.BrokerErrorDescription]) : string.Empty,
                    SubError = responseDictionary.ContainsKey(OAuth2ResponseBaseClaim.SubError) ? responseDictionary[OAuth2ResponseBaseClaim.SubError] : string.Empty,
                    AccountUserId = homeAcctId != null?AccountId.ParseFromString(homeAcctId).ObjectId : null,
                    TenantId = homeAcctId != null?AccountId.ParseFromString(homeAcctId).TenantId : null,
                    Upn = (metadataDictionary?.ContainsKey(TokenResponseClaim.Upn) ?? false) ? metadataDictionary[TokenResponseClaim.Upn] : null,
                });
            }

            var response = new MsalTokenResponse
            {
                AccessToken  = responseDictionary[BrokerResponseConst.AccessToken],
                RefreshToken = responseDictionary.ContainsKey(BrokerResponseConst.RefreshToken)
                    ? responseDictionary[BrokerResponseConst.RefreshToken]
                    : null,
                IdToken       = responseDictionary[BrokerResponseConst.IdToken],
                TokenType     = BrokerResponseConst.Bearer,
                CorrelationId = responseDictionary[BrokerResponseConst.CorrelationId],
                Scope         = responseDictionary[BrokerResponseConst.Scope],
                ExpiresIn     = responseDictionary.TryGetValue(BrokerResponseConst.ExpiresOn, out string expiresOn) ?
                                DateTimeHelpers.GetDurationFromNowInSeconds(expiresOn) :
                                0,
                ClientInfo = responseDictionary.ContainsKey(BrokerResponseConst.ClientInfo)
                                ? responseDictionary[BrokerResponseConst.ClientInfo]
                                : null,
                TokenSource = TokenSource.Broker
            };

            if (responseDictionary.ContainsKey(TokenResponseClaim.RefreshIn))
            {
                response.RefreshIn = long.Parse(
                    responseDictionary[TokenResponseClaim.RefreshIn],
                    CultureInfo.InvariantCulture);
            }

            return(response);
        }