-
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
// SPDX-FileCopyrightText: 2025 Shota FUJI <pockawoooh@gmail.com>
// SPDX-License-Identifier: AGPL-3.0-only
package workspace
import (
"bytes"
"context"
"crypto/rand"
"strings"
"connectrpc.com/connect"
"github.com/google/uuid"
"google.golang.org/protobuf/proto"
eventV1 "pocka.jp/x/yamori/proto/go/backend/events/v1"
errorV1 "pocka.jp/x/yamori/proto/go/error/v1"
workspaceV2 "pocka.jp/x/yamori/proto/go/workspace/v2"
"pocka.jp/x/yamori/backend/core/event"
"pocka.jp/x/yamori/backend/core/projection"
"pocka.jp/x/yamori/backend/crypto"
workspaceEvent "pocka.jp/x/yamori/backend/events/workspace"
)
func (s *Service) CreateInitialAdmin(
ctx context.Context,
req *connect.Request[workspaceV2.CreateInitialAdminRequest],
) (*connect.Response[workspaceV2.CreateInitialAdminResponse], error) {
initialAdminPassword := req.Msg.GetInitialAdminPassword()
if initialAdminPassword == "" {
return connect.NewResponse(&workspaceV2.CreateInitialAdminResponse{
Result: &workspaceV2.CreateInitialAdminResponse_MissingFieldError{
MissingFieldError: &errorV1.MissingFieldError{
Path: proto.String("initial_admin_password"),
},
},
}), nil
}
name := req.Msg.GetName()
if name == "" {
return connect.NewResponse(&workspaceV2.CreateInitialAdminResponse{
Result: &workspaceV2.CreateInitialAdminResponse_MissingFieldError{
MissingFieldError: &errorV1.MissingFieldError{
Path: proto.String("name"),
},
},
}), nil
}
// TODO: SystemError 以外にする
if strings.Trim(name, " \r\n\t") != name {
return connect.NewResponse(&workspaceV2.CreateInitialAdminResponse{
Result: &workspaceV2.CreateInitialAdminResponse_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.CreateInitialAdminResponse{
Result: &workspaceV2.CreateInitialAdminResponse_MissingFieldError{
MissingFieldError: &errorV1.MissingFieldError{
Path: proto.String("password"),
},
},
}), nil
}
// TODO: SystemError 以外にする
if len(password) <= 8 {
return connect.NewResponse(&workspaceV2.CreateInitialAdminResponse{
Result: &workspaceV2.CreateInitialAdminResponse_SystemError{
SystemError: &errorV1.SystemError{
Message: proto.String("Password has to be longer than or equals to 8 bytes"),
},
},
}), nil
}
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
}
users, err := projection.GetUsers(tx)
if err != nil {
return nil, err
}
if err := event.UpdateProjections(tx, ws, pw, users); err != nil {
return nil, err
}
if pw.Projection.Password == nil {
tx.Commit()
return connect.NewResponse(&workspaceV2.CreateInitialAdminResponse{
Result: &workspaceV2.CreateInitialAdminResponse_PasswordExpired{
PasswordExpired: &errorV1.AuthenticationError{},
},
}), nil
}
hash := crypto.HashPassword([]byte(initialAdminPassword), pw.Projection.Password.Salt)
if !bytes.Equal(hash, pw.Projection.Password.Hash) {
return connect.NewResponse(&workspaceV2.CreateInitialAdminResponse{
Result: &workspaceV2.CreateInitialAdminResponse_AuthenticationError{
AuthenticationError: &errorV1.AuthenticationError{},
},
}), nil
}
for _, u := range users.Projection.Users {
if u.GetName() == name {
// TODO: SystemError 以外にする
return connect.NewResponse(&workspaceV2.CreateInitialAdminResponse{
Result: &workspaceV2.CreateInitialAdminResponse_SystemError{
SystemError: &errorV1.SystemError{
Message: proto.String("User with same name already exists."),
},
},
}), nil
}
}
id, err := uuid.NewRandom()
if err != nil {
return nil, err
}
keyID := make([]byte, 32)
rand.Read(keyID)
err = event.AppendEvents(tx, []*eventV1.Event{
workspaceEvent.ExpireAdminCreationPassword(),
workspaceEvent.CreateUser(id.String(), name, displayName, keyID),
workspaceEvent.GrantAdminAccess(id.String()),
workspaceEvent.ConfigurePasswordLogin(id.String(), password),
})
if err != nil {
return nil, err
}
if err := tx.Commit(); err != nil {
return nil, err
}
return connect.NewResponse(&workspaceV2.CreateInitialAdminResponse{
Result: &workspaceV2.CreateInitialAdminResponse_Ok{
Ok: &workspaceV2.User{
Id: &workspaceV2.UserID{
Value: proto.String(id.String()),
},
Name: proto.String(name),
DisplayName: proto.String(displayName),
IsAdmin: proto.Bool(true),
},
},
}), nil
}