feat(billing): egg switching on reinstall, admin user billing tab & token decrypt fixes902279f4 months ago54 lines · 1.1 KB | 1 | package crypto |
| 2 | |
| 3 | import ( |
| 4 | "crypto/aes" |
| 5 | "crypto/cipher" |
| 6 | "encoding/hex" |
| 7 | "fmt" |
| 8 | "os" |
| 9 | "strings" |
| 10 | ) |
| 11 | |
| 12 | func DecryptToken(ciphertext string |
| 13 | keyHex := os. |
| 14 | if keyHex == "" |
| 15 | return "", fmt. |
| 16 | } |
| 17 | key, err := |
| 18 | if err != nil |
| 19 | return "", fmt. |
| 20 | } |
| 21 | |
| 22 | parts := strings. |
| 23 | if len(parts) |
| 24 | return ciphertext, nil |
| 25 | } |
| 26 | iv, err := |
| 27 | if err != nil |
| 28 | return ciphertext, nil |
| 29 | } |
| 30 | tag, err := |
| 31 | if err != nil |
| 32 | return ciphertext, nil |
| 33 | } |
| 34 | enc, err := |
| 35 | if err != nil |
| 36 | return ciphertext, nil |
| 37 | } |
| 38 | |
| 39 | block, err := |
| 40 | if err != nil |
| 41 | return "", err |
| 42 | } |
| 43 | gcm, err := |
| 44 | if err != nil |
| 45 | return "", err |
| 46 | } |
| 47 | |
| 48 | combined := append( |
| 49 | plain, err := |
| 50 | if err != nil |
| 51 | return "", fmt. |
| 52 | } |
| 53 | return string(plain) |
| 54 | } |
)
(
string
,
error
)
{
Getenv
(
"
DATABASE_ENCRYPTION_KEY
"
)
{
Errorf
(
"
DATABASE_ENCRYPTION_KEY is not set
"
)
hex
.
DecodeString
(
keyHex
)
{
Errorf
(
"
invalid DATABASE_ENCRYPTION_KEY:
%w
"
,
err
)
SplitN
(
ciphertext
,
"
:
"
,
3
)
!=
3
{
//
not encrypted, return as-is
hex
.
DecodeString
(
parts
[
0
])
{
hex
.
DecodeString
(
parts
[
1
])
{
hex
.
DecodeString
(
parts
[
2
])
{
aes
.
NewCipher
(
key
)
{
cipher
.
NewGCM
(
block
)
{
enc
,
tag
...
)
gcm
.
Open
(
nil
,
iv
,
combined
,
nil
)
{
Errorf
(
"
decrypt failed:
%w
"
,
err
)
,
nil