Skip to content

Commit 79f8584

Browse files
committed
refactor: improvements;
1 parent b09e923 commit 79f8584

File tree

7 files changed

+273
-226
lines changed

7 files changed

+273
-226
lines changed

cmd/yeahapi/main.go

+20-9
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func main() {
4949
}
5050

5151
type Main struct {
52-
Config Config
52+
Config *Config
5353
ConfigPath string
5454
Pool *pgxpool.Pool
5555
Server *http.Server
@@ -61,14 +61,14 @@ const (
6161

6262
func NewMain() *Main {
6363
return &Main{
64-
Config: Config{},
64+
Config: &Config{},
6565
Server: http.NewServer(),
6666
ConfigPath: defaultConfigPath,
6767
}
6868
}
6969

7070
func (m *Main) Run(ctx context.Context) (err error) {
71-
if m.Pool, err = pgxpool.New(ctx, m.Config.DB.DSN); err != nil {
71+
if m.Pool, err = pgxpool.New(ctx, m.Config.DB.Postgres); err != nil {
7272
return err
7373
}
7474

@@ -79,7 +79,8 @@ func (m *Main) Run(ctx context.Context) (err error) {
7979
Threads: 4,
8080
KeyLen: 32,
8181
})
82-
highwayHasher := inmem.NewHighwayHasher("some-key")
82+
83+
highwayHasher := inmem.NewHighwayHasher(m.Config.HighwayHash.Key)
8384

8485
authService := postgres.NewAuthService(m.Pool)
8586
authService.ArgonHasher = argonHasher
@@ -93,6 +94,8 @@ func (m *Main) Run(ctx context.Context) (err error) {
9394
// Streams: map[string][]string{},
9495
// })
9596

97+
m.Server.Addr = m.Config.HTTP.Addr
98+
9699
m.Server.UserService = userService
97100
m.Server.AuthService = authService
98101
m.Server.LocalizerService = localizerService
@@ -107,6 +110,9 @@ func (m *Main) Run(ctx context.Context) (err error) {
107110

108111
func (m *Main) Close() error {
109112
if m.Server != nil {
113+
if err := m.Server.Close(); err != nil {
114+
return err
115+
}
110116
}
111117

112118
if m.Pool != nil {
@@ -144,7 +150,7 @@ func (m *Main) ParseFlags(ctx context.Context, args []string) error {
144150

145151
type Config struct {
146152
DB struct {
147-
DSN string `toml:"dsn"`
153+
Postgres string `toml:"postgres"`
148154
} `toml:"db"`
149155

150156
HTTP struct {
@@ -156,6 +162,10 @@ type Config struct {
156162
Key string `toml:"key"`
157163
} `toml:"aws"`
158164

165+
HighwayHash struct {
166+
Key string `toml:"key"`
167+
} `toml:"highwayhash"`
168+
159169
Nats struct {
160170
AuthToken string `toml:"auth-token"`
161171
URL string `toml:"url"`
@@ -169,14 +179,15 @@ type Config struct {
169179
} `toml:"google"`
170180
}
171181

172-
func ReadConfigFile(filename string) (Config, error) {
182+
func ReadConfigFile(filename string) (*Config, error) {
173183
var config Config
174184
if buf, err := os.ReadFile(filename); err != nil {
175-
return config, err
185+
return &config, err
176186
} else if err := toml.Unmarshal(buf, &config); err != nil {
177-
return config, err
187+
return &config, err
178188
}
179-
return config, nil
189+
190+
return &config, nil
180191
}
181192

182193
func expand(path string) (string, error) {

credential.go

+19-12
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ import (
66
"crypto/ecdsa"
77
"crypto/rsa"
88
"crypto/sha256"
9+
"crypto/sha512"
910
"crypto/x509"
1011
"encoding/asn1"
1112
"encoding/base64"
13+
"hash"
1214
"math/big"
1315

1416
"github.com/gofrs/uuid"
@@ -220,7 +222,7 @@ type PubKeyCredentialRequestOpts struct {
220222
func (c *PubKeyCredential) Verify(clientData []byte, authnData []byte, sig string) error {
221223
sigbytes, err := base64.RawURLEncoding.DecodeString(sig)
222224
if err != nil {
223-
return errors.Internal
225+
return E(EInternal, "unable to decode signature")
224226
}
225227

226228
clientDataHash := sha256.Sum256(clientData)
@@ -231,27 +233,33 @@ func (c *PubKeyCredential) Verify(clientData []byte, authnData []byte, sig strin
231233
return c.verifySignature(message, sigbytes)
232234
}
233235

236+
var hashers = map[COSEAlgorithmIdentifier]func() hash.Hash{
237+
COSEAlgES256: sha256.New,
238+
COSEAlgEdDSA: sha512.New,
239+
COSEAlgRS256: sha256.New,
240+
}
241+
234242
func (c *PubKeyCredential) verifySignature(message []byte, sig []byte) error {
235243
bytes, err := base64.RawURLEncoding.DecodeString(c.PubKey)
236244
if err != nil {
237-
return errors.NewInternal(l.T("Couldn't decode pubkey"))
245+
return E(EInvalid, "unable to decode pubkey")
238246
}
239247

240248
parsed, err := x509.ParsePKIXPublicKey(bytes)
241249

242250
if err != nil {
243-
return errors.NewInternal(l.T("Unable to parse pubkey"))
251+
return E(EInvalid, "unable to parse pubkey")
244252
}
245253

246254
hasher := hashers[COSEAlgorithmIdentifier(c.PubKeyAlg)]
247255
if hasher == nil {
248-
return errors.NewInternal(l.T("Unsupported hashing algorithm"))
256+
return E(EInvalid, "unsupported hashing algorithm")
249257
}
250258

251259
h := hasher()
252260
_, err = h.Write(message)
253261
if err != nil {
254-
return errors.NewInternal(l.T("Couldn't hash the data"))
262+
return E(EInternal, "unable to hash the data")
255263
}
256264

257265
digest := h.Sum(nil)
@@ -263,23 +271,22 @@ func (c *PubKeyCredential) verifySignature(message []byte, sig []byte) error {
263271
}
264272
var ecdsaSig ecdsaSignature
265273
if rest, err := asn1.Unmarshal(sig, &ecdsaSig); err != nil {
266-
return errors.Internal
274+
return E(EInternal)
267275
} else if len(rest) != 0 {
268-
return errors.NewBadRequest(l.T("Trailing data after ECDSA signature"))
276+
return E(EInvalid, "trailing data after ECDSA signature")
269277
}
270278
if ecdsaSig.R.Sign() <= 0 || ecdsaSig.S.Sign() <= 0 {
271-
return errors.NewBadRequest(l.T("ECDSA signature contained zero or negative values"))
279+
return E(EInvalid, "ECDSA signature contained zero or negative values")
272280
}
273281
if !ecdsa.Verify(pk, digest, ecdsaSig.R, ecdsaSig.S) {
274-
return errors.NewBadRequest(l.T("ECDSA signature verification failed"))
282+
return E(EInvalid, "ECDSA signature verification failed")
275283
}
276284
case *rsa.PublicKey:
277285
if err := rsa.VerifyPKCS1v15(pk, crypto.SHA256, digest, sig); err != nil {
278-
return errors.NewBadRequest(l.T("RSA signature verification failed"))
286+
return E(EInvalid, "RSA signature verification failed")
279287
}
280288
default:
281-
return errors.NewInternal("Unsupported key type")
282-
289+
return E(EInternal, "unsupported key type")
283290
}
284291

285292
return nil

0 commit comments

Comments
 (0)