public ClientRegistration Update(AtlModel.ClientRegistrationData data)
 {
     Key            = data.Key;
     ClientKey      = data.ClientKey;
     PublicKey      = data.PublicKey;
     SharedSecret   = data.SharedSecret;
     ServerVersion  = data.ServerVersion;
     PluginsVersion = data.PluginsVersion;
     BaseUrl        = data.BaseUrl;
     ProductType    = data.ProductType;
     Description    = data.Description;
     EventType      = data.EventType;
     return(this);
 }
        public async Task AppInstalled([FromQuery] string userAccountId, [FromBody] AtlModel.ClientRegistrationData data)
        {
            _logger.LogInformation($"AppInstalled client: {data.ClientKey}");

            /* as stated
             *  https://developer.atlassian.com/cloud/confluence/security-for-connect-apps/
             *  First install after being uninstalled:
             *  The shared secret sent in the preceding installed callback. This allows apps to allow the new installation to access previous tenant data (if any exists).
             *  A valid signature demonstrates that the sender is in possession of the shared secret from when the old tenant data was accessed.
             */
            ClientRegistration registration = null;

            // new clients should have _client.RegistrationData set to null
            if (null == _client.RegistrationData)
            {
                registration = await _dbContext.ClientRegistration.Where(c => c.Key == _client.AtlassianAppKey && c.ClientKey == data.ClientKey).FirstOrDefaultAsync();

                if (default(ClientRegistration) != registration)
                {
                    throw new ArgumentException($"Client {data.ClientKey} already registered but no token was provided");
                }
            }
            if (null != _client.AuthException)
            {
                throw new ControllerException(message: $"Exception occired during token validation: {_client.AuthException.Message}", innerException: _client.AuthException);
            }

            // we have checked that either client is new client (no token provided) or already registered (token provided, verified, _client.RegistrationData filled
            //foreach (var r in _dbContext.ClientRegistration.Where(c => c.Key == _client.AtlassianAppKey && c.ClientKey == data.ClientKey))
            //    _dbContext.ClientRegistration.Remove(r);

            if (null != _client.RegistrationData)
            {
                registration = await _dbContext.ClientRegistration.Where(c => c.Id == _client.RegistrationData.Id)
                               .FirstOrDefaultAsync();

                registration.Update(data);
            }
            else
            {
                registration = ClientRegistration.From(data);
                registration.UserAccountId = userAccountId;
                _dbContext.ClientRegistration.Add(registration);
            }
            registration.ClientStateId = (int)AtlConnect.Enum.eRegTypes.AppInstalled;
            await _dbContext.SaveChangesAsync();

            _client.RegistrationData = registration;
            await ReportSetupEvent(registration, "installed", true);
        }
        public async Task AppDisabled([FromBody] AtlModel.ClientRegistrationData data)
        {
            _logger.LogInformation($"AppDisabled client: {data.ClientKey}");
            // we should already have _client.RegistrationData filled.
            ClientRegistration registration = await _dbContext.ClientRegistration.Where(c => c.Id == _client.RegistrationData.Id).FirstOrDefaultAsync();

            if (default(ClientRegistration) != registration)
            {
                registration.ClientStateId = (int)AtlConnect.Enum.eRegTypes.AppDisabled;
                await _dbContext.SaveChangesAsync();
            }
            else
            {
                _logger.LogError($"AppDisabled: Unable to find registered client: {data.ClientKey} with id {_client.RegistrationData?.Id}");
            }
            await ReportSetupEvent(registration ?? ClientRegistration.From(data), "disabled", default(ClientRegistration) != registration);
        }
 public static ClientRegistration From(AtlModel.ClientRegistrationData data) => new ClientRegistration().Update(data);