github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/cmd/srv-applet-mgr/apis/login/put.go (about) 1 package login 2 3 import ( 4 "context" 5 "time" 6 7 "github.com/machinefi/w3bstream/pkg/depends/base/types" 8 "github.com/machinefi/w3bstream/pkg/depends/conf/jwt" 9 "github.com/machinefi/w3bstream/pkg/depends/kit/httptransport/httpx" 10 "github.com/machinefi/w3bstream/pkg/errors/status" 11 "github.com/machinefi/w3bstream/pkg/models" 12 "github.com/machinefi/w3bstream/pkg/modules/account" 13 ) 14 15 type LoginByUsername struct { 16 httpx.MethodPut 17 account.LoginByUsernameReq `in:"body"` 18 } 19 20 func (r *LoginByUsername) Path() string { return "" } 21 22 func (r *LoginByUsername) Output(ctx context.Context) (interface{}, error) { 23 ac, err := account.ValidateLoginByUsername(ctx, &r.LoginByUsernameReq) 24 if err != nil { 25 return nil, err 26 } 27 return token(ctx, ac) 28 } 29 30 type LoginByEthAddress struct { 31 httpx.MethodPut 32 account.LoginByEthAddressReq `in:"body"` 33 } 34 35 func (r *LoginByEthAddress) Path() string { return "/wallet" } 36 37 func (r *LoginByEthAddress) Output(ctx context.Context) (interface{}, error) { 38 ac, err := account.ValidateLoginByEthAddress(ctx, &r.LoginByEthAddressReq) 39 if err != nil { 40 return nil, err 41 } 42 return token(ctx, ac) 43 } 44 45 func token(ctx context.Context, a *models.Account) (*account.LoginRsp, error) { 46 j := jwt.MustConfFromContext(ctx) 47 48 tok, err := j.GenerateTokenByPayload(a.AccountID) 49 if err != nil { 50 return nil, status.GenTokenFailed.StatusErr().WithDesc(err.Error()) 51 } 52 53 return &account.LoginRsp{ 54 AccountID: a.AccountID, 55 AccountRole: a.Role, 56 Token: tok, 57 ExpireAt: types.Timestamp{Time: time.Now().Add(j.ExpIn.Duration())}, 58 Issuer: j.Issuer, 59 }, nil 60 }