feat: basic auth shit + db init

This commit is contained in:
hexlocation 2025-05-05 19:34:28 +02:00
parent db49da5fd9
commit d8caef7e5d
10 changed files with 261 additions and 69 deletions

View file

@ -8,4 +8,5 @@ import (
func Register(cfg *types.StereoConfig) {
api := cfg.Router.Group("/api")
routes.RegisterUploadRoutes(cfg, api)
routes.RegisterAuthRoutes(cfg, api)
}

View file

@ -0,0 +1,28 @@
package routes
import (
"net/http"
"github.com/gin-gonic/gin"
"stereo.cat/backend/internal/types"
)
func RegisterAuthRoutes(cfg *types.StereoConfig, api *gin.RouterGroup) {
api.GET("/auth/callback", func(c *gin.Context) {
code := c.Query("code")
t, err := cfg.Client.ExchangeCode(code)
if err != nil {
panic(err)
}
user, err := cfg.Client.GetUser(t)
if err != nil {
panic(err)
}
c.JSON(http.StatusOK, user)
})
}

View file

@ -0,0 +1,116 @@
package client
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
"stereo.cat/backend/internal/auth"
)
type Client struct {
RedirectUri string
ClientSecret string
ClientId string
}
const api = "https://discord.com/api/v10"
func New(redirectUri, clientId, clientSecret string) Client {
return Client{
RedirectUri: redirectUri,
ClientId: clientId,
ClientSecret: clientSecret,
}
}
func (c Client) GetUser(t auth.TokenResponse) (auth.User, error) {
user := auth.User {
Blacklisted: false,
}
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/%s", api, "users/@me"), nil)
if err != nil {
return user, nil
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", "Bearer "+t.AccessToken)
resp, err := http.DefaultClient.Do(req)
if resp.StatusCode != http.StatusOK {
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return user, err
}
return user, errors.New(string(bodyBytes))
}
if err != nil {
return user, err
}
defer resp.Body.Close()
err = json.NewDecoder(resp.Body).Decode(&user)
if err != nil {
return user, err
}
return user, nil
}
func (c Client) ExchangeCode(code string) (auth.TokenResponse, error) {
var tokenResponse auth.TokenResponse
requestBody := url.Values{
"grant_type": {"authorization_code"},
"code": {code},
"redirect_uri": {c.RedirectUri},
}
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/%s", api, "/oauth2/token"), strings.NewReader(requestBody.Encode()))
if err != nil {
return tokenResponse, err
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.SetBasicAuth(c.ClientId, c.ClientSecret)
resp, err := http.DefaultClient.Do(req)
if resp.StatusCode != http.StatusOK {
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return tokenResponse, err
}
return tokenResponse, errors.New(string(bodyBytes))
}
if err != nil {
return tokenResponse, err
}
defer resp.Body.Close()
err = json.NewDecoder(resp.Body).Decode(&tokenResponse)
if err != nil {
return tokenResponse, err
}
return tokenResponse, nil
}

32
internal/auth/types.go Normal file
View file

@ -0,0 +1,32 @@
package auth
import (
"gorm.io/gorm"
)
type TokenResponse struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
ExpiresIn uint64 `json:"expires_in"`
RefreshToken string `json:"refresh_token"`
Scope string `json:"scope"`
}
type User struct {
gorm.Model
ID string `json:"id" gorm:"primaryKey;autoIncrement:false"`
Username string `json:"username"`
Blacklisted bool
Email string `json:"email"`
}
type AvatarDecorationData struct {
Asset string
SkuID string
}
type ExchangeCodeRequest struct {
GrantType string `json:"grant_type"`
Code string `json:"code"`
RedirectUri string `json:"redirect_uri"`
}

View file

@ -1,10 +1,9 @@
package types
import (
"time"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"stereo.cat/backend/internal/auth/client"
)
type Route struct {
@ -14,23 +13,8 @@ type Route struct {
}
type StereoConfig struct {
Router *gin.Engine
ImagePath string
}
type User struct {
gorm.Model
ID uint
Username string
RegistrationDate time.Time
Blacklisted bool
Email string
}
func RegisterRoutes(groupName string, routes []Route, cfg *StereoConfig) {
group := cfg.Router.Group(groupName)
for _, route := range routes {
group.Handle(route.Method, route.Path, route.Exec(cfg))
}
Router *gin.Engine
Client client.Client
Database *gorm.DB
}