File size: 1,013 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
package apikey
import (
"context"
"crypto/sha256"
"database/sql"
"encoding/json"
"errors"
"github.com/google/uuid"
"github.com/target/goalert/gadb"
)
type policyInfo struct {
Hash []byte
Policy GQLPolicy
}
func parsePolicyInfo(data []byte) (*policyInfo, error) {
var info policyInfo
err := json.Unmarshal(data, &info.Policy)
if err != nil {
return nil, err
}
// re-encode policy to get a consistent hash
data, err = json.Marshal(info.Policy)
if err != nil {
return nil, err
}
h := sha256.Sum256(data)
info.Hash = h[:]
return &info, nil
}
// _fetchPolicyInfo will fetch the policyInfo for the given key.
func (s *Store) _fetchPolicyInfo(ctx context.Context, id uuid.UUID) (*policyInfo, bool, error) {
polData, err := gadb.New(s.db).APIKeyAuthPolicy(ctx, id)
if errors.Is(err, sql.ErrNoRows) {
return nil, false, nil
}
if err != nil {
return nil, false, err
}
info, err := parsePolicyInfo(polData)
if err != nil {
return nil, false, err
}
return info, true, nil
}
|