()
| 162 | } |
| 163 | |
| 164 | func InitMainServer() error { |
| 165 | ctx, cancelFn := context.WithTimeout(context.Background(), 5*time.Second) |
| 166 | defer cancelFn() |
| 167 | |
| 168 | mainServer, err := wstore.DBGetSingleton[*waveobj.MainServer](ctx) |
| 169 | if err == wstore.ErrNotFound { |
| 170 | mainServer = &waveobj.MainServer{ |
| 171 | OID: uuid.NewString(), |
| 172 | } |
| 173 | err = wstore.DBInsert(ctx, mainServer) |
| 174 | if err != nil { |
| 175 | return fmt.Errorf("error inserting mainserver: %w", err) |
| 176 | } |
| 177 | } else if err != nil { |
| 178 | return fmt.Errorf("error getting mainserver: %w", err) |
| 179 | } |
| 180 | |
| 181 | needsUpdate := false |
| 182 | if mainServer.JwtPrivateKey == "" || mainServer.JwtPublicKey == "" { |
| 183 | keyPair, err := wavejwt.GenerateKeyPair() |
| 184 | if err != nil { |
| 185 | return fmt.Errorf("error generating jwt keypair: %w", err) |
| 186 | } |
| 187 | mainServer.JwtPrivateKey = base64.StdEncoding.EncodeToString(keyPair.PrivateKey) |
| 188 | mainServer.JwtPublicKey = base64.StdEncoding.EncodeToString(keyPair.PublicKey) |
| 189 | needsUpdate = true |
| 190 | } |
| 191 | |
| 192 | if needsUpdate { |
| 193 | err = wstore.DBUpdate(ctx, mainServer) |
| 194 | if err != nil { |
| 195 | return fmt.Errorf("error updating mainserver: %w", err) |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | privateKeyBytes, err := base64.StdEncoding.DecodeString(mainServer.JwtPrivateKey) |
| 200 | if err != nil { |
| 201 | return fmt.Errorf("error decoding jwt private key: %w", err) |
| 202 | } |
| 203 | publicKeyBytes, err := base64.StdEncoding.DecodeString(mainServer.JwtPublicKey) |
| 204 | if err != nil { |
| 205 | return fmt.Errorf("error decoding jwt public key: %w", err) |
| 206 | } |
| 207 | |
| 208 | err = wavejwt.SetPrivateKey(privateKeyBytes) |
| 209 | if err != nil { |
| 210 | return fmt.Errorf("error setting jwt private key: %w", err) |
| 211 | } |
| 212 | err = wavejwt.SetPublicKey(publicKeyBytes) |
| 213 | if err != nil { |
| 214 | return fmt.Errorf("error setting jwt public key: %w", err) |
| 215 | } |
| 216 | |
| 217 | pubKeyDer, err := x509.MarshalPKIXPublicKey(ed25519.PublicKey(publicKeyBytes)) |
| 218 | if err != nil { |
| 219 | log.Printf("warning: could not marshal public key for logging: %v", err) |
| 220 | } else { |
| 221 | pubKeyPem := pem.EncodeToMemory(&pem.Block{ |
no test coverage detected