[InlineData(new[] { "something something %%unreplacedTOKEN%% something else" }, new[] { "%%unreplacedTOKEN%%" })] //one unreplaced token with other ok content
        public async Task Test(IEnumerable <string> jtokenValues, IEnumerable <string> requiredTokens)
        {
            var messageCollector = new TestReporter();
            var stage            = new UnresolvedTokenVerifierStage(messageCollector);
            var json             = new JObject();

            foreach (var value in jtokenValues)
            {
                json.Add(Guid.NewGuid().ToString(), new JValue(value));
            }
            var t = new Auth0ResourceTemplate
            {
                Type     = ResourceType.Clients,
                Template = json
            };
            await stage.Process(t);

            var requiredTokenList = requiredTokens.ToList();

            if (!requiredTokenList.Any())
            {
                messageCollector.Messages.ShouldBeEmpty();
            }
            else
            {
                foreach (var token in requiredTokenList)
                {
                    messageCollector.ErrorMessages.ShouldHaveMessageThatContains(token);
                }
            }
        }
Exemplo n.º 2
0
        public override async Task ProcessAsync(Auth0ResourceTemplate template)
        {
            var page = _converter.Convert(template);

            if (!template.Preprocessed)
            {
                throw new Auth0InvalidTemplateException(
                          $"{template.Type.Name} template {template.Filename} has not been preprocessed prior to being executed.");
            }

            switch (page.PageType)
            {
            case PageType.Login:
                await SetLoginPage(page);

                break;

            case PageType.PasswordReset:
                await SetPasswordResetPage(page);

                break;

            case PageType.GuardianMultifactor:
                await SetGuardianMfaPage(page);

                break;

            default:
                throw new Auth0InvalidTemplateException("Page name must be one of [login, reset_password]");
            }
        }
Exemplo n.º 3
0
        public override async Task ProcessAsync(Auth0ResourceTemplate template)
        {
            using var managementClient = await _managementApiClientFactory.CreateAsync();

            var rule = _converter.Convert(template);

            //todo support proper pagination - how to do this where every api call is different?!
            var getRulesRequest = new GetRulesRequest
            {
                IncludeFields = true, Fields = "name,id"
            };
            var results = managementClient.Rules.GetAllAsync(getRulesRequest, Reporter);

            var matchingRule = await results.FirstOrDefaultAsync(x => string.Equals(x.Name, rule.Name));

            if (matchingRule == null)
            {
                var createRequest = Reflectorisor.CopyMembers <Rule, RuleCreateRequest>(rule);
                await Create(
                    async() => await managementClient.Rules.CreateAsync(createRequest),
                    request => request.Id,
                    rule.Name);
            }
            else
            {
                var updateRequest = Reflectorisor.CopyMembers <Rule, RuleUpdateRequest>(rule);
                await Update(
                    async() => await managementClient.Rules.UpdateAsync(matchingRule.Id, updateRequest),
                    matchingRule.Id,
                    rule.Name
                    );
            }
        }
Exemplo n.º 4
0
        public override async Task ProcessAsync(Auth0ResourceTemplate template)
        {
            using var managementClient = await _managementApiClientFactory.CreateAsync();

            var templatedResourceServer = _converter.Convert(template);

            var results = managementClient.ResourceServers.GetAllAsync(_reporter);

            FixIllegalOptions(templatedResourceServer);

            var matchingResourceServer = await results.FirstOrDefaultAsync(x => string.Equals(x.Name, templatedResourceServer.Name));

            if (matchingResourceServer == null)
            {
                var createRequest = Reflectorisor.CopyMembers <ResourceServer, ResourceServerCreateRequest>(templatedResourceServer);
                await Create(
                    async() => await managementClient.ResourceServers.CreateAsync(createRequest),
                    request => request.Id,
                    templatedResourceServer.Name
                    );
            }
            else
            {
                var updateRequest = Reflectorisor.CopyMembers <ResourceServer, ResourceServerUpdateRequest>(templatedResourceServer);
                await Update(
                    async() => await managementClient.ResourceServers.UpdateAsync(matchingResourceServer.Id, updateRequest),
                    matchingResourceServer.Id,
                    templatedResourceServer.Name
                    );
            }
        }
Exemplo n.º 5
0
        public override async Task Preprocess(Auth0ResourceTemplate template)
        {
            var scriptToken = template.Template.SelectTokens("script").OfType <JValue>().FirstOrDefault(x => x.Type == JTokenType.String);

            if (scriptToken == null)
            {
                throw new Auth0InvalidTemplateException(
                          $"{Type.Name} template {template.Filename} does not contain a required property \'script\' " +
                          $"of type String.");
            }

            var scriptFilename = scriptToken.Value as string;

            if (string.IsNullOrWhiteSpace(scriptFilename))
            {
                throw new Auth0InvalidTemplateException(
                          $"{Type.Name} template {template.Filename} does not contain a valid value for property " +
                          $"\'script\'. Must not be null or empty.");
            }

            var scriptPath = Path.Combine(Directory.GetParent(template.Location.FullName).FullName, scriptFilename);

            if (!File.Exists(scriptPath))
            {
                throw new Auth0InvalidTemplateException(
                          $"{Type.Name} template {template.Filename} does not contain a valid value for property " +
                          $"\'script\'. The file must exist but does not: {scriptPath}");
            }

            scriptToken.Replace(await File.ReadAllTextAsync(scriptPath));
        }
