public async Task GetTenants(IOwinContext context)
        {
            if (!_registrationOwnership.IsAuthenticated)
            {
                await ServiceHelpers.WriteErrorResponse(context, "user does not have access to the service", HttpStatusCode.Forbidden);
                return;
            }

            IEnumerable<string> tenants = await _registrationOwnership.GetTenants();
            await ServiceHelpers.WriteResponse(context, new JArray(tenants.ToArray()), HttpStatusCode.OK);
        }
        async Task ProcessRequest(IOwinContext context, JObject obj)
        {
            string ns      = obj["namespace"].ToString();
            string id      = obj["id"].ToString();
            string version = obj["version"].ToString();

            JObject content;

            if (await _registrationOwnership.HasRegistration(ns, id))
            {
                if (await _registrationOwnership.HasOwner(ns, id))
                {
                    if (await _registrationOwnership.HasVersion(ns, id, version))
                    {
                        content = new JObject
                        {
                            { "status", false },
                            { "message", string.Format("The package version {0}/{1}/{2} already exists", ns, id, version) }
                        };
                    }
                    else
                    {
                        content = new JObject
                        {
                            { "status", true },
                            { "message", string.Format("The package identification {0}/{1}/{2} is available and access is permitted", ns, id, version) }
                        };
                    }
                }
                else
                {
                    content = new JObject
                    {
                        { "status", false },
                        { "message", string.Format("The current user is not an owner of the package registration {0}/{1}", ns, id) }
                    };
                }
            }
            else
            {
                content = new JObject
                {
                    { "status", true },
                    { "message", string.Format("The package identification {0}/{1}/{2} is available and access is permitted", ns, id, version) }
                };
            }

            await ServiceHelpers.WriteResponse(context, content, HttpStatusCode.OK);
        }
