Beispiel #1
0
        private static APIResultSync GetCursorResult(ISimpleJsonRest web, StandardNoteData dat, APIBodySync d)
        {
            var masterResult = new APIResultSync
            {
                retrieved_items = new List <APIResultItem>(),
                unsaved         = new List <APIResultErrorItem>(),
                saved_items     = new List <APIResultItem>()
            };

            for (;;)
            {
                var result = web.PostTwoWay <APIResultSync>(d, "items/sync");
                dat.SyncToken = result.sync_token.Trim();

                masterResult.sync_token = result.sync_token;
                masterResult.unsaved.AddRange(result.unsaved);
                masterResult.cursor_token = result.cursor_token;
                masterResult.retrieved_items.AddRange(result.retrieved_items);
                masterResult.saved_items.AddRange(result.saved_items);

                if (result.cursor_token == null)
                {
                    return(masterResult);
                }

                d.cursor_token = result.cursor_token;
                d.items.Clear();
            }
        }
Beispiel #2
0
        private static APIResultAuthorize Authenticate003(ISimpleJsonRest web, APIAuthParams apiparams, string mail, string uip, AlephLogger logger)
        {
            try
            {
                logger.Debug(StandardNotePlugin.Name, $"AutParams[version:{apiparams.version}, pw_cost:{apiparams.pw_cost}, pw_nonce:{apiparams.pw_nonce}]");

                if (apiparams.pw_cost < 100000)
                {
                    throw new StandardNoteAPIException($"Account pw_cost is too small ({apiparams.pw_cost})");
                }

                var    salt  = StandardNoteCrypt.SHA256(string.Join(":", mail, "SF", "003", apiparams.pw_cost.ToString(), apiparams.pw_nonce));
                byte[] bytes = PBKDF2.GenerateDerivedKey(768 / 8, Encoding.UTF8.GetBytes(uip), Encoding.UTF8.GetBytes(salt), apiparams.pw_cost, PBKDF2.HMACType.SHA512);

                var pw = bytes.Skip(0 * (bytes.Length / 3)).Take(bytes.Length / 3).ToArray();
                var mk = bytes.Skip(1 * (bytes.Length / 3)).Take(bytes.Length / 3).ToArray();
                var ak = bytes.Skip(2 * (bytes.Length / 3)).Take(bytes.Length / 3).ToArray();

                var reqpw = EncodingConverter.ByteToHexBitFiddleUppercase(pw).ToLower();
                APIResultAuthorize tok;
                try
                {
                    tok = web.PostTwoWay <APIResultAuthorize>(new APIRequestUser {
                        email = mail, password = reqpw
                    }, "auth/sign_in");
                }
                catch (RestStatuscodeException e1)
                {
                    if (e1.StatusCode / 100 == 4 && !string.IsNullOrWhiteSpace(e1.HTTPContent))
                    {
                        var req = web.ParseJsonOrNull <APIBadRequest>(e1.HTTPContent);
                        if (req != null)
                        {
                            throw new StandardNoteAPIException($"Server returned status {e1.StatusCode}.\nMessage: '{req.error.message}'", e1);
                        }
                    }

                    throw;
                }

                tok.masterkey     = mk;
                tok.masterauthkey = ak;
                tok.version       = "003";
                return(tok);
            }
            catch (RestException)
            {
                throw;
            }
            catch (StandardNoteAPIException)
            {
                throw;
            }
            catch (Exception e)
            {
                throw new StandardNoteAPIException("Authentification with StandardNoteAPI failed.", e);
            }
        }
Beispiel #3
0
        private static APIResultAuthorize Authenticate002(ISimpleJsonRest web, APIAuthParams apiparams, string mail, string password, IAlephLogger logger)
        {
            try
            {
                logger.Debug(StandardNotePlugin.Name, $"AutParams[version:2, pw_cost:{apiparams.pw_cost}]");

                if (apiparams.pw_func != PasswordFunc.pbkdf2)
                {
                    throw new Exception("Unknown pw_func: " + apiparams.pw_func);
                }

                byte[] bytes = PBKDF2.GenerateDerivedKey(768 / 8, Encoding.UTF8.GetBytes(password), Encoding.UTF8.GetBytes(apiparams.pw_salt), apiparams.pw_cost, PBKDF2.HMACType.SHA512);

                var pw = bytes.Skip(0 * (bytes.Length / 3)).Take(bytes.Length / 3).ToArray();
                var mk = bytes.Skip(1 * (bytes.Length / 3)).Take(bytes.Length / 3).ToArray();
                var ak = bytes.Skip(2 * (bytes.Length / 3)).Take(bytes.Length / 3).ToArray();

                var reqpw = EncodingConverter.ByteToHexBitFiddleUppercase(pw).ToLower();
                APIResultAuthorize tok;
                try
                {
                    tok = web.PostTwoWay <APIResultAuthorize>(new APIRequestUser {
                        email = mail, password = reqpw
                    }, "auth/sign_in");
                }
                catch (RestStatuscodeException e1)
                {
                    if (e1.StatusCode / 100 == 4 && !string.IsNullOrWhiteSpace(e1.HTTPContent))
                    {
                        var req = web.ParseJsonOrNull <APIBadRequest>(e1.HTTPContent);
                        if (req != null)
                        {
                            throw new StandardNoteAPIException($"Server returned status {e1.StatusCode}.\nMessage: '{req.error.message}'", e1);
                        }
                    }

                    throw;
                }

                tok.masterkey     = mk;
                tok.masterauthkey = ak;
                tok.version       = "002";
                return(tok);
            }
            catch (RestException)
            {
                throw;
            }
            catch (StandardNoteAPIException)
            {
                throw;
            }
            catch (Exception e)
            {
                throw new StandardNoteAPIException("Authentification with StandardNoteAPI failed.", e);
            }
        }
