Example #1
0
 public static Credential GetIdToken()
 {
     return(Credential.CreateIdToken(
                GetHomeAccountId(),
                Environment,
                Realm,
                ClientId,
                CachedAt,
                GetRawIdToken(),
                AdditionalFieldsJson));
 }
Example #2
0
        /// <inheritdoc />
        public IAccount CacheTokenResponse(MsalTokenResponse msalTokenResponse)
        {
            var tokenResponse = new TokenResponse(msalTokenResponse);

            string homeAccountId = GetHomeAccountId(tokenResponse);
            var    authority     = new Uri(_authParameters.Authority.CanonicalAuthority);
            string environment   = authority.GetEnvironment();
            string realm         = authority.GetRealm();
            string clientId      = _authParameters.ClientId;
            string target        = ScopeUtils.JoinScopes(tokenResponse.GrantedScopes);

            if (string.IsNullOrWhiteSpace(homeAccountId) || string.IsNullOrWhiteSpace(environment) ||
                string.IsNullOrWhiteSpace(realm) || string.IsNullOrWhiteSpace(clientId) || string.IsNullOrWhiteSpace(target))
            {
                // skipping writing to the cache, PK is empty
                return(null);
            }

            var  credentialsToWrite = new List <Credential>();
            long cachedAt           = DateTime.UtcNow.Ticks; // todo: this is probably wrong

            if (tokenResponse.HasRefreshToken)
            {
                credentialsToWrite.Add(
                    Credential.CreateRefreshToken(
                        homeAccountId,
                        environment,
                        clientId,
                        cachedAt,
                        tokenResponse.RefreshToken,
                        string.Empty));
            }

            if (tokenResponse.HasAccessToken)
            {
                long expiresOn         = tokenResponse.ExpiresOn.Ticks;         // todo: this is probably wrong
                long extendedExpiresOn = tokenResponse.ExtendedExpiresOn.Ticks; // todo: this is probably wrong

                var accessToken = Credential.CreateAccessToken(
                    homeAccountId,
                    environment,
                    realm,
                    clientId,
                    target,
                    cachedAt,
                    expiresOn,
                    extendedExpiresOn,
                    tokenResponse.AccessToken,
                    string.Empty);
                if (IsAccessTokenValid(accessToken))
                {
                    credentialsToWrite.Add(accessToken);
                }
            }

            var idTokenJwt = tokenResponse.IdToken;

            if (!idTokenJwt.IsEmpty)
            {
                credentialsToWrite.Add(
                    Credential.CreateIdToken(
                        homeAccountId,
                        environment,
                        realm,
                        clientId,
                        cachedAt,
                        idTokenJwt.Raw,
                        string.Empty));
            }

            var status = _storageManager.WriteCredentials(string.Empty, credentialsToWrite);

            if (status.StatusType != OperationStatusType.Success)
            {
                // warning error writing to cache
            }

            // if id token jwt is empty, return null

            string localAccountId = GetLocalAccountId(idTokenJwt);
            var    authorityType  = GetAuthorityType();

            var account = Microsoft.Identity.Client.CacheV2.Schema.Account.Create(
                homeAccountId,
                environment,
                realm,
                localAccountId,
                authorityType,
                idTokenJwt.PreferredUsername,
                idTokenJwt.GivenName,
                idTokenJwt.FamilyName,
                idTokenJwt.MiddleName,
                idTokenJwt.Name,
                idTokenJwt.AlternativeId,
                tokenResponse.RawClientInfo,
                string.Empty);

            status = _storageManager.WriteAccount(string.Empty, account);

            if (status.StatusType != OperationStatusType.Success)
            {
                // warning error writing account to cache
            }

            return(account);
        }