Files
kopia/repo/token.go
Jarek Kowalski fa7976599c repo: refactored repository interfaces (#780)
- `repo.Repository` is now read-only and only has methods that can be supported over kopia server
- `repo.RepositoryWriter` has read-write methods that can be supported over kopia server
- `repo.DirectRepository` is read-only and contains all methods of `repo.Repository` plus some low-level methods for data inspection
- `repo.DirectRepositoryWriter` contains write methods for `repo.DirectRepository`

- `repo.Reader` removed and merged with `repo.Repository`
- `repo.Writer` became `repo.RepositoryWriter`
- `*repo.DirectRepository` struct became `repo.DirectRepository`
  interface

Getting `{Direct}RepositoryWriter` requires using `NewWriter()` or `NewDirectWriter()` on a read-only repository and multiple simultaneous writers are supported at the same time, each writing to their own indexes and pack blobs.

`repo.Open` returns `repo.Repository` (which is also `repo.RepositoryWriter`).

* content: removed implicit flush on content manager close
* repo: added tests for WriteSession() and implicit flush behavior
* invalidate manifest manager after write session

* cli: disable maintenance in 'kopia server start'
  Server will close the repository before completing.

* repo: unconditionally close RepositoryWriter in {Direct,}WriteSession
* repo: added panic in case somebody tries to create RepositoryWriter after closing repository
  - used atomic to manage SharedManager.closed

* removed stale example
* linter: fixed spurious failures

Co-authored-by: Julio López <julio+gh@kasten.io>
2021-01-20 11:41:47 -08:00

54 lines
1.3 KiB
Go

package repo
import (
"encoding/base64"
"encoding/json"
"github.com/pkg/errors"
"github.com/kopia/kopia/repo/blob"
)
type tokenInfo struct {
Version string `json:"version"`
Storage blob.ConnectionInfo `json:"storage"`
Password string `json:"password,omitempty"`
}
// Token returns an opaque token that contains repository connection information
// and optionally the provided password.
func (r *directRepository) Token(password string) (string, error) {
ti := &tokenInfo{
Version: "1",
Storage: r.blobs.ConnectionInfo(),
Password: password,
}
v, err := json.Marshal(ti)
if err != nil {
return "", errors.Wrap(err, "marshal token")
}
return base64.RawURLEncoding.EncodeToString(v), nil
}
// DecodeToken decodes the provided token and returns connection info and password if persisted.
func DecodeToken(token string) (blob.ConnectionInfo, string, error) {
t := &tokenInfo{}
v, err := base64.RawURLEncoding.DecodeString(token)
if err != nil {
return blob.ConnectionInfo{}, "", errors.New("unable to decode token")
}
if err := json.Unmarshal(v, t); err != nil {
return blob.ConnectionInfo{}, "", errors.New("unable to decode token")
}
if t.Version != "1" {
return blob.ConnectionInfo{}, "", errors.New("unsupported token version")
}
return t.Storage, t.Password, nil
}