github.com/safedep/dry@v0.0.0-20241016050132-a15651f0548b/apiguard/key.go (about)

     1  package apiguard
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/safedep/dry/crypto"
     8  )
     9  
    10  const (
    11  	defaultKeyPrefix = "sfd"
    12  	defaultKeyLength = 56
    13  )
    14  
    15  type KeyArgs struct {
    16  	Info      KeyInfo
    17  	Tags      []string
    18  	Alias     string
    19  	PolicyId  string
    20  	Policies  []string
    21  	ExpiresAt time.Time
    22  }
    23  
    24  type ApiKey struct {
    25  	Key       string
    26  	KeyId     string // API Guard specific key ID
    27  	ExpiresAt time.Time
    28  }
    29  
    30  type KeyGen func() (string, error)
    31  
    32  func defaultKeyGen() KeyGen {
    33  	return func() (string, error) {
    34  		str, err := crypto.RandomUrlSafeString(defaultKeyLength)
    35  		if err != nil {
    36  			return "", err
    37  		}
    38  
    39  		return fmt.Sprintf("%s_%s", defaultKeyPrefix, str), nil
    40  	}
    41  }