Exemplo n.º 6
0
        public override async Task ProcessAsync(Auth0ResourceTemplate template)
        {
            using var managementClient = await _managementApiClientFactory.CreateAsync();

            var role = _converter.Convert(template);

            var results = managementClient.Roles.GetAllAsync(new GetRolesRequest(), Reporter);

            var matchingRole = await results.FirstOrDefaultAsync(x => string.Equals(x.Name, role.Name));

            if (matchingRole == null)
            {
                var createRequest = Reflectorisor.CopyMembers <Role, RoleCreateRequest>(role);
                await Create(
                    async() => await managementClient.Roles.CreateAsync(createRequest),
                    request => request.Id,
                    role.Name);
            }
            else
            {
                var updateRequest = Reflectorisor.CopyMembers <Role, RoleUpdateRequest>(role);
                await Update(
                    async() => await managementClient.Roles.UpdateAsync(matchingRole.Id, updateRequest),
                    matchingRole.Id,
                    role.Name
                    );
            }
        }
Exemplo n.º 7
0
        public override async Task ProcessAsync(Auth0ResourceTemplate template)
        {
            using var managementClient = await _managementApiClientFactory.CreateAsync();

            var client = _converter.Convert(template);

            var getClientsRequest = new GetClientsRequest
            {
                IsGlobal = false, IncludeFields = true, Fields = "name,client_id"
            };
            var results = managementClient.Clients.GetAllAsync(getClientsRequest, Reporter);

            FixIllegalOptions(client);

            var matchingClient = await results.FirstOrDefaultAsync(x => string.Equals(x.Name, client.Name));

            if (matchingClient == null)
            {
                var createRequest = Reflectorisor.CopyMembers <Client, ClientCreateRequest>(client);
                await Create(
                    async() => await managementClient.Clients.CreateAsync(createRequest),
                    request => request.ClientId,
                    client.Name);
            }
            else
            {
                var updateRequest = Reflectorisor.CopyMembers <Client, ClientUpdateRequest>(client);
                await Update(
                    async() => await managementClient.Clients.UpdateAsync(matchingClient.ClientId, updateRequest),
                    matchingClient.ClientId,
                    client.Name
                    );
            }
        }
Exemplo n.º 8
0
        public override async Task ProcessAsync(Auth0ResourceTemplate template)
        {
            var settings = _converter.Convert(template);

            using var managementClient = await _managementApiClientFactory.CreateAsync();

            var updateRequest = Reflectorisor.CopyMembers <TenantSettings, TenantSettingsUpdateRequest>(settings);

            await Update(async() => await managementClient.TenantSettings.UpdateAsync(updateRequest),
                         template.Type.Name);
        }
Exemplo n.º 9
0
        public override async Task Preprocess(Auth0ResourceTemplate template)
        {
            //Connection templates have a property enabled_clients_match_conditions that contains regexes or string
            //literals of client names that the connection should be associated with.
            //This preprocessing step looks up all the deployed clients and

            var matchConditions = new JsonSerializer().Deserialize <ConnectionClientMatchConditions>(
                new JTokenReader(template.Template))
                                  ?.EnabledClientsMatchConditions?.ToList() ?? new List <string>();

            if (matchConditions.Count == 0)
            {
                template.Preprocessed = true;
                return;
            }

            using var managementClient = await _managementApiClientFactory.CreateAsync();

            var getClientsRequest = new GetClientsRequest()
            {
                IsGlobal = false, IncludeFields = true, Fields = "name,client_id"
            };
            var clients = await managementClient.Clients.GetAllAsync(getClientsRequest, new PaginationInfo());

            var matchConditionsRegexes = matchConditions.Select(x => new Regex(x));
            var matchingClientIds      = clients.Where(x =>
                                                       //check for exact string match OR regex match
                                                       matchConditionsRegexes.Any(regex => string.Equals(regex.ToString(), x.Name) || regex.IsMatch(x.Name))
                                                       ).Select(x => (object)new JValue(x.ClientId)).ToList();

            if (!(template.Template is JObject t))
            {
                throw new InvalidOperationException(
                          $"{Type.Name} template {template.Filename} processed type is not of type JObject." +
                          $" Found {template.Template.GetType().Name}");
            }

            //add the enabled_clients
            t.Add("enabled_clients", new JArray(matchingClientIds.ToArray()));

            //remove the enabled_clients_match_conditions
            t.Remove("enabled_clients_match_conditions");

            if (_args.CurrentValue.DryRun)
            {
                Reporter.Warn(
                    "Dry-run flag is set. Any clients that do not exist but that will be created by " +
                    "these templates when run without the dry-run flag may not be found and included in this " +
                    "connections\' enabled_clients list. The complete list of matching clients will be found when " +
                    "run without the dry-run flag.");
            }

            template.Preprocessed = true;
        }
