MCPcopy Create free account
hub / github.com/cloudbase/garm / InitController

Method InitController

auth/auth.go:121–161  ·  view source on GitHub ↗
(ctx context.Context, param params.NewUserParams)

Source from the content-addressed store, hash-verified

119}
120
121func (a *Authenticator) InitController(ctx context.Context, param params.NewUserParams) (params.User, error) {
122 _, err := a.store.ControllerInfo()
123 if err != nil {
124 if !errors.Is(err, runnerErrors.ErrNotFound) {
125 return params.User{}, fmt.Errorf("error initializing controller: %w", err)
126 }
127 }
128 if a.store.HasAdminUser(ctx) {
129 return params.User{}, runnerErrors.ErrNotFound
130 }
131
132 if param.Email == "" || param.Username == "" {
133 return params.User{}, runnerErrors.NewBadRequestError("missing username or email")
134 }
135
136 if !util.IsValidEmail(param.Email) {
137 return params.User{}, runnerErrors.NewBadRequestError("invalid email address")
138 }
139
140 // username is varchar(64)
141 if len(param.Username) > 64 || !util.IsAlphanumeric(param.Username) {
142 return params.User{}, runnerErrors.NewBadRequestError("invalid username")
143 }
144
145 param.IsAdmin = true
146 param.Enabled = true
147
148 passwordStenght := zxcvbn.PasswordStrength(param.Password, nil)
149 if passwordStenght.Score < 4 {
150 return params.User{}, runnerErrors.NewBadRequestError("password is too weak")
151 }
152
153 hashed, err := util.PaswsordToBcrypt(param.Password)
154 if err != nil {
155 return params.User{}, fmt.Errorf("error creating user: %w", err)
156 }
157
158 param.Password = hashed
159
160 return a.store.CreateUser(ctx, param)
161}
162
163func (a *Authenticator) AuthenticateUser(ctx context.Context, info params.PasswordLoginParams) (context.Context, error) {
164 if info.Username == "" || info.Password == "" {

Callers

nothing calls this directly

Calls 3

ControllerInfoMethod · 0.65
HasAdminUserMethod · 0.65
CreateUserMethod · 0.65

Tested by

no test coverage detected