common

package
v0.0.0-...-233af93 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 11, 2022 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const AuthenticationSignatureKeySettingKey = "authentication_signature_key"

AuthenticationSignatureKeySettingKey setting key for authentication_signature_key

View Source
const FileDeleted = "deleted"

FileDeleted when a file has been deleted from the data backend

View Source
const FileMissing = "missing"

FileMissing when a file is waiting to be uploaded

View Source
const FileRemoved = "removed"

FileRemoved when a file has been removed and can't be downloaded anymore but has not yet been deleted

View Source
const FileUploaded = "uploaded"

FileUploaded when a file has been uploaded and is ready to be downloaded

View Source
const FileUploading = "uploading"

FileUploading when a file is being uploaded

View Source
const ProviderGoogle = "google"

ProviderGoogle for authentication

View Source
const ProviderLocal = "local"

ProviderLocal for authentication

View Source
const ProviderOVH = "ovh"

ProviderOVH for authentication

Variables

View Source
var APIMockServerDefaultPort = 44142

APIMockServerDefaultPort is the default port to use for testing HTTP server

View Source
var DummyHandler = http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {})

DummyHandler is a dummy http.Handler

Functions

func AskConfirmation

func AskConfirmation(defaultValue bool) (bool, error)

AskConfirmation from process input

func CheckHTTPServer

func CheckHTTPServer(port int) (err error)

CheckHTTPServer for HTTP Server to be UP and running HTTP Server should must respond 404 to URL/not_found to be considered ok

func CheckPasswordHash

func CheckPasswordHash(password, hash string) bool

CheckPasswordHash check password against bcrypt password hash

func EncodeAuthBasicHeader

func EncodeAuthBasicHeader(login string, password string) (value string)

EncodeAuthBasicHeader return the base64 version of "login:password"

func GenerateRandomID

func GenerateRandomID(length int) string

GenerateRandomID generates a random string with specified length. Used to generate upload id, tokens, ...

func GetUserID

func GetUserID(provider string, providerID string) string

GetUserID return user ID from provider and login

func HashPassword

func HashPassword(password string) (string, error)

HashPassword return bcrypt password hash ( with salt )

func IsValidProvider

func IsValidProvider(provider string) bool

IsValidProvider return true if the provider string is valid

func Logout

func Logout(resp http.ResponseWriter, sa *SessionAuthenticator)

Logout delete session cookies

func MarshalUpload

func MarshalUpload(upload *Upload, version int) (bytes []byte, err error)

MarshalUpload unmarshal upload if version is (1) marshal using UploadV1 format

func RequireError

func RequireError(t *testing.T, err error, message string)

RequireError is a helper to test the error and it's message

func StartAPIMockServer

func StartAPIMockServer(next http.Handler) (shutdown func(), err error)

StartAPIMockServer starts a new temporary API Server to be used in tests

func StartAPIMockServerCustomPort

func StartAPIMockServerCustomPort(port int, next http.Handler) (shutdown func(), err error)

StartAPIMockServerCustomPort starts a new temporary API Server using a custom port Adds a middleware that handle the /not_found path called by the CheckHTTPServer function

func StripPrefix

func StripPrefix(prefix string, handler http.Handler) http.Handler

StripPrefix returns a handler that serves HTTP requests removing the given prefix from the request URL's Path It differs from http.StripPrefix by defaulting to "/" and not ""

func TestTimeout

func TestTimeout(f func(), duration time.Duration) (err error)

TestTimeout execute a function and return an error if the defined timeout happen before

func UnmarshalUpload

func UnmarshalUpload(bytes []byte, upload *Upload) (version int, err error)

UnmarshalUpload unmarshal upload, if that fails try again with UploadV1 format with files in a map instead of an array

func WriteJSONResponse

func WriteJSONResponse(resp http.ResponseWriter, obj interface{})

WriteJSONResponse serialize the response to json and write it to the HTTP response body

Types

type Configuration

