Beispiel #1
0
        /// <summary>
        /// Loads the API client details for the supplied token from the Admin database.
        /// </summary>
        /// <param name="token">The OAuth security token for which API client details should be retrieved.</param>
        /// <returns>A populated <see cref="ApiClientDetails"/> instance.</returns>
        public async Task <ApiClientDetails> GetClientDetailsForTokenAsync(string token)
        {
            if (!Guid.TryParse(token, out Guid tokenAsGuid))
            {
                return(new ApiClientDetails());
            }

            // TODO SF ODS-3459: this ought to be running as an independent task, scheduled multiple times a day.
            await _accessTokenClientRepo.DeleteExpiredTokensAsync();

            var clientForToken = await _accessTokenClientRepo.GetClientForTokenAsync(tokenAsGuid);

            return(ApiClientDetails.Create(clientForToken));
        }
        /// <summary>
        /// Factory method to create a <see cref="ApiClientDetails"/> from a collection of <see cref="OAuthTokenClient"/>.
        /// </summary>
        /// <param name="tokenClientRecords"><see cref="OAuthTokenClient"/> records from the data layer</param>
        /// <returns>Single <see cref="ApiClientDetails"/> object with information about a client for a given token</returns>
        public static ApiClientDetails Create(IReadOnlyList <OAuthTokenClient> tokenClientRecords)
        {
            Preconditions.ThrowIfNull(tokenClientRecords, nameof(tokenClientRecords));

            var tokenClient = tokenClientRecords.FirstOrDefault();

            if (tokenClient == null)
            {
                return(new ApiClientDetails());
            }

            var response = CreateInitialObjectFromFirstRecord(tokenClient);

            response = LoadAllEducationOrganizationIds(response);
            response = LoadAllVendorNamespacePrefixes(response);
            response = LoadAllProfileNames(response);
            response = LoadAllOwnershipTokenIds(response);

            return(response);

            ApiClientDetails CreateInitialObjectFromFirstRecord(OAuthTokenClient input)
            {
                var dto = new ApiClientDetails
                {
                    ApiKey          = input.Key,
                    ClaimSetName    = input.ClaimSetName,
                    IsSandboxClient = input.UseSandbox,
                    StudentIdentificationSystemDescriptor = input.StudentIdentificationSystemDescriptor,
                    CreatorOwnershipTokenId = input.CreatorOwnershipTokenId
                };

                return(dto);
            }

            ApiClientDetails LoadAllEducationOrganizationIds(ApiClientDetails dto)
            {
                tokenClientRecords
                .Where(x => x.EducationOrganizationId.HasValue)
                .Select(x => x.EducationOrganizationId.Value)
                .Distinct()
                .ForEach(x => dto.EducationOrganizationIds.Add(x));

                return(dto);
            }

            ApiClientDetails LoadAllVendorNamespacePrefixes(ApiClientDetails dto)
            {
                tokenClientRecords
                .Where(x => !string.IsNullOrWhiteSpace(x.NamespacePrefix))
                .Select(x => x.NamespacePrefix)
                .Distinct()
                .ForEach(x => dto.NamespacePrefixes.Add(x));

                return(dto);
            }

            ApiClientDetails LoadAllProfileNames(ApiClientDetails dto)
            {
                tokenClientRecords
                .Where(x => !string.IsNullOrWhiteSpace(x.ProfileName))
                .Select(x => x.ProfileName)
                .Distinct()
                .ForEach(x => dto.Profiles.Add(x));

                return(dto);
            }

            ApiClientDetails LoadAllOwnershipTokenIds(ApiClientDetails dto)
            {
                tokenClientRecords
                .Where(x => x.OwnershipTokenId.HasValue)
                .Select(x => x.OwnershipTokenId.Value)
                .Distinct()
                .ForEach(x => dto.OwnershipTokenIds.Add(x));

                return(dto);
            }
        }