func insertDummyUser() { log.Println("Initializing default user") tx, err := Database.Begin() if err != nil { log.Println(err) panic("Could not start a transaction for the dummy user") // panic since we need this to happen in a transaction. } _, userInsertErr := Database.Exec("INSERT OR IGNORE INTO user (id) VALUES ('1337h4x0rZ')") _, passwordInsertErr := Database.Exec("INSERT OR IGNORE INTO password (user, hash, salt) VALUES ('1337h4x0rZ', '8450eca01665516d9aeb5317764902b78495502637c96192c81b1683d32d691a0965cf037feca8b9ed9ee6fc6ab8f27fce8f77c4fd9b4a442a00fc317b8237e6', 'admin')") _, emailInsertErr := Database.Exec("INSERT OR IGNORE INTO email (user, email, token, verified) VALUES ('1337h4x0rZ', '*****@*****.**', '1337h4x0rZ', 1)") _, claimInsert := Database.Exec("INSERT OR IGNORE INTO claim (name, required) VALUES ('dummy_user', 0)") _, userClaimInsert := Database.Exec("INSERT OR IGNORE INTO user_claim (user, claim, value) VALUES ('1337h4x0rZ', 'dummy_user', 42)") if !utils.AreNil(userInsertErr, passwordInsertErr, emailInsertErr, claimInsert, userClaimInsert) { log.Println(userInsertErr, passwordInsertErr, emailInsertErr, claimInsert, userClaimInsert) tx.Rollback() panic("Could not save the dummy user") // panic since we want this user to exist. } err = tx.Commit() if err != nil { log.Println(err) panic("Could not conclude the transaction for the dummy user") } log.Println("Default user has been initialized") }
// Store the given user in the database. This is done by several "upsert" // commands. All of them have to succeed. func (r UserRepository) saveUser(user User) *utils.GoAuthError { tx, err := r.database.Begin() if err != nil { log.Println(err) customErr := utils.NewGoAuthError(500, "Could not start a transaction") return &customErr } _, userInsertErr := r.database.Exec("INSERT OR IGNORE INTO user (id) VALUES (?)", user.id) _, passwordInsertErr := r.database.Exec("INSERT OR IGNORE INTO password (user, hash, salt) VALUES (?, ?, ?)", user.id, user.password.hash, user.password.salt) _, passwordUpdateErr := r.database.Exec("UPDATE password SET hash=?, salt=? WHERE user=?", user.password.hash, user.password.salt, user.id) _, emailInsertErr := r.database.Exec("INSERT OR IGNORE INTO email (user, email, token, verified) VALUES (?, ?, ?, ?)", user.id, user.email.email, user.email.token, user.email.verified) _, emailUpdateErr := r.database.Exec("UPDATE email SET email=?, token=?, verified=? WHERE user=?", user.email.email, user.email.token, user.email.verified, user.id) if !utils.AreNil(userInsertErr, passwordInsertErr, passwordUpdateErr, emailInsertErr, emailUpdateErr) { log.Println(userInsertErr, passwordInsertErr, passwordUpdateErr, emailInsertErr, emailUpdateErr) tx.Rollback() customErr := utils.NewGoAuthError(500, "Could not upsert the given user") return &customErr } for k, v := range user.claims { _, userClaimInsertErr := r.database.Exec("INSERT OR IGNORE INTO user_claim (user, claim, value) VALUES (?, ?, ?)", user.id, k.name, v) _, userClaimUpdateErr := r.database.Exec("UPDATE user_claim SET value=? WHERE user=? AND claim=?", v, user.id, k.name) if !utils.AreNil(userClaimInsertErr, userClaimUpdateErr) { log.Println(userClaimInsertErr, userClaimUpdateErr) tx.Rollback() customErr := utils.NewGoAuthError(500, "Could not upsert the user's claims") return &customErr } } err = tx.Commit() if err != nil { log.Println(err) customErr := utils.NewGoAuthError(500, "Could not conclude the transaction") return &customErr } return nil }