type Configuration struct {
	Debug         bool   `json:"-"`
	DebugRequests bool   `json:"-"`
	LogLevel      string `json:"-"` // DEPRECATED since 1.3

	ListenAddress string `json:"-"`
	ListenPort    int    `json:"-"`
	Path          string `json:"-"`

	MaxFileSize      int64 `json:"maxFileSize"`
	MaxFilePerUpload int   `json:"maxFilePerUpload"`

	DefaultTTL int `json:"defaultTTL"`
	MaxTTL     int `json:"maxTTL"`

	SslEnabled bool   `json:"-"`
	SslCert    string `json:"-"`
	SslKey     string `json:"-"`

	NoWebInterface      bool   `json:"-"`
	DownloadDomain      string `json:"downloadDomain"`
	EnhancedWebSecurity bool   `json:"-"`

	SourceIPHeader  string   `json:"-"`
	UploadWhitelist []string `json:"-"`

	Authentication           bool     `json:"authentication"`
	NoAnonymousUploads       bool     `json:"noAnonymousUploads"`
	OneShot                  bool     `json:"oneShot"`
	Removable                bool     `json:"removable"`
	Stream                   bool     `json:"stream"`
	ProtectedByPassword      bool     `json:"protectedByPassword"`
	ProtectedByPasswordForce bool     `json:"protectedByPasswordForce"`
	GoogleAuthentication     bool     `json:"googleAuthentication"`
	GoogleAPISecret          string   `json:"-"`
	GoogleAPIClientID        string   `json:"-"`
	GoogleValidDomains       []string `json:"-"`
	OvhAuthentication        bool     `json:"ovhAuthentication"`
	OvhAPIEndpoint           string   `json:"ovhApiEndpoint"`
	OvhAPIKey                string   `json:"-"`
	OvhAPISecret             string   `json:"-"`

	MetadataBackendConfig map[string]interface{} `json:"-"`

	DataBackend       string                 `json:"-"`
	DataBackendConfig map[string]interface{} `json:"-"`
	// contains filtered or unexported fields
}

Configuration object

func LoadConfiguration

func LoadConfiguration(file string) (config *Configuration, err error)

LoadConfiguration creates a new empty configuration and try to load specified file with toml library to override default params

func NewConfiguration

func NewConfiguration() (config *Configuration)

NewConfiguration creates a new configuration object with default values

func (*Configuration) AutoClean

func (config *Configuration) AutoClean(value bool)

AutoClean enable or disables the periodical upload cleaning goroutine. This needs to be called before Plik server starts to have effect

func (*Configuration) GetDownloadDomain

func (config *Configuration) GetDownloadDomain() *url.URL

GetDownloadDomain return the parsed download domain URL

func (*Configuration) GetServerURL

func (config *Configuration) GetServerURL() *url.URL

GetServerURL is a helper to get the server HTTP URL

func (*Configuration) GetUploadWhitelist

func (config *Configuration) GetUploadWhitelist() []*net.IPNet

GetUploadWhitelist return the parsed IP upload whitelist

func (*Configuration) Initialize

func (config *Configuration) Initialize() (err error)

Initialize config internal parameters

func (*Configuration) IsAutoClean

func (config *Configuration) IsAutoClean() bool

IsAutoClean return weather or not to start the cleaning goroutine

func (*Configuration) IsWhitelisted

func (config *Configuration) IsWhitelisted(ip net.IP) bool

IsWhitelisted return weather or not the IP matches of the config upload whitelist

func (*Configuration) NewLogger

func (config *Configuration) NewLogger() (log *logger.Logger)

NewLogger returns a new logger instance

type ErrorReader

type ErrorReader struct {
	// contains filtered or unexported fields
}

ErrorReader impement io.Reader and return err for every read call attempted

func NewErrorReader

func NewErrorReader(err error) (reader *ErrorReader)

NewErrorReader return a new ErrorReader

func NewErrorReaderString

func NewErrorReaderString(err string) (reader *ErrorReader)

NewErrorReaderString return a new ErrorReader from the provided string

func (*ErrorReader) Read

func (reader *ErrorReader) Read(p []byte) (n int, err error)

Read method to implement io.Reader

type File

type File struct {
	ID       string `json:"id"`
	UploadID string `json:"-"  gorm:"type:varchar(255) REFERENCES uploads(id) ON UPDATE RESTRICT ON DELETE RESTRICT"`
	Name     string `json:"fileName"`

	Status string `json:"status"`

	Md5       string `json:"fileMd5"`
	Type      string `json:"fileType"`
	Size      int64  `json:"fileSize"`
	Reference string `json:"reference"`

	BackendDetails string `json:"-"`

	CreatedAt time.Time `json:"createdAt"`
}

File object

func NewFile

func NewFile() (file *File)

NewFile instantiate a new object and generate a random id

func (*File) GenerateID

func (file *File) GenerateID()

