2021-02-26 07:15:45 +05:30
|
|
|
package di
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/pem"
|
|
|
|
"errors"
|
|
|
|
"strings"
|
|
|
|
|
2024-02-01 12:28:26 +05:30
|
|
|
"github.com/elyby/chrly/internal/http"
|
|
|
|
. "github.com/elyby/chrly/internal/signer"
|
|
|
|
|
2023-12-13 21:59:12 +05:30
|
|
|
"github.com/defval/di"
|
2021-02-26 07:15:45 +05:30
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
var signer = di.Options(
|
|
|
|
di.Provide(newTexturesSigner,
|
|
|
|
di.As(new(http.TexturesSigner)),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
func newTexturesSigner(config *viper.Viper) (*Signer, error) {
|
2021-02-27 07:07:59 +05:30
|
|
|
keyStr := config.GetString("chrly.signing.key")
|
2021-02-26 07:15:45 +05:30
|
|
|
if keyStr == "" {
|
2021-02-27 07:07:59 +05:30
|
|
|
return nil, errors.New("chrly.signing.key must be set in order to sign textures")
|
2021-02-26 07:15:45 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
var keyBytes []byte
|
|
|
|
if strings.HasPrefix(keyStr, "base64:") {
|
|
|
|
base64Value := keyStr[7:]
|
|
|
|
decodedKey, err := base64.URLEncoding.DecodeString(base64Value)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
keyBytes = decodedKey
|
|
|
|
} else {
|
|
|
|
keyBytes = []byte(keyStr)
|
|
|
|
}
|
|
|
|
|
|
|
|
rawPem, _ := pem.Decode(keyBytes)
|
|
|
|
key, err := x509.ParsePKCS1PrivateKey(rawPem.Bytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Signer{Key: key}, nil
|
|
|
|
}
|