Esempio n. 1
0
        public RESTReplyData set_public_key(RESTRequestData pReq, List <string> pArgs)
        {
            RESTReplyData replyData = new RESTReplyData();  // The HTTP response info
            ResponseBody  respBody  = new ResponseBody();   // The request's "data" response info

            string domainID = pArgs.Count == 1 ? pArgs[0] : null;

            if (Domains.Instance.TryGetDomainWithID(domainID, out DomainEntity aDomain))
            {
                try
                {
                    string includedAPIKey = pReq.RequestBodyMultipart("api_key");

                    // If this is a temp domain, the supplied API key must match
                    // TODO: is there another Authorization later (ie, maybe user auth?)
                    if (String.IsNullOrEmpty(aDomain.API_Key) || aDomain.API_Key == includedAPIKey)
                    {
                        // The PUT sends the key as binary but it is later sent around
                        //    and processed as a Base64 string.
                        Stream byteStream = pReq.RequestBodyMultipartStream("public_key");
                        if (byteStream != null)
                        {
                            using var memStream = new MemoryStream();
                            byteStream.CopyTo(memStream);
                            aDomain.Public_Key = Convert.ToBase64String(memStream.ToArray());
                            aDomain.Updated();
                            // Context.Log.Debug("{0} successful set of public_key for {1}", _logHeader, domainID);
                        }
                        else
                        {
                            Context.Log.Error("{0} could not extract public key from request body: domain {1}",
                                              _logHeader, domainID);
                            replyData.Status = 401; // not authorized
                            respBody.RespondFailure();
                        }
                    }
                    else
                    {
                        Context.Log.Error("{0} attempt to set public_key with non-matching APIKeys: domain {1}",
                                          _logHeader, domainID);
                        replyData.Status = 401; // not authorized
                        respBody.RespondFailure();
                    }
                }
                catch (Exception e)
                {
                    Context.Log.Error("{0} exception parsing multi-part http: {1}", _logHeader, e.ToString());
                    respBody.RespondFailure();
                }
            }
            else
            {
                Context.Log.Error("{0} attempt to set public_key for unknown domain {1}", _logHeader, domainID);
                respBody.RespondFailure();
            }

            replyData.Body = respBody;
            return(replyData);
        }
Esempio n. 2
0
        public RESTReplyData set_public_key(RESTRequestData pReq, List <string> pArgs)
        {
            RESTReplyData replyData = new RESTReplyData();  // The HTTP response info
            ResponseBody  respBody  = new ResponseBody();   // The request's "data" response info

            SessionEntity aSession = Sessions.Instance.UpdateSession(pReq.SenderKey);

            string domainID = pArgs.Count == 1 ? pArgs[0] : null;

            if (Domains.Instance.TryGetDomainWithID(domainID, out DomainEntity aDomain))
            {
                try
                {
                    string includedAPIKey = pReq.RequestBodyMultipart("api_key");
                    if (VerifyDomainAccess(aDomain, pReq, includedAPIKey, out string oFailureReason))
                    {
                        // The PUT sends the key as binary but it is later sent around
                        //    and processed as a Base64 string.
                        Stream byteStream = pReq.RequestBodyMultipartStream("public_key");
                        if (byteStream != null)
                        {
                            aDomain.Public_Key = Tools.ConvertPublicKeyStreamToBase64(byteStream);
                            aDomain.Updated();
                            // Context.Log.Debug("{0} successful set of public_key for {1}", _logHeader, domainID);
                        }
                        else
                        {
                            Context.Log.Error("{0} could not extract public key from request body: domain {1}",
                                              _logHeader, domainID);
                            replyData.Status = (int)HttpStatusCode.Unauthorized;
                            respBody.RespondFailure("Could not extract public key from request body");
                        }
                    }
                    else
                    {
                        Context.Log.Error("{0} attempt to set public_key when no authorized: domain {1}",
                                          _logHeader, domainID);
                        replyData.Status = (int)HttpStatusCode.Unauthorized;
                        respBody.RespondFailure(oFailureReason);
                    }
                }
                catch (Exception e)
                {
                    Context.Log.Error("{0} exception parsing multi-part http: {1}", _logHeader, e.ToString());
                    respBody.RespondFailure("Exception parsing multi-part http", e.ToString());
                }
            }
            else
            {
                Context.Log.Error("{0} attempt to set public_key for unknown domain {1}", _logHeader, domainID);
                respBody.RespondFailure("unknown domain");
            }

            replyData.SetBody(respBody, pReq);
            return(replyData);
        }
Esempio n. 3
0
        public RESTReplyData set_public_key(RESTRequestData pReq, List <string> pArgs)
        {
            RESTReplyData replyData = new RESTReplyData();  // The HTTP response info
            ResponseBody  respBody  = new ResponseBody();

            string includedAPIKey    = null;
            string includedPublicKey = null;
            bool   parsed            = false; // whether multipart was parsed

            try
            {
                // The PUT sends the key as binary but it is later sent around
                //    and processed as a Base64 string.
                Stream byteStream = pReq.RequestBodyMultipartStream("public_key");
                if (byteStream != null)
                {
                    includedPublicKey = Tools.ConvertPublicKeyStreamToBase64(byteStream);
                    // There might has been an APIKey
                    includedAPIKey = pReq.RequestBodyMultipart("api_key");
                    parsed         = true;
                }
                else
                {
                    Context.Log.Error("{0} could not extract public key from request body in PUT user/public_key", _logHeader);
                }
            }
            catch (Exception e)
            {
                Context.Log.Error("{0} exception parsing multi-part http: {1}", _logHeader, e.ToString());
                parsed = false;
            }

            if (parsed)
            {
                // If there is account authorization in the header, set the account public key.
                // If no account authorization (the client is not logged in), check for matching
                //     APIkey and then set the session public key.
                if (String.IsNullOrEmpty(pReq.AuthToken))
                {
                    if (includedAPIKey != null &&
                        Domains.Instance.TryGetDomainWithAPIKey(includedAPIKey, out DomainEntity aDomain))
                    {
                        aDomain.Public_Key = includedPublicKey;
                    }
                    else
                    {
                        Context.Log.Error("{0} PUT user/set_public_key requested without APIKey. APIKey={1}",
                                          _logHeader, includedAPIKey);
                        respBody.RespondFailure("APIkey does not work");
                        replyData.Status = (int)HttpStatusCode.Unauthorized;
                    }
                }
                else
                {
                    if (Accounts.Instance.TryGetAccountWithAuthToken(pReq.AuthToken, out AccountEntity aAccount))
                    {
                        aAccount.Public_Key = includedPublicKey;
                    }
                    else
                    {
                        Context.Log.Error("{0} PUT user/set_public_key requested but could not find acct. Token={1}",
                                          _logHeader, pReq.AuthToken);
                        respBody.RespondFailure("AuthToken unauthorized");
                        replyData.Status = (int)HttpStatusCode.Unauthorized;
                    }
                }
            }
            else
            {
                Context.Log.Error("{0} PUT user/set_public_key failure parsing", _logHeader);
                respBody.RespondFailure("Multi-part body failed parsing");
            }

            replyData.SetBody(respBody, pReq);
            return(replyData);
        }