couchbase
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
couchbase [2015/05/07 20:29] – [CouchBase Lite] skipidar | couchbase [2020/12/27 20:35] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ===== CouchBase ===== | ||
+ | ==== CouchBase Lite ==== | ||
+ | |||
+ | |||
+ | === Authentication === | ||
+ | |||
+ | == Registration of new users == | ||
+ | Creating a User is done via the Admin Port of the REST API: \\ | ||
+ | http:// | ||
+ | |||
+ | < | ||
+ | curl -X POST http:// | ||
+ | </ | ||
+ | == Restrict reading the document == | ||
+ | Channels legitimate a user to read documents. | ||
+ | A user is legitimated to access a channel. | ||
+ | User's documents are marked by channels. | ||
+ | |||
+ | == Restrict writing the document to it's owner == | ||
+ | This is done inside the **Sync function**, which is defined during the start of the Sync Gateway | ||
+ | < | ||
+ | # this required the currently logged in user - to be the sam as mentioned in oldDoc' | ||
+ | requireUser(oldDoc.user) | ||
+ | </ | ||
+ | |||
+ | The write restriction may be defined by: | ||
+ | * rolename | ||
+ | * channel | ||
+ | * username | ||
+ | |||
+ | http:// | ||
+ | |||
+ | |||
+ | |||
+ | === Manual Authentication === | ||
+ | |||
+ | * Your app prompts user for credentials | ||
+ | * Your app directly contacts your app server with these credentials | ||
+ | * Your app server creates a session on the Sync Gateway, which returns a cookie | ||
+ | * Your app server returns this cookie to your app | ||
+ | |||
+ | http:// | ||
+ | NGinx may be used | ||
+ | * to implement a reverse Proxy, to protect the connection with SSL | ||
+ | * with embedded PERL | ||
+ | * provide the user registration API | ||
+ | |||
+ | |||
+ | ==== CouchBase ==== | ||
+ | The NoSQL Server. Stores data as documents. | ||
+ | |||
+ | |||
+ | ==== Sync Gateway ==== | ||
+ | |||
+ | == Glossary == | ||
+ | |||
+ | |DataBase| The database maps to a bucket. A single Sync Gateway may publish buckets as databases. Below the DB **gw** points to the bucket **sync_gateway**| | ||
+ | |Server| The server is the storage, where the Sync Gateway will put hte data in. It may be a Couchase Server or a Walrus Server (File System DB) " | ||
+ | |||
+ | < | ||
+ | { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | }, | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | if (doc._deleted) { | ||
+ | requireUser(oldDoc.writers); | ||
+ | return; | ||
+ | } | ||
+ | if (!doc.title || !doc.creator || !doc.writers) { | ||
+ | throw({forbidden: | ||
+ | } else if (doc.writers.length == 0) { | ||
+ | throw({forbidden: | ||
+ | } | ||
+ | if (oldDoc == null) { | ||
+ | requireUser(doc.creator); | ||
+ | } else { | ||
+ | requireUser(oldDoc.writers); | ||
+ | if (doc.creator != oldDoc.creator) { | ||
+ | throw({forbidden: | ||
+ | } | ||
+ | } | ||
+ | // add channel with the username to the doc | ||
+ | channel (" | ||
+ | |||
+ | // add access to the channel username to the user | ||
+ | access (doc.creator, | ||
+ | |||
+ | }`, | ||
+ | " | ||
+ | " | ||
+ | } | ||
+ | } | ||
+ | }, | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | == Create user, assign channel== | ||
+ | User has to have access to it's own channel, in order to have the ability to read docs. | ||
+ | |||
+ | <sxh powershell> | ||
+ | # Sync Gateway request: | ||
+ | $method = " | ||
+ | $resource = " | ||
+ | $body = "{ | ||
+ | "" | ||
+ | "" | ||
+ | "" | ||
+ | "" | ||
+ | "" | ||
+ | }" | ||
+ | echo $body; | ||
+ | Invoke-RestMethod -Method $method -Uri $resource -Body $body -ContentType ' | ||
+ | </ | ||
+ | |||
+ | |||
+ | == Create a doc which is assigned to user's channel== | ||
+ | Assigning is done automatically, | ||
+ | < | ||
+ | // add channel with the username to the doc | ||
+ | channel (" | ||
+ | </ | ||
+ | |||
+ | Alternatively the doc may be added by powershell | ||
+ | <sxh powershell> | ||
+ | # create a document by REST API | ||
+ | $now = $(get-date); | ||
+ | $resource = " | ||
+ | $method = " | ||
+ | $body = "{ | ||
+ | "" | ||
+ | "" | ||
+ | "" | ||
+ | "" | ||
+ | "" | ||
+ | "" | ||
+ | }" | ||
+ | $securepassword = ConvertTo-SecureString " | ||
+ | $credentials = New-Object System.Management.Automation.PSCredential(" | ||
+ | Invoke-WebRequest -Uri $resource -Method POST -Credential $credentials -Body $body -ContentType application/ | ||
+ | </ | ||
+ | |||
+ | This is how the document may look like | ||
+ | |||
+ | < | ||
+ | { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | ], | ||
+ | " | ||
+ | -1 | ||
+ | ], | ||
+ | " | ||
+ | "" | ||
+ | ], | ||
+ | " | ||
+ | [ | ||
+ | " | ||
+ | ] | ||
+ | ] | ||
+ | }, | ||
+ | " | ||
+ | " | ||
+ | }, | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | } | ||
+ | }, | ||
+ | " | ||
+ | }, | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | ] | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | The channels which this doc is assigned to is listed here: | ||
+ | < | ||
+ | " | ||
+ | " | ||
+ | }, | ||
+ | </ | ||
+ | |||
+ | |||
+ | == Now the docs may be synced | ||
+ | The docs may be synced, without setting any channel. | ||
+ | Then all available docs will be pulled | ||
+ | <sxh java> | ||
+ | pullReplication = database.createPullReplication(syncUrl); | ||
+ | pullReplication.setContinuous(true); | ||
+ | |||
+ | pushReplication = database.createPushReplication(syncUrl); | ||
+ | pushReplication.setContinuous(true); | ||
+ | | ||
+ | pushReplication.start(); | ||
+ | pullReplication.start(); | ||
+ | </ |