Beispiel #4
0
        public static SimpleNote ChangeExistingNote(ISimpleJsonRest web, SimpleNote note, SimpleNoteConfig cfg, SimpleNoteConnection conn, out bool updated)
        {
            if (note.Deleted)
            {
                throw new SimpleNoteAPIException("Cannot update an already deleted note");
            }
            if (note.ID == "")
            {
                throw new SimpleNoteAPIException("Cannot change a not uploaded note");
            }
            note.ModificationDate = DateTimeOffset.Now;

            APISendNoteData data = new APISendNoteData
            {
                tags             = note.Tags.ToList(),
                content          = note.Content,
                modificationDate = ConvertToEpochDate(note.ModificationDate),
                systemTags       = note.SystemTags.ToList(),
            };

            try
            {
                var r = web.PostTwoWay <APIResultNoteData>(data, "note/i/" + note.ID, new[] { 412 }, "response=1");

                if (r == null)
                {
                    // Statuscode 412 - Empty change

                    updated = false;
                    return((SimpleNote)note.Clone());
                }

                updated = true;
                return(GetNoteFromQuery(r, web, note.ID, cfg, conn));
            }
            catch (RestStatuscodeException e1)
            {
                if (e1.StatusCode == 400 && !string.IsNullOrWhiteSpace(e1.HTTPContent))
                {
                    var req = web.ParseJsonOrNull <APIBadRequest>(e1.HTTPContent);
                    if (req != null)
                    {
                        throw new SimpleNoteAPIException($"Server returned status 400.\nField: '{req.field}'.\nMessage: '{req.message}'", e1);
                    }
                }

                throw;
            }
        }
Beispiel #5
0
        public static APIResultAuthorize Authenticate(ISimpleJsonRest web, string userName, string password)
        {
            try
            {
                return(web.PostTwoWay <APIResultAuthorize>(new APISendAuth {
                    username = userName, password = password
                }, "authorize/"));
            }
            catch (RestStatuscodeException e1)
            {
                if (e1.StatusCode == 400 && !string.IsNullOrWhiteSpace(e1.HTTPContent))
                {
                    var req = web.ParseJsonOrNull <APIBadRequest>(e1.HTTPContent);
                    if (req != null)
                    {
                        throw new SimpleNoteAPIException($"Server returned status 400.\nField: '{req.field}'.\nMessage: '{req.message}'", e1);
                    }
                }

                throw;
            }
        }
Beispiel #6
0
        public static SimpleNote UploadNewNote(ISimpleJsonRest web, SimpleNote note, SimpleNoteConfig cfg, SimpleNoteConnection conn)
        {
            note.Deleted          = false;
            note.CreationDate     = DateTimeOffset.Now;
            note.ModificationDate = DateTimeOffset.Now;

            APIResultNoteData data = new APIResultNoteData
            {
                tags             = note.Tags.ToList(),
                deleted          = false,
                shareURL         = note.ShareURL,
                publishURL       = note.PublicURL,
                systemTags       = note.SystemTags,
                content          = note.Content,
                creationDate     = ConvertToEpochDate(note.CreationDate),
                modificationDate = ConvertToEpochDate(note.ModificationDate),
            };

            try
            {
                var r = web.PostTwoWay <APIResultNoteData>(data, "note/i/" + note.ID, "response=1");

                return(GetNoteFromQuery(r, web, note.ID, cfg, conn));
            }
            catch (RestStatuscodeException e1)
            {
                if (e1.StatusCode == 400 && !string.IsNullOrWhiteSpace(e1.HTTPContent))
                {
                    var req = web.ParseJsonOrNull <APIBadRequest>(e1.HTTPContent);
                    if (req != null)
                    {
                        throw new SimpleNoteAPIException($"Server returned status 400.\nField: '{req.field}'.\nMessage: '{req.message}'", e1);
                    }
                }

                throw;
            }
        }
Beispiel #7
0
        public static NextcloudNote UploadNewNote(ISimpleJsonRest web, NextcloudNote note, NextcloudConfig config)
        {
            var data = new ApiCreateNote {
                content = note.Content, category = CreateCategoryFromPath(note.Path)
            };
            var result = web.PostTwoWay <ApiNoteResult>(data, "notes");

            var rnote = new NextcloudNote(result.id, note.LocalID, config)
            {
                CreationDate     = DateTime.Now,
                RemoteTimestamp  = result.modified,
                Content          = result.content,
                Path             = ExtractPathFromCategory(result.category),
                ModificationDate = ConvertFromEpochDate(result.modified),
                ETag             = result.etag,
            };

            if (rnote.Title.ToLower() != result.title.ToLower())
            {
                rnote.Title = result.title;
            }
            return(rnote);
        }