GenerateID generate a new File ID

func (*File) PrepareInsert

func (file *File) PrepareInsert(upload *Upload) (err error)

PrepareInsert prepares a new file object to be persisted in DB ( create file ID, link upload ID, check name, ... )

func (*File) Sanitize

func (file *File) Sanitize()

Sanitize removes sensible information from object. Used to hide information in API.

type HTTPError

type HTTPError struct {
	Message    string
	Err        error
	StatusCode int
}

HTTPError allows to return an error and a HTTP status code

func NewHTTPError

func NewHTTPError(message string, err error, code int) HTTPError

NewHTTPError return a new HTTPError

func (HTTPError) Error

func (e HTTPError) Error() string

Error return the error

func (HTTPError) String

func (e HTTPError) String() string

type PagingQuery

type PagingQuery struct {
	Before *string `json:"before"`
	After  *string `json:"after"`
	Limit  *int    `json:"limit"`
	Order  *string `json:"order"`
}

PagingQuery for the paging system

func NewPagingQuery

func NewPagingQuery() (pq *PagingQuery)

NewPagingQuery return a new paging query

func (*PagingQuery) Paginator

func (pq *PagingQuery) Paginator() *paginator.Paginator

Paginator return a new Paginator for the PagingQuery

func (*PagingQuery) WithAfterCursor

func (pq *PagingQuery) WithAfterCursor(cursor string) *PagingQuery

WithAfterCursor set the paging query after cursor and unset the before cursor

func (*PagingQuery) WithBeforeCursor

func (pq *PagingQuery) WithBeforeCursor(cursor string) *PagingQuery

WithBeforeCursor set the paging query before cursor and unset the after cursor

func (*PagingQuery) WithLimit

func (pq *PagingQuery) WithLimit(limit int) *PagingQuery

WithLimit set the paging query limit if limit is a valid positive integer

func (*PagingQuery) WithOrder

func (pq *PagingQuery) WithOrder(order string) *PagingQuery

WithOrder set the paging query order if oder is a valid order string "asc" or "desc"

type PagingResponse

type PagingResponse struct {
	After   *string       `json:"after"`
	Before  *string       `json:"before"`
	Results []interface{} `json:"results"`
}

PagingResponse for the paging system

func NewPagingResponse

func NewPagingResponse(results interface{}, cursor *paginator.Cursor) (pr *PagingResponse)

NewPagingResponse create a new PagingResponse from query results ( results must be a slice )

type ServerStats

type ServerStats struct {
	Users            int   `json:"users"`
	Uploads          int   `json:"uploads"`
	AnonymousUploads int   `json:"anonymousUploads"`
	Files            int   `json:"files"`
	TotalSize        int64 `json:"totalSize"`
	AnonymousSize    int64 `json:"anonymousTotalSize"`
}

ServerStats server statistics

type SessionAuthenticator

type SessionAuthenticator struct {
	SignatureKey  string
	SecureCookies bool
}

SessionAuthenticator to generate and authenticate session cookies

func (*SessionAuthenticator) GenAuthCookies

func (sa *SessionAuthenticator) GenAuthCookies(user *User) (sessionCookie *http.Cookie, xsrfCookie *http.Cookie, err error)

GenAuthCookies generate a sign a jwt session cookie to authenticate a user

func (*SessionAuthenticator) Logout

func (sa *SessionAuthenticator) Logout() (sessionCookie *http.Cookie, xsrfCookie *http.Cookie, err error)

Logout generate logout session cookies

func (*SessionAuthenticator) ParseSessionCookie

func (sa *SessionAuthenticator) ParseSessionCookie(value string) (uid string, xsrf string, err error)

ParseSessionCookie parse and validate the session cookie

type Setting

type Setting struct {
	Key   string `gorm:"primary_key"`
	Value string
}

Setting is a config object meant to be shard by all Plik instances using the metadata backend

func GenerateAuthenticationSignatureKey

func GenerateAuthenticationSignatureKey() (s *Setting)

GenerateAuthenticationSignatureKey create an new random key

type Token

type Token struct {
	Token   string `json:"token" gorm:"primary_key"`
	Comment string `json:"comment,omitempty"`

	UserID string `json:"-" gorm:"type:varchar(255) REFERENCES users(id) ON UPDATE RESTRICT ON DELETE CASCADE"`

	CreatedAt time.Time `json:"createdAt"`
}

