-
Notifications
You must be signed in to change notification settings - Fork 39
/
account_handler_view.go
146 lines (106 loc) · 2.75 KB
/
account_handler_view.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package auths
import (
"errors"
"fmt"
"github.com/gin-gonic/gin"
"github.com/seal-io/walrus/pkg/apis/runtime"
"github.com/seal-io/walrus/pkg/auths/session"
"github.com/seal-io/walrus/pkg/dao/model"
"github.com/seal-io/walrus/pkg/dao/model/predicate"
"github.com/seal-io/walrus/pkg/dao/model/token"
"github.com/seal-io/walrus/utils/validation"
)
type (
LoginRequest struct {
_ struct{} `route:"POST=/login"`
Username string `json:"username"`
Password string `json:"password"`
Context *gin.Context
}
LoginResponse = *session.Subject
)
func (r *LoginRequest) SetGinContext(ctx *gin.Context) {
r.Context = ctx
}
func (r *LoginRequest) Validate() error {
if r.Username == "" {
return errors.New("invalid username: blank")
}
if r.Password == "" {
return errors.New("invalid password: blank")
}
return nil
}
type LogoutRequest struct {
_ struct{} `route:"DELETE=/logout"`
Context *gin.Context
}
func (r *LogoutRequest) SetGinContext(ctx *gin.Context) {
r.Context = ctx
}
type (
GetInfoRequest struct {
_ struct{} `route:"GET=/info"`
Context *gin.Context
}
GetInfoResponse = *session.Subject
)
func (r *GetInfoRequest) SetGinContext(ctx *gin.Context) {
r.Context = ctx
}
type UpdateInfoRequest struct {
_ struct{} `route:"PUT=/info"`
Password string `json:"password,omitempty"`
OldPassword string `json:"oldPassword,omitempty"`
Context *gin.Context
}
func (r *UpdateInfoRequest) SetGinContext(ctx *gin.Context) {
r.Context = ctx
}
func (r *UpdateInfoRequest) Validate() error {
if r.Password == "" {
return errors.New("invalid password: blank")
}
if r.OldPassword == "" {
return errors.New("invalid old password: blank")
}
if r.Password == r.OldPassword {
return errors.New("invalid password: the same")
}
return nil
}
type (
CreateTokenRequest struct {
_ struct{} `route:"POST=/tokens"`
Name string `json:"name"`
ExpirationSeconds *int `json:"expirationSeconds,omitempty"`
Context *gin.Context
}
CreateTokenResponse = *model.TokenOutput
)
func (r *CreateTokenRequest) SetGinContext(ctx *gin.Context) {
r.Context = ctx
}
func (r *CreateTokenRequest) Validate() error {
if err := validation.IsValidName(r.Name); err != nil {
return fmt.Errorf("invalid name: %w", err)
}
if r.ExpirationSeconds != nil && *r.ExpirationSeconds < 0 {
return errors.New("invalid expiration seconds: negative")
}
return nil
}
type DeleteTokenRequest struct {
_ struct{} `route:"DELETE=/tokens/:token"`
model.TokenDeleteInput `path:",inline"`
}
type (
GetTokensRequest struct {
_ struct{} `route:"GET=/tokens"`
model.TokenQueryInputs `path:",inline" query:",inline"`
runtime.RequestCollection[
predicate.Token, token.OrderOption,
] `query:",inline"`
}
GetTokensResponse = []*model.TokenOutput
)