Example #3
0
        public static async Task Upload(IOwinContext context)
        {
            string name = await ValidateRequest(context);

            if (name != null)
            {
                Stream nupkgStream = context.Request.Body;

                Uri nupkgAddress = await SaveNupkg(nupkgStream, name);

                Uri catalogAddress = await AddToCatalog(nupkgStream);

                JToken response = new JObject
                {
                    { "download", nupkgAddress.ToString() },
                    { "catalog", catalogAddress.ToString() }
                };

                await ServiceHelpers.WriteResponse(context, response, HttpStatusCode.OK);
            }
        }
        public static async Task ListPackageRegistrations(IOwinContext context)
        {
            //
            // The Scope claim tells you what permissions the client application has in the service.
            // In this case we look for a scope value of user_impersonation, or full access to the service as the user.
            //
            if (ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/scope").Value != "user_impersonation")
            {
                await context.Response.WriteAsync("The Scope claim does not contain 'user_impersonation' or scope claim not found");

                context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
            }

            ActiveDirectoryClient activeDirectoryClient = await ServiceHelpers.GetActiveDirectoryClient();

            string userObjectID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;

            IUser user = await activeDirectoryClient.Users.GetByObjectId(userObjectID).ExecuteAsync();

            IPagedCollection <IDirectoryObject> groups = await((IUserFetcher)user).MemberOf.ExecuteAsync();

            JArray array = new JArray();

            while (true)
            {
                foreach (IDirectoryObject group in groups.CurrentPage)
                {
                    array.Add(((Group)group).DisplayName);
                }

                if (!groups.MorePagesAvailable)
                {
                    break;
                }

                groups = await groups.GetNextPageAsync();
            }

            await ServiceHelpers.WriteResponse(context, array, HttpStatusCode.OK);
        }
        public async Task Upload(IOwinContext context)
        {
            Trace.TraceInformation("PublishImpl.Upload");

            if (!_registrationOwnership.IsAuthenticated)
            {
                await ServiceHelpers.WriteErrorResponse(context, "user does not have access to the service", HttpStatusCode.Forbidden);
                return;
            }

            if (!await _registrationOwnership.HasTenantEnabled())
            {
                await ServiceHelpers.WriteErrorResponse(context, "package publication has not been enabled in this tenant", HttpStatusCode.Forbidden);
                return;
            }

            PublicationVisibility publicationVisibility;
            if (!PublicationVisibility.TryCreate(context, out publicationVisibility))
            {
                await ServiceHelpers.WriteErrorResponse(context, "specify either organization OR subscription NOT BOTH", HttpStatusCode.BadRequest);
                return;
            }

            //  no-commit mode - used for just running the validation

            bool isCommit = GetIsCommit(context);

            Stream packageStream = context.Request.Body;
            
            //  validation

            ValidationResult validationResult = await Validate(packageStream);

            if (validationResult.HasErrors)
            {
                await ServiceHelpers.WriteErrorResponse(context, validationResult.Errors, HttpStatusCode.BadRequest);
                return;
            }
            
            //  registration authorization

            IList<string> authorizationErrors = await OwnershipHelpers.CheckRegistrationAuthorization(_registrationOwnership, validationResult.PackageIdentity);

            if (authorizationErrors.Count > 0)
            {
                await ServiceHelpers.WriteErrorResponse(context, authorizationErrors, HttpStatusCode.Forbidden);
                return;
            }

            //  listed

            bool isListed = true;
            string unlist = context.Request.Query["unlist"];
            if (unlist != null)
            {
                isListed = !unlist.Equals(Boolean.TrueString, StringComparison.InvariantCultureIgnoreCase);
            }

            Trace.TraceInformation("UPLOAD Processing package {0}/{1}/{2} isListed: {3} isCommit: {4}", validationResult.PackageIdentity.Namespace, validationResult.PackageIdentity.Id, validationResult.PackageIdentity.Version, isListed, isCommit);

            //  process the package

            IDictionary<string, JObject> metadata = new Dictionary<string, JObject>();

            //  (1) save all the artifacts

            if (isCommit)
            {
                await Artifacts.Save(metadata, packageStream, Configuration.StoragePrimary, Configuration.StorageContainerArtifacts);

                Trace.TraceInformation("Save");
            }

            InferArtifactTypes(metadata);

            //  (2) promote the relevant peices of metadata so they later can appear on the catalog page 

            await ExtractMetadata(metadata, packageStream);

            Trace.TraceInformation("ExtractMetadata");

            //  (3) gather all the publication details

            PublicationDetails publicationDetails = await OwnershipHelpers.CreatePublicationDetails(_registrationOwnership, publicationVisibility);

            Trace.TraceInformation("CreatePublicationDetails");

            //  (4) add the new item to the catalog

            Uri catalogAddress = null;

            if (isCommit)
            {
                catalogAddress = await AddToCatalog(metadata["nuspec"], GetItemType(), publicationDetails, isListed);

                Trace.TraceInformation("AddToCatalog");
            }

            //  (5) update the registration ownership record

            if (isCommit)
            {
                await UpdateRegistrationOwnership(validationResult.PackageIdentity);

                Trace.TraceInformation("UpdateRegistrationOwnership");
            }

            //  (6) create response

            if (isCommit)
            {
                JToken response = new JObject
                { 
                    { "download", metadata["nuspec"]["packageContent"] },
                    { "catalog", catalogAddress.ToString() }
                };

                await ServiceHelpers.WriteResponse(context, response, HttpStatusCode.Created);
            }
            else
            {
                context.Response.StatusCode = (int)HttpStatusCode.OK;
            }
        }
        public async Task Edit(IOwinContext context)
        {
            Trace.TraceInformation("PublishImpl.Edit");

            if (!_registrationOwnership.IsAuthenticated)
            {
                await ServiceHelpers.WriteErrorResponse(context, "user does not have access to the service", HttpStatusCode.Forbidden);
                return;
            }

            if (!await _registrationOwnership.HasTenantEnabled())
            {
                await ServiceHelpers.WriteErrorResponse(context, "package publication has not been enabled in this tenant", HttpStatusCode.Forbidden);
                return;
            }

            PublicationVisibility publicationVisibility;
            if (!PublicationVisibility.TryCreate(context, out publicationVisibility))
            {
                await ServiceHelpers.WriteErrorResponse(context, "specify either organization OR subscription NOT BOTH", HttpStatusCode.BadRequest);
                return;
            }

            Stream metadataStream = context.Request.Body;

            //  validation

            EditValidationResult validationResult = await ValidateEdit(metadataStream);

            if (validationResult.HasErrors)
            {
                await ServiceHelpers.WriteErrorResponse(context, validationResult.Errors, HttpStatusCode.BadRequest);
                return;
            }

            //  registration authorization

            IList<string> authorizationErrors = await OwnershipHelpers.CheckRegistrationAuthorizationForEdit(_registrationOwnership, validationResult.PackageIdentity);

            if (authorizationErrors.Count > 0)
            {
                await ServiceHelpers.WriteErrorResponse(context, authorizationErrors, HttpStatusCode.Forbidden);
                return;
            }

            Trace.TraceInformation("EDIT Processing package {0}/{1}/{2} listed: {3}", validationResult.PackageIdentity.Namespace, validationResult.PackageIdentity.Id, validationResult.PackageIdentity.Version, validationResult.Listed);

            //  process the edit

            IDictionary<string, JObject> metadata = new Dictionary<string, JObject>();

            //  (1) generate any new or replacement artifacts based on the current catalogEntry and the editMetadata

            IDictionary<string, PackageArtifact> artifacts = await GenerateNewArtifactsFromEdit(metadata, validationResult.CatalogEntry, validationResult.EditMetadata, Configuration.StoragePrimary);

            Trace.TraceInformation("GenerateNewArtifactsFromEdit");
            
            //  (2) save the new package

            await Artifacts.Save(metadata, artifacts, Configuration.StoragePrimary, Configuration.StorageContainerArtifacts);

            InferArtifactTypes(metadata);

            Trace.TraceInformation("Save");

            //  (3) promote the relevant peices of metadata so they later can appear on the catalog page 

            await GenerateNuspec(metadata);

            Trace.TraceInformation("GenerateNuspec");

            //  (4) gather all the publication details

            PublicationDetails publicationDetails = await OwnershipHelpers.CreatePublicationDetails(_registrationOwnership, publicationVisibility);

            Trace.TraceInformation("CreatePublicationDetails");

            //  (5) add the new item to the catalog

            Uri catalogAddress = await AddToCatalog(metadata["nuspec"], GetItemType(), publicationDetails, validationResult.Listed);

            Trace.TraceInformation("AddToCatalog");

            //  (6) update the registration ownership record

            await UpdateRegistrationOwnership(validationResult.PackageIdentity);

            Trace.TraceInformation("UpdateRegistrationOwnership");

            //  (7) create response

            JToken response = new JObject
            { 
                { "download", metadata["nuspec"]["packageContent"] },
                { "catalog", catalogAddress.ToString() }
            };

            await ServiceHelpers.WriteResponse(context, response, HttpStatusCode.OK);
        }
Example #7
0
        public async Task Delete(IOwinContext context)
        {
            Trace.TraceInformation("DeleteImpl.Upload");

            if (!_registrationOwnership.IsAuthenticated)
            {
                await ServiceHelpers.WriteErrorResponse(context, "user does not have access to the service", HttpStatusCode.Forbidden);

                return;
            }

            if (!await _registrationOwnership.HasTenantEnabled())
            {
                await ServiceHelpers.WriteErrorResponse(context, "package publication has not been enabled in this tenant", HttpStatusCode.Forbidden);

                return;
            }

            PublicationVisibility publicationVisibility;

            if (!PublicationVisibility.TryCreate(context, out publicationVisibility))
            {
                await ServiceHelpers.WriteErrorResponse(context, "specify either organization OR subscription NOT BOTH", HttpStatusCode.BadRequest);

                return;
            }

            Stream packageStream = context.Request.Body;

            //  validation

            ValidationResult validationResult = await Validate(packageStream);

            if (validationResult.HasErrors)
            {
                await ServiceHelpers.WriteErrorResponse(context, validationResult.Errors, HttpStatusCode.BadRequest);

                return;
            }

            //  registration authorization

            IList <string> authorizationErrors = await OwnershipHelpers.CheckRegistrationAuthorizationForEdit(_registrationOwnership, validationResult.PackageIdentity);

            if (authorizationErrors.Count > 0)
            {
                await ServiceHelpers.WriteErrorResponse(context, authorizationErrors, HttpStatusCode.Forbidden);

                return;
            }

            Trace.TraceInformation("DELETE Processing package {0}/{1}/{2}", validationResult.PackageIdentity.Namespace, validationResult.PackageIdentity.Id, validationResult.PackageIdentity.Version);

            //  process delete

            //  (1) gather all the publication details

            PublicationDetails publicationDetails = await OwnershipHelpers.CreatePublicationDetails(_registrationOwnership, publicationVisibility);

            Trace.TraceInformation("CreatePublicationDetails");

            //  (2) add the new item to the catalog

            Uri catalogAddress = await AddToCatalog(validationResult.PackageIdentity, publicationDetails);

            Trace.TraceInformation("AddToCatalog");

            //  (3) update the registration ownership record

            await UpdateRegistrationOwnership(validationResult.PackageIdentity);

            Trace.TraceInformation("UpdateRegistrationOwnership");

            //  (4) create response

            JToken response = new JObject
            {
                { "catalog", catalogAddress.ToString() }
            };

            await ServiceHelpers.WriteResponse(context, response, HttpStatusCode.OK);
        }