-
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
-
147
-
148
-
149
-
150
-
151
-
152
-
153
-
154
-
155
-
156
-
157
-
158
-
159
-
160
-
161
-
162
-
163
-
164
-
165
-
166
-
167
-
168
-
169
-
170
-
171
-
172
-
173
-
174
-
175
-
176
-
177
-
178
-
179
-
180
-
181
-
182
-
183
-
184
-
185
-
186
-
187
-
188
-
189
-
190
-
191
-
192
-
193
-
194
-
195
-
196
-
197
-
198
-
199
-
200
-
201
-
202
-
203
-
204
-
205
-
206
-
207
-
208
-
209
-
210
-
211
-
212
-
213
-
214
-
215
-
216
-
217
-
218
-
219
-
220
-
221
-
222
-
223
-
224
-
225
-
226
-
227
-
228
-
229
-
230
-
231
-
232
-
233
-
234
-
235
-
236
-
237
-
238
-
239
-
240
-
241
-
242
-
243
-
244
-
245
-
246
-
247
-
248
-
249
-
250
-
251
-
252
-
253
-
254
-
255
-
256
-
257
-
258
-
259
-
260
-
261
-
262
-
263
-
264
-
265
-
266
-
267
-
268
-
269
-
270
-
271
-
272
-
273
-
274
-
275
-
276
// SPDX-FileCopyrightText: 2025 Shota FUJI <pockawoooh@gmail.com>
// SPDX-License-Identifier: AGPL-3.0-only
package workspace
import (
"bytes"
"context"
"crypto/rand"
"net/http"
"strings"
"connectrpc.com/connect"
"github.com/google/uuid"
"google.golang.org/protobuf/proto"
"pocka.jp/x/yamori/backend/core"
"pocka.jp/x/yamori/backend/core/event"
"pocka.jp/x/yamori/backend/core/projection"
"pocka.jp/x/yamori/backend/crypto"
eventV1 "pocka.jp/x/yamori/proto/go/backend/events/v1"
workspaceEvent "pocka.jp/x/yamori/proto/go/backend/events/workspace/v1"
errorV1 "pocka.jp/x/yamori/proto/go/error/v1"
workspaceV2 "pocka.jp/x/yamori/proto/go/workspace/v2"
workspaceV2connect "pocka.jp/x/yamori/proto/go/workspace/v2/v2connect"
)
type Service struct {
core *core.Core
}
func New(core *core.Core) *Service {
return &Service{core: core}
}
func (s *Service) Login(
ctx context.Context,
req *connect.Request[workspaceV2.LoginRequest],
) (*connect.Response[workspaceV2.LoginResponse], error) {
return connect.NewResponse(&workspaceV2.LoginResponse{
Result: &workspaceV2.LoginResponse_SystemError{
SystemError: &errorV1.SystemError{
Message: proto.String("Not Implemented"),
},
},
}), nil
}
func (s *Service) Logout(
ctx context.Context,
req *connect.Request[workspaceV2.LogoutRequest],
) (*connect.Response[workspaceV2.LogoutResponse], error) {
res := workspaceV2.LogoutResponse{
SystemError: &errorV1.SystemError{
Message: proto.String("Not Implemented"),
},
}
return connect.NewResponse(&res), nil
}
func (s *Service) Get(
ctx context.Context,
req *connect.Request[workspaceV2.GetRequest],
) (*connect.Response[workspaceV2.GetResponse], error) {
res := workspaceV2.GetResponse{
Result: &workspaceV2.GetResponse_SystemError{
SystemError: &errorV1.SystemError{
Message: proto.String("Not Implemented"),
},
},
}
return connect.NewResponse(&res), nil
}
func (s *Service) Update(
ctx context.Context,
req *connect.Request[workspaceV2.UpdateRequest],
) (*connect.Response[workspaceV2.UpdateResponse], error) {
res := workspaceV2.UpdateResponse{
Result: &workspaceV2.UpdateResponse_SystemError{
SystemError: &errorV1.SystemError{
Message: proto.String("Not Implemented"),
},
},
}
return connect.NewResponse(&res), nil
}
// TODO: 通常の追加フローも書く
func (s *Service) CreateUser(
ctx context.Context,
req *connect.Request[workspaceV2.CreateUserRequest],
) (*connect.Response[workspaceV2.CreateUserResponse], error) {
name := req.Msg.GetName()
if name == "" {
return connect.NewResponse(&workspaceV2.CreateUserResponse{
Result: &workspaceV2.CreateUserResponse_MissingFieldError{
MissingFieldError: &errorV1.MissingFieldError{
Path: proto.String("name"),
},
},
}), nil
}
// TODO: SystemError 以外にする
if strings.Trim(name, " \r\n\t") != name {
return connect.NewResponse(&workspaceV2.CreateUserResponse{
Result: &workspaceV2.CreateUserResponse_SystemError{
SystemError: &errorV1.SystemError{
Message: proto.String("Name cannot have heading or trailing spaces"),
},
},
}), nil
}
displayName := strings.Trim(req.Msg.GetDisplayName(), " \r\n\t")
if displayName == "" {
displayName = name
}
password := req.Msg.GetPassword()
if password == "" {
return connect.NewResponse(&workspaceV2.CreateUserResponse{
Result: &workspaceV2.CreateUserResponse_MissingFieldError{
MissingFieldError: &errorV1.MissingFieldError{
Path: proto.String("password"),
},
},
}), nil
}
// TODO: SystemError 以外にする
if len(password) <= 8 {
return connect.NewResponse(&workspaceV2.CreateUserResponse{
Result: &workspaceV2.CreateUserResponse_SystemError{
SystemError: &errorV1.SystemError{
Message: proto.String("Password has to be longer than or equals to 8 bytes"),
},
},
}), nil
}
// TODO: チェックが煩雑なので CreateUser と別のエンドポイントにする
initialAdminPassword := req.Msg.GetInitialAdminPassword()
if initialAdminPassword != "" {
tx, err := s.core.DB.Begin()
if err != nil {
return nil, err
}
defer tx.Rollback()
ws, err := projection.GetWorkspace(tx)
if err != nil {
return nil, err
}
pw, err := projection.GetAdminCreationPassword(tx)
if err != nil {
return nil, err
}
if err := event.UpdateProjections(tx, ws, pw); err != nil {
return nil, err
}
if pw.Projection.Password == nil {
tx.Commit()
return connect.NewResponse(&workspaceV2.CreateUserResponse{
Result: &workspaceV2.CreateUserResponse_AuthenticationError{
AuthenticationError: &errorV1.AuthenticationError{},
},
}), nil
}
hash := crypto.HashPassword([]byte(initialAdminPassword), pw.Projection.Password.Salt)
if !bytes.Equal(hash, pw.Projection.Password.Hash) {
return connect.NewResponse(&workspaceV2.CreateUserResponse{
Result: &workspaceV2.CreateUserResponse_AuthenticationError{
AuthenticationError: &errorV1.AuthenticationError{},
},
}), nil
}
id, err := uuid.NewRandom()
if err != nil {
return nil, err
}
keyID := make([]byte, 32)
rand.Read(keyID)
pwHash, pwSalt := crypto.SaltAndHashPassword([]byte(req.Msg.GetPassword()))
err = event.AppendEvents(tx, []*eventV1.Event{
{
Event: &eventV1.Event_WorkspaceEvent{
WorkspaceEvent: &workspaceEvent.Event{
Event: &workspaceEvent.Event_AdminCreationPasswordExpired{
AdminCreationPasswordExpired: &workspaceEvent.AdminCreationPasswordExpired{},
},
},
},
},
{
Event: &eventV1.Event_WorkspaceEvent{
WorkspaceEvent: &workspaceEvent.Event{
Event: &workspaceEvent.Event_UserCreated{
UserCreated: &workspaceEvent.UserCreated{
Id: proto.String(id.String()),
Name: proto.String(name),
DisplayName: proto.String(displayName),
KeyId: keyID,
},
},
},
},
},
{
Event: &eventV1.Event_WorkspaceEvent{
WorkspaceEvent: &workspaceEvent.Event{
Event: &workspaceEvent.Event_AdminAccessGranted{
AdminAccessGranted: &workspaceEvent.AdminAccessGranted{
UserId: proto.String(id.String()),
},
},
},
},
},
{
Event: &eventV1.Event_WorkspaceEvent{
WorkspaceEvent: &workspaceEvent.Event{
Event: &workspaceEvent.Event_PasswordLoginConfigured{
PasswordLoginConfigured: &workspaceEvent.PasswordLoginConfigured{
UserId: proto.String(id.String()),
PasswordHash: pwHash,
PasswordSalt: pwSalt,
},
},
},
},
},
})
if err != nil {
return nil, err
}
if err := tx.Commit(); err != nil {
return nil, err
}
return connect.NewResponse(&workspaceV2.CreateUserResponse{
Result: &workspaceV2.CreateUserResponse_Ok{
Ok: nil,
},
}), nil
}
res := workspaceV2.CreateUserResponse{
Result: &workspaceV2.CreateUserResponse_SystemError{
SystemError: &errorV1.SystemError{
Message: proto.String("Not Implemented"),
},
},
}
return connect.NewResponse(&res), nil
}
func (s *Service) Register(mux *http.ServeMux) {
path, handler := workspaceV2connect.NewWorkspaceServiceHandler(s)
mux.Handle(path, handler)
}