public PexOAuthSessionEntity(PexOAuthSessionModel model, string partitionKey)
 {
     PartitionKey      = partitionKey;
     RowKey            = model.SessionGuid.ToString();
     SessionGuid       = model.SessionGuid;
     ExternalToken     = model.ExternalToken;
     CreatedUtc        = model.CreatedUtc;
     RevokedUtc        = model.RevokedUtc;
     LastRenewedUtc    = model.LastRenewedUtc;
     PEXBusinessAcctId = model.PEXBusinessAcctId;
 }
        public async Task UpdateAsync(PexOAuthSessionModel model, CancellationToken cancellationToken)
        {
            if (model.SessionGuid == Guid.Empty)
            {
                throw new ArgumentException("session.SessionGuid is not specified");
            }
            var entity = await GetEntityBySessionGuidAsync(model.SessionGuid, cancellationToken);

            entity.ExternalToken  = model.ExternalToken;
            entity.RevokedUtc     = model.RevokedUtc;
            entity.LastRenewedUtc = model.LastRenewedUtc;
            var operation = TableOperation.InsertOrReplace(entity);
            await Table.ExecuteAsync(operation);
        }
        public async Task CreateSession(string sessionId, [FromBody] TokenModel model, CancellationToken cancellationToken)
        {
            var business = await _pexApiClient.GetBusinessDetails(model.Token);

            var session = new PexOAuthSessionModel
            {
                SessionGuid       = new Guid(sessionId),
                ExternalToken     = model.Token,
                CreatedUtc        = DateTime.UtcNow,
                LastRenewedUtc    = DateTime.UtcNow,
                PEXBusinessAcctId = business.BusinessAccountId
            };
            await _pexOAuthSessionStorage.CreateAsync(session, cancellationToken);

            await _aplosIntegrationService.EnsureMappingInstalled(session, cancellationToken);
        }
        public async Task <ActionResult> CreateAplosToken(string sessionId, [FromBody] AplosTokenRequestModel model, CancellationToken cancellationToken)
        {
            if (!Guid.TryParse(sessionId, out var sessionGuid))
            {
                return(BadRequest());
            }

            PexOAuthSessionModel session = await _pexOAuthSessionStorage.GetBySessionGuidAsync(sessionGuid, cancellationToken);

            if (session == null)
            {
                return(Unauthorized());
            }

            Pex2AplosMappingModel mapping = await _pex2AplosMappingStorage.GetByBusinessAcctIdAsync(session.PEXBusinessAcctId, cancellationToken);

            if (!string.IsNullOrWhiteSpace(model.AplosClientId))
            {
                mapping.AplosClientId = model.AplosClientId;
            }

            if (!string.IsNullOrWhiteSpace(model.AplosPrivateKey))
            {
                mapping.AplosPrivateKey = model.AplosPrivateKey;
            }

            bool result = await _aplosIntegrationService.ValidateAplosApiCredentials(mapping, cancellationToken);

            if (!result)
            {
                return(BadRequest());
            }

            await _pex2AplosMappingStorage.UpdateAsync(mapping, cancellationToken);

            return(Ok());
        }
        public async Task <TokenModel> CreateSessionFromJwt([FromBody] TokenModel model, CancellationToken cancellationToken)
        {
            var externalToken = await _pexApiClient.ExchangeJwtForApiToken(model.Token,
                                                                           new ExchangeTokenRequestModel { AppId = _appSettings.PexApiClientId, AppSecret = _appSettings.PexApiClientSecret });

            var business = await _pexApiClient.GetBusinessDetails(externalToken, cancellationToken);

            var sessionGuid = Guid.NewGuid();
            var session     = new PexOAuthSessionModel
            {
                SessionGuid       = sessionGuid,
                ExternalToken     = externalToken,
                CreatedUtc        = DateTime.UtcNow,
                LastRenewedUtc    = DateTime.UtcNow,
                PEXBusinessAcctId = business.BusinessAccountId,
            };
            await _pexOAuthSessionStorage.CreateAsync(session, cancellationToken);

            await _aplosIntegrationService.EnsureMappingInstalled(session, cancellationToken);

            return(new TokenModel {
                Token = sessionGuid.ToString()
            });
        }
 public async Task CreateAsync(PexOAuthSessionModel model, CancellationToken cancellationToken)
 {
     var entity    = new PexOAuthSessionEntity(model, PartitionKey);
     var operation = TableOperation.Insert(entity);
     await Table.ExecuteAsync(operation);
 }