diff --git a/src/middleware/auth.go b/src/middleware/auth.go index 348c6d9..8df4a5a 100644 --- a/src/middleware/auth.go +++ b/src/middleware/auth.go @@ -8,6 +8,7 @@ import ( "github.com/teambition/gear" "github.com/yiwen-ai/yiwen-api/src/conf" + "github.com/yiwen-ai/yiwen-api/src/logging" "github.com/yiwen-ai/yiwen-api/src/util" ) @@ -51,9 +52,12 @@ func (m AuthLevel) Auth(ctx *gear.Context) error { return gear.ErrUnauthorized.From(err) } + log := logging.FromCtx(ctx) + log["uid"] = sess.UserID if l > 1 && !sess.HasToken() { return gear.ErrUnauthorized.WithMsg("invalid token") } + log["aud"] = sess.AppID ctxHeader := make(http.Header) // inject auth headers into context for base service diff --git a/src/util/http.go b/src/util/http.go index a08c831..f5adb1c 100644 --- a/src/util/http.go +++ b/src/util/http.go @@ -165,12 +165,11 @@ func RequestCBOR(ctx context.Context, cli *http.Client, method, api string, inpu data, err := io.ReadAll(resp.Body) if resp.StatusCode > 206 || err != nil { + str, _ := cbor.Diagnose(data) return fmt.Errorf("RequestCBOR %q failed, code: %d, error: %v, body: %s", - api, resp.StatusCode, err, string(data)) + api, resp.StatusCode, err, str) } - // fmt.Printf("%x\n", data) - return cbor.Unmarshal(data, output) } diff --git a/src/util/id.go b/src/util/id.go index 0af51be..9574313 100644 --- a/src/util/id.go +++ b/src/util/id.go @@ -15,6 +15,10 @@ var ZeroID ID var JARVIS ID = mustParseID("0000000000000jarvis0") // system user var ANON ID = mustParseID("000000000000000anon0") // anonymous user +func NewID() ID { + return ID(xid.New()) +} + func ParseID(s string) (ID, error) { id, err := xid.FromString(s) if err != nil { @@ -33,6 +37,10 @@ func mustParseID(s string) ID { type ID xid.ID +func (id ID) Unwrap() xid.ID { + return xid.ID(id) +} + func (id *ID) String() string { if id == nil { return "" @@ -88,6 +96,14 @@ func (id *ID) UnmarshalText(data []byte) error { type UUID uuid.UUID +func NewUUID() UUID { + id, err := uuid.NewUUID() + if err != nil { + panic(err) + } + return UUID(id) +} + func (id *UUID) String() string { if id == nil { return "" @@ -96,6 +112,10 @@ func (id *UUID) String() string { return uuid.UUID(*id).String() } +func (id UUID) Unwrap() uuid.UUID { + return uuid.UUID(id) +} + func (id UUID) Base64() string { return base64.RawURLEncoding.EncodeToString(id[:]) }