|
| 1 | +package authorization |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/go-crypt/crypt" |
| 8 | + "github.com/go-crypt/crypt/algorithm" |
| 9 | + influxdb2_algo "github.com/influxdata/influxdb/v2/pkg/crypt/algorithm/influxdb2" |
| 10 | +) |
| 11 | + |
| 12 | +var ( |
| 13 | + ErrNoDecoders = errors.New("no authorization decoders specified") |
| 14 | +) |
| 15 | + |
| 16 | +type AuthorizationHasher struct { |
| 17 | + // hasher encodes tokens into hashed PHC-encoded tokens. |
| 18 | + hasher algorithm.Hash |
| 19 | + |
| 20 | + // decoder decodes hashed PHC-encoded tokens into crypt.Digest objects. |
| 21 | + decoder *crypt.Decoder |
| 22 | + |
| 23 | + // allHashers is the list of all hashers which could be used for hashed index lookup. |
| 24 | + allHashers []algorithm.Hash |
| 25 | +} |
| 26 | + |
| 27 | +const ( |
| 28 | + DefaultHashVariant = influxdb2_algo.VariantSHA256 |
| 29 | + DefaultHashVariantName = influxdb2_algo.VariantIdentifierSHA256 |
| 30 | + |
| 31 | + // HashVariantNameUnknown is the placeholder name used for unknown or unsupported hash variants. |
| 32 | + HashVariantNameUnknown = "N/A" |
| 33 | +) |
| 34 | + |
| 35 | +type authorizationHasherOptions struct { |
| 36 | + hasherVariant influxdb2_algo.Variant |
| 37 | + decoderVariants []influxdb2_algo.Variant |
| 38 | +} |
| 39 | + |
| 40 | +type AuthorizationHasherOption func(o *authorizationHasherOptions) |
| 41 | + |
| 42 | +func WithHasherVariant(variant influxdb2_algo.Variant) AuthorizationHasherOption { |
| 43 | + return func(o *authorizationHasherOptions) { |
| 44 | + o.hasherVariant = variant |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func WithDecoderVariants(variants []influxdb2_algo.Variant) AuthorizationHasherOption { |
| 49 | + return func(o *authorizationHasherOptions) { |
| 50 | + o.decoderVariants = variants |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +// NewAuthorizationHasher creates an AuthorizationHasher for influxdb2 algorithm hashed tokens. |
| 55 | +// variantName specifies which token hashing variant to use, with blank indicating to use the default |
| 56 | +// hashing variant. By default, all variants of the influxdb2 hashing scheme are supported for |
| 57 | +// maximal compatibility. |
| 58 | +func NewAuthorizationHasher(opts ...AuthorizationHasherOption) (*AuthorizationHasher, error) { |
| 59 | + options := authorizationHasherOptions{ |
| 60 | + hasherVariant: DefaultHashVariant, |
| 61 | + decoderVariants: influxdb2_algo.AllVariants, |
| 62 | + } |
| 63 | + |
| 64 | + for _, o := range opts { |
| 65 | + o(&options) |
| 66 | + } |
| 67 | + |
| 68 | + if len(options.decoderVariants) == 0 { |
| 69 | + return nil, fmt.Errorf("error in NewAuthorizationHasher: %w", ErrNoDecoders) |
| 70 | + } |
| 71 | + |
| 72 | + // Create the hasher used for hashing new tokens before storage. |
| 73 | + hasher, err := influxdb2_algo.New(influxdb2_algo.WithVariant(options.hasherVariant)) |
| 74 | + if err != nil { |
| 75 | + return nil, fmt.Errorf("creating hasher %s for AuthorizationHasher: %w", options.hasherVariant.Prefix(), err) |
| 76 | + } |
| 77 | + |
| 78 | + // Create decoder and register all requested decoder variants. |
| 79 | + decoder := crypt.NewDecoder() |
| 80 | + for _, variant := range options.decoderVariants { |
| 81 | + if err := variant.RegisterDecoder(decoder); err != nil { |
| 82 | + return nil, fmt.Errorf("registering variant %s with decoder: %w", variant.Prefix(), err) |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + // Create all variant hashers needed for requested decoder variants. This is required for operations where |
| 87 | + // all potential variations of a raw token must be hashed, such as looking up a hash in the hashed token index. |
| 88 | + var allHashers []algorithm.Hash |
| 89 | + for _, variant := range options.decoderVariants { |
| 90 | + h, err := influxdb2_algo.New(influxdb2_algo.WithVariant(variant)) |
| 91 | + if err != nil { |
| 92 | + return nil, fmt.Errorf("creating hasher %s for authorization service index lookups: %w", variant.Prefix(), err) |
| 93 | + } |
| 94 | + allHashers = append(allHashers, h) |
| 95 | + } |
| 96 | + |
| 97 | + return &AuthorizationHasher{ |
| 98 | + hasher: hasher, |
| 99 | + decoder: decoder, |
| 100 | + allHashers: allHashers, |
| 101 | + }, nil |
| 102 | +} |
| 103 | + |
| 104 | +// Hash generates a PHC-encoded hash of token using the selected hash algorithm variant. |
| 105 | +func (h *AuthorizationHasher) Hash(token string) (string, error) { |
| 106 | + digest, err := h.hasher.Hash(token) |
| 107 | + if err != nil { |
| 108 | + return "", fmt.Errorf("hashing raw token failed: %w", err) |
| 109 | + } |
| 110 | + return digest.Encode(), nil |
| 111 | +} |
| 112 | + |
| 113 | +// AllHashes generates a list of PHC-encoded hashes of token for all deterministic (i.e. non-salted) supported hashes. |
| 114 | +func (h *AuthorizationHasher) AllHashes(token string) ([]string, error) { |
| 115 | + hashes := make([]string, len(h.allHashers)) |
| 116 | + for idx, hasher := range h.allHashers { |
| 117 | + digest, err := hasher.Hash(token) |
| 118 | + if err != nil { |
| 119 | + variantName := HashVariantNameUnknown |
| 120 | + if influxdb_hasher, ok := hasher.(*influxdb2_algo.Hasher); ok { |
| 121 | + variantName = influxdb_hasher.Variant().Prefix() |
| 122 | + } |
| 123 | + return nil, fmt.Errorf("hashing raw token failed (variant=%s): %w", variantName, err) |
| 124 | + } |
| 125 | + hashes[idx] = digest.Encode() |
| 126 | + } |
| 127 | + return hashes, nil |
| 128 | +} |
| 129 | + |
| 130 | +// AllHashesCount returns the number of hash variants available through AllHashes. |
| 131 | +func (h *AuthorizationHasher) AllHashesCount() int { |
| 132 | + return len(h.allHashers) |
| 133 | +} |
| 134 | + |
| 135 | +// Decode decodes a PHC-encoded hash into a Digest object that can be matched. |
| 136 | +func (h *AuthorizationHasher) Decode(phc string) (algorithm.Digest, error) { |
| 137 | + return h.decoder.Decode(phc) |
| 138 | +} |
| 139 | + |
| 140 | +// Match determines if a raw token matches a PHC-encoded token. |
| 141 | +func (h *AuthorizationHasher) Match(phc string, token string) (bool, error) { |
| 142 | + digest, err := h.Decode(phc) |
| 143 | + if err != nil { |
| 144 | + return false, err |
| 145 | + } |
| 146 | + |
| 147 | + return digest.MatchAdvanced(token) |
| 148 | +} |
0 commit comments