示例#1
0
        public async Task <IHttpActionResult> PutRequestSwap(int id, RequestSwap requestSwap)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != requestSwap.Id)
            {
                return(BadRequest());
            }

            db.Entry(requestSwap).State = EntityState.Modified;

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!RequestSwapExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
示例#2
0
        public async Task <IHttpActionResult> GetRequestSwap(int id)
        {
            RequestSwap requestSwap = await db.RequestsSwap.FindAsync(id);

            if (requestSwap == null)
            {
                return(NotFound());
            }

            return(Ok(requestSwap));
        }
示例#3
0
        public async Task <IHttpActionResult> PostRequestSwap(RequestSwap requestSwap)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            db.RequestsSwap.Add(requestSwap);
            await db.SaveChangesAsync();

            return(CreatedAtRoute("DefaultApi", new { id = requestSwap.Id }, requestSwap));
        }
示例#4
0
        public async Task <IHttpActionResult> DeleteRequestSwap(int id)
        {
            RequestSwap requestSwap = await db.RequestsSwap.FindAsync(id);

            if (requestSwap == null)
            {
                return(NotFound());
            }

            db.RequestsSwap.Remove(requestSwap);
            await db.SaveChangesAsync();

            return(Ok(requestSwap));
        }
示例#5
0
        public void Swap(int userId, String credVal)
        {
            RequestSwap reqSwap = Front(userId);

            // Transform RequestSwap into ProxySwap
            var proxySwap = new ProxySwap();

            proxySwap.Ip         = reqSwap.Ip;
            proxySwap.Domain     = reqSwap.Domain;
            proxySwap.RandToken  = reqSwap.RandToken;
            proxySwap.UserId     = reqSwap.UserId;
            proxySwap.Credential = credVal;

            // Add to ProxySwap Database, remove from RequestSwap Database
            _context.ProxySwaps.Add(proxySwap);
            _context.RequestSwaps.Remove(reqSwap);
            _context.SaveChanges();
        }
示例#6
0
        public IActionResult Submit([FromBody] SubmitSwapModel model)
        {
            int userId = int.Parse(User.Identity.Name);

            // Grab the top request Swap as well as the credential the user wants to use
            RequestSwap reqSwap = _swapService.Front(userId);

            if (reqSwap == null)
            {
                return(BadRequest(new { Title = "User does not have any pending request Swaps" }));
            }

            Credential cred = new Credential();

            cred.Id     = model.CredentialId;
            cred.UserId = userId;
            cred.Domain = reqSwap.Domain;
            cred        = _credService.Read(cred)[0];
            if (cred == null)
            {
                return(BadRequest(new { Title = "User is not allowed to use this credential on this domain" }));
            }

            try {
                String valueHash = Decrypt(cred.ValueHash, model.PrivateKey);
                _swapService.Swap(userId, valueHash);
                return(Ok());
            }
            catch (AppException e) {
                return(BadRequest(new { Title = e.Message }));

                // If private key is not correct, this exception will trigger
            } catch (FormatException e) {
                return(BadRequest(new { Title = e.Message }));
            } catch (Exception e) {
                return(BadRequest(new { Title = e.Message }));
            }
        }
示例#7
0
        public RequestSwap Front(int userId)
        {
            RequestSwap topReq = _context.RequestSwaps.OrderBy(r => r.Id).FirstOrDefault(r => r.UserId == userId);

            return(topReq);
        }
示例#8
0
 public void Enqueue(RequestSwap reqSwap)
 {
     _context.RequestSwaps.Add(reqSwap);
     _context.SaveChanges();
 }