2023-11-03 23:48:50 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ectrc/snow/aid"
|
|
|
|
p "github.com/ectrc/snow/person"
|
2023-12-19 16:47:13 +00:00
|
|
|
"github.com/ectrc/snow/storage"
|
2023-11-03 23:48:50 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2023-12-09 15:35:33 +00:00
|
|
|
oauthTokenGrantTypes = map[string]func(c *fiber.Ctx, body *FortniteTokenBody) error{
|
|
|
|
"client_credentials": PostTokenClientCredentials,
|
|
|
|
"password": PostTokenPassword,
|
2023-11-03 23:48:50 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2023-12-09 15:35:33 +00:00
|
|
|
type FortniteTokenBody struct {
|
2023-11-03 23:48:50 +00:00
|
|
|
GrantType string `form:"grant_type" binding:"required"`
|
|
|
|
Username string `form:"username"`
|
|
|
|
Password string `form:"password"`
|
|
|
|
}
|
|
|
|
|
2023-12-09 15:35:33 +00:00
|
|
|
func PostFortniteToken(c *fiber.Ctx) error {
|
|
|
|
var body FortniteTokenBody
|
2023-11-03 23:48:50 +00:00
|
|
|
|
|
|
|
if err := c.BodyParser(&body); err != nil {
|
|
|
|
return c.Status(fiber.StatusBadRequest).JSON(aid.ErrorBadRequest("Invalid Request Body"))
|
|
|
|
}
|
|
|
|
|
2023-11-20 21:20:26 +00:00
|
|
|
if action, ok := oauthTokenGrantTypes[body.GrantType]; ok {
|
2023-11-03 23:48:50 +00:00
|
|
|
return action(c, &body)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Status(fiber.StatusBadRequest).JSON(aid.ErrorBadRequest("Invalid Grant Type"))
|
|
|
|
}
|
|
|
|
|
2023-12-09 15:35:33 +00:00
|
|
|
func PostTokenClientCredentials(c *fiber.Ctx, body *FortniteTokenBody) error {
|
2023-12-19 16:47:13 +00:00
|
|
|
client, sig := aid.KeyPair.EncryptAndSignB64([]byte(c.IP()))
|
|
|
|
hash := aid.Hash([]byte(client + "." + sig))
|
2023-11-03 23:48:50 +00:00
|
|
|
|
|
|
|
return c.Status(fiber.StatusOK).JSON(aid.JSON{
|
2023-12-19 16:47:13 +00:00
|
|
|
"access_token": hash,
|
2023-11-03 23:48:50 +00:00
|
|
|
"token_type": "bearer",
|
|
|
|
"client_id": c.IP(),
|
2023-12-16 22:08:37 +00:00
|
|
|
"client_service": "fortnite",
|
2023-11-03 23:48:50 +00:00
|
|
|
"internal_client": true,
|
|
|
|
"expires_in": 3600,
|
|
|
|
"expires_at": time.Now().Add(time.Hour).Format("2006-01-02T15:04:05.999Z"),
|
2023-12-19 16:47:13 +00:00
|
|
|
"product_id": "prod-fn",
|
|
|
|
"sandbox_id": "fn",
|
2023-11-03 23:48:50 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-12-09 15:35:33 +00:00
|
|
|
func PostTokenPassword(c *fiber.Ctx, body *FortniteTokenBody) error {
|
2023-11-03 23:48:50 +00:00
|
|
|
if body.Username == "" || body.Password == "" {
|
|
|
|
return c.Status(fiber.StatusBadRequest).JSON(aid.ErrorBadRequest("Username/Password is empty"))
|
|
|
|
}
|
|
|
|
|
|
|
|
person := p.FindByDisplay(strings.Split(body.Username, "@")[0])
|
|
|
|
if person == nil {
|
|
|
|
return c.Status(fiber.StatusBadRequest).JSON(aid.ErrorBadRequest("No Account Found"))
|
|
|
|
}
|
|
|
|
|
2023-12-19 16:47:13 +00:00
|
|
|
access, ac_sig := aid.KeyPair.EncryptAndSignB64([]byte(person.ID))
|
|
|
|
ac_hash := aid.Hash([]byte(access + "." + ac_sig))
|
|
|
|
|
|
|
|
ac_token := &storage.DB_GameToken{
|
|
|
|
ID: ac_hash,
|
|
|
|
PersonID: person.ID,
|
|
|
|
AccessToken: access + "." + ac_sig,
|
|
|
|
Type: "access",
|
2023-11-03 23:48:50 +00:00
|
|
|
}
|
2023-12-19 16:47:13 +00:00
|
|
|
storage.Repo.SaveToken(ac_token)
|
2023-11-03 23:48:50 +00:00
|
|
|
|
2023-12-19 16:47:13 +00:00
|
|
|
refresh, re_sig := aid.KeyPair.EncryptAndSignB64([]byte(person.ID))
|
|
|
|
re_hash := aid.Hash([]byte(refresh + "." + re_sig))
|
|
|
|
|
|
|
|
re_token := &storage.DB_GameToken{
|
|
|
|
ID: re_hash,
|
|
|
|
PersonID: person.ID,
|
|
|
|
AccessToken: refresh + "." + re_sig,
|
|
|
|
Type: "refresh",
|
2023-11-03 23:48:50 +00:00
|
|
|
}
|
2023-12-19 16:47:13 +00:00
|
|
|
storage.Repo.SaveToken(re_token)
|
2023-11-03 23:48:50 +00:00
|
|
|
|
|
|
|
return c.Status(fiber.StatusOK).JSON(aid.JSON{
|
2023-12-19 16:47:13 +00:00
|
|
|
// "access_token": access + "." + ac_sig,
|
|
|
|
"access_token": ac_hash,
|
2023-11-03 23:48:50 +00:00
|
|
|
"account_id": person.ID,
|
|
|
|
"client_id": c.IP(),
|
2023-12-16 22:08:37 +00:00
|
|
|
"client_service": "fortnite",
|
|
|
|
"app": "fortnite",
|
2023-11-03 23:48:50 +00:00
|
|
|
"device_id": "default",
|
|
|
|
"display_name": person.DisplayName,
|
|
|
|
"expires_at": time.Now().Add(time.Hour * 24).Format("2006-01-02T15:04:05.999Z"),
|
|
|
|
"expires_in": 86400,
|
|
|
|
"internal_client": true,
|
|
|
|
"refresh_expires": 86400,
|
|
|
|
"refresh_expires_at": time.Now().Add(time.Hour * 24).Format("2006-01-02T15:04:05.999Z"),
|
2023-12-19 16:47:13 +00:00
|
|
|
// "refresh_token": refresh + "." + re_sig,
|
|
|
|
"refresh_token": re_hash,
|
2023-11-03 23:48:50 +00:00
|
|
|
"token_type": "bearer",
|
2023-12-19 16:47:13 +00:00
|
|
|
"product_id": "prod-fn",
|
|
|
|
"sandbox_id": "fn",
|
2023-11-03 23:48:50 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-11-05 22:08:53 +00:00
|
|
|
func GetOAuthVerify(c *fiber.Ctx) error {
|
|
|
|
auth := c.Get("Authorization")
|
|
|
|
if auth == "" {
|
2023-11-09 18:42:28 +00:00
|
|
|
return c.Status(fiber.StatusForbidden).JSON(aid.ErrorBadRequest("Authorization Header is empty"))
|
2023-11-05 22:08:53 +00:00
|
|
|
}
|
2023-12-19 16:47:13 +00:00
|
|
|
real := strings.ReplaceAll(auth, "bearer ", "")
|
2023-11-05 22:08:53 +00:00
|
|
|
|
2023-12-19 16:47:13 +00:00
|
|
|
found := storage.Repo.GetToken(real)
|
|
|
|
if found == nil {
|
2023-11-09 18:42:28 +00:00
|
|
|
return c.Status(fiber.StatusForbidden).JSON(aid.ErrorBadRequest("Invalid Access Token"))
|
2023-11-05 22:08:53 +00:00
|
|
|
}
|
2023-12-19 16:47:13 +00:00
|
|
|
snowId := found.PersonID
|
2023-11-05 22:08:53 +00:00
|
|
|
|
|
|
|
person := p.Find(snowId)
|
|
|
|
if person == nil {
|
2023-11-09 18:42:28 +00:00
|
|
|
return c.Status(fiber.StatusForbidden).JSON(aid.ErrorBadRequest("Invalid Access Token"))
|
2023-11-05 22:08:53 +00:00
|
|
|
}
|
|
|
|
|
2023-11-09 18:53:19 +00:00
|
|
|
return c.Status(fiber.StatusOK).JSON(aid.JSON{
|
|
|
|
"app": "fortnite",
|
2023-12-19 16:47:13 +00:00
|
|
|
"token": real,
|
2023-11-09 18:53:19 +00:00
|
|
|
"token_type": "bearer",
|
|
|
|
"expires_at": time.Now().Add(time.Hour * 24).Format("2006-01-02T15:04:05.999Z"),
|
|
|
|
"expires_in": 86400,
|
|
|
|
"client_id": c.IP(),
|
|
|
|
"session_id": "0",
|
|
|
|
"device_id": "default",
|
|
|
|
"internal_client": true,
|
2023-12-16 22:08:37 +00:00
|
|
|
"client_service": "fortnite",
|
2023-11-09 18:53:19 +00:00
|
|
|
"in_app_id": person.ID,
|
|
|
|
"account_id": person.ID,
|
|
|
|
"displayName": person.DisplayName,
|
2023-12-19 16:47:13 +00:00
|
|
|
"product_id": "prod-fn",
|
|
|
|
"sandbox_id": "fn",
|
2023-11-09 18:53:19 +00:00
|
|
|
})
|
2023-11-05 22:08:53 +00:00
|
|
|
}
|
|
|
|
|
2023-12-09 15:35:33 +00:00
|
|
|
func FortniteMiddleware(c *fiber.Ctx) error {
|
2023-11-05 22:08:53 +00:00
|
|
|
auth := c.Get("Authorization")
|
|
|
|
if auth == "" {
|
2023-11-09 18:42:28 +00:00
|
|
|
return c.Status(fiber.StatusForbidden).JSON(aid.ErrorBadRequest("Authorization Header is empty"))
|
2023-11-05 22:08:53 +00:00
|
|
|
}
|
2023-12-19 16:47:13 +00:00
|
|
|
real := strings.ReplaceAll(auth, "bearer ", "")
|
2023-11-05 22:08:53 +00:00
|
|
|
|
2023-12-19 16:47:13 +00:00
|
|
|
found := storage.Repo.GetToken(real)
|
|
|
|
if found == nil {
|
2023-11-09 18:42:28 +00:00
|
|
|
return c.Status(fiber.StatusForbidden).JSON(aid.ErrorBadRequest("Invalid Access Token"))
|
2023-11-05 22:08:53 +00:00
|
|
|
}
|
2023-12-19 16:47:13 +00:00
|
|
|
snowId := found.PersonID
|
2023-11-05 22:08:53 +00:00
|
|
|
|
|
|
|
person := p.Find(snowId)
|
|
|
|
if person == nil {
|
2023-11-09 18:42:28 +00:00
|
|
|
return c.Status(fiber.StatusForbidden).JSON(aid.ErrorBadRequest("Invalid Access Token"))
|
2023-11-05 22:08:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c.Locals("person", person)
|
2023-12-09 15:35:33 +00:00
|
|
|
return c.Next()
|
|
|
|
}
|
|
|
|
|
|
|
|
func FrontendMiddleware(c *fiber.Ctx) error {
|
|
|
|
auth := c.Get("Authorization")
|
|
|
|
if auth == "" {
|
|
|
|
return c.Status(fiber.StatusForbidden).JSON(aid.ErrorBadRequest("Authorization Header is empty"))
|
|
|
|
}
|
|
|
|
|
2023-12-19 16:47:13 +00:00
|
|
|
found := storage.Repo.GetToken(auth)
|
|
|
|
if found == nil {
|
2023-12-09 15:35:33 +00:00
|
|
|
return c.Status(fiber.StatusForbidden).JSON(aid.ErrorBadRequest("Invalid Access Token"))
|
|
|
|
}
|
2023-12-19 16:47:13 +00:00
|
|
|
snowId := found.PersonID
|
2023-11-05 22:08:53 +00:00
|
|
|
|
2023-12-09 15:35:33 +00:00
|
|
|
person := p.Find(snowId)
|
|
|
|
if person == nil {
|
|
|
|
return c.Status(fiber.StatusForbidden).JSON(aid.ErrorBadRequest("Invalid Access Token"))
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Locals("person", person)
|
2023-11-05 22:08:53 +00:00
|
|
|
return c.Next()
|
|
|
|
}
|
|
|
|
|
2023-11-03 23:48:50 +00:00
|
|
|
func DeleteOAuthSessions(c *fiber.Ctx) error {
|
|
|
|
return c.Status(fiber.StatusOK).JSON(aid.JSON{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetPublicAccount(c *fiber.Ctx) error {
|
|
|
|
person := p.Find(c.Params("accountId"))
|
|
|
|
if person == nil {
|
|
|
|
return c.Status(fiber.StatusBadRequest).JSON(aid.ErrorBadRequest("No Account Found"))
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Status(fiber.StatusOK).JSON(aid.JSON{
|
|
|
|
"id": person.ID,
|
|
|
|
"displayName": person.DisplayName,
|
2023-11-09 18:42:28 +00:00
|
|
|
"externalAuths": []aid.JSON{},
|
2023-11-03 23:48:50 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-11-09 18:42:28 +00:00
|
|
|
func GetPublicAccounts(c *fiber.Ctx) error {
|
|
|
|
response := []aid.JSON{}
|
|
|
|
|
|
|
|
accountIds := c.Request().URI().QueryArgs().PeekMulti("accountId")
|
|
|
|
for _, accountIdSlice := range accountIds {
|
|
|
|
person := p.Find(string(accountIdSlice))
|
|
|
|
if person == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
response = append(response, aid.JSON{
|
|
|
|
"id": person.ID,
|
|
|
|
"displayName": person.DisplayName,
|
|
|
|
"externalAuths": []aid.JSON{},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Status(fiber.StatusOK).JSON(response)
|
|
|
|
}
|
|
|
|
|
2023-11-03 23:48:50 +00:00
|
|
|
func GetPublicAccountExternalAuths(c *fiber.Ctx) error {
|
|
|
|
person := p.Find(c.Params("accountId"))
|
|
|
|
if person == nil {
|
|
|
|
return c.Status(fiber.StatusBadRequest).JSON(aid.ErrorBadRequest("No Account Found"))
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Status(fiber.StatusOK).JSON([]aid.JSON{})
|
2023-11-09 18:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetPublicAccountByDisplayName(c *fiber.Ctx) error {
|
|
|
|
person := p.FindByDisplay(c.Params("displayName"))
|
|
|
|
if person == nil {
|
|
|
|
return c.Status(fiber.StatusBadRequest).JSON(aid.ErrorBadRequest("No Account Found"))
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Status(fiber.StatusOK).JSON(aid.JSON{
|
|
|
|
"id": person.ID,
|
|
|
|
"displayName": person.DisplayName,
|
|
|
|
"externalAuths": []aid.JSON{},
|
|
|
|
})
|
2023-11-03 23:48:50 +00:00
|
|
|
}
|