Ejemplo n.º 1
0
        /// <summary>
        /// Return the deploy Token generated to let azure devops pipeline clone private GITEA repos on behalf of app developer
        /// </summary>
        /// <returns>The deploy app token</returns>
        public async Task <string> GetDeployToken()
        {
            string deployToken = string.Empty;

            KeyValuePair <string, string> deployKeyValuePair = await _gitea.GetSessionAppKey("AltinnDeployToken") ?? default(KeyValuePair <string, string>);

            if (!deployKeyValuePair.Equals(default(KeyValuePair <string, string>)))
            {
                deployToken = deployKeyValuePair.Value;
            }

            return(deployToken);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Return the deploy Token generated to let azure devops pipeline clone private GITEA repos on behalf of service developer
        /// </summary>
        /// <returns>The deploy app token</returns>
        public string GetDeployToken()
        {
            string deployToken = _httpContextAccessor.HttpContext.Request.Cookies[_settings.DeployCookieName];

            if (deployToken == null)
            {
                KeyValuePair <string, string> deployKeyValuePair = _gitea.GetSessionAppKey("AltinnDeployToken").Result ?? default(KeyValuePair <string, string>);
                if (!deployKeyValuePair.Equals(default(KeyValuePair <string, string>)))
                {
                    deployToken = deployKeyValuePair.Value;
                }

                _httpContextAccessor.HttpContext.Response.Cookies.Append(_settings.DeployCookieName, deployToken);
            }

            return(deployToken);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Login
        /// </summary>
        /// <returns>The login page</returns>
        public async Task <IActionResult> Login()
        {
            string userName = string.Empty;
            string goToUrl  = "/";

            // Verify that user is not logged in already.
            if (!string.IsNullOrEmpty(AuthenticationHelper.GetDeveloperUserName(HttpContext)))
            {
                return(LocalRedirect(goToUrl));
            }

            // Temporary catch errors until we figure out how to force this.
            try
            {
                userName = _giteaApi.GetUserNameFromUI().Result;
                if (string.IsNullOrEmpty(userName))
                {
                    if (Environment.GetEnvironmentVariable("GiteaLoginEndpoint") != null)
                    {
                        return(Redirect(Environment.GetEnvironmentVariable("GiteaLoginEndpoint")));
                    }

                    return(Redirect(_settings.GiteaLoginUrl));
                }
            }
            catch (Exception ex)
            {
                return(Content(ex.ToString()));
            }

            _logger.LogInformation("Updating app key for " + userName);
            KeyValuePair <string, string> accessKeyValuePair = await _giteaApi.GetSessionAppKey() ?? default(KeyValuePair <string, string>);

            List <Claim> claims = new List <Claim>();
            const string Issuer = "https://altinn.no";

            if (!accessKeyValuePair.Equals(default(KeyValuePair <string, string>)))
            {
                string accessToken = accessKeyValuePair.Value;
                string accessId    = accessKeyValuePair.Key;
                _logger.LogInformation("Adding key to claims: " + accessId);
                claims.Add(new Claim(AltinnCoreClaimTypes.DeveloperToken, accessToken, ClaimValueTypes.String, Issuer));
                claims.Add(new Claim(AltinnCoreClaimTypes.DeveloperTokenId, accessId, ClaimValueTypes.String, Issuer));
            }

            claims.Add(new Claim(AltinnCoreClaimTypes.Developer, userName, ClaimValueTypes.String, Issuer));
            ClaimsIdentity identity = new ClaimsIdentity("TestUserLogin");

            identity.AddClaims(claims);

            ClaimsPrincipal principal = new ClaimsPrincipal(identity);

            await HttpContext.SignInAsync(
                CookieAuthenticationDefaults.AuthenticationScheme,
                principal,
                new AuthenticationProperties
            {
                ExpiresUtc   = DateTime.UtcNow.AddMinutes(200),
                IsPersistent = false,
                AllowRefresh = false,
            });

            return(LocalRedirect(goToUrl));
        }