Beispiel #1
0
 /// <inheritdoc />
 public Auth0ValueProvider(
     HttpRequest request,
     Auth0Options options,
     FunctionTokenAttribute attribute)
     : base(request, options.SigningOptions, attribute)
 {
 }
        public Auth0UserService([NotNull] IAuth0ClientFactory factory, [NotNull] IOptions <Auth0Options> options)
        {
            Guard.NotNull(factory, nameof(factory));
            Guard.NotNull(options, nameof(options));

            _factory = factory;
            _options = options.Value;
        }
Beispiel #3
0
 public Auth0ValueProvider(
     HttpRequest request,
     Auth0Options options,
     FunctionTokenAttribute attribute,
     ISecurityTokenValidator securityHandler)
     : base(request, options.SigningOptions, attribute, securityHandler)
 {
 }
        public static IServiceCollection AddAuth0(
            this IServiceCollection services,
            Auth0Options auth0Options)
        {
            if (string.IsNullOrWhiteSpace(auth0Options.Audience))
            {
                throw new ArgumentException("Audience is null or empty");
            }

            if (string.IsNullOrWhiteSpace(auth0Options.Authority))
            {
                throw new ArgumentException("Authority is null or empty");
            }

            if (auth0Options.Permissions == null || auth0Options.Permissions.Any(string.IsNullOrWhiteSpace))
            {
                throw new ArgumentException("Permissions is null or empty or contains an empty element");
            }


            services.AddSingleton <IAuthorizationHandler, HasScopeHandler>();

            services
            .AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(options =>
            {
                options.Authority = auth0Options.Authority;
                options.Audience  = auth0Options.Audience;
            });

            services
            .AddAuthorization(options =>
            {
                foreach (var permission in auth0Options.Permissions)
                {
                    options
                    .AddPolicy(
                        permission,
                        policy =>
                        policy.Requirements.Add(new HasScopeRequirement(permission, auth0Options.Authority)));
                }
            });

            return(services);
        }
 public UserController(NetatmoDbContext db, HttpClient httpClient, IOptionsMonitor <Auth0Options> options)
 {
     this.db         = db ?? throw new ArgumentNullException(nameof(db));
     this.httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
     this.options    = (options ?? throw new ArgumentNullException(nameof(options))).CurrentValue;
 }
 public static IServiceCollection AddCustomAuthentication(this IServiceCollection services, Auth0Options auth0Options) =>
 services
 .AddAuthentication(options =>
 {
     options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
     options.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
 })
 .AddJwtBearer(options =>
 {
     options.Authority = $"https://{auth0Options.Domain}/";
     options.Audience  = auth0Options.ApiIdentifier;
 })
 .Services;
        public void GetValueAsyncWorksForScope(string[] requiredScopes, string[] authorizedRoles, List <Claim> claims, TokenStatus tokenStatus)
        {
            Mock <HttpRequest> request = new Mock <HttpRequest>();

            request
            .SetupGet(r => r.Headers)
            .Returns(new HeaderDictionary {
                { "Authorization", "Bearer abc123" }
            });
            request
            .SetupGet(r => r.HttpContext)
            .Returns(Mock.Of <HttpContext>());
            Mock <IConfigurationManager <OpenIdConnectConfiguration> > mockConfigurationManager = new Mock <IConfigurationManager <OpenIdConnectConfiguration> >();

            mockConfigurationManager
            .Setup(c => c.GetConfigurationAsync(It.IsAny <CancellationToken>()))
            .Returns(
                Task.FromResult(
                    new OpenIdConnectConfiguration
            {
                JsonWebKeySet = new JsonWebKeySet(
                    @"{
    ""keys"": [
        {
            ""alg"": ""RS256"",
            ""kty"": ""RSA"",
            ""use"": ""sig"",
            ""n"": ""big string1"",
            ""e"": ""AQAB"",
            ""kid"": ""big string 2"",
            ""x5t"": ""big string 2"",
            ""x5c"": [
                ""big string 3""
            ]
        }
    ]
}"
                    )
            }
                    )
                );
            Auth0Options options = new Auth0Options(mockConfigurationManager.Object, "someaudience");

            FunctionTokenAttribute attribute = new FunctionTokenAttribute(
                AuthLevel.Authorized,
                requiredScopes,
                authorizedRoles
                );
            SecurityToken mockSecurityToken = Mock.Of <SecurityToken>();
            Mock <ISecurityTokenValidator> mockSecurityTokenValidator = new Mock <ISecurityTokenValidator>();

            mockSecurityTokenValidator
            .Setup(v => v.ValidateToken(
                       It.IsAny <string>(),
                       It.IsAny <TokenValidationParameters>(),
                       out mockSecurityToken
                       )
                   )
            .Returns(
                new ClaimsPrincipal(
                    new List <ClaimsIdentity> {
                new ClaimsIdentity(
                    claims,
                    "Bearer"
                    )
            })
                );

            Auth0ValueProvider provider = new Auth0ValueProvider(
                request.Object,
                options,
                attribute,
                mockSecurityTokenValidator.Object
                );

            ((FunctionTokenResult)(provider
                                   .GetValueAsync()
                                   .GetAwaiter()
                                   .GetResult()))
            .Status
            .Should()
            .Be(tokenStatus);
        }