private ProjectIdOrPathRef(ProjectIdentity project)
        {
            if ((project == null))
            {
                throw new System.ArgumentNullException(nameof(project));
            }

            this._value = project.Id;
        }
        public static Meziantou.GitLab.ProjectIdOrPathRef FromProject(ProjectIdentity project)
        {
            if ((project == null))
            {
                throw new System.ArgumentNullException(nameof(project));
            }

            return(new Meziantou.GitLab.ProjectIdOrPathRef(project));
        }
Exemple #3
0
        public async Task <ProjectIdentity> RunActivity(
            [ActivityTrigger] IDurableActivityContext activityContext,
            ILogger log)
        {
            if (activityContext is null)
            {
                throw new ArgumentNullException(nameof(activityContext));
            }

            try
            {
                var project = activityContext.GetInput <ProjectDocument>();

                if (string.IsNullOrEmpty(project.Identity?.Id))
                {
                    var servicePrincipal = await azureDirectoryService
                                           .CreateServicePrincipalAsync(project.Id.ToString())
                                           .ConfigureAwait(false);

                    try
                    {
                        var projectIdentity = new ProjectIdentity
                        {
                            Id            = servicePrincipal.ObjectId.ToString(),
                            ApplicationId = servicePrincipal.ApplicationId,
                            TenantId      = servicePrincipal.TenantId,
                            Secret        = servicePrincipal.Password
                        };

                        return(projectIdentity);
                    }
                    catch
                    {
                        await azureDirectoryService
                        .DeleteServicePrincipalAsync(project.Id.ToString())
                        .ConfigureAwait(false);
                    }
                }

                return(project.Identity);
            }
            catch (Exception exc)
            {
                log.LogError(exc, $"{nameof(ProjectIdentityCreateActivity)} failed with error: {exc.Message}");

                throw exc.AsSerializable();
            }
        }
Exemple #4
0
        private void PostAuthenticateRequestHandler(object sender, EventArgs e)
        {
            HttpCookie authCookie = Context.Request.Cookies[FormsAuthentication.FormsCookieName];

            if (authCookie != null && !string.IsNullOrEmpty(authCookie.Value))
            {
                var formsAuthentication = ServiceLocator.Current.GetInstance <IFormsAuthentication>();

                var ticket          = formsAuthentication.Decrypt(authCookie.Value);
                var projectIdentity = new ProjectIdentity(ticket);
                Context.User = new GenericPrincipal(projectIdentity, null);

                // Reset cookie for a sliding expiration
                formsAuthentication.SetAuthCookie(Context, ticket);
            }
        }
        public async Task RunActivity(
            [ActivityTrigger] IDurableActivityContext functionContext)
        {
            if (functionContext is null)
            {
                throw new ArgumentNullException(nameof(functionContext));
            }

            var project = functionContext.GetInput <Project>();

            var keyVault = await azureResourceService
                           .GetResourceAsync <AzureKeyVaultResource>(project.KeyVault.VaultId, throwIfNotExists : true)
                           .ConfigureAwait(false);

            var projectIdentityJson = await keyVault
                                      .GetSecretAsync(nameof(ProjectIdentity))
                                      .ConfigureAwait(false);

            if (string.IsNullOrEmpty(projectIdentityJson))
            {
                var servicePrincipal = await azureDirectoryService
                                       .CreateServicePrincipalAsync(project.Id.ToString())
                                       .ConfigureAwait(false);

                try
                {
                    var projectIdentity = new ProjectIdentity()
                    {
                        Id            = servicePrincipal.ObjectId.ToString(),
                        ApplicationId = servicePrincipal.ApplicationId,
                        Secret        = servicePrincipal.Password
                    };

                    projectIdentityJson = JsonConvert.SerializeObject(projectIdentity);

                    await keyVault
                    .SetSecretAsync(nameof(ProjectIdentity), projectIdentityJson)
                    .ConfigureAwait(false);
                }
                catch
                {
                    await azureDirectoryService
                    .DeleteServicePrincipalAsync(project.Id.ToString())
                    .ConfigureAwait(false);
                }
            }
        }
Exemple #6
0
        public static User GetUserFromIdentity(this IUserRepository services, ProjectIdentity identity)
        {
            var user = services.GetUser(identity.UserId);

            return(user);
        }
Exemple #7
0
        protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            try
            {
                IEnumerable <string> apiTokenHeaderValues = null;
                Guid             accessTokenID            = Guid.Empty;
                DatabaseSettings databaseSettings         = new DatabaseSettings();

                if (request.Headers.TryGetValues("AccessTokenID", out apiTokenHeaderValues))
                {
                    Guid.TryParse(apiTokenHeaderValues.First(), out accessTokenID);
                }

                if (Guid.Empty.Equals(accessTokenID) == true)
                {
                    Debug.Print("No Access Token");
                }


                if (Guid.Empty.Equals(accessTokenID) == false)
                {
                    AccessToken accessToken;

                    accessToken = await AccessTokenLogic.GetAccessTokenAsync(databaseSettings, accessTokenID);

                    if (accessToken != null)
                    {
                        ProjectIdentity identity;

                        identity = new ProjectIdentity(accessToken, "AccessToken", databaseSettings);

                        if (identity != null)
                        {
                            var principal = new ClaimsPrincipal(identity);

                            //Thread.CurrentPrincipal = principal;
                            request.GetRequestContext().Principal = principal;

                            // Update the last access date and time in the background
                            ThreadPool.QueueUserWorkItem(o =>
                            {
                                try
                                {
                                    AccessTokenLogic.UpdateLastAccessDateTime(databaseSettings, accessTokenID);
                                }
                                catch (Exception ex)
                                {
                                    Debug.Print(ex.ToString());
                                }
                            });
                        }
                    }
                }
                return(await base.SendAsync(request, cancellationToken));
            }
            catch (Exception ex)
            {
                Debug.Print(ex.ToString());
                return(new HttpResponseMessage(System.Net.HttpStatusCode.InternalServerError));
            }
        }