1
0
mirror of https://github.com/lus/pasty.git synced 2023-08-10 21:13:09 +03:00

Add option to define ID and modification token generation characters (#17)

This commit is contained in:
Lukas Schulte Pelkum 2021-07-24 17:00:29 +02:00
parent 7af1f9431b
commit e8f7fa239e
No known key found for this signature in database
GPG Key ID: 408DA7CA81DB885C
7 changed files with 47 additions and 44 deletions

View File

@ -73,14 +73,16 @@ Pasty will be available at http://localhost:8080.
## General environment variables ## General environment variables
| Environment Variable | Default Value | Type | Description | | Environment Variable | Default Value | Type | Description |
|-----------------------------------|---------------|----------|----------------------------------------------------------------------------------------------------------------------------| |---------------------------------------|------------------------------------------------------------------|----------|----------------------------------------------------------------------------------------------------------------------------|
| `PASTY_WEB_ADDRESS` | `:8080` | `string` | Defines the address the web server listens to | | `PASTY_WEB_ADDRESS` | `:8080` | `string` | Defines the address the web server listens to |
| `PASTY_STORAGE_TYPE` | `file` | `string` | Defines the storage type the pastes are saved to | | `PASTY_STORAGE_TYPE` | `file` | `string` | Defines the storage type the pastes are saved to |
| `PASTY_HASTEBIN_SUPPORT` | `false` | `bool` | Defines whether or not the `POST /documents` endpoint should be enabled, as known from the hastebin servers | | `PASTY_HASTEBIN_SUPPORT` | `false` | `bool` | Defines whether or not the `POST /documents` endpoint should be enabled, as known from the hastebin servers |
| `PASTY_ID_LENGTH` | `6` | `number` | Defines the length of the ID of a paste | | `PASTY_ID_LENGTH` | `6` | `number` | Defines the length of the ID of a paste |
| `PASTY_ID_CHARACTERS` | `abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789` | `string` | Defines the characters to use when generating paste IDs |
| `PASTY_MODIFICATION_TOKENS` | `true` | `bool` | Defines whether or not modification tokens should be generated | | `PASTY_MODIFICATION_TOKENS` | `true` | `bool` | Defines whether or not modification tokens should be generated |
| `PASTY_MODIFICATION_TOKEN_MASTER` | `<empty>` | `string` | Defines the master modification token which is authorized to modify every paste (even if modification tokens are disabled) | | `PASTY_MODIFICATION_TOKEN_MASTER` | `<empty>` | `string` | Defines the master modification token which is authorized to modify every paste (even if modification tokens are disabled) |
| `PASTY_MODIFICATION_TOKEN_LENGTH` | `12` | `number` | Defines the length of the modification token of a paste | | `PASTY_MODIFICATION_TOKEN_LENGTH` | `12` | `number` | Defines the length of the modification token of a paste |
| `PASTY_MODIFICATION_TOKEN_CHARACTERS` | `abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789` | `string` | Defines the characters to use when generating modification tokens |
| `PASTY_RATE_LIMIT` | `30-M` | `string` | Defines the rate limit of the API (see https://github.com/ulule/limiter#usage) | | `PASTY_RATE_LIMIT` | `30-M` | `string` | Defines the rate limit of the API (see https://github.com/ulule/limiter#usage) |
| `PASTY_LENGTH_CAP` | `50000` | `number` | Defines the maximum amount of characters a paste is allowed to contain (a value `<= 0` means no limit) | | `PASTY_LENGTH_CAP` | `50000` | `number` | Defines the maximum amount of characters a paste is allowed to contain (a value `<= 0` means no limit) |

View File

@ -14,9 +14,11 @@ type Config struct {
StorageType shared.StorageType StorageType shared.StorageType
HastebinSupport bool HastebinSupport bool
IDLength int IDLength int
IDCharacters string
ModificationTokens bool ModificationTokens bool
ModificationTokenMaster string ModificationTokenMaster string
ModificationTokenLength int ModificationTokenLength int
ModificationTokenCharacters string
RateLimit string RateLimit string
LengthCap int LengthCap int
AutoDelete *AutoDeleteConfig AutoDelete *AutoDeleteConfig
@ -81,9 +83,11 @@ func Load() {
StorageType: shared.StorageType(strings.ToLower(env.MustString("STORAGE_TYPE", "file"))), StorageType: shared.StorageType(strings.ToLower(env.MustString("STORAGE_TYPE", "file"))),
HastebinSupport: env.MustBool("HASTEBIN_SUPPORT", false), HastebinSupport: env.MustBool("HASTEBIN_SUPPORT", false),
IDLength: env.MustInt("ID_LENGTH", 6), IDLength: env.MustInt("ID_LENGTH", 6),
IDCharacters: env.MustString("ID_CHARACTERS", "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"),
ModificationTokens: env.MustBool("MODIFICATION_TOKENS", env.MustBool("DELETION_TOKENS", true)), // --- ModificationTokens: env.MustBool("MODIFICATION_TOKENS", env.MustBool("DELETION_TOKENS", true)), // ---
ModificationTokenMaster: env.MustString("MODIFICATION_TOKEN_MASTER", env.MustString("DELETION_TOKEN_MASTER", "")), // - We don't want to destroy peoples old configuration ModificationTokenMaster: env.MustString("MODIFICATION_TOKEN_MASTER", env.MustString("DELETION_TOKEN_MASTER", "")), // - We don't want to destroy peoples old configuration
ModificationTokenLength: env.MustInt("MODIFICATION_TOKEN_LENGTH", env.MustInt("DELETION_TOKEN_LENGTH", 12)), // --- ModificationTokenLength: env.MustInt("MODIFICATION_TOKEN_LENGTH", env.MustInt("DELETION_TOKEN_LENGTH", 12)), // ---
ModificationTokenCharacters: env.MustString("MODIFICATION_TOKEN_CHARACTERS", "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"),
RateLimit: env.MustString("RATE_LIMIT", "30-M"), RateLimit: env.MustString("RATE_LIMIT", "30-M"),
LengthCap: env.MustInt("LENGTH_CAP", 50_000), LengthCap: env.MustInt("LENGTH_CAP", 50_000),
AutoDelete: &AutoDeleteConfig{ AutoDelete: &AutoDeleteConfig{

View File

@ -8,7 +8,7 @@ import (
// AcquireID generates a new unique ID // AcquireID generates a new unique ID
func AcquireID() (string, error) { func AcquireID() (string, error) {
for { for {
id := utils.RandomString(config.Current.IDLength) id := utils.RandomString(config.Current.IDCharacters, config.Current.IDLength)
paste, err := Current.Get(id) paste, err := Current.Get(id)
if err != nil { if err != nil {
return "", err return "", err

View File

@ -5,15 +5,12 @@ import (
"time" "time"
) )
// stringContents holds the chars a random string can contain
const stringContents = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
// RandomString returns a random string with the given length // RandomString returns a random string with the given length
func RandomString(length int) string { func RandomString(characters string, length int) string {
rand.Seed(time.Now().UnixNano()) rand.Seed(time.Now().UnixNano())
bytes := make([]byte, length) bytes := make([]byte, length)
for i := range bytes { for i := range bytes {
bytes[i] = stringContents[rand.Int63()%int64(len(stringContents))] bytes[i] = characters[rand.Int63()%int64(len(characters))]
} }
return string(bytes) return string(bytes)
} }

View File

@ -52,7 +52,7 @@ func HastebinSupportHandler(ctx *fasthttp.RequestCtx) {
// Set a modification token // Set a modification token
if config.Current.ModificationTokens { if config.Current.ModificationTokens {
paste.ModificationToken = utils.RandomString(config.Current.ModificationTokenLength) paste.ModificationToken = utils.RandomString(config.Current.ModificationTokenCharacters, config.Current.ModificationTokenLength)
err = paste.HashModificationToken() err = paste.HashModificationToken()
if err != nil { if err != nil {

View File

@ -95,7 +95,7 @@ func v1PostPaste(ctx *fasthttp.RequestCtx) {
// Set a modification token // Set a modification token
modificationToken := "" modificationToken := ""
if config.Current.ModificationTokens { if config.Current.ModificationTokens {
modificationToken = utils.RandomString(config.Current.ModificationTokenLength) modificationToken = utils.RandomString(config.Current.ModificationTokenCharacters, config.Current.ModificationTokenLength)
paste.ModificationToken = modificationToken paste.ModificationToken = modificationToken
err = paste.HashModificationToken() err = paste.HashModificationToken()

View File

@ -146,7 +146,7 @@ func endpointCreatePaste(ctx *fasthttp.RequestCtx) {
// Create a new modification token if enabled // Create a new modification token if enabled
modificationToken := "" modificationToken := ""
if config.Current.ModificationTokens { if config.Current.ModificationTokens {
modificationToken = utils.RandomString(config.Current.ModificationTokenLength) modificationToken = utils.RandomString(config.Current.ModificationTokenCharacters, config.Current.ModificationTokenLength)
paste.ModificationToken = modificationToken paste.ModificationToken = modificationToken
err = paste.HashModificationToken() err = paste.HashModificationToken()