-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
461 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package v1 | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/ImpactDevelopment/ImpactServer/src/database" | ||
"github.com/ImpactDevelopment/ImpactServer/src/discord" | ||
"github.com/ImpactDevelopment/ImpactServer/src/util" | ||
"github.com/labstack/echo/v4" | ||
"golang.org/x/crypto/bcrypt" | ||
) | ||
|
||
type registrationCheck struct { | ||
Token string `json:"token" form:"token" query:"token"` | ||
} | ||
|
||
type registration struct { | ||
Token string `json:"token" form:"token" query:"token"` | ||
Discord string `json:"discord" form:"discord" query:"discord"` | ||
Mcuuid string `json:"mcuuid" form:"mcuuid" query:"mcuuid"` | ||
Email string `json:"email" form:"email" query:"email"` | ||
Password string `json:"password" form:"password" query:"password"` | ||
} | ||
|
||
func checkToken(c echo.Context) error { | ||
body := ®istrationCheck{} | ||
err := c.Bind(body) | ||
if err != nil { | ||
return err | ||
} | ||
if body.Token == "" { | ||
return echo.NewHTTPError(http.StatusBadRequest, "token missing") | ||
} | ||
var createdAt int64 | ||
err = database.DB.QueryRow("SELECT created_at FROM pending_donations WHERE token = $1 AND NOT used", body.Token).Scan(&createdAt) | ||
if err != nil { | ||
return echo.NewHTTPError(http.StatusBadRequest, "invalid token") | ||
} | ||
return c.String(200, "true") | ||
} | ||
|
||
func registerWithToken(c echo.Context) error { | ||
body := ®istration{} | ||
err := c.Bind(body) | ||
if err != nil { | ||
return err | ||
} | ||
if body.Token == "" || body.Discord == "" || body.Mcuuid == "" || body.Email == "" || body.Password == "" { | ||
return echo.NewHTTPError(http.StatusBadRequest, "empty field(s)") | ||
} | ||
var createdAt int64 | ||
err = database.DB.QueryRow("SELECT created_at FROM pending_donations WHERE token = $1 AND NOT used", body.Token).Scan(&createdAt) | ||
if err != nil { | ||
return echo.NewHTTPError(http.StatusBadRequest, "invalid token") | ||
} | ||
log.Println(body) | ||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(body.Password), bcrypt.DefaultCost) | ||
if err != nil { | ||
return err | ||
} | ||
if !discord.CheckServerMembership(body.Discord) { | ||
return echo.NewHTTPError(http.StatusBadRequest, "join our discord first lol") | ||
} | ||
|
||
req, err := util.GetRequest("https://api.mojang.com/user/profiles/" + strings.Replace(body.Mcuuid, "-", "", -1) + "/names") | ||
if err != nil { | ||
return echo.NewHTTPError(http.StatusBadRequest, "bad mc uuid") | ||
} | ||
resp, err := req.Do() | ||
if err != nil { | ||
return echo.NewHTTPError(http.StatusBadRequest, "bad mc uuid") | ||
} | ||
if resp.Code() != 200 { | ||
return echo.NewHTTPError(http.StatusBadRequest, "bad mc uuid") | ||
} | ||
_, err = database.DB.Exec("UPDATE pending_donations SET used = true WHERE token = $1", body.Token) | ||
if err != nil { | ||
log.Println(err) | ||
return err | ||
} | ||
_, err = database.DB.Exec("INSERT INTO users(email, password_hash, mc_uuid, discord_id) VALUES ($1, $2, $3, $4)", body.Email, hashedPassword, body.Mcuuid, body.Discord) | ||
if err != nil { | ||
log.Println(err) | ||
return err | ||
} | ||
err = discord.GiveDonator(body.Discord) | ||
if err != nil { | ||
log.Println(err) | ||
return err | ||
} | ||
return c.String(200, "SUCCESS") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package discord | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/bwmarrin/discordgo" | ||
) | ||
|
||
var discord *discordgo.Session | ||
|
||
var guildID string | ||
var donatorRole string | ||
|
||
func init() { | ||
token := os.Getenv("DISCORD_BOT_TOKEN") | ||
if token == "" { | ||
fmt.Println("WARNING: No discord bot token, will not be able to grant donator role!") | ||
return | ||
} | ||
guildID = os.Getenv("DISCORD_GUILD_ID") | ||
donatorRole = os.Getenv("DISCORD_DONATOR_ROLE_ID") | ||
if guildID == "" || donatorRole == "" { | ||
fmt.Println("WARNING: Discord info is bad") | ||
return | ||
} | ||
var err error | ||
discord, err = discordgo.New("Bot " + token) | ||
if err != nil { | ||
panic(err) | ||
} | ||
user, err := discord.User("@me") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
myselfID := user.ID | ||
fmt.Println("I am", myselfID) | ||
} | ||
|
||
func GiveDonator(discordID string) error { | ||
return discord.GuildMemberRoleAdd(guildID, discordID, donatorRole) | ||
} | ||
|
||
func CheckServerMembership(discordID string) bool { | ||
member, err := discord.GuildMember(guildID, discordID) | ||
return err == nil && member != nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.