Exemplo n.º 10
0
        public override async Task ProcessAsync(Auth0ResourceTemplate template)
        {
            //get the client that matches the ClientGrant.ClientId so we get Client.Id

            using var managementClient = await _managementApiClientFactory.CreateAsync();

            var templatedGrant = _converter.Convert(template);

            var getClientsRequest = new GetClientsRequest
            {
                IsGlobal = false, IncludeFields = true, Fields = "name,client_id"
            };
            var allClients = await managementClient.Clients.GetAllAsync(getClientsRequest, new PaginationInfo());

            var matchingClient = allClients.FirstOrDefault(x => x.Name == templatedGrant.ClientId);

            if (matchingClient == null)
            {
                throw new Auth0ResourceNotFoundException($"No {ResourceType.Clients.Name} exists with name " +
                                                         $"{templatedGrant.ClientId}. Cannot create/update " +
                                                         $"{ResourceType.ClientGrants.Name} {template.Filename}");
            }

            templatedGrant.ClientId = matchingClient.ClientId;

            var allGrants     = managementClient.ClientGrants.GetAllAsync(new GetClientGrantsRequest(), Reporter);
            var existingGrant = await allGrants.FirstOrDefaultAsync(
                x => string.Equals(x.ClientId, templatedGrant.ClientId) &&
                string.Equals(x.Audience, templatedGrant.Audience));

            if (existingGrant == null)
            {
                var createRequest = Reflectorisor.CopyMembers <ClientGrant, ClientGrantCreateRequest>(templatedGrant);
                await Create(
                    async() => await managementClient.ClientGrants.CreateAsync(createRequest),
                    request => request.Id,
                    $"[{matchingClient.Name}|{createRequest.Audience}]");
            }
            else
            {
                var updateRequest = Reflectorisor.CopyMembers <ClientGrant, ClientGrantUpdateRequest>(templatedGrant);
                await Update(
                    async() => await managementClient.ClientGrants.UpdateAsync(existingGrant.Id, updateRequest),
                    existingGrant.Id,
                    $"[{matchingClient.Name}|{existingGrant.Audience}]");
            }
        }
Exemplo n.º 11
0
        public override async Task ProcessAsync(Auth0ResourceTemplate template)
        {
            using var managementClient = await _managementApiClientFactory.CreateAsync();

            var templatedConnection = _converter.Convert(template);

            var getConnectionsRequest = new GetConnectionsRequest
            {
                IncludeFields = true,
                Fields        = "name,id"
            };
            var allConnections = managementClient.Connections.GetAllAsync(getConnectionsRequest, Reporter);

            var matchingConnection = await allConnections.FirstOrDefaultAsync(x => string.Equals(x.Name, templatedConnection.Name));

            if (matchingConnection == null)
            {
                var createRequest = Reflectorisor.CopyMembers <Connection, ConnectionCreateRequest>(templatedConnection);
                await Create(
                    async() => await managementClient.Connections.CreateAsync(createRequest),
                    request => request.Id,
                    templatedConnection.Name
                    );
            }
            else
            {
                var updateRequest = Reflectorisor.CopyMembers <Connection, ConnectionUpdateRequest>(templatedConnection);
                //Remove the name property. It's not allowed on an update.
                updateRequest.Name = null;

                await Update(
                    async() => await managementClient.Connections.UpdateAsync(matchingConnection.Id, updateRequest),
                    matchingConnection.Id,
                    templatedConnection.Name
                    );
            }
        }
Exemplo n.º 12
0
 public override async Task <Auth0ResourceTemplate> Process(Auth0ResourceTemplate t)
 {
     _reporter.Output(JsonConvert.SerializeObject(t.Template, Formatting.Indented));
     return(t);
 }
 public abstract Task ProcessAsync(Auth0ResourceTemplate template);
 public virtual async Task Preprocess(Auth0ResourceTemplate template)
 {
     //no-op
 }
 public virtual async Task Validate(Auth0ResourceTemplate template)
 {
     _converter.Convert(template);
 }
Exemplo n.º 16
0
 public override async Task <Auth0ResourceTemplate> Process(Auth0ResourceTemplate t)
 {
     return(t);
 }
 public To Convert(Auth0ResourceTemplate @from)
 {
     return(new JsonSerializer().Deserialize <To>(new JTokenReader(@from.Template)));
 }