Exemplo n.º 1
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IUrlRepository urlRepository)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseRouting();

            app.UseEndpoints(endpoints => {
                endpoints.MapHealthChecks("/hc", new HealthCheckOptions {
                    Predicate      = check => !check.Tags.Contains("ready"),
                    ResponseWriter = this.ResponseWriter
                });
                endpoints.MapHealthChecks("/rd", new HealthCheckOptions {
                    Predicate      = check => check.Tags.Contains("ready"),
                    ResponseWriter = this.ResponseWriter
                });
                endpoints.MapGet("/{token}", async context => {
                    var token = context.Request.RouteValues["token"];
                    var Id    = Base62Convertor.Decode(token.ToString());
                    var url   = await urlRepository.GetUrl(Id);
                    context.Response.Redirect(new Uri(url).AbsoluteUri, true);
                    return;
                });
                endpoints.MapDefaultControllerRoute();
            });
        }
        public void Base62_Guid_Base62()
        {
            var base62 = "00uawhkxQbMHrZaTl5d5";

            var guid      = Base62Convertor.ConvertFrom(base62);
            var converted = Base62Convertor.ConvertTo(guid);

            Assert.Equal(base62, converted);
        }
        public void Guid_Base62_Guid()
        {
            var guid = Guid.NewGuid();

            var base62    = Base62Convertor.ConvertTo(guid);
            var converted = Base62Convertor.ConvertFrom(base62);

            Assert.Equal(guid, converted);
        }
Exemplo n.º 4
0
        public static IServiceCollection AddOpenIdConnectionAuthentication(this IServiceCollection services, IConfiguration configuration)
        {
            var openIdOptions = configuration.GetSection(OpenIdConnectOptions.SectionName).Get <OpenIdConnectOptions>();

            JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
            JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Add("scp", "scope");
            JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Add(ClaimTypes.Name, "name");

            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options =>
            {
                options.Authority                 = openIdOptions.Authority;
                options.Audience                  = openIdOptions.Audience;
                options.RequireHttpsMetadata      = true;
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuer           = true,
                    ValidateAudience         = true,
                    ValidateIssuerSigningKey = true,
                };
                options.SaveToken = true;

                options.Events = new JwtBearerEvents
                {
                    OnMessageReceived = (context) => Task.CompletedTask,
                    OnTokenValidated  = (context) =>
                    {
                        if (!(context.Principal is { Identity: ClaimsIdentity identity }))
                        {
                            return(Task.CompletedTask);
                        }

                        // Okta base62 to Guid conversion
                        var uid = identity.Claims.First(c => c.Type == "uid").Value;

                        var uuid = Base62Convertor.ConvertFrom(uid);
                        identity.AddClaim(new Claim(identity.NameClaimType, uuid.ToString()));

                        // Map sub claim to email
                        var email = identity.Claims.First(c => c.Type == "sub").Value;
                        identity.AddClaim(new Claim(ClaimTypes.Email, email));

                        return(Task.CompletedTask);
                    }
Exemplo n.º 5
0
        public async Task <IActionResult> CreateShortUrl([FromBody] string url)
        {
            bool isUri = Uri.IsWellFormedUriString(url, UriKind.Absolute);

            if (!isUri)
            {
                return(BadRequest("Not a valid URL!!! Come on you can do better"));
            }
            var id = await this.uniqueIdGenerator.GetNext();

            var shorturl          = $"{this.Request.Scheme}://{this.Request.Host}{this.Request.PathBase}/{Base62Convertor.Convert(id)}";
            var persistanceStatus = await this.urlRepository.SaveUrl(id, url);

            if (persistanceStatus == false)
            {
                throw new Exception("Sorry!! We have some temporary down time. We request to retry after sometime");
            }
            return(Created(new Uri(shorturl), shorturl));
        }
Exemplo n.º 6
0
        public void GenerateUniqueKeyTests()
        {
            var token = Base62Convertor.Convert(1000000);

            Assert.Equal(1000000, Base62Convertor.Decode(token));
        }