github.com/decred/politeia@v1.4.0/politeiawww/plugin/v1/user.go (about)

     1  // Copyright (c) 2022 The Decred developers
     2  // Use of this source code is governed by an ISC
     3  // license that can be found in the LICENSE file.
     4  
     5  package v1
     6  
     7  import "github.com/google/uuid"
     8  
     9  // User represents a politeia user. The user will contain the PluginData for
    10  // the plugin that is executing the command or hook.
    11  type User struct {
    12  	ID         uuid.UUID // Unique ID
    13  	PluginData *PluginData
    14  }
    15  
    16  // PluginData contains the user data that is owned by the plugin.
    17  //
    18  // These fields can be updated by the plugin during execution of write
    19  // commands. Updates are persisted by the backend on successful completion of
    20  // the plugin command execution. Any updates made during the execution of
    21  // read-only commands are ignored.
    22  //
    23  // The encrypted data blob will be provided to the plugin as clear text, but
    24  // will be saved to the database by the backend as encrypted. The plugin does
    25  // not need to worry about encrypting/decrypting the data.
    26  type PluginData struct {
    27  	clearText []byte
    28  	encrypted []byte
    29  	updated   bool
    30  }
    31  
    32  // NewPluginData returns a new PluginData.
    33  func NewPluginData(clearText, encrypted []byte) *PluginData {
    34  	return &PluginData{
    35  		clearText: clearText,
    36  		encrypted: encrypted,
    37  	}
    38  }
    39  
    40  // ClearText returns the clear text plugin data.
    41  func (d *PluginData) ClearText() []byte {
    42  	return d.clearText
    43  }
    44  
    45  // SetClearText updates the clear text plugin data.
    46  func (d *PluginData) SetClearText(b []byte) {
    47  	d.clearText = b
    48  	d.updated = true
    49  }
    50  
    51  // Encrypted returns the encrypted plugin data. The data is returned as clear
    52  // text to the plugin, but is saved to the database as encrypted. The plugin
    53  // does not need to worry about encrypting/decrypting the data.
    54  func (d *PluginData) Encrypted() []byte {
    55  	return d.encrypted
    56  }
    57  
    58  // SetEncrypted updates the encrypted plugin data. The provided data should be
    59  // clear text. It will be encrypted prior to being saved to the database. The
    60  // plugin does not need to worry about encrypting/decrypting the data.
    61  func (d *PluginData) SetEncrypted(b []byte) {
    62  	d.encrypted = b
    63  	d.updated = true
    64  }
    65  
    66  // Updated returns whether the plugin data has been updated.
    67  func (d *PluginData) Updated() bool {
    68  	return d.updated
    69  }