create gitea account
This commit is contained in:
23
src/main.go
23
src/main.go
@ -4,7 +4,6 @@ import (
|
|||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
|
||||||
"github.com/bwmarrin/discordgo"
|
"github.com/bwmarrin/discordgo"
|
||||||
"github.com/dlclark/regexp2"
|
"github.com/dlclark/regexp2"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
@ -19,6 +18,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"time"
|
"time"
|
||||||
"github.com/cornelk/hashmap"
|
"github.com/cornelk/hashmap"
|
||||||
|
"code.gitea.io/sdk/gitea"
|
||||||
)
|
)
|
||||||
var discord *discordgo.Session
|
var discord *discordgo.Session
|
||||||
var secret secrets_json
|
var secret secrets_json
|
||||||
@ -51,6 +51,7 @@ type secrets_json struct {
|
|||||||
MysqlIndentify string `json:"mysqlIndentify"`
|
MysqlIndentify string `json:"mysqlIndentify"`
|
||||||
DiscordServerID string `json:"discordServerID"`
|
DiscordServerID string `json:"discordServerID"`
|
||||||
MoodleToken string `json:"moodleToken"`
|
MoodleToken string `json:"moodleToken"`
|
||||||
|
GiteaToken string `json:"giteaToken"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@ -83,6 +84,8 @@ func main() {
|
|||||||
"PRIMARY KEY ( username )" +
|
"PRIMARY KEY ( username )" +
|
||||||
");")
|
");")
|
||||||
log(err)
|
log(err)
|
||||||
|
giteaClient, err := gitea.NewClient("https://git.redstoneunion.de", gitea.SetToken(secret.GiteaToken))
|
||||||
|
log(err)
|
||||||
moodle := moodle.NewMoodleApi("https://exam.redstoneunion.de/", secret.MoodleToken)
|
moodle := moodle.NewMoodleApi("https://exam.redstoneunion.de/", secret.MoodleToken)
|
||||||
_ = moodle
|
_ = moodle
|
||||||
tmpl := template.Must(template.ParseFiles("tmpl/register.html"))
|
tmpl := template.Must(template.ParseFiles("tmpl/register.html"))
|
||||||
@ -122,7 +125,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
tmpl.Execute(w, registerstruct)
|
tmpl.Execute(w, registerstruct)
|
||||||
fmt.Println(registerstruct)
|
|
||||||
})
|
})
|
||||||
http.HandleFunc("/submit", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/submit", func(w http.ResponseWriter, r *http.Request) {
|
||||||
token := r.FormValue("token")
|
token := r.FormValue("token")
|
||||||
@ -130,11 +132,11 @@ func main() {
|
|||||||
accInter, SubmitStruct.Success = cacheAccounts.GetStringKey(token)
|
accInter, SubmitStruct.Success = cacheAccounts.GetStringKey(token)
|
||||||
var account account = accInter.(account)
|
var account account = accInter.(account)
|
||||||
if SubmitStruct.Success {
|
if SubmitStruct.Success {
|
||||||
fmt.Println(token);
|
cacheAccounts.Del(token)
|
||||||
salt := make([]byte, 32)
|
salt := make([]byte, 32)
|
||||||
_, err := rand.Read(salt)
|
_, err := rand.Read(salt)
|
||||||
log(err)
|
log(err)
|
||||||
hash := argon2.IDKey([]byte(account.password), salt[:], 1, 64*1024, 4, 32)
|
hash := argon2.IDKey([]byte(account.password), salt, 1, 64*1024, 4, 32)
|
||||||
// add user to the database
|
// add user to the database
|
||||||
query := "INSERT INTO account(username, email, hash, salt, discordUsername) VALUES (?, ?, ?, ?, ?)"
|
query := "INSERT INTO account(username, email, hash, salt, discordUsername) VALUES (?, ?, ?, ?, ?)"
|
||||||
ctx, cancelfunc := context.WithTimeout(context.Background(), 5*time.Second)
|
ctx, cancelfunc := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
@ -144,9 +146,18 @@ func main() {
|
|||||||
defer stmt.Close()
|
defer stmt.Close()
|
||||||
_, err = stmt.ExecContext(ctx, account.username, account.email, hash, salt, account.discordUsername)
|
_, err = stmt.ExecContext(ctx, account.username, account.email, hash, salt, account.discordUsername)
|
||||||
log(err)
|
log(err)
|
||||||
//_, err = moodle.AddUser(account.username, account.username, account.email, account.username, account.password)
|
//_, err = moodle.AddUser(account.username + "wg", account.username, account.email, account.username, account.password)
|
||||||
log(err)
|
log(err)
|
||||||
cacheAccounts.Del(token)
|
opt := gitea.CreateUserOption{
|
||||||
|
Email: account.email,
|
||||||
|
Username: account.username,
|
||||||
|
SourceID: 0,
|
||||||
|
Password: account.password,
|
||||||
|
SendNotify: false,
|
||||||
|
}
|
||||||
|
_, _, err = giteaClient.AdminCreateUser(opt)
|
||||||
|
log(err)
|
||||||
|
|
||||||
}
|
}
|
||||||
err = submitTmpl.Execute(w, SubmitStruct)
|
err = submitTmpl.Execute(w, SubmitStruct)
|
||||||
log(err)
|
log(err)
|
||||||
|
|||||||
Reference in New Issue
Block a user