Token provide a very basic authentication mechanism

func NewToken

func NewToken() (t *Token)

NewToken create a new Token instance

func (*Token) Initialize

func (t *Token) Initialize()

Initialize generate the token uuid and sets the creation date

type Upload

type Upload struct {
	ID  string `json:"id"`
	TTL int    `json:"ttl"`

	DownloadDomain string `json:"downloadDomain"`
	RemoteIP       string `json:"uploadIp,omitempty"`
	Comments       string `json:"comments"`

	Files []*File `json:"files"`

	UploadToken string `json:"uploadToken,omitempty"`
	User        string `json:"user,omitempty" gorm:"index:idx_upload_user"`
	Token       string `json:"token,omitempty" gorm:"index:idx_upload_user_token"`
	IsAdmin     bool   `json:"admin"`

	Stream    bool `json:"stream"`
	OneShot   bool `json:"oneShot"`
	Removable bool `json:"removable"`

	ProtectedByPassword bool   `json:"protectedByPassword"`
	Login               string `json:"login,omitempty"`
	Password            string `json:"password,omitempty"`

	CreatedAt time.Time  `json:"createdAt"`
	DeletedAt *time.Time `json:"-" gorm:"index:idx_upload_deleted_at"`
	ExpireAt  *time.Time `json:"expireAt" gorm:"index:idx_upload_expire_at"`
}

Upload object

func (*Upload) GetFile

func (upload *Upload) GetFile(ID string) (file *File)

GetFile get file with ID from upload files. Return nil if not found

func (*Upload) GetFileByReference

func (upload *Upload) GetFileByReference(ref string) (file *File)

GetFileByReference get file with Reference from upload files. Return nil if not found

func (*Upload) IsExpired

func (upload *Upload) IsExpired() bool

IsExpired check if the upload is expired

func (*Upload) NewFile

func (upload *Upload) NewFile() (file *File)

NewFile creates a new file and add it to the current upload

func (*Upload) PrepareInsert

func (upload *Upload) PrepareInsert(config *Configuration) (err error)

PrepareInsert upload for database insert ( check configuration and default values, generate upload and file IDs, ... )

func (*Upload) PrepareInsertForTests

func (upload *Upload) PrepareInsertForTests()

PrepareInsertForTests upload for database insert without config checks and override for testing purpose

func (*Upload) Sanitize

func (upload *Upload) Sanitize()

Sanitize removes sensible information from object. Used to hide information in API.

type UploadV1

type UploadV1 struct {
	ID       string `json:"id"`
	Creation int64  `json:"uploadDate"`
	TTL      int    `json:"ttl"`

	DownloadDomain string `json:"downloadDomain"`
	RemoteIP       string `json:"uploadIp,omitempty"`
	Comments       string `json:"comments"`

	Files map[string]*File `json:"files"`

	UploadToken string `json:"uploadToken,omitempty"`
	User        string `json:"user,omitempty"`
	Token       string `json:"token,omitempty"`
	IsAdmin     bool   `json:"admin"`

	Stream    bool `json:"stream"`
	OneShot   bool `json:"oneShot"`
	Removable bool `json:"removable"`

	ProtectedByPassword bool   `json:"protectedByPassword"`
	Login               string `json:"login,omitempty"`
	Password            string `json:"password,omitempty"`

	ProtectedByYubikey bool   `json:"protectedByYubikey"`
	Yubikey            string `json:"yubikey,omitempty"`
}

UploadV1 upload object compatible with Plik <1.3

type User

type User struct {
	ID       string `json:"id,omitempty"`
	Provider string `json:"provider"`
	Login    string `json:"login,omitempty"`
	Password string `json:"-"`
	Name     string `json:"name,omitempty"`
	Email    string `json:"email,omitempty"`
	IsAdmin  bool   `json:"admin"`

	Tokens []*Token `json:"tokens,omitempty"`

	CreatedAt time.Time `json:"createdAt"`
}

User is a plik user

func NewUser

func NewUser(provider string, providerID string) (user *User)

NewUser create a new user object

func (*User) NewToken

func (user *User) NewToken() (token *Token)

NewToken add a new token to a user

func (*User) String

func (user *User) String() string

NewToken add a new token to a user

type UserStats

type UserStats struct {
	Uploads   int   `json:"uploads"`
	Files     int   `json:"files"`
	TotalSize int64 `json:"totalSize"`
}

UserStats